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
| """ SOCKS5 to HTTP Proxy Converter 将 Trojan SOCKS5 代理转换为 HTTP 代理
SOCKS5: 127.0.0.1:1080 -> HTTP: 127.0.0.1:8118 """
import socket import select import struct import threading import sys
SOCKS5_HOST = '127.0.0.1' SOCKS5_PORT = 1080 HTTP_PORT = 8118 BUFFER_SIZE = 65536
def connect_socks5(target_host, target_port): """通过 SOCKS5 代理连接目标""" sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(10) try: sock.connect((SOCKS5_HOST, SOCKS5_PORT)) sock.send(b'\x05\x01\x00') response = sock.recv(2) if response != b'\x05\x00': return None request = b'\x05\x01\x00\x03' request += bytes([len(target_host)]) + target_host.encode() request += struct.pack('>H', target_port) sock.send(request) response = sock.recv(10) if response[1] != 0: return None return sock except Exception as e: print(f"SOCKS5 连接失败: {e}", file=sys.stderr) return None
def handle_client(client_socket): """处理客户端请求""" try: request = client_socket.recv(BUFFER_SIZE).decode('utf-8', errors='ignore') if not request: client_socket.close() return lines = request.split('\r\n') if not lines: client_socket.close() return request_line = lines[0] parts = request_line.split(' ') if len(parts) < 3: client_socket.close() return method = parts[0] url = parts[1] if method == 'CONNECT': host_port = url.split(':') target_host = host_port[0] target_port = int(host_port[1]) if len(host_port) > 1 else 443 target_socket = connect_socks5(target_host, target_port) if target_socket: client_socket.send(b'HTTP/1.1 200 Connection Established\r\n\r\n') def forward(src, dst): try: while True: data = src.recv(BUFFER_SIZE) if not data: break dst.send(data) except: pass t1 = threading.Thread(target=forward, args=(client_socket, target_socket)) t2 = threading.Thread(target=forward, args=(target_socket, client_socket)) t1.daemon = True t2.daemon = True t1.start() t2.start() t1.join() t2.join() else: client_socket.send(b'HTTP/1.1 502 Bad Gateway\r\n\r\n') client_socket.close() else: if url.startswith('http://'): url_part = url[7:] if '/' in url_part: host_port = url_part.split('/')[0] path = '/' + '/'.join(url_part.split('/')[1:]) else: host_port = url_part path = '/' else: client_socket.close() return if ':' in host_port: target_host, port = host_port.split(':') target_port = int(port) else: target_host = host_port target_port = 80 target_socket = connect_socks5(target_host, target_port) if target_socket: new_request = f"{method} {path} HTTP/1.1\r\n" new_request += '\r\n'.join(lines[1:]) + '\r\n\r\n' target_socket.send(new_request.encode()) while True: data = target_socket.recv(BUFFER_SIZE) if not data: break client_socket.send(data) target_socket.close() client_socket.close() except Exception as e: print(f"处理请求失败: {e}", file=sys.stderr) client_socket.close()
def main(): """主函数""" server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server.bind(("0.0.0.0", HTTP_PORT)) server.listen(100) print(f"HTTP 代理服务器启动: 127.0.0.1:{HTTP_PORT}") print(f"转发到 SOCKS5: {SOCKS5_HOST}:{SOCKS5_PORT}") while True: client_socket, addr = server.accept() thread = threading.Thread(target=handle_client, args=(client_socket,)) thread.daemon = True thread.start()
if __name__ == "__main__": main()
|