修改 proxy 方案

This commit is contained in:
cheney 2026-05-25 10:28:57 +08:00
parent ebcec1c463
commit cfc009f60d
3 changed files with 238 additions and 229 deletions

View File

@ -1,80 +1,163 @@
# -*- coding: utf-8 -*-
import socket import socket
import threading import threading
import sys import sys
TARGET_HOST = 'superstar.geelytravel.com' def tunnel_thread(src, dst):
TARGET_PORT = 443 """Tunnel thread: continuously forward data"""
def handle_request(sock):
"""处理从A设备收到的请求转发到目标网站"""
try: try:
# 接收来自A设备的请求
request = b""
while True: while True:
data = sock.recv(4096) data = src.recv(8192)
if not data: if not data:
break break
request += data dst.sendall(data)
except Exception as e:
pass
def handle_b_device(sock):
"""Handle communication with device B, establish tunnel to target website"""
print("\n[System] Connected to device B")
try:
while True:
request = b""
while True:
chunk = sock.recv(8192)
if not chunk:
print("[Device B] Disconnected")
return
request += chunk
if b"\r\n\r\n" in request: if b"\r\n\r\n" in request:
break break
if not request: print("[System] Received request:", len(request), "bytes")
return
# 连接到目标网站 target_host = None
target_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) target_port = 443
target_sock.connect((TARGET_HOST, TARGET_PORT))
# 发送请求 if request.startswith(b"CONNECT"):
target_sock.send(request)
# 接收响应并转发回A设备
while True:
response = target_sock.recv(4096)
if not response:
break
sock.send(response)
target_sock.close()
except Exception as e:
print(f"处理请求出错: {e}")
try: try:
sock.send(b"HTTP/1.1 500 Internal Server Error\r\n\r\n") lines = request.decode().split("\r\n")
except: connect_line = lines[0]
pass target = connect_line.split()[1]
if ':' in target:
target_host, port_str = target.split(':')
target_port = int(port_str)
else:
target_host = target
target_port = 443
except Exception as e:
print("[Error] Failed to parse CONNECT request:", e)
sock.send(b"HTTP/1.1 400 Bad Request\r\n\r\n")
continue
else:
try:
for line in request.split(b"\r\n"):
if line.lower().startswith(b"host:"):
host_part = line[5:].strip()
if b':' in host_part:
target_host, port_str = host_part.split(b':')
target_port = int(port_str)
else:
target_host = host_part.decode()
break
except Exception as e:
print("[Error] Failed to parse HTTP request:", e)
sock.send(b"HTTP/1.1 400 Bad Request\r\n\r\n")
continue
def connect_to_a_device(a_host, a_port): if not target_host:
"""连接到A设备""" sock.send(b"HTTP/1.1 400 Bad Request\r\n\r\n")
continue
print("[Target] Host:", target_host, "Port:", target_port)
target_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
target_sock.settimeout(30)
try:
target_sock.connect((target_host, target_port))
print("[Target] Connected successfully")
if request.startswith(b"CONNECT"):
sock.send(b"HTTP/1.1 200 Connection Established\r\n\r\n")
else:
target_sock.sendall(request)
t1 = threading.Thread(target=tunnel_thread, args=(sock, target_sock))
t2 = threading.Thread(target=tunnel_thread, args=(target_sock, sock))
t1.start()
t2.start()
t1.join()
t2.join()
except ConnectionRefusedError:
print("[Error] Cannot connect to target:", target_host, target_port)
error_msg = "HTTP/1.1 503 Service Unavailable\r\nContent-Type: text/plain\r\n\r\nCannot connect to " + target_host + ":" + str(target_port)
sock.send(error_msg.encode())
except socket.timeout:
print("[Error] Connection timeout")
sock.send(b"HTTP/1.1 504 Gateway Timeout\r\n\r\n")
except Exception as e:
print("[Error] Failed to connect to target:", e)
error_msg = "HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/plain\r\n\r\n" + str(e)
sock.send(error_msg.encode())
finally:
target_sock.close()
except ConnectionResetError:
print("[Device B] Connection forcibly closed")
except Exception as e:
print("[Error] Communication error with device B:", e)
finally:
sock.close()
def connect_to_b_device(b_host, b_port):
"""Connect to device B"""
while True: while True:
try: try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((a_host, a_port)) sock.settimeout(10)
print(f"成功连接到A设备: {a_host}:{a_port}") sock.connect((b_host, b_port))
print(f"准备转发请求到: {TARGET_HOST}:{TARGET_PORT}") sock.settimeout(None)
handle_request(sock) print("\n[System] Successfully connected to device B:", b_host, ":", b_port)
handle_b_device(sock)
sock.close()
print("与A设备的连接已断开")
except ConnectionRefusedError: except ConnectionRefusedError:
print(f"A设备未就绪重试中...") print("[System] Device B not ready (", b_host, ":", b_port, "), retrying in 3 seconds...")
import time
time.sleep(3)
except socket.timeout:
print("[System] Connection timeout, retrying in 3 seconds...")
import time import time
time.sleep(3) time.sleep(3)
except Exception as e: except Exception as e:
print(f"连接出错: {e}") print("[Error] Connection error:", e)
import time import time
time.sleep(3) time.sleep(3)
def main(): def main():
if len(sys.argv) < 3: if len(sys.argv) < 3:
print("用法: python proxy_client.py <A设备IP> <A设备端口>") print("=" * 60)
print("示例: python proxy_client.py 192.168.1.50 8080") print(" Proxy Client (Run on Computer A)")
print("=" * 60)
print("Usage: python proxy_client.py <Device B IP> <Device B Port>")
print("Example: python proxy_client.py 192.168.1.100 8080")
print("=" * 60)
sys.exit(1) sys.exit(1)
a_host = sys.argv[1] b_host = sys.argv[1]
a_port = int(sys.argv[2]) b_port = int(sys.argv[2])
connect_to_a_device(a_host, a_port) print("=" * 60)
print(" Proxy Client (Run on Computer A)")
print("=" * 60)
print("Device B address:", b_host, ":", b_port)
print("=" * 60)
connect_to_b_device(b_host, b_port)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -1,112 +0,0 @@
import sys
import winreg
def get_current_proxy():
"""获取当前系统代理设置"""
try:
internet_settings = winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
r'Software\Microsoft\Windows\CurrentVersion\Internet Settings',
0,
winreg.KEY_READ
)
proxy_server, _ = winreg.QueryValueEx(internet_settings, 'ProxyServer')
proxy_enable, _ = winreg.QueryValueEx(internet_settings, 'ProxyEnable')
winreg.CloseKey(internet_settings)
return {
'enabled': bool(proxy_enable),
'server': proxy_server
}
except FileNotFoundError:
return {'enabled': False, 'server': ''}
except Exception as e:
print(f"获取代理设置失败: {e}")
return {'enabled': False, 'server': ''}
def set_proxy(proxy_host, proxy_port):
"""设置系统代理"""
try:
internet_settings = winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
r'Software\Microsoft\Windows\CurrentVersion\Internet Settings',
0,
winreg.KEY_ALL_ACCESS
)
proxy_server = f'{proxy_host}:{proxy_port}'
winreg.SetValueEx(internet_settings, 'ProxyServer', 0, winreg.REG_SZ, proxy_server)
winreg.SetValueEx(internet_settings, 'ProxyEnable', 0, winreg.REG_DWORD, 1)
winreg.CloseKey(internet_settings)
print(f"✅ 系统代理已设置为: {proxy_server}")
return True
except Exception as e:
print(f"❌ 设置代理失败: {e}")
return False
def unset_proxy():
"""取消系统代理"""
try:
internet_settings = winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
r'Software\Microsoft\Windows\CurrentVersion\Internet Settings',
0,
winreg.KEY_ALL_ACCESS
)
winreg.SetValueEx(internet_settings, 'ProxyEnable', 0, winreg.REG_DWORD, 0)
winreg.CloseKey(internet_settings)
print("✅ 系统代理已取消")
return True
except Exception as e:
print(f"❌ 取消代理失败: {e}")
return False
def show_status():
"""显示当前代理状态"""
proxy = get_current_proxy()
print("\n当前代理状态:")
print(f" 代理已启用: {'' if proxy['enabled'] else ''}")
if proxy['enabled']:
print(f" 代理服务器: {proxy['server']}")
print()
def main():
if len(sys.argv) < 2:
print("=" * 50)
print(" Windows系统代理管理脚本")
print("=" * 50)
print("用法:")
print(f" {sys.argv[0]} status - 查看当前代理状态")
print(f" {sys.argv[0]} set <host> <port> - 设置代理服务器")
print(f" {sys.argv[0]} unset - 取消代理设置")
print(f" {sys.argv[0]} localhost - 快速设置为 localhost:8080")
print("\n示例:")
print(f" {sys.argv[0]} set 192.168.1.50 8080")
print(f" {sys.argv[0]} localhost")
print(f" {sys.argv[0]} unset")
print("=" * 50)
return
command = sys.argv[1].lower()
if command == 'status':
show_status()
elif command == 'set':
if len(sys.argv) < 4:
print("❌ 参数不足,请提供代理服务器地址和端口")
print(f" 示例: {sys.argv[0]} set 192.168.1.50 8080")
return
proxy_host = sys.argv[2]
proxy_port = sys.argv[3]
set_proxy(proxy_host, proxy_port)
elif command == 'unset':
unset_proxy()
elif command == 'localhost':
set_proxy('localhost', '8080')
else:
print(f"❌ 未知命令: {command}")
if __name__ == '__main__':
main()

View File

@ -1,86 +1,124 @@
# -*- coding: utf-8 -*-
import socket import socket
import threading import threading
import select import sys
TARGET_HOST = 'superstar.geelytravel.com' PORT_FOR_A = 8080 # Port for device A to connect
TARGET_PORT = 443 PORT_FOR_BROWSER = 8081 # Port for browser to connect
LISTEN_PORT = 8080
b_device_socket = None a_device_socket = None
client_socket = None a_device_lock = threading.Lock()
def handle_b_device(conn):
"""处理B设备的连接"""
global b_device_socket
b_device_socket = conn
print(f"B设备已连接: {conn.getpeername()}")
def tunnel_thread(src, dst):
"""Tunnel thread: continuously forward data"""
try: try:
while True: while True:
data = conn.recv(4096) data = src.recv(8192)
if not data: if not data:
print("B设备断开连接")
b_device_socket = None
break break
dst.sendall(data)
if client_socket: except Exception as e:
pass
finally:
try: try:
client_socket.send(data) src.close()
except:
pass
try:
dst.close()
except: except:
pass pass
except Exception as e:
print(f"B设备连接出错: {e}")
b_device_socket = None
def handle_client(conn): def handle_browser(conn, addr):
"""处理用户请求""" """Handle browser connection, establish tunnel with device A"""
global client_socket global a_device_socket
client_socket = conn
print(f"用户连接: {conn.getpeername()}") print("\n[Browser] Connected from:", addr)
with a_device_lock:
if not a_device_socket:
conn.send(b"HTTP/1.1 503 Service Unavailable\r\nContent-Type: text/plain\r\n\r\nDevice A not connected")
conn.close()
print("[Browser] Disconnected (Device A not connected):", addr)
return
t1 = threading.Thread(target=tunnel_thread, args=(conn, a_device_socket))
t2 = threading.Thread(target=tunnel_thread, args=(a_device_socket, conn))
t1.start()
t2.start()
print("[System] Tunnel established between browser and device A")
def handle_a_device(conn):
"""Handle device A connection"""
global a_device_socket
with a_device_lock:
if a_device_socket:
conn.send(b"ERROR: Another A device is already connected")
conn.close()
return
a_device_socket = conn
print("\n[Device A] Connected:", conn.getpeername())
try: try:
while True: while True:
data = conn.recv(4096) data = conn.recv(1)
if not data: if not data:
print("用户断开连接")
client_socket = None
break break
if b_device_socket:
try:
b_device_socket.send(data)
except: except:
conn.send(b"HTTP/1.1 503 Service Unavailable\r\n\r\nDevice B not connected") pass
break finally:
else: with a_device_lock:
conn.send(b"HTTP/1.1 503 Service Unavailable\r\n\r\nDevice B not connected") a_device_socket = None
break conn.close()
except Exception as e: print("[Device A] Disconnected")
print(f"用户连接出错: {e}")
client_socket = None
def run_server(): def run_browser_server():
"""Start browser listening port"""
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('0.0.0.0', LISTEN_PORT)) server.bind(('0.0.0.0', PORT_FOR_BROWSER))
server.listen(5) server.listen(10)
print(f"A设备代理服务器已启动监听端口 {LISTEN_PORT}") print("[System] Browser proxy port started, listening on 0.0.0.0:", PORT_FOR_BROWSER)
print(f"等待B设备连接和用户请求...")
while True: while True:
conn, addr = server.accept() conn, addr = server.accept()
print(f"新连接: {addr}") t = threading.Thread(target=handle_browser, args=(conn, addr))
t.start()
# 判断是B设备还是用户简单判断先连接的是B设备 def run_a_device_server():
if b_device_socket is None: """Start device A connection port"""
# 第一个连接作为B设备 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
t = threading.Thread(target=handle_b_device, args=(conn,)) server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
t.start() server.bind(('0.0.0.0', PORT_FOR_A))
else: server.listen(1)
# 后续连接作为用户
t = threading.Thread(target=handle_client, args=(conn,)) print("[System] Device A port started, listening on 0.0.0.0:", PORT_FOR_A)
while True:
conn, addr = server.accept()
t = threading.Thread(target=handle_a_device, args=(conn,))
t.start() t.start()
def main():
print("=" * 60)
print(" Proxy Server (Run on Computer B)")
print("=" * 60)
print("Device A connection port:", PORT_FOR_A)
print("Browser access port:", PORT_FOR_BROWSER)
print("=" * 60)
print("Waiting for connections...\n")
t1 = threading.Thread(target=run_a_device_server)
t2 = threading.Thread(target=run_browser_server)
t1.start()
t2.start()
t1.join()
t2.join()
if __name__ == '__main__': if __name__ == '__main__':
run_server() main()