Add FTP and magnet backends, tracker and proxy settings, and organize client modules

Integrate pinned libcurl, libtorrent and static OpenSSL dependencies with license notices. Enable verified HTTPS trackers and authenticated HTTP/SOCKS5 proxies, add tracker TXT import without arbitrary list caps, and allow resizing the main window.

Organize client sources by responsibility and update Visual Studio projects, filters and tests. Debug and Release builds passed, along with UI, HTTP, tracker import and browser bridge regressions. Full protocol tests have an unresolved intermittent HTTPS tracker wait timeout; Windows 7 runtime validation remains outstanding.
This commit is contained in:
tongtong committed 2026-11-27 11:28:30 +08:00
1 parent a787f470ed
commit eb3df9aa3d
63 files changed
+2185 -100

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
import socket,socketserver,threading,subprocess,sys,struct,urllib.parse,ssl,tempfile
from proxy_fixture import HTTPProxy,SOCKSProxy,ProxyServer
from http.server import ThreadingHTTPServer,BaseHTTPRequestHandler
DATA=b'X'*(256*1024)
class FTP(socketserver.StreamRequestHandler):
def handle(self):
listener=None; offset=0; no_rest=False
def reply(text): self.wfile.write((text+'\r\n').encode()); self.wfile.flush()
reply('220 local test FTP')
try:
for line in self.rfile:
command,_,arg=line.decode().strip().partition(' '); command=command.upper()
if command=='USER': reply('331 password')
elif command=='PASS': reply('230 logged in')
elif command=='SYST': reply('215 UNIX Type: L8')
elif command=='PWD': reply('257 "/"')
elif command=='FEAT': reply('211-Features\r\n SIZE\r\n REST STREAM\r\n211 End')
elif command in ('TYPE','CWD','OPTS','NOOP'): reply('200 OK')
elif command=='SIZE':
no_rest='no-rest' in arg; reply('550 missing' if 'missing' in arg else '213 '+str(len(DATA)))
elif command=='MDTM': reply('213 20261120000000')
elif command in ('EPSV','PASV'):
if listener: listener.close()
listener=socket.socket(); listener.bind(('127.0.0.1',0)); listener.listen(); listener.settimeout(10); port=listener.getsockname()[1]
reply('229 Entering Extended Passive Mode (|||'+str(port)+'|)' if command=='EPSV' else '227 Entering Passive Mode (127,0,0,1,'+str(port//256)+','+str(port%256)+')')
elif command=='REST':
if no_rest: reply('502 restart unavailable')
else: offset=int(arg); reply('350 restart accepted')
elif command=='RETR':
if 'no-rest' in arg and offset:
offset=0; reply('550 restart not supported'); continue
reply('150 data')
with listener.accept()[0] as peer: peer.sendall(DATA[offset:])
listener.close(); listener=None; offset=0; reply('226 transfer complete')
elif command=='QUIT': reply('221 bye'); break
else: reply('502 unsupported')
except (ConnectionError,OSError): pass
finally:
if listener: listener.close()
class Server(socketserver.ThreadingTCPServer):
allow_reuse_address=True
daemon_threads=True
class Tracker(BaseHTTPRequestHandler):
peer_port=0
announces=0
def log_message(self,*args): pass
def do_GET(self):
parsed=urllib.parse.urlsplit(self.path)
if parsed.path=='/trackers.txt': body=b'\xef\xbb\xbfudp://127.0.0.1:1234/announce\nhttp://127.0.0.1:8080/announce\nudp://127.0.0.1:1234/announce\nhttps://unsupported.test/announce\n'
elif parsed.path=='/large.txt': body=b''.join(b'https://tracker.example/'+str(i).encode()+b'/'+b'a'*180+b'\n' for i in range(6000))
elif parsed.path=='/missing.txt':
self.send_response(404); self.end_headers(); return
elif parsed.path=='/redirect.txt':
self.send_response(302); self.send_header('Location','/trackers.txt'); self.end_headers(); return
elif parsed.path=='/seed':
Tracker.peer_port=int(urllib.parse.parse_qs(parsed.query)['port'][0]); body=b'OK'
else:
Tracker.announces+=1
peers=socket.inet_aton('127.0.0.1')+struct.pack('!H',Tracker.peer_port)
body=b'd8:intervali1e5:peers6:'+peers+b'e'
self.send_response(200); self.send_header('Content-Length',str(len(body))); self.end_headers(); self.wfile.write(body)
if '--tracker-import-only' in sys.argv[2:]:
with ThreadingHTTPServer(('127.0.0.1',0),Tracker) as tracker:
threading.Thread(target=tracker.serve_forever,daemon=True).start()
result=subprocess.run([sys.argv[1],'--tracker-import-tests','http://127.0.0.1:'+str(tracker.server_address[1])],timeout=60)
tracker.shutdown()
sys.exit(result.returncode)
with tempfile.TemporaryDirectory(prefix='TTDownloader_tls_test_') as certificate_dir:
subprocess.run([sys.argv[1],'--tls-fixture',certificate_dir],check=True,timeout=20)
context=ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER); context.load_cert_chain(certificate_dir+'/cert.pem',certificate_dir+'/key.pem')
with Server(('127.0.0.1',0),FTP) as server, ThreadingHTTPServer(('127.0.0.1',0),Tracker) as tracker, ThreadingHTTPServer(('127.0.0.1',0),Tracker) as secure, ProxyServer(('127.0.0.1',0),HTTPProxy) as http_proxy, ProxyServer(('127.0.0.1',0),SOCKSProxy) as socks:
secure.socket=context.wrap_socket(secure.socket,server_side=True)
for service in (server,tracker,secure,http_proxy,socks): threading.Thread(target=service.serve_forever,daemon=True).start()
result=subprocess.run([sys.argv[1],'ftp://127.0.0.1:'+str(server.server_address[1]),'http://127.0.0.1:'+str(tracker.server_address[1]),str(http_proxy.server_address[1]),str(socks.server_address[1]),'https://127.0.0.1:'+str(secure.server_address[1])],timeout=160)
for service in (server,tracker,secure,http_proxy,socks): service.shutdown()
print("fixture paths:", Tracker.announces, HTTPProxy.gets, HTTPProxy.connects, SOCKSProxy.connections, SOCKSProxy.domains)
if result.returncode==0:
assert Tracker.announces>0 and HTTPProxy.gets>0 and HTTPProxy.connects>0 and SOCKSProxy.connections>0 and SOCKSProxy.domains>0,'proxy or tracker path was not exercised'
sys.exit(result.returncode)