#!/bin/bash
# Extract cPanel security token from the UI page, then test json-api
OUT=/home/john/public_html/i5365_final.txt
> "$OUT"

# Login
curl -sk -c /tmp/i5365e.txt https://cpanel.linlab.dk:2083/login/ -o /dev/null 2>&1
LOGIN=$(curl -sk -b /tmp/i5365e.txt -c /tmp/i5365e.txt \
  -d 'user=john&pass=Ip7*sunfO0#P5xxg' \
  https://cpanel.linlab.dk:2083/login/ 2>&1)
echo "Login: $LOGIN" >> "$OUT"

# Extract session path from redirect
SESSPATH=$(echo "$LOGIN" | grep -o '/cpsess[0-9]*/' | head -1)
echo "Session path: $SESSPATH" >> "$OUT"

# Follow redirect to cPanel UI
UIPAGE=$(curl -sk -b /tmp/i5365e.txt "https://cpanel.linlab.dk:2083${SESSPATH}frontend/jupiter/index.html" 2>&1)
echo "UI page length: ${#UIPAGE}" >> "$OUT"

# Look for security token in the UI page
echo "--- Token search in UI page ---" >> "$OUT"
echo "$UIPAGE" | grep -oP 'cpanelapi_token["\s:=]+["\x27]?[a-f0-9]{32}' | head -3 >> "$OUT"
echo "$UIPAGE" | grep -oP 'security_token["\s:=]+["\x27]?[a-f0-9]{32}' | head -3 >> "$OUT"
echo "$UIPAGE" | grep -oP 'api_token["\s:=]+["\x27]?[a-f0-9]{32}' | head -3 >> "$OUT"
echo "$UIPAGE" | grep -oP 'token["\s:=]+["\x27]?[a-f0-9]{32}' | head -5 >> "$OUT"
# Also look for the token in meta tags or data attributes
echo "$UIPAGE" | grep -oP 'data-token="[^"]*"' | head -3 >> "$OUT"
echo "$UIPAGE" | grep -oP 'name="token"[^>]*value="[^"]*"' | head -3 >> "$OUT"
# Look for any 32-char hex strings that could be tokens
echo "$UIPAGE" | grep -oP '[a-f0-9]{32}' | sort -u | head -10 >> "$OUT"

# Try the /execute/ path with session in URL
echo "" >> "$OUT"
echo "=== /execute/ with session path ===" >> "$OUT"
curl -sk -b /tmp/i5365e.txt "https://cpanel.linlab.dk:2083${SESSPATH}execute/uapi/cron/list_cron_jobs?u=root" 2>&1 | head -c 500 >> "$OUT"
echo "" >> "$OUT"

echo "" >> "$OUT"
echo "=== /execute/ with session path (no u=root) ===" >> "$OUT"
curl -sk -b /tmp/i5365e.txt "https://cpanel.linlab.dk:2083${SESSPATH}execute/uapi/cron/list_cron_jobs" 2>&1 | head -c 500 >> "$OUT"
echo "" >> "$OUT"

echo "" >> "$OUT"
echo "=== /json-api/ with session path ===" >> "$OUT"
curl -sk -b /tmp/i5365e.txt "https://cpanel.linlab.dk:2083${SESSPATH}json-api/cpanel/cron?cpanel_jsonapi_module=cron&u=root" 2>&1 | head -c 500 >> "$OUT"
echo "" >> "$OUT"

echo "" >> "$OUT"
echo "=== DONE ===" >> "$OUT"
echo "DONE"
