Update app.py
Browse files
app.py
CHANGED
@@ -1,153 +1,65 @@
|
|
1 |
-
from flask import Flask, request
|
2 |
-
import requests
|
3 |
-
import base64
|
4 |
-
import json
|
5 |
import re
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
def parse_node_input(node_input):
|
10 |
-
proxies_match = re.search(r'proxies:\s*\n([\s\S]*?)(?:\n\w+:|$)', node_input)
|
11 |
if not proxies_match:
|
12 |
-
return
|
13 |
|
14 |
proxies_text = proxies_match.group(1)
|
15 |
-
nodes = re.findall(r'^\s*-\s*({[^}]+})', proxies_text, re.MULTILINE)
|
16 |
|
17 |
-
parsed_nodes = []
|
18 |
-
for node in nodes:
|
19 |
-
node_dict = {}
|
20 |
-
items = re.findall(r'(\w+):\s*([^,}\n]+)', node)
|
21 |
-
for key, value in items:
|
22 |
-
if key in ['port', 'alterId']:
|
23 |
-
node_dict[key] = int(value)
|
24 |
-
elif key in ['udp', 'tls']:
|
25 |
-
node_dict[key] = value.lower() == 'true'
|
26 |
-
else:
|
27 |
-
node_dict[key] = value.strip("'\"")
|
28 |
-
|
29 |
-
ws_opts_match = re.search(r'ws-opts:\s*({[^}]+})', node)
|
30 |
-
if ws_opts_match:
|
31 |
-
ws_opts = ws_opts_match.group(1)
|
32 |
-
path_match = re.search(r'path:\s*([^,}]+)', ws_opts)
|
33 |
-
if path_match:
|
34 |
-
node_dict['ws-opts.path'] = path_match.group(1).strip("'\"")
|
35 |
-
headers_match = re.search(r'headers:\s*({[^}]+})', ws_opts)
|
36 |
-
if headers_match:
|
37 |
-
headers = headers_match.group(1)
|
38 |
-
host_match = re.search(r'Host:\s*([^,}]+)', headers)
|
39 |
-
if host_match:
|
40 |
-
node_dict['ws-opts.headers.Host'] = host_match.group(1).strip("'\"")
|
41 |
-
|
42 |
-
parsed_nodes.append(node_dict)
|
43 |
-
|
44 |
-
return parsed_nodes
|
45 |
-
|
46 |
-
def convert_to_vmess(node):
|
47 |
-
try:
|
48 |
-
path = node.get('ws-opts.path', '')
|
49 |
-
vmess_node = {
|
50 |
-
"v": "2",
|
51 |
-
"ps": node['name'],
|
52 |
-
"add": node['server'],
|
53 |
-
"port": str(node['port']),
|
54 |
-
"id": node['uuid'],
|
55 |
-
"aid": str(node.get('alterId', 0)),
|
56 |
-
"scy": node.get('cipher', 'auto'),
|
57 |
-
"net": node.get('network', 'tcp'),
|
58 |
-
"type": "none",
|
59 |
-
"host": node.get('ws-opts.headers.Host', node.get('servername', "")),
|
60 |
-
"path": path,
|
61 |
-
"tls": "tls" if node.get('tls', False) else ""
|
62 |
-
}
|
63 |
-
json_str = json.dumps(vmess_node, separators=(',', ':'))
|
64 |
-
vmess_base64 = base64.b64encode(json_str.encode()).decode()
|
65 |
-
return f"vmess://{vmess_base64}"
|
66 |
-
except KeyError as e:
|
67 |
-
return f"Error in VMess conversion: Missing {e} key"
|
68 |
-
|
69 |
-
def convert_to_vless(node):
|
70 |
try:
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
if node.get('flow', ''):
|
75 |
-
params.append(f"flow={node['flow']}")
|
76 |
-
if node.get('servername', ''):
|
77 |
-
params.append(f"sni={node['servername']}")
|
78 |
-
if node.get('ws-opts.path', ''):
|
79 |
-
params.append(f"path={node['ws-opts.path']}")
|
80 |
-
if node.get('ws-opts.headers.Host', ''):
|
81 |
-
params.append(f"host={node['ws-opts.headers.Host']}")
|
82 |
-
if node.get('client-fingerprint', ''):
|
83 |
-
params.append(f"fp={node['client-fingerprint']}")
|
84 |
-
|
85 |
-
param_str = "&".join(params)
|
86 |
-
vless_link = f"vless://{node['uuid']}@{node['server']}:{node['port']}?{param_str}#{node['name']}"
|
87 |
-
return vless_link
|
88 |
-
except KeyError as e:
|
89 |
-
return f"Error in VLess conversion: Missing {e} key"
|
90 |
|
91 |
-
|
92 |
-
try:
|
93 |
-
trojan_link = f"trojan://{node['password']}@{node['server']}:{node['port']}?sni={node.get('sni', '')}#{node['name']}"
|
94 |
-
return trojan_link
|
95 |
-
except KeyError as e:
|
96 |
-
return f"Error in Trojan conversion: Missing {e} key"
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
hysteria2_link = (
|
110 |
-
f"hysteria2://{node['password']}@{node['server']}:{node['port']}"
|
111 |
-
f"?auth={node.get('auth', '')}&skip-cert-verify={str(node.get('skip-cert-verify', False)).lower()}"
|
112 |
-
f"&udp={str(node.get('udp', False)).lower()}#{node['name']}"
|
113 |
-
)
|
114 |
-
return hysteria2_link
|
115 |
-
except KeyError as e:
|
116 |
-
return f"Error in Hysteria2 conversion: Missing {e} key"
|
117 |
|
118 |
-
|
119 |
-
def convert_nodes():
|
120 |
-
url = request.args.get('url')
|
121 |
-
if not url:
|
122 |
-
return "Please provide a URL", 400
|
123 |
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
except requests.RequestException as e:
|
129 |
-
return f"Error fetching URL: {str(e)}", 500
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
else:
|
147 |
-
|
148 |
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
151 |
|
152 |
-
if __name__ ==
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
1 |
import re
|
2 |
+
import yaml
|
3 |
+
import requests
|
4 |
+
from http.server import HTTPServer, BaseHTTPRequestHandler
|
5 |
+
from urllib.parse import urlparse, parse_qs
|
6 |
|
7 |
+
def extract_and_transform_proxies(input_text):
|
8 |
+
proxies_match = re.search(r'proxies:\s*\n((?:{.*\n)*)', input_text)
|
|
|
|
|
9 |
if not proxies_match:
|
10 |
+
return "未找到proxies部分"
|
11 |
|
12 |
proxies_text = proxies_match.group(1)
|
|
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
try:
|
15 |
+
proxies_list = yaml.safe_load(proxies_text)
|
16 |
+
except yaml.YAMLError as e:
|
17 |
+
return f"YAML解析错误: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
transformed_proxies = []
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
for proxy in proxies_list:
|
22 |
+
if proxy['type'] == 'ss':
|
23 |
+
name = proxy['name']
|
24 |
+
server = proxy['server']
|
25 |
+
port = proxy['port']
|
26 |
+
cipher = proxy['cipher']
|
27 |
+
password = proxy['password']
|
28 |
+
udp = 'true' if proxy.get('udp', False) else 'false'
|
29 |
|
30 |
+
transformed = f"{name} = ss, {server}, {port}, encrypt-method={cipher}, password={password}, udp-relay={udp}"
|
31 |
+
transformed_proxies.append(transformed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
return "\n".join(transformed_proxies)
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
class RequestHandler(BaseHTTPRequestHandler):
|
36 |
+
def do_GET(self):
|
37 |
+
parsed_path = urlparse(self.path)
|
38 |
+
query_params = parse_qs(parsed_path.query)
|
|
|
|
|
39 |
|
40 |
+
if 'url' in query_params:
|
41 |
+
url = query_params['url'][0]
|
42 |
+
try:
|
43 |
+
response = requests.get(url)
|
44 |
+
if response.status_code == 200:
|
45 |
+
input_text = response.text
|
46 |
+
result = extract_and_transform_proxies(input_text)
|
47 |
+
self.send_response(200)
|
48 |
+
self.send_header('Content-type', 'text/plain; charset=utf-8')
|
49 |
+
self.end_headers()
|
50 |
+
self.wfile.write(result.encode('utf-8'))
|
51 |
+
else:
|
52 |
+
self.send_error(response.status_code, f"Failed to fetch data from {url}")
|
53 |
+
except requests.RequestException as e:
|
54 |
+
self.send_error(500, f"Error fetching data: {str(e)}")
|
55 |
else:
|
56 |
+
self.send_error(400, "Missing 'url' parameter")
|
57 |
|
58 |
+
def run_server(port=8080):
|
59 |
+
server_address = ('', port)
|
60 |
+
httpd = HTTPServer(server_address, RequestHandler)
|
61 |
+
print(f"Server running on port {port}")
|
62 |
+
httpd.serve_forever()
|
63 |
|
64 |
+
if __name__ == "__main__":
|
65 |
+
run_server()
|