Update app.py
Browse files
app.py
CHANGED
|
@@ -60,6 +60,8 @@ async def extract_and_transform_proxies(input_text):
|
|
| 60 |
if proxy['type'] == 'ss':
|
| 61 |
if 'cipher' in proxy:
|
| 62 |
parts.append(f"encrypt-method={proxy['cipher'].strip()}")
|
|
|
|
|
|
|
| 63 |
elif proxy['type'] == 'trojan':
|
| 64 |
if 'password' in proxy:
|
| 65 |
parts.append(f"password={proxy['password'].strip()}")
|
|
@@ -75,6 +77,23 @@ async def extract_and_transform_proxies(input_text):
|
|
| 75 |
|
| 76 |
return "\n".join(transformed_proxies)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
async def handle_request(request):
|
| 79 |
if request.path == '/':
|
| 80 |
query_params = parse_qs(request.query_string)
|
|
@@ -120,13 +139,13 @@ async def logging_middleware(request, handler):
|
|
| 120 |
end_time = datetime.datetime.now()
|
| 121 |
|
| 122 |
timestamp = end_time.strftime('%Y-%m-%d %H:%M:%S')
|
| 123 |
-
client_ip = request
|
| 124 |
target_url = request.query.get('url', '-')
|
| 125 |
status_code = response.status
|
| 126 |
proxy_count = response.headers.get('X-Proxy-Count', '0')
|
| 127 |
cache_hit = "Hit" if response.headers.get('X-Cache-Hit') == 'True' else "Miss"
|
| 128 |
|
| 129 |
-
log_message = f"{timestamp} - {client_ip} - \"{request.method} /?url={target_url}\" -
|
| 130 |
print(log_message, flush=True)
|
| 131 |
|
| 132 |
return response
|
|
|
|
| 60 |
if proxy['type'] == 'ss':
|
| 61 |
if 'cipher' in proxy:
|
| 62 |
parts.append(f"encrypt-method={proxy['cipher'].strip()}")
|
| 63 |
+
if 'password' in proxy:
|
| 64 |
+
parts.append(f"password={proxy['password'].strip()}")
|
| 65 |
elif proxy['type'] == 'trojan':
|
| 66 |
if 'password' in proxy:
|
| 67 |
parts.append(f"password={proxy['password'].strip()}")
|
|
|
|
| 77 |
|
| 78 |
return "\n".join(transformed_proxies)
|
| 79 |
|
| 80 |
+
def get_client_ip(request):
|
| 81 |
+
"""获取客户端真实IP地址"""
|
| 82 |
+
headers_to_check = [
|
| 83 |
+
'X-Forwarded-For',
|
| 84 |
+
'X-Real-IP',
|
| 85 |
+
'CF-Connecting-IP', # Cloudflare
|
| 86 |
+
'True-Client-IP', # Akamai and Cloudflare
|
| 87 |
+
'X-Client-IP',
|
| 88 |
+
]
|
| 89 |
+
for header in headers_to_check:
|
| 90 |
+
ip = request.headers.get(header)
|
| 91 |
+
if ip:
|
| 92 |
+
# X-Forwarded-For可能包含多个IP,我们取第一个
|
| 93 |
+
return ip.split(',')[0].strip()
|
| 94 |
+
# 如果没有找到,返回远程地址
|
| 95 |
+
return request.remote
|
| 96 |
+
|
| 97 |
async def handle_request(request):
|
| 98 |
if request.path == '/':
|
| 99 |
query_params = parse_qs(request.query_string)
|
|
|
|
| 139 |
end_time = datetime.datetime.now()
|
| 140 |
|
| 141 |
timestamp = end_time.strftime('%Y-%m-%d %H:%M:%S')
|
| 142 |
+
client_ip = get_client_ip(request)
|
| 143 |
target_url = request.query.get('url', '-')
|
| 144 |
status_code = response.status
|
| 145 |
proxy_count = response.headers.get('X-Proxy-Count', '0')
|
| 146 |
cache_hit = "Hit" if response.headers.get('X-Cache-Hit') == 'True' else "Miss"
|
| 147 |
|
| 148 |
+
log_message = f"{timestamp} - {client_ip} - \"{request.method} /?url={target_url}\" - {status_code} Proxies: {proxy_count} - Cache: {cache_hit}"
|
| 149 |
print(log_message, flush=True)
|
| 150 |
|
| 151 |
return response
|