sfun commited on
Commit
85d3c28
1 Parent(s): 4c39a50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -4
app.py CHANGED
@@ -72,13 +72,29 @@ async def extract_and_transform_proxies(input_text):
72
 
73
  return "\n".join(transformed_proxies) if transformed_proxies else "未找到有效的SS或Trojan代理配置"
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  async def handle_request(request):
76
  if request.path == '/':
77
  query_params = parse_qs(request.query_string)
78
  if 'url' in query_params:
79
  url = query_params['url'][0]
80
- timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
81
- print(f"{timestamp} - {request.remote} - GET /?url={url}")
82
  try:
83
  async with aiohttp.ClientSession() as session:
84
  input_text = await fetch_url(url, session)
@@ -101,11 +117,11 @@ async def handle_request(request):
101
  return web.Response(text="Not Found", status=404)
102
 
103
  async def init_app():
104
- app = web.Application()
105
  app.router.add_get('/', handle_request)
106
  return app
107
 
108
  if __name__ == "__main__":
109
  print(f"===== Application Startup at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} =====")
110
  print("Server running on port 8080")
111
- web.run_app(init_app(), port=8080)
 
72
 
73
  return "\n".join(transformed_proxies) if transformed_proxies else "未找到有效的SS或Trojan代理配置"
74
 
75
+ async def log_request(request, response):
76
+ timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
77
+ client_ip = request.remote
78
+ request_line = f"{request.method} {request.path}"
79
+ if request.query_string:
80
+ request_line += f"?{request.query_string}"
81
+ status_code = response.status
82
+ content_length = response.content_length
83
+
84
+ log_message = f"{timestamp} - {client_ip} - \"{request_line}\" {status_code} {content_length}"
85
+ print(log_message, flush=True)
86
+
87
+ @web.middleware
88
+ async def logging_middleware(request, handler):
89
+ response = await handler(request)
90
+ await log_request(request, response)
91
+ return response
92
+
93
  async def handle_request(request):
94
  if request.path == '/':
95
  query_params = parse_qs(request.query_string)
96
  if 'url' in query_params:
97
  url = query_params['url'][0]
 
 
98
  try:
99
  async with aiohttp.ClientSession() as session:
100
  input_text = await fetch_url(url, session)
 
117
  return web.Response(text="Not Found", status=404)
118
 
119
  async def init_app():
120
+ app = web.Application(middlewares=[logging_middleware])
121
  app.router.add_get('/', handle_request)
122
  return app
123
 
124
  if __name__ == "__main__":
125
  print(f"===== Application Startup at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} =====")
126
  print("Server running on port 8080")
127
+ web.run_app(init_app(), port=8080, print=lambda _: None) # Disable default startup message