sfun commited on
Commit
a1b4ebb
1 Parent(s): 49bdf69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -113,7 +113,8 @@ async def handle_request(request):
113
  query_params = parse_qs(request.query_string)
114
  if 'url' in query_params:
115
  url = query_params['url'][0]
116
- cache_entry = cache.get(url)
 
117
  cache_hit = False
118
  new_data = False
119
 
@@ -122,7 +123,7 @@ async def handle_request(request):
122
  cache_hit = True
123
  cache_time = cache_entry.timestamp
124
 
125
- if not cache_hit or (datetime.datetime.now() - cache_entry.timestamp).total_seconds() >= 1800:
126
  try:
127
  async with aiohttp.ClientSession(connector=TCPConnector(ssl=False)) as session:
128
  input_text = await fetch_url(url, session)
@@ -146,7 +147,8 @@ async def handle_request(request):
146
  'X-Proxy-Count': str(proxy_count),
147
  'X-Cache-Hit': str(cache_hit),
148
  'X-Cache-Time': cache_time.strftime('%Y-%m-%d %H:%M:%S'),
149
- 'X-New-Data': str(new_data)
 
150
  })
151
  else:
152
  usage_guide = """
@@ -155,6 +157,7 @@ async def handle_request(request):
155
  <h1>代理配置转换工具</h1>
156
  <p>使用方法:在URL参数中提供包含代理配置的网址。</p>
157
  <p>示例:<code>http://localhost:8080/?url=https://example.com/path-to-proxy-config</code></p>
 
158
  </body>
159
  </html>
160
  """
@@ -177,8 +180,9 @@ async def logging_middleware(request, handler):
177
  cache_hit = "Hit" if response.headers.get('X-Cache-Hit') == 'True' else "Miss"
178
  cache_time = response.headers.get('X-Cache-Time', '-')
179
  new_data = "Yes" if response.headers.get('X-New-Data') == 'True' else "No"
 
180
 
181
- log_message = f"{timestamp} - {client_ip} - \"GET /?url={target_url}\" - Status: {status_code} - Proxies: {proxy_count} - Cache: {cache_hit} - CacheTime: {cache_time} - NewData: {new_data}"
182
  print(log_message, flush=True)
183
 
184
  return response
 
113
  query_params = parse_qs(request.query_string)
114
  if 'url' in query_params:
115
  url = query_params['url'][0]
116
+ no_cache = 'nocache' in query_params
117
+ cache_entry = None if no_cache else cache.get(url)
118
  cache_hit = False
119
  new_data = False
120
 
 
123
  cache_hit = True
124
  cache_time = cache_entry.timestamp
125
 
126
+ if not cache_hit or no_cache or (datetime.datetime.now() - cache_entry.timestamp).total_seconds() >= 1800:
127
  try:
128
  async with aiohttp.ClientSession(connector=TCPConnector(ssl=False)) as session:
129
  input_text = await fetch_url(url, session)
 
147
  'X-Proxy-Count': str(proxy_count),
148
  'X-Cache-Hit': str(cache_hit),
149
  'X-Cache-Time': cache_time.strftime('%Y-%m-%d %H:%M:%S'),
150
+ 'X-New-Data': str(new_data),
151
+ 'X-No-Cache': str(no_cache)
152
  })
153
  else:
154
  usage_guide = """
 
157
  <h1>代理配置转换工具</h1>
158
  <p>使用方法:在URL参数中提供包含代理配置的网址。</p>
159
  <p>示例:<code>http://localhost:8080/?url=https://example.com/path-to-proxy-config</code></p>
160
+ <p>强制获取新数据:<code>http://localhost:8080/?url=https://example.com/path-to-proxy-config&nocache</code></p>
161
  </body>
162
  </html>
163
  """
 
180
  cache_hit = "Hit" if response.headers.get('X-Cache-Hit') == 'True' else "Miss"
181
  cache_time = response.headers.get('X-Cache-Time', '-')
182
  new_data = "Yes" if response.headers.get('X-New-Data') == 'True' else "No"
183
+ no_cache = "Yes" if response.headers.get('X-No-Cache') == 'True' else "No"
184
 
185
+ log_message = f"{timestamp} - {client_ip} - \"GET /?url={target_url}\" - Status: {status_code} - Proxies: {proxy_count} - Cache: {cache_hit} - CacheTime: {cache_time} - NewData: {new_data} - NoCache: {no_cache}"
186
  print(log_message, flush=True)
187
 
188
  return response