Repository navigation
Expand file tree
/
Copy pathhttp_proxy_server.py
More file actions
450 lines (385 loc) · 17.3 KB
/
Copy pathhttp_proxy_server.py
File metadata and controls
450 lines (385 loc) · 17.3 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# -*- coding: utf-8 -*-
# @Author : yangsongh
# @File : http_proxy_server.py
import time
import errno
import select
import socket
import threading
from typing import Optional
from urllib.parse import urlparse
from utils.utils_lib import ConfigManager
from utils.utils_lib import LoggerManager
from utils.utils_lib import Utils
from utils.whitelist_manager import WhitelistManager
class HTTPProxyServer:
def __init__(self, logger: LoggerManager, config_manager: ConfigManager):
self.logger = logger
self.config_manager = config_manager
self.server_socket: Optional[socket.socket] = None
self.is_running = False
self.active_threads = {}
self.thread_counter = 0
self.thread_lock = threading.Lock()
# 从配置中获取参数
self.port = self.config_manager.cfgs.get('proxy_port', 8080)
self.max_connections = self.config_manager.cfgs.get(
'max_connections', 100)
self.timeout = self.config_manager.cfgs.get('timeout', 30)
self.bind_host = self.config_manager.cfgs.get('bind_host', '0.0.0.0')
# 初始化白名单管理器
self.whitelist_manager = WhitelistManager(logger)
self.whitelist_manager.update_config(config_manager)
def is_client_ip_allowed(self, client_address: tuple) -> bool:
"""检查客户端IP是否在白名单中"""
client_ip = client_address[0] if isinstance(
client_address, tuple) else str(client_address)
return self.whitelist_manager.is_client_ip_allowed(client_ip)
def is_domain_allowed(self, url: str, method: str) -> bool:
"""检查目标域名是否在白名单中"""
hostname = self.whitelist_manager.extract_hostname_from_url(
url, method)
if hostname is None:
# 无法提取域名时,直接拒绝
return False
return self.whitelist_manager.is_domain_allowed(hostname)
def handle_client(self, client_socket: socket.socket, client_address: tuple, thread_id: int):
"""处理客户端连接"""
try:
# 检查IP白名单
if not self.is_client_ip_allowed(client_address):
self.logger.warning(
f"线程[{thread_id}] 客户端IP {client_address[0]} 不在白名单中,拒绝连接")
client_socket.sendall(
b"HTTP/1.1 403 Forbidden\r\n\r\nIP not allowed")
return
# 接收客户端请求
request_data = client_socket.recv(4096)
if not request_data:
return
# 解析HTTP请求
request_lines = request_data.decode(
'utf-8', errors='ignore').split('\r\n')
if not request_lines:
self.logger.warning(f"线程[{thread_id}] 无法解析请求")
return
# 解析请求行
first_line = request_lines[0].split()
if len(first_line) < 2:
self.logger.warning(f"线程[{thread_id}] 无效的请求行: {first_line}")
return
# 检查域名白名单
method = first_line[0]
url = first_line[1]
if not self.is_domain_allowed(url, method):
self.logger.warning(f"线程[{thread_id}] 域名不在白名单中,拒绝访问: {url}")
client_socket.sendall(
b"HTTP/1.1 403 Forbidden\r\n\r\nDomain not allowed")
return
# 合并日志输出
client_ip = client_address[0] if isinstance(
client_address, tuple) else str(client_address)
with self.thread_lock:
current_thread_count = len(self.active_threads)
self.logger.info(
f"线程[{thread_id}] {client_ip} {method} {url} 活动线程数: {current_thread_count}")
if method == "CONNECT":
# HTTPS代理请求
self.handle_https_proxy(client_socket, url, thread_id)
else:
# HTTP代理请求
self.handle_http_proxy(
client_socket, request_data, url, thread_id)
except (ConnectionRefusedError, ConnectionAbortedError, ConnectionResetError):
self.logger.debug(f"线程[{thread_id}] 断开连接")
except socket.timeout:
self.logger.debug(f"线程[{thread_id}] 连接超时")
except Exception as e:
self.logger.error(f"线程[{thread_id}] 处理客户端请求时发生错误: {repr(e)}")
finally:
# 关闭连接
try:
client_socket.close()
except:
pass
with self.thread_lock:
if thread_id in self.active_threads:
del self.active_threads[thread_id]
current_thread_count = len(self.active_threads)
self.logger.debug(f"线程[{thread_id}] 连接处理完成,活动线程数: {current_thread_count}")
def handle_http_proxy(self, client_socket: socket.socket, request_data: bytes, url: str, thread_id: int):
"""处理HTTP代理请求"""
target_socket = None
try:
# 解析目标URL
parsed_url = urlparse(url)
target_host = parsed_url.hostname
target_port = parsed_url.port or 80
if not target_host:
self.logger.warning(f"线程[{thread_id}] 无法解析目标主机: {url}")
return
# 连接到目标服务器
self.logger.debug(
f"线程[{thread_id}] HTTP连接到 {target_host}:{target_port}")
target_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
target_socket.settimeout(self.timeout)
target_socket.connect((target_host, target_port))
# 发送请求到目标服务器
target_socket.sendall(request_data)
# 转发响应给客户端
while True:
response_data = target_socket.recv(4096)
if not response_data:
break
client_socket.sendall(response_data)
except (ConnectionRefusedError, ConnectionAbortedError, ConnectionResetError):
self.logger.debug(f"线程[{thread_id}] 断开连接")
except socket.timeout:
self.logger.debug(f"线程[{thread_id}] HTTP请求超时")
except BrokenPipeError:
pass
except Exception as e:
self.logger.error(f"线程[{thread_id}] HTTP代理处理错误: {repr(e)}")
finally:
if target_socket:
try:
target_socket.close()
except:
pass
def handle_https_proxy(self, client_socket: socket.socket, url: str, thread_id: int):
"""处理HTTPS代理请求"""
target_socket = None
try:
# 解析CONNECT请求中的目标地址
host_port = url.split(':')
target_host = host_port[0]
target_port = int(host_port[1]) if len(host_port) > 1 else 443
# 连接到目标服务器
self.logger.debug(
f"线程[{thread_id}] HTTPS连接到 {target_host}:{target_port}")
target_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
target_socket.settimeout(self.timeout)
target_socket.connect((target_host, target_port))
# 告诉客户端连接已建立
client_socket.sendall(
b"HTTP/1.1 200 Connection Established\r\n\r\n")
# 双向转发数据
self.forward_data(client_socket, target_socket)
except (ConnectionRefusedError, ConnectionAbortedError, ConnectionResetError):
self.logger.debug(f"线程[{thread_id}] 断开连接")
except socket.timeout:
self.logger.warning(f"线程[{thread_id}] HTTPS连接超时")
except Exception as e:
self.logger.error(f"线程[{thread_id}] HTTPS代理处理错误: {repr(e)}")
finally:
if target_socket:
try:
target_socket.close()
except:
pass
def forward_data(self, client_socket: socket.socket, target_socket: socket.socket):
"""使用 select 实现双向数据转发,避免多线程"""
inputs = [client_socket, target_socket]
outputs = []
# 标记套接字是否已关闭
client_closed = False
target_closed = False
while inputs and not (client_closed and target_closed):
try:
readable, writable, exceptional = select.select(
inputs, outputs, inputs, 1.0) # 1秒超时
for s in readable:
try:
# 接收数据前先检查套接字是否有效
if s.fileno() == -1:
raise OSError("Socket has been closed")
data = s.recv(4096)
if not data:
# 连接关闭:标记对应套接字状态,移除监听
if s is client_socket:
client_closed = True
if target_socket.fileno() != -1:
try:
target_socket.shutdown(socket.SHUT_WR)
except:
pass
else:
target_closed = True
if client_socket.fileno() != -1:
try:
client_socket.shutdown(socket.SHUT_WR)
except:
pass
inputs.remove(s)
continue
# 转发数据时检查目标套接字状态
if s is client_socket and not target_closed:
if target_socket.fileno() != -1:
target_socket.sendall(data)
elif s is target_socket and not client_closed:
if client_socket.fileno() != -1:
client_socket.sendall(data)
except (ConnectionResetError, BrokenPipeError, OSError) as e:
# 捕获连接重置/管道破裂/套接字关闭异常
self.logger.debug(f"数据转发异常: {repr(e)}")
if s is client_socket:
client_closed = True
else:
target_closed = True
if s in inputs:
inputs.remove(s)
continue
# 处理异常套接字(连接出错/超时等)
for s in exceptional:
self.logger.debug(
f"套接字异常,关闭连接: {s.getpeername() if s.fileno() != -1 else 'closed socket'}")
if s in inputs:
inputs.remove(s)
if s in outputs:
outputs.remove(s)
try:
s.shutdown(socket.SHUT_RDWR)
s.close()
except:
pass
if s is client_socket:
client_closed = True
else:
target_closed = True
except (socket.error, OSError) as e:
# 过滤已知的无害错误,仅记录未预期的异常
errno_code = e.errno if hasattr(e, 'errno') else -1
if errno_code in [errno.ECONNRESET, errno.EPIPE, errno.ENOTCONN]:
self.logger.debug(f"Select处理已知异常: {repr(e)}")
else:
self.logger.error(f"Select出错: {e}")
break
# 最终清理连接(确保套接字关闭)
try:
client_socket.shutdown(socket.SHUT_RDWR)
except:
pass
try:
client_socket.close()
except:
pass
try:
target_socket.shutdown(socket.SHUT_RDWR)
except:
pass
try:
target_socket.close()
except:
pass
def log_whitelist_stats(self):
"""记录白名单统计信息"""
ip_stats = self.whitelist_manager.get_ip_whitelist_stats()
domain_stats = self.whitelist_manager.get_domain_whitelist_stats()
self.logger.info(
f"IP白名单: {'启用' if ip_stats['enabled'] else '禁用'}, 规则数: {ip_stats['rule_count']}")
self.logger.info(
f"域名白名单: {'启用' if domain_stats['enabled'] else '禁用'}, 规则数: {domain_stats['rule_count']}")
def start_server(self):
"""启动代理服务器"""
try:
# 创建服务器socket
self.server_socket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.setsockopt(
socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.settimeout(1)
# 绑定地址和端口
self.server_socket.bind((self.bind_host, self.port))
self.server_socket.listen(self.max_connections)
self.is_running = True
self.logger.info(
f"HTTP代理服务器启动在 {self.bind_host}:{self.port}, 最大连接数: {self.max_connections}, 超时时间: {self.timeout}秒")
# 记录白名单状态
self.log_whitelist_stats()
while self.is_running:
try:
# 接受客户端连接
client_socket, client_address = self.server_socket.accept()
client_socket.settimeout(self.timeout)
# 检查是否超过最大连接数
with self.thread_lock:
if len(self.active_threads) >= self.max_connections:
self.logger.warning(
f"达到最大连接数限制,拒绝来自 {client_address} 的连接")
client_socket.close()
continue
# 创建新线程处理连接
self.thread_counter += 1
thread_id = self.thread_counter
client_thread = threading.Thread(
target=self.handle_client,
args=(client_socket, client_address, thread_id),
name=f'http-{self.thread_counter}',
daemon=True
)
self.active_threads[thread_id] = client_thread
client_thread.start()
except socket.timeout:
# 超时是正常的,用于检查停止信号
continue
except Exception as e:
if self.is_running:
self.logger.error(f"接受连接时发生错误: {repr(e)}")
except Exception as e:
self.logger.error(f"服务器运行错误: {repr(e)}")
finally:
self.stop_server()
def stop_server(self):
"""停止HTTP代理服务器"""
self.is_running = False
self.logger.info("正在停止HTTP代理服务器...")
if self.server_socket:
try:
self.server_socket.close()
except:
pass
# 等待所有活动线程结束
with self.thread_lock:
thread_count = len(self.active_threads)
if thread_count > 0:
self.logger.info(f"等待 {thread_count} 个活动线程结束...")
# 给线程一些时间自然结束
max_wait_time = 20 # 最大等待时间(秒)
start_time = time.time()
while time.time() - start_time < max_wait_time:
with self.thread_lock:
if not self.active_threads:
break
time.sleep(0.5)
with self.thread_lock:
if self.active_threads:
self.logger.warning(f"强制终止 {len(self.active_threads)} 个活动线程")
self.logger.info("HTTP代理服务器已停止")
def main():
"""主函数"""
Utils.sync_work_dir()
logger = LoggerManager(file_name='http_server')
deft_cfgs = {
"proxy_port": 8080,
"max_connections": 3000,
"timeout": 30,
"bind_host": "0.0.0.0",
"enable_ip_whitelist": False,
"ip_whitelist": [],
"enable_domain_whitelist": False,
"domain_whitelist": []
}
config_manager = ConfigManager(
logger, deft_cfgs=deft_cfgs, cfg_file='config_http.jsonc')
config_manager.load_configs()
http_server = HTTPProxyServer(logger, config_manager)
try:
http_server.start_server()
except KeyboardInterrupt:
logger.info("收到中断信号,正在关闭服务器...")
except Exception as e:
logger.error(f"服务器运行异常: {repr(e)}")
finally:
http_server.stop_server()
if __name__ == "__main__":
main()