Upload socks5_bridge.py with huggingface_hub
Browse files- socks5_bridge.py +112 -0
socks5_bridge.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
SOCKS5 proxy bridge: listens on 1080, forwards via HTTP CONNECT to upstream proxy.
|
| 4 |
+
Upstream = tinyproxy through Cloudflare quick tunnel.
|
| 5 |
+
"""
|
| 6 |
+
import asyncio, struct, logging, os
|
| 7 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
|
| 8 |
+
log = logging.getLogger('socks5_bridge')
|
| 9 |
+
|
| 10 |
+
UPSTREAM_PROXY = os.environ.get('UPSTREAM_PROXY', 'https://relying-technology-word-talented.trycloudflare.com')
|
| 11 |
+
LISTEN_HOST = '127.0.0.1'
|
| 12 |
+
LISTEN_PORT = 1080
|
| 13 |
+
|
| 14 |
+
async def relay(reader, writer):
|
| 15 |
+
try:
|
| 16 |
+
while True:
|
| 17 |
+
data = await reader.read(65536)
|
| 18 |
+
if not data:
|
| 19 |
+
break
|
| 20 |
+
writer.write(data)
|
| 21 |
+
await writer.drain()
|
| 22 |
+
except:
|
| 23 |
+
pass
|
| 24 |
+
finally:
|
| 25 |
+
writer.close()
|
| 26 |
+
|
| 27 |
+
async def handle_socks5(reader, writer):
|
| 28 |
+
try:
|
| 29 |
+
# SOCKS5 greeting
|
| 30 |
+
ver, nmethods = struct.unpack('!BB', await reader.readexactly(2))
|
| 31 |
+
methods = await reader.readexactly(nmethods)
|
| 32 |
+
# No auth
|
| 33 |
+
writer.write(b'\x05\x00')
|
| 34 |
+
await writer.drain()
|
| 35 |
+
|
| 36 |
+
# SOCKS5 request
|
| 37 |
+
ver, cmd, rsv, atyp = struct.unpack('!BBBB', await reader.readexactly(4))
|
| 38 |
+
if cmd != 1: # Only CONNECT
|
| 39 |
+
writer.write(b'\x05\x07\x00\x01\x00\x00\x00\x00\x00\x00')
|
| 40 |
+
writer.close()
|
| 41 |
+
return
|
| 42 |
+
|
| 43 |
+
if atyp == 1: # IPv4
|
| 44 |
+
addr = '.'.join(str(b) for b in await reader.readexactly(4))
|
| 45 |
+
elif atyp == 3: # Domain
|
| 46 |
+
alen = (await reader.readexactly(1))[0]
|
| 47 |
+
addr = (await reader.readexactly(alen)).decode()
|
| 48 |
+
elif atyp == 4: # IPv6
|
| 49 |
+
addr = ':'.join('{:02x}{:02x}'.format(*b) for b in zip(*[iter(await reader.readexactly(16))]*2))
|
| 50 |
+
else:
|
| 51 |
+
writer.close()
|
| 52 |
+
return
|
| 53 |
+
|
| 54 |
+
port = struct.unpack('!H', await reader.readexactly(2))[0]
|
| 55 |
+
target = f'{addr}:{port}'
|
| 56 |
+
log.info(f'CONNECT {target}')
|
| 57 |
+
|
| 58 |
+
# Connect to upstream proxy (CF tunnel -> tinyproxy)
|
| 59 |
+
import ssl
|
| 60 |
+
from urllib.parse import urlparse
|
| 61 |
+
parsed = urlparse(UPSTREAM_PROXY)
|
| 62 |
+
upstream_host = parsed.hostname
|
| 63 |
+
upstream_port = parsed.port or 443
|
| 64 |
+
|
| 65 |
+
ctx = ssl.create_default_context()
|
| 66 |
+
ctx.check_hostname = False
|
| 67 |
+
ctx.verify_mode = ssl.CERT_NONE
|
| 68 |
+
|
| 69 |
+
loop = asyncio.get_event_loop()
|
| 70 |
+
up_reader, up_writer = await asyncio.open_connection(
|
| 71 |
+
upstream_host, upstream_port, ssl=ctx, server_hostname=upstream_host
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Send HTTP CONNECT to upstream proxy
|
| 75 |
+
connect_req = f'CONNECT {target} HTTP/1.1\r\nHost: {target}\r\n\r\n'
|
| 76 |
+
up_writer.write(connect_req.encode())
|
| 77 |
+
await up_writer.drain()
|
| 78 |
+
|
| 79 |
+
# Read response
|
| 80 |
+
response = await up_reader.readuntil(b'\r\n\r\n')
|
| 81 |
+
resp_line = response.split(b'\r\n')[0].decode()
|
| 82 |
+
log.info(f'Upstream response: {resp_line}')
|
| 83 |
+
|
| 84 |
+
if '200' not in resp_line:
|
| 85 |
+
# Failed
|
| 86 |
+
writer.write(b'\x05\x05\x00\x01\x00\x00\x00\x00\x00\x00')
|
| 87 |
+
writer.close()
|
| 88 |
+
up_writer.close()
|
| 89 |
+
return
|
| 90 |
+
|
| 91 |
+
# Success - tell client
|
| 92 |
+
writer.write(b'\x05\x00\x00\x01\x00\x00\x00\x00\x00\x00')
|
| 93 |
+
await writer.drain()
|
| 94 |
+
|
| 95 |
+
# Relay both ways
|
| 96 |
+
await asyncio.gather(relay(reader, up_writer), relay(up_reader, writer))
|
| 97 |
+
except Exception as e:
|
| 98 |
+
log.error(f'Error: {e}')
|
| 99 |
+
try:
|
| 100 |
+
writer.close()
|
| 101 |
+
except:
|
| 102 |
+
pass
|
| 103 |
+
|
| 104 |
+
async def main():
|
| 105 |
+
server = await asyncio.start_server(handle_socks5, LISTEN_HOST, LISTEN_PORT)
|
| 106 |
+
log.info(f'SOCKS5 bridge listening on {LISTEN_HOST}:{LISTEN_PORT}')
|
| 107 |
+
log.info(f'Upstream proxy: {UPSTREAM_PROXY}')
|
| 108 |
+
async with server:
|
| 109 |
+
await server.serve_forever()
|
| 110 |
+
|
| 111 |
+
if __name__ == '__main__':
|
| 112 |
+
asyncio.run(main())
|