forked from fptn-project/FptnClient-iOS
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoken_debug.py
More file actions
203 lines (162 loc) · 6.6 KB
/
Copy pathtoken_debug.py
File metadata and controls
203 lines (162 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
"""
token_debug.py — FPTN token format analyser
Usage:
python3 token_debug.py token_working.txt token_new.txt
python3 token_debug.py <any .txt file>
Install deps (once, in a venv):
python3 -m venv .venv && .venv/bin/pip install brotli
.venv/bin/python3 token_debug.py ...
"""
import sys
import base64
import json
import binascii
import hashlib
import struct
from pathlib import Path
try:
import brotli
BROTLI_AVAILABLE = True
except ImportError:
BROTLI_AVAILABLE = False
# ── helpers ──────────────────────────────────────────────────────────────────
def pad(s: str) -> str:
"""Add standard base64 padding."""
rem = len(s) % 4
return s + "=" * (4 - rem) if rem else s
def sanitize(raw: str) -> tuple[str, str]:
"""Strip known FPTN prefixes and whitespace; return (prefix, cleaned_b64)."""
raw = raw.strip()
prefix = ""
for p in ("fptnb://", "fptnb:", "fptn://", "fptn:"):
if raw.startswith(p):
prefix = p.rstrip(":/")
raw = raw[len(p):]
break
# strip whitespace, backticks, existing padding (mirror C++ RemoveSubstring)
raw = "".join(raw.split())
raw = raw.replace("`", "").rstrip("=")
return prefix, raw
def try_decode_b64(b64: str) -> bytes | None:
"""Attempt standard → URL-safe base64 decode."""
for variant in [b64, b64.replace("-", "+").replace("_", "/")]:
try:
return base64.b64decode(pad(variant))
except Exception:
pass
return None
def hexdump(data: bytes, width: int = 16) -> str:
lines = []
for i in range(0, len(data), width):
chunk = data[i : i + width]
hex_part = " ".join(f"{b:02x}" for b in chunk)
asc_part = "".join(chr(b) if 32 <= b < 127 else "." for b in chunk)
lines.append(f" {i:04x} {hex_part:<{width*3}} {asc_part}")
return "\n".join(lines)
def print_section(title: str) -> None:
print(f"\n{'─'*60}")
print(f" {title}")
print(f"{'─'*60}")
def analyse_servers(servers: list, label: str = "servers") -> None:
print(f"\n [{label}] ({len(servers)} entries)")
for s in servers:
name = s.get("name", "?")
host = s.get("host", "?")
port = s.get("port", "?")
fp = s.get("md5_fingerprint", "")
fp_display = fp if fp else "(empty)"
print(f" {name:<30} {host}:{port} md5={fp_display}")
def analyse_json_token(data: bytes) -> None:
try:
obj = json.loads(data.decode("utf-8"))
except Exception as e:
print(f" ✗ JSON parse failed: {e}")
return
print(f" ✓ Valid JSON ({len(data)} bytes decoded)")
print()
print(f" version : {obj.get('version', '(missing)')}")
print(f" service_name : {obj.get('service_name', '(missing)')}")
print(f" username : {obj.get('username', '(missing)')}")
password = obj.get('password', '(missing)')
print(f" password : {password[:4]}{'*'*(len(password)-4) if len(password)>4 else ''}")
servers = obj.get("servers", [])
analyse_servers(servers, "servers")
cz = obj.get("censored_zone_servers", [])
if cz:
analyse_servers(cz, "censored_zone_servers")
extra = [k for k in obj if k not in
("version", "service_name", "username", "password",
"servers", "censored_zone_servers")]
if extra:
print(f"\n ⚠ Unknown top-level fields (new format?): {extra}")
for k in extra:
print(f" {k} = {json.dumps(obj[k])[:120]}")
# iOS compatibility check
print()
if not servers:
print(" ✗ iOS: no servers — FPTNToken decode would fail (empty list)")
else:
missing_fp = [s.get("name") for s in servers
if not s.get("md5_fingerprint")]
if missing_fp:
print(f" ⚠ iOS: {len(missing_fp)} server(s) have empty md5_fingerprint:")
for n in missing_fp:
print(f" {n}")
else:
print(" ✓ iOS: all servers have md5_fingerprint")
def analyse_brotli_token(data: bytes) -> None:
"""Decompress a Brotli payload and analyse the resulting JSON."""
if not BROTLI_AVAILABLE:
print(" ✗ 'brotli' module not available — install it:")
print(" python3 -m venv .venv && .venv/bin/pip install brotli")
print(" .venv/bin/python3 token_debug.py ...")
return
try:
decompressed = brotli.decompress(data)
except Exception as e:
print(f" ✗ Brotli decompress failed: {e}")
return
print(f" ✓ Brotli decompressed {len(data)} → {len(decompressed)} bytes"
f" ({len(decompressed)/len(data):.1f}x expansion)")
analyse_json_token(decompressed)
def analyse_file(path: Path) -> None:
raw = path.read_text(encoding="utf-8").strip()
print_section(f"FILE: {path.name} ({len(raw)} chars)")
print(f" Raw preview : {raw[:80]}{'...' if len(raw)>80 else ''}")
prefix, b64 = sanitize(raw)
print(f" Detected prefix : '{prefix}' {'(NEW format — fptnb)' if 'fptnb' in prefix else '(legacy fptn)'}")
print(f" Cleaned b64 len : {len(b64)} chars")
decoded = try_decode_b64(b64)
if decoded is None:
print(f"\n ✗ base64 decode FAILED")
print(f" Non-base64 chars: "
f"{set(c for c in b64 if c not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=')}")
return
if "fptnb" in prefix:
print_section(f"BROTLI-compressed payload ({path.name})")
analyse_brotli_token(decoded)
else:
print_section(f"JSON payload ({path.name})")
analyse_json_token(decoded)
# ── main ─────────────────────────────────────────────────────────────────────
def main() -> None:
files = sys.argv[1:] if len(sys.argv) > 1 else []
if not files:
# auto-discover token*.txt in cwd
files = sorted(Path(".").glob("token*.txt"))
if not files:
print("Usage: python3 token_debug.py <token_file> [token_file2 ...]")
sys.exit(1)
for arg in files:
p = Path(arg)
if not p.exists():
print(f" ✗ File not found: {p}")
continue
try:
analyse_file(p)
except Exception as e:
print(f" ✗ Unexpected error analysing {p}: {e}")
print(f"\n{'─'*60}\n")
if __name__ == "__main__":
main()