133 lines
4.0 KiB
Python
133 lines
4.0 KiB
Python
import socket
|
|
import threading
|
|
import sys
|
|
import configparser
|
|
import os
|
|
|
|
|
|
def load_config(config_path='config.ini'):
|
|
"""加载配置文件"""
|
|
config = configparser.ConfigParser()
|
|
|
|
# 如果配置文件不存在,创建默认配置
|
|
if not os.path.exists(config_path):
|
|
print(f"配置文件 {config_path} 不存在,使用默认配置")
|
|
return {
|
|
'protocol': 'https',
|
|
'target_host': 'zentao.sunyard.com.cn',
|
|
'target_port': 9788
|
|
}
|
|
|
|
config.read(config_path, encoding='utf-8')
|
|
|
|
if 'proxy' not in config:
|
|
print(f"配置文件格式错误,使用默认配置")
|
|
return {
|
|
'protocol': 'https',
|
|
'target_host': 'zentao.sunyard.com.cn',
|
|
'target_port': 9788
|
|
}
|
|
|
|
protocol = config['proxy'].get('protocol', 'https').lower()
|
|
target_host = config['proxy'].get('target_host', 'zentao.sunyard.com.cn')
|
|
target_port = config['proxy'].getint('target_port', 9788)
|
|
|
|
# 如果没有设置端口,根据协议设置默认端口
|
|
if target_port == 0:
|
|
target_port = 443 if protocol == 'https' else 80
|
|
|
|
return {
|
|
'protocol': protocol,
|
|
'target_host': target_host,
|
|
'target_port': target_port
|
|
}
|
|
|
|
def handle_request(sock, target_host, target_port):
|
|
"""处理从A设备收到的请求,转发到目标网站"""
|
|
|
|
try:
|
|
# 接收来自A设备的请求
|
|
request = b""
|
|
while True:
|
|
data = sock.recv(4096)
|
|
if not data:
|
|
break
|
|
request += data
|
|
if b"\r\n\r\n" in request:
|
|
break
|
|
|
|
if not request:
|
|
return
|
|
|
|
# 连接到目标网站
|
|
target_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
target_sock.connect((target_host, target_port))
|
|
|
|
# 发送请求
|
|
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:
|
|
sock.send(b"HTTP/1.1 500 Internal Server Error\r\n\r\n")
|
|
except:
|
|
pass
|
|
|
|
def connect_to_a_device(a_host, a_port, target_host, target_port):
|
|
"""连接到A设备"""
|
|
while True:
|
|
try:
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
sock.connect((a_host, a_port))
|
|
print(f"成功连接到A设备: {a_host}:{a_port}")
|
|
print(f"准备转发请求到: {target_host}:{target_port}")
|
|
|
|
handle_request(sock, target_host, target_port)
|
|
|
|
sock.close()
|
|
print("与A设备的连接已断开")
|
|
except ConnectionRefusedError:
|
|
print(f"A设备未就绪,重试中...")
|
|
import time
|
|
time.sleep(3)
|
|
except Exception as e:
|
|
print(f"连接出错: {e}")
|
|
import time
|
|
time.sleep(3)
|
|
|
|
def main():
|
|
|
|
# 加载配置
|
|
config = load_config()
|
|
|
|
|
|
# 解析命令行参数
|
|
if len(sys.argv) < 3:
|
|
print("用法: python proxy_client.py <A设备IP> <A设备端口>")
|
|
print(f"当前配置: {config['protocol']}://{config['target_host']}:{config['target_port']}")
|
|
print("示例: python proxy_client.py 192.168.1.50 8080")
|
|
sys.exit(1)
|
|
|
|
a_host = sys.argv[1]
|
|
a_port = int(sys.argv[2])
|
|
|
|
# 可选:通过命令行覆盖目标配置
|
|
if len(sys.argv) >= 4:
|
|
config['target_host'] = sys.argv[3]
|
|
if len(sys.argv) >= 5:
|
|
config['target_port'] = int(sys.argv[4])
|
|
|
|
print(f"使用配置: {config['protocol']}://{config['target_host']}:{config['target_port']}")
|
|
connect_to_a_device(a_host, a_port, config['target_host'], config['target_port'])
|
|
|
|
if __name__ == '__main__':
|
|
main()
|