我还是习惯写 js
This commit is contained in:
+99
-49
@@ -2,7 +2,6 @@
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
import inspect
|
||||
import sys
|
||||
|
||||
# Configuration
|
||||
@@ -11,42 +10,63 @@ TARGET_PORT = 9788
|
||||
B_HOST = "localhost"
|
||||
B_PORT = 8080
|
||||
|
||||
# Global lock for socket write operations
|
||||
socket_write_lock = threading.Lock()
|
||||
|
||||
def get_thread_id():
|
||||
"""Get thread identifier for logging"""
|
||||
return threading.current_thread().getName()
|
||||
|
||||
def log_info(source, message):
|
||||
"""Log info message with line number"""
|
||||
line_number = inspect.currentframe().f_back.f_lineno
|
||||
print("[%s] [INFO] [%s] (line %d) %s" % (
|
||||
"""Log info message with thread ID"""
|
||||
thread_id = get_thread_id()
|
||||
log_line = "[%s] [INFO] [%s] [Thread:%s] %s" % (
|
||||
time.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
source,
|
||||
line_number,
|
||||
thread_id,
|
||||
message
|
||||
))
|
||||
)
|
||||
print(log_line)
|
||||
sys.stdout.flush()
|
||||
|
||||
def log_error(source, message):
|
||||
"""Log error message with line number"""
|
||||
line_number = inspect.currentframe().f_back.f_lineno
|
||||
print("[%s] [ERROR] [%s] (line %d) %s" % (
|
||||
"""Log error message with thread ID"""
|
||||
thread_id = get_thread_id()
|
||||
log_line = "[%s] [ERROR] [%s] [Thread:%s] %s" % (
|
||||
time.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
source,
|
||||
line_number,
|
||||
thread_id,
|
||||
message
|
||||
))
|
||||
)
|
||||
print(log_line)
|
||||
sys.stdout.flush()
|
||||
|
||||
def extract_request_id(request_data):
|
||||
"""Extract X-Proxy-Request-ID from request headers"""
|
||||
if "\r\n\r\n" in request_data:
|
||||
headers_end = request_data.find("\r\n\r\n")
|
||||
headers = request_data[:headers_end]
|
||||
for line in headers.split("\r\n"):
|
||||
if line.lower().startswith("x-proxy-request-id:"):
|
||||
return line.split(":", 1)[1].strip()
|
||||
return None
|
||||
|
||||
def modify_request_headers(request_data):
|
||||
"""Modify request headers: replace Origin, Referer, Host; remove Connection headers"""
|
||||
lines = request_data.split(b"\r\n")
|
||||
lines = request_data.split("\r\n")
|
||||
new_lines = []
|
||||
method = ""
|
||||
path = ""
|
||||
|
||||
if lines:
|
||||
first_line = lines[0].decode('utf-8', errors='ignore')
|
||||
first_line = lines[0]
|
||||
parts = first_line.split()
|
||||
if len(parts) >= 2:
|
||||
method = parts[0]
|
||||
path = parts[1]
|
||||
|
||||
for line in lines:
|
||||
line_str = line.decode('utf-8', errors='ignore')
|
||||
line_str = line
|
||||
|
||||
if line_str.startswith("Origin:"):
|
||||
new_lines.append("Origin: http://%s:%d" % (TARGET_HOST, TARGET_PORT))
|
||||
@@ -62,20 +82,31 @@ def modify_request_headers(request_data):
|
||||
continue
|
||||
elif line_str.lower().startswith("x-forwarded-for:"):
|
||||
continue
|
||||
elif line_str.lower().startswith("x-proxy-request-id:"):
|
||||
continue
|
||||
else:
|
||||
new_lines.append(line_str)
|
||||
|
||||
return "\r\n".join(new_lines).encode('utf-8'), method, path
|
||||
modified_data = "\r\n".join(new_lines)
|
||||
|
||||
if path.startswith("/"):
|
||||
new_path = "http://%s:%d%s" % (TARGET_HOST, TARGET_PORT, path)
|
||||
if method and modified_data:
|
||||
modified_data = modified_data.replace(path, new_path, 1)
|
||||
|
||||
return modified_data, method, path
|
||||
|
||||
def handle_request(b_socket, request_data):
|
||||
"""Handle a single request from device B"""
|
||||
def handle_request(request_data):
|
||||
"""Handle a single request from device B - completely independent thread"""
|
||||
request_id = extract_request_id(request_data)
|
||||
|
||||
modified_data, method, path = modify_request_headers(request_data)
|
||||
|
||||
body_size = 0
|
||||
if b"\r\n\r\n" in modified_data:
|
||||
body_size = len(modified_data) - modified_data.find(b"\r\n\r\n") - 4
|
||||
if "\r\n\r\n" in modified_data:
|
||||
body_size = len(modified_data) - modified_data.find("\r\n\r\n") - 4
|
||||
|
||||
log_info("System", "Request: %s %s - Body: %d bytes" % (method, path, body_size))
|
||||
log_info("System", "Request: %s %s - Body: %d bytes - ID: %s" % (method, path, body_size, request_id))
|
||||
|
||||
target_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
target_sock.settimeout(30)
|
||||
@@ -86,92 +117,105 @@ def handle_request(b_socket, request_data):
|
||||
|
||||
target_sock.sendall(modified_data)
|
||||
|
||||
response = b""
|
||||
response = ""
|
||||
while True:
|
||||
chunk = target_sock.recv(8192)
|
||||
if not chunk:
|
||||
break
|
||||
response += chunk
|
||||
|
||||
if b"\r\n\r\n" in response:
|
||||
headers_end = response.find(b"\r\n\r\n")
|
||||
headers = response[:headers_end].decode('utf-8', errors='ignore')
|
||||
if "\r\n\r\n" in response:
|
||||
headers_end = response.find("\r\n\r\n")
|
||||
headers = response[:headers_end]
|
||||
|
||||
content_length = 0
|
||||
for line in headers.split("\r\n"):
|
||||
if line.lower().startswith("content-length:"):
|
||||
content_length = int(line.split(":")[1].strip())
|
||||
content_length = int(line.split(":", 1)[1].strip())
|
||||
break
|
||||
|
||||
body_start = headers_end + 4
|
||||
if len(response) - body_start >= content_length:
|
||||
break
|
||||
|
||||
b_socket.sendall(response)
|
||||
if request_id:
|
||||
if "\r\n\r\n" in response:
|
||||
headers_end = response.find("\r\n\r\n")
|
||||
status_line = response[:headers_end].split("\r\n")[0]
|
||||
rest = response[headers_end:]
|
||||
response = status_line + "\r\nX-Proxy-Request-ID: " + request_id + rest
|
||||
|
||||
status_code = "500"
|
||||
if b"\r\n\r\n" in response:
|
||||
headers_end = response.find(b"\r\n\r\n")
|
||||
headers = response[:headers_end].decode('utf-8', errors='ignore')
|
||||
if "\r\n\r\n" in response:
|
||||
headers_end = response.find("\r\n\r\n")
|
||||
headers = response[:headers_end]
|
||||
for line in headers.split("\r\n"):
|
||||
if line.startswith("HTTP/"):
|
||||
status_code = line.split()[1]
|
||||
break
|
||||
|
||||
log_info("System", "Response: %s %s - Status: %s" % (method, path, status_code))
|
||||
log_info("System", "Response: %s %s - Status: %s - ID: %s" % (method, path, status_code, request_id))
|
||||
|
||||
return True
|
||||
return response
|
||||
|
||||
except socket.error as e:
|
||||
if e.errno == 111 or e.errno == 10061:
|
||||
log_error("System", "Cannot connect to target: %s:%d" % (TARGET_HOST, TARGET_PORT))
|
||||
error_msg = b"HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/plain\r\n\r\nCannot connect to target: %s:%d\r\n" % (TARGET_HOST.encode(), TARGET_PORT)
|
||||
b_socket.sendall(error_msg)
|
||||
return False
|
||||
error_response = "HTTP/1.1 502 Bad Gateway\r\nX-Proxy-Request-ID: " + (request_id if request_id else "") + "\r\nContent-Type: text/plain\r\n\r\nCannot connect to target: %s:%d\r\n" % (TARGET_HOST, TARGET_PORT)
|
||||
return error_response
|
||||
else:
|
||||
log_error("System", "Socket error: %s" % str(e))
|
||||
error_msg = b"HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/plain\r\n\r\nSocket error\r\n"
|
||||
b_socket.sendall(error_msg)
|
||||
return False
|
||||
error_response = "HTTP/1.1 502 Bad Gateway\r\nX-Proxy-Request-ID: " + (request_id if request_id else "") + "\r\nContent-Type: text/plain\r\n\r\nSocket error\r\n"
|
||||
return error_response
|
||||
except socket.timeout:
|
||||
log_error("System", "Target connection timeout")
|
||||
error_msg = b"HTTP/1.1 504 Gateway Timeout\r\nContent-Type: text/plain\r\n\r\nConnection timeout\r\n"
|
||||
b_socket.sendall(error_msg)
|
||||
return False
|
||||
error_response = "HTTP/1.1 504 Gateway Timeout\r\nX-Proxy-Request-ID: " + (request_id if request_id else "") + "\r\nContent-Type: text/plain\r\n\r\nConnection timeout\r\n"
|
||||
return error_response
|
||||
except Exception as e:
|
||||
log_error("System", "Error handling request: %s" % str(e))
|
||||
return False
|
||||
error_response = "HTTP/1.1 500 Internal Server Error\r\nX-Proxy-Request-ID: " + (request_id if request_id else "") + "\r\nContent-Type: text/plain\r\n\r\nInternal error\r\n"
|
||||
return error_response
|
||||
finally:
|
||||
target_sock.close()
|
||||
|
||||
def process_request(b_socket, request_data):
|
||||
"""Process a request in separate thread and send response back"""
|
||||
response = handle_request(request_data)
|
||||
|
||||
with socket_write_lock:
|
||||
try:
|
||||
b_socket.sendall(response)
|
||||
except Exception as e:
|
||||
log_error("System", "Failed to send response: %s" % str(e))
|
||||
|
||||
def listen_for_requests(sock):
|
||||
"""Listen for requests from device B"""
|
||||
sock.settimeout(300)
|
||||
|
||||
while True:
|
||||
try:
|
||||
data = b""
|
||||
data = ""
|
||||
while True:
|
||||
chunk = sock.recv(8192)
|
||||
if not chunk:
|
||||
log_info("System", "Connection closed by device B")
|
||||
return
|
||||
data += chunk
|
||||
if b"\r\n\r\n" in data:
|
||||
headers_end = data.find(b"\r\n\r\n")
|
||||
headers = data[:headers_end].decode('utf-8', errors='ignore')
|
||||
if "\r\n\r\n" in data:
|
||||
headers_end = data.find("\r\n\r\n")
|
||||
headers = data[:headers_end]
|
||||
|
||||
content_length = 0
|
||||
for line in headers.split("\r\n"):
|
||||
if line.lower().startswith("content-length:"):
|
||||
content_length = int(line.split(":")[1].strip())
|
||||
content_length = int(line.split(":", 1)[1].strip())
|
||||
break
|
||||
|
||||
body_start = headers_end + 4
|
||||
if len(data) - body_start >= content_length:
|
||||
break
|
||||
|
||||
t = threading.Thread(target=handle_request, args=(sock, data))
|
||||
t = threading.Thread(target=process_request, args=(sock, data), name="ReqHandler-%d" % threading.activeCount())
|
||||
t.daemon = True
|
||||
t.start()
|
||||
|
||||
@@ -188,16 +232,21 @@ def connect_to_b_device(b_host, b_port):
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.settimeout(10)
|
||||
log_info("System", "Connecting to device B: %s:%d" % (b_host, b_port))
|
||||
sock.connect((b_host, b_port))
|
||||
|
||||
log_info("System", "Connected to device B, waiting for PROXY_CONNECTED")
|
||||
response = sock.recv(1024)
|
||||
if not response or b"PROXY_CONNECTED" not in response:
|
||||
log_info("System", "Received from B: %r" % response)
|
||||
|
||||
if not response or "PROXY_CONNECTED" not in response:
|
||||
log_error("System", "Failed to receive connection confirmation")
|
||||
sock.close()
|
||||
time.sleep(3)
|
||||
continue
|
||||
|
||||
sock.send(b"READY\r\n")
|
||||
log_info("System", "Sending READY to device B")
|
||||
sock.sendall("READY\r\n")
|
||||
log_info("System", "Successfully connected to device B: %s:%d" % (b_host, b_port))
|
||||
|
||||
listen_for_requests(sock)
|
||||
@@ -219,6 +268,7 @@ def main():
|
||||
print("Target website:", TARGET_HOST, ":", TARGET_PORT)
|
||||
print("Device B address:", B_HOST, ":", B_PORT)
|
||||
print("=" * 60)
|
||||
sys.stdout.flush()
|
||||
|
||||
try:
|
||||
connect_to_b_device(B_HOST, B_PORT)
|
||||
|
||||
Reference in New Issue
Block a user