-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbittorrent_peer_ids.py
More file actions
209 lines (180 loc) · 6.2 KB
/
Copy pathbittorrent_peer_ids.py
File metadata and controls
209 lines (180 loc) · 6.2 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
204
205
206
207
208
209
"""Peer ID constants and parser based on BEP 0020.
Source: docs/bep_0020.rst_post.html
"""
from __future__ import annotations
from dataclasses import dataclass
import re
from typing import Optional
PEER_ID_LENGTH = 20
# Azureus-style: -XXyyyy-
AZUREUS_STYLE_CLIENTS: dict[str, str] = {
"AG": "Ares",
"A~": "Ares",
"AR": "Arctic",
"AV": "Avicora",
"AX": "BitPump",
"AZ": "Azureus",
"BB": "BitBuddy",
"BC": "BitComet",
"BF": "Bitflu",
"BG": "BTG (Rasterbar libtorrent)",
"BR": "BitRocket",
"BS": "BTSlave",
"BX": "~Bittorrent X",
"CD": "Enhanced CTorrent",
"CT": "CTorrent",
"DE": "DelugeTorrent",
"DP": "Propagate Data Client",
"EB": "EBit",
"ES": "electric sheep",
"FT": "FoxTorrent",
"FW": "FrostWire",
"FX": "Freebox BitTorrent",
"GS": "GSTorrent",
"HL": "Halite",
"HN": "Hydranode",
"KG": "KGet",
"KT": "KTorrent",
"LH": "LH-ABC",
"LP": "Lphant",
"LT": "libtorrent",
"lt": "libTorrent",
"LW": "LimeWire",
"MO": "MonoTorrent",
"MP": "MooPolice",
"MR": "Miro",
"MT": "MoonlightTorrent",
"NX": "Net Transport",
"PD": "Pando",
"qB": "qBittorrent",
"QD": "QQDownload",
"QT": "Qt 4 Torrent example",
"RT": "Retriever",
"S~": "Shareaza alpha/beta",
"SB": "~Swiftbit",
"SS": "SwarmScope",
"ST": "SymTorrent",
"st": "sharktorrent",
"SZ": "Shareaza",
"TN": "TorrentDotNET",
"TR": "Transmission",
"TS": "Torrentstorm",
"TT": "TuoTu",
"UL": "uLeecher!",
"UT": "uTorrent",
"UW": "uTorrent Web",
"VG": "Vagaa",
"WD": "WebTorrent Desktop",
"WT": "BitLet",
"WW": "WebTorrent",
"WY": "FireTorrent",
"XL": "Xunlei",
"XT": "XanTorrent",
"XX": "Xtorrent",
"ZT": "ZipTorrent",
}
AZUREUS_STYLE_UNKNOWN_IDS: dict[str, str] = {
"BD": "Unknown client (seen in wild)",
"NP": "Unknown client (seen in wild)",
"wF": "Unknown client (seen in wild)",
}
SHADOW_STYLE_CLIENTS: dict[str, str] = {
"A": "ABC",
"O": "Osprey Permaseed",
"Q": "BTQueue",
"R": "Tribler",
"S": "Shadow's client",
"T": "BitTornado",
"U": "UPnP NAT Bit Torrent",
}
SHADOW_VERSION_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-"
@dataclass(slots=True)
class ParsedPeerID:
raw: bytes
style: str
client_code: str
client_name: str
version: Optional[str] = None
extra: Optional[str] = None
def _safe_ascii(peer_id: bytes) -> str:
return peer_id.decode("ascii", errors="replace")
def _format_azureus_version(v: str) -> str:
# BEP 20: four ascii digits after client code.
return ".".join(str(int(ch)) for ch in v) if v.isdigit() else v
def parse_peer_id(peer_id: bytes) -> ParsedPeerID:
"""Parse known BitTorrent peer ID conventions from BEP 0020."""
if len(peer_id) != PEER_ID_LENGTH:
raise ValueError(f"peer_id must be {PEER_ID_LENGTH} bytes")
text = _safe_ascii(peer_id)
# Mainline style: Mx-y-z-- (remaining bytes random)
if text.startswith("M"):
m = re.match(r"^M([0-9]+)-([0-9]+)-([0-9]+)-", text[:8])
if m:
version = ".".join(m.groups())
return ParsedPeerID(peer_id, "mainline", "M", "Mainline", version)
return ParsedPeerID(peer_id, "mainline", "M", "Mainline")
# Azureus style: -XXyyyy-
m = re.match(r"^-([ -~]{2})([0-9]{4})-", text[:8])
if m:
code = m.group(1)
version_digits = m.group(2)
client_name = AZUREUS_STYLE_CLIENTS.get(code) or AZUREUS_STYLE_UNKNOWN_IDS.get(code) or "Unknown"
return ParsedPeerID(
raw=peer_id,
style="azureus",
client_code=code,
client_name=client_name,
version=_format_azureus_version(version_digits),
)
# Shadow style: [A-Z][up to 5 chars]---
if len(text) >= 8 and text[6:8] == "--":
code = text[0]
if code in SHADOW_STYLE_CLIENTS:
version_token = text[1:6].rstrip("-")
if all(ch in SHADOW_VERSION_CHARS for ch in version_token):
return ParsedPeerID(
raw=peer_id,
style="shadow",
client_code=code,
client_name=SHADOW_STYLE_CLIENTS[code],
version=version_token or None,
)
# BitComet old style: exbc + two version bytes.
if peer_id.startswith(b"exbc") or peer_id.startswith(b"FUTB"):
major = peer_id[4]
minor = peer_id[5]
name = "BitComet (FUTB patch)" if peer_id.startswith(b"FUTB") else "BitComet"
if b"LORD" in peer_id[6:12]:
name = "BitLord"
return ParsedPeerID(peer_id, "bitcomet", _safe_ascii(peer_id[:4]), name, f"{major}.{minor:02d}")
# XBT style: XBTddd[d|-]-
m = re.match(r"^XBT([0-9]{3})([d-])-", text[:8])
if m:
digits = m.group(1)
version = f"{digits[0]}.{digits[1]}.{digits[2]}"
extra = "debug" if m.group(2) == "d" else "release"
return ParsedPeerID(peer_id, "xbt", "XBT", "XBT Client", version, extra=extra)
# Opera style: OP + 4 build digits.
m = re.match(r"^OP([0-9]{4})", text[:6])
if m:
return ParsedPeerID(peer_id, "opera", "OP", "Opera", version=m.group(1))
# MLdonkey: -ML<dotted-version>-
m = re.match(r"^-ML([0-9]+(?:\.[0-9]+)*)-", text)
if m:
return ParsedPeerID(peer_id, "mldonkey", "ML", "MLdonkey", version=m.group(1))
# Bits on Wheels: -BOWxxx-
m = re.match(r"^-BOW([A-Za-z0-9]{3})-", text[:8])
if m:
return ParsedPeerID(peer_id, "bow", "BOW", "Bits on Wheels", version=m.group(1))
# Queen Bee (mainline-like but Q prefix)
if text.startswith("Q"):
m = re.match(r"^Q([0-9]+)-([0-9]+)-([0-9]+)-", text[:8])
if m:
return ParsedPeerID(peer_id, "queenbee", "Q", "Queen Bee", version=".".join(m.groups()))
# BitTyrant special case.
if text.startswith("AZ") and text[6:8] == "BT":
return ParsedPeerID(peer_id, "bittyrant", "AZ", "BitTyrant")
# TorrenTopia style noted in BEP 20.
if text.startswith("346------"):
return ParsedPeerID(peer_id, "torrentopia", "346", "TorrenTopia", version="1.90")
return ParsedPeerID(peer_id, "unknown", text[:2], "Unknown")