Files
tongtong eb3df9aa3d 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.
2026-11-27 11:28:30 +08:00

70 lines
3.4 KiB
Python

import socket,socketserver,select,base64,urllib.parse,http.client
class HTTPProxy(socketserver.StreamRequestHandler):
connects=0
gets=0
def handle(self):
try:
line=self.rfile.readline().decode().strip(); method,target,_=line.split(' ',2); headers={}
while True:
line=self.rfile.readline()
if line in (b'\r\n',b'\n',b''): break
key,value=line.decode().split(':',1); headers[key.lower()]=value.strip()
expected='Basic '+base64.b64encode(b'test:pw').decode()
if headers.get('proxy-authorization')!=expected:
self.wfile.write(b'HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic realm="local"\r\nContent-Length: 0\r\nConnection: close\r\n\r\n'); return
if method=='CONNECT':
host,port=target.rsplit(':',1); port=int(port)
if host not in ('127.0.0.1','localhost'): return
with socket.create_connection(('127.0.0.1',port),5) as peer:
HTTPProxy.connects+=1; self.wfile.write(b'HTTP/1.1 200 Connection established\r\n\r\n'); self.wfile.flush(); pump(self.connection,peer)
else:
HTTPProxy.gets+=1
parsed=urllib.parse.urlsplit(target)
if parsed.hostname in ('127.0.0.1','localhost'):
connection=http.client.HTTPConnection('127.0.0.1',parsed.port,timeout=5)
connection.request('GET',parsed.path+('?' + parsed.query if parsed.query else ''))
response=connection.getresponse(); data=response.read(); connection.close()
self.wfile.write(b'HTTP/1.1 200 OK\r\nContent-Length: '+str(len(data)).encode()+b'\r\nConnection: close\r\n\r\n'+data); return
data=b'proxied-payload'; code=b'200 OK'; extra=b''
if 'range' in headers:
start=int(headers['range'].split('=')[1].split('-')[0]); data=data[start:]; code=b'206 Partial Content'; extra=('Content-Range: bytes '+str(start)+'-14/15\r\n').encode()
self.wfile.write(b'HTTP/1.1 '+code+b'\r\n'+extra+b'Content-Length: '+str(len(data)).encode()+b'\r\nConnection: close\r\n\r\n'+data)
except (OSError,ValueError): pass
class SOCKSProxy(socketserver.StreamRequestHandler):
connections=0
domains=0
def handle(self):
def read(n):
data=self.rfile.read(n)
if len(data)!=n: raise OSError('short SOCKS frame')
return data
try:
version,n=read(2); methods=read(n)
if version!=5 or 2 not in methods: self.wfile.write(b'\x05\xff'); return
self.wfile.write(b'\x05\x02'); self.wfile.flush()
version,n=read(2); user=read(n); password=read(read(1)[0])
if user!=b'test' or password!=b'pw': self.wfile.write(b'\x01\x01'); return
self.wfile.write(b'\x01\x00'); self.wfile.flush()
version,command,_,kind=read(4)
if kind==1: host=socket.inet_ntoa(read(4))
elif kind==3: host=read(read(1)[0]).decode(); SOCKSProxy.domains+=1
elif kind==4: host=socket.inet_ntop(socket.AF_INET6,read(16))
else: return
port=int.from_bytes(read(2),'big')
if command!=1: self.wfile.write(b'\x05\x07\x00\x01'+b'\0'*6); return
if host not in ('127.0.0.1','localhost','proxy-only.invalid'): return
with socket.create_connection(('127.0.0.1',port),5) as peer:
SOCKSProxy.connections+=1; self.wfile.write(b'\x05\x00\x00\x01\x7f\x00\x00\x01'+port.to_bytes(2,'big')); self.wfile.flush(); pump(self.connection,peer)
except (OSError,ValueError): pass
def pump(a,b):
while True:
ready,_,_=select.select([a,b],[],[],10)
if not ready: return
for source in ready:
data=source.recv(65536)
if not data: return
(b if source is a else a).sendall(data)
class ProxyServer(socketserver.ThreadingTCPServer):
allow_reuse_address=True
daemon_threads=True