Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,24 @@
|
|
| 1 |
import re
|
| 2 |
import yaml
|
| 3 |
-
import
|
|
|
|
| 4 |
import datetime
|
| 5 |
import sys
|
| 6 |
-
from
|
| 7 |
-
from urllib.parse import
|
| 8 |
|
| 9 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
-
# 尝试解析整个输入作为YAML
|
| 12 |
data = yaml.safe_load(input_text)
|
| 13 |
if isinstance(data, dict) and 'proxies' in data:
|
| 14 |
proxies_list = data['proxies']
|
| 15 |
elif isinstance(data, list):
|
| 16 |
proxies_list = data
|
| 17 |
else:
|
| 18 |
-
# 如果不是预期的格式,尝试提取proxies部分
|
| 19 |
proxies_match = re.search(r'proxies:\s*\n((?:[-\s]*{.*\n?)*)', input_text, re.MULTILINE)
|
| 20 |
if proxies_match:
|
| 21 |
proxies_text = proxies_match.group(1)
|
|
@@ -69,61 +72,40 @@ def extract_and_transform_proxies(input_text):
|
|
| 69 |
|
| 70 |
return "\n".join(transformed_proxies) if transformed_proxies else "未找到有效的SS或Trojan代理配置"
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
if 'url' in query_params:
|
| 85 |
-
url = query_params['url'][0]
|
| 86 |
-
sys.stdout.write(f"处理URL: {url}\n")
|
| 87 |
-
sys.stdout.flush()
|
| 88 |
-
try:
|
| 89 |
-
response = requests.get(url)
|
| 90 |
-
response.raise_for_status()
|
| 91 |
-
input_text = response.text
|
| 92 |
-
result = extract_and_transform_proxies(input_text)
|
| 93 |
-
self.send_response(200)
|
| 94 |
-
self.send_header('Content-type', 'text/plain; charset=utf-8')
|
| 95 |
-
self.end_headers()
|
| 96 |
-
self.wfile.write(result.encode('utf-8'))
|
| 97 |
-
except requests.RequestException as e:
|
| 98 |
-
self.send_error(500, f"Error fetching data: {str(e)}")
|
| 99 |
-
else:
|
| 100 |
-
self.send_response(200)
|
| 101 |
-
self.send_header('Content-type', 'text/html; charset=utf-8')
|
| 102 |
-
self.end_headers()
|
| 103 |
-
usage_guide = """
|
| 104 |
-
<html>
|
| 105 |
-
<body>
|
| 106 |
-
<h1>代理配置转换工具</h1>
|
| 107 |
-
<p>使用方法:在URL参数中提供包含代理配置的网址。</p>
|
| 108 |
-
<p>示例:<code>http://localhost:8080/?url=https://example.com/path-to-proxy-config</code></p>
|
| 109 |
-
</body>
|
| 110 |
-
</html>
|
| 111 |
-
"""
|
| 112 |
-
self.wfile.write(usage_guide.encode('utf-8'))
|
| 113 |
else:
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
def
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
sys.stdout.write(f"Server running on port {port}\n")
|
| 125 |
-
sys.stdout.flush()
|
| 126 |
-
httpd.serve_forever()
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|
| 129 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import yaml
|
| 3 |
+
import aiohttp
|
| 4 |
+
import asyncio
|
| 5 |
import datetime
|
| 6 |
import sys
|
| 7 |
+
from aiohttp import web
|
| 8 |
+
from urllib.parse import parse_qs
|
| 9 |
|
| 10 |
+
async def fetch_url(url, session):
|
| 11 |
+
async with session.get(url) as response:
|
| 12 |
+
return await response.text()
|
| 13 |
+
|
| 14 |
+
async def extract_and_transform_proxies(input_text):
|
| 15 |
try:
|
|
|
|
| 16 |
data = yaml.safe_load(input_text)
|
| 17 |
if isinstance(data, dict) and 'proxies' in data:
|
| 18 |
proxies_list = data['proxies']
|
| 19 |
elif isinstance(data, list):
|
| 20 |
proxies_list = data
|
| 21 |
else:
|
|
|
|
| 22 |
proxies_match = re.search(r'proxies:\s*\n((?:[-\s]*{.*\n?)*)', input_text, re.MULTILINE)
|
| 23 |
if proxies_match:
|
| 24 |
proxies_text = proxies_match.group(1)
|
|
|
|
| 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)
|
| 85 |
+
result = await extract_and_transform_proxies(input_text)
|
| 86 |
+
return web.Response(text=result, content_type='text/plain')
|
| 87 |
+
except Exception as e:
|
| 88 |
+
return web.Response(text=f"Error: {str(e)}", status=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
else:
|
| 90 |
+
usage_guide = """
|
| 91 |
+
<html>
|
| 92 |
+
<body>
|
| 93 |
+
<h1>代理配置转换工具</h1>
|
| 94 |
+
<p>使用方法:在URL参数中提供包含代理配置的网址。</p>
|
| 95 |
+
<p>示例:<code>http://localhost:8080/?url=https://example.com/path-to-proxy-config</code></p>
|
| 96 |
+
</body>
|
| 97 |
+
</html>
|
| 98 |
+
"""
|
| 99 |
+
return web.Response(text=usage_guide, content_type='text/html')
|
| 100 |
+
else:
|
| 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)
|