sfun commited on
Commit
b6cd2aa
1 Parent(s): c5e49d6

Create app.js

Browse files
Files changed (1) hide show
  1. app.js +153 -0
app.js ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request
2
+ import requests
3
+ import base64
4
+ import json
5
+ import re
6
+
7
+ app = Flask(__name__)
8
+
9
+ def parse_node_input(node_input):
10
+ # 使用正则表达式提取 proxies 部分
11
+ proxies_match = re.search(r'proxies:\s*\n([\s\S]*?)(?:\n\w+:|$)', node_input)
12
+ if not proxies_match:
13
+ return []
14
+
15
+ proxies_text = proxies_match.group(1)
16
+ nodes = re.findall(r'^\s*-\s*({[^}]+})', proxies_text, re.MULTILINE)
17
+
18
+ parsed_nodes = []
19
+ for node in nodes:
20
+ node_dict = {}
21
+ items = re.findall(r'(\w+):\s*([^,}\n]+)', node)
22
+ for key, value in items:
23
+ if key in ['port', 'alterId']:
24
+ node_dict[key] = int(value)
25
+ elif key in ['udp', 'tls']:
26
+ node_dict[key] = value.lower() == 'true'
27
+ else:
28
+ node_dict[key] = value.strip("'\"")
29
+
30
+ # 处理 ws-opts
31
+ ws_opts_match = re.search(r'ws-opts:\s*({[^}]+})', node)
32
+ if ws_opts_match:
33
+ ws_opts = ws_opts_match.group(1)
34
+ path_match = re.search(r'path:\s*([^,}]+)', ws_opts)
35
+ if path_match:
36
+ node_dict['ws-opts.path'] = path_match.group(1).strip("'\"")
37
+ headers_match = re.search(r'headers:\s*({[^}]+})', ws_opts)
38
+ if headers_match:
39
+ headers = headers_match.group(1)
40
+ host_match = re.search(r'Host:\s*([^,}]+)', headers)
41
+ if host_match:
42
+ node_dict['ws-opts.headers.Host'] = host_match.group(1).strip("'\"")
43
+
44
+ parsed_nodes.append(node_dict)
45
+
46
+ return parsed_nodes
47
+
48
+ def convert_to_vmess(node):
49
+ try:
50
+ path = node.get('ws-opts.path', '')
51
+ vmess_node = {
52
+ "v": "2",
53
+ "ps": node['name'],
54
+ "add": node['server'],
55
+ "port": str(node['port']),
56
+ "id": node['uuid'],
57
+ "aid": str(node.get('alterId', 0)),
58
+ "scy": node.get('cipher', 'auto'),
59
+ "net": node.get('network', 'tcp'),
60
+ "type": "none",
61
+ "host": node.get('ws-opts.headers.Host', node.get('servername', "")),
62
+ "path": path,
63
+ "tls": "tls" if node.get('tls', False) else ""
64
+ }
65
+ json_str = json.dumps(vmess_node, separators=(',', ':'))
66
+ vmess_base64 = base64.b64encode(json_str.encode()).decode()
67
+ return f"vmess://{vmess_base64}"
68
+ except KeyError as e:
69
+ return f"Error in VMess conversion: Missing {e} key"
70
+
71
+ def convert_to_vless(node):
72
+ try:
73
+ params = []
74
+ if node.get('tls', False):
75
+ params.append("security=tls")
76
+ if node.get('flow', ''):
77
+ params.append(f"flow={node['flow']}")
78
+ if node.get('servername', ''):
79
+ params.append(f"sni={node['servername']}")
80
+ if node.get('ws-opts.path', ''):
81
+ params.append(f"path={node['ws-opts.path']}")
82
+ if node.get('ws-opts.headers.Host', ''):
83
+ params.append(f"host={node['ws-opts.headers.Host']}")
84
+ if node.get('client-fingerprint', ''):
85
+ params.append(f"fp={node['client-fingerprint']}")
86
+
87
+ param_str = "&".join(params)
88
+ vless_link = f"vless://{node['uuid']}@{node['server']}:{node['port']}?{param_str}#{node['name']}"
89
+ return vless_link
90
+ except KeyError as e:
91
+ return f"Error in VLess conversion: Missing {e} key"
92
+
93
+ def convert_to_trojan(node):
94
+ try:
95
+ trojan_link = f"trojan://{node['password']}@{node['server']}:{node['port']}?sni={node.get('sni', '')}#{node['name']}"
96
+ return trojan_link
97
+ except KeyError as e:
98
+ return f"Error in Trojan conversion: Missing {e} key"
99
+
100
+ def convert_to_ss(node):
101
+ try:
102
+ ss_link = f"ss://{base64.b64encode(f'{node['cipher']}:{node['password']}'.encode()).decode()}@{node['server']}:{node['port']}#{node['name']}"
103
+ return ss_link
104
+ except KeyError as e:
105
+ return f"Error in Shadowsocks conversion: Missing {e} key"
106
+
107
+ def convert_to_hysteria2(node):
108
+ try:
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
+ @app.route('/', methods=['GET'])
119
+ def convert_nodes():
120
+ url = request.args.get('url')
121
+ if not url:
122
+ return "Please provide a URL", 400
123
+
124
+ try:
125
+ response = requests.get(url)
126
+ response.raise_for_status()
127
+ node_input = response.text
128
+ except requests.RequestException as e:
129
+ return f"Error fetching URL: {str(e)}", 500
130
+
131
+ nodes = parse_node_input(node_input)
132
+
133
+ converted_nodes = []
134
+
135
+ for node in nodes:
136
+ if node['type'] == 'ss':
137
+ converted_nodes.append(convert_to_ss(node))
138
+ elif node['type'] == 'vmess':
139
+ converted_nodes.append(convert_to_vmess(node))
140
+ elif node['type'] == 'vless':
141
+ converted_nodes.append(convert_to_vless(node))
142
+ elif node['type'] == 'trojan':
143
+ converted_nodes.append(convert_to_trojan(node))
144
+ elif node['type'] == 'hysteria2':
145
+ converted_nodes.append(convert_to_hysteria2(node))
146
+ else:
147
+ converted_nodes.append(f"Unknown node type: {node['type']}")
148
+
149
+ result = "\n".join(converted_nodes)
150
+ return result, 200, {'Content-Type': 'text/plain; charset=utf-8'}
151
+
152
+ if __name__ == '__main__':
153
+ app.run(host='0.0.0.0', port=8080)