-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.py
197 lines (175 loc) · 4.45 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from flask import Flask, request, jsonify
import subprocess
import json
import copy
app = Flask(__name__)
# Base
base_config_template = {
"remarks": "Xray-Load-Balancer (Surfboardv2ray)",
"log": {
"access": "",
"error": "",
"loglevel": "warning"
},
"inbounds": [
{
"tag": "socks",
"port": 10808,
"listen": "0.0.0.0",
"protocol": "socks",
"sniffing": {
"enabled": True,
"destOverride": [
"http",
"tls"
],
"routeOnly": False
},
"settings": {
"auth": "noauth",
"udp": True,
"allowTransparent": False
}
},
{
"tag": "http",
"port": 10809,
"listen": "0.0.0.0",
"protocol": "http",
"sniffing": {
"enabled": True,
"destOverride": [
"http",
"tls"
],
"routeOnly": False
},
"settings": {
"auth": "noauth",
"udp": True,
"allowTransparent": False
}
},
{
"tag": "api",
"port": 10813,
"listen": "127.0.0.1",
"protocol": "dokodemo-door",
"settings": {
"udp": False,
"address": "127.0.0.1",
"allowTransparent": False
}
}
],
"outbounds": [
{
"protocol": "freedom",
"tag": "direct-out"
}
],
"stats": {},
"api": {
"tag": "api",
"services": [
"StatsService"
]
},
"policy": {
"system": {
"statsOutboundUplink": True,
"statsOutboundDownlink": True
}
},
"burstObservatory": {
"pingConfig": {
"connectivity": "http://connectivitycheck.platform.hicloud.com/generate_204",
"destination": "http://www.google.com/gen_204",
"interval": "15m",
"sampling": 10,
"timeout": "3s"
},
"subjectSelector": []
},
"dns": {
"hosts": {
"domain:googleapis.cn": "googleapis.com"
},
"servers": [
"1.1.1.1"
]
},
"routing": {
"balancers": [
{
"selector": [],
"strategy": {
"type": "leastLoad"
},
"tag": "xray-load-balancer"
}
],
"domainMatcher": "hybrid",
"domainStrategy": "IPIfNonMatch",
"rules": [
{
"balancerTag": "xray-load-balancer",
"inboundTag": [
"socks",
"http"
],
"type": "field"
}
]
}
}
@app.route("/")
def home():
return open("index.html").read()
@app.route("/convert", methods=["POST"])
def convert():
base_config = copy.deepcopy(base_config_template)
data = request.get_json()
configs = data.get("config", "").strip().splitlines()
processed_proxies = []
proxy_count = 0
for config in configs:
try:
result = subprocess.run(
["python3", "v2tj.py", config],
text=True,
capture_output=True
)
# Skip configs that fail to convert
if result.returncode != 0:
continue
# Parse the converted JSON
converted_config = json.loads(result.stdout)
# Extract outbounds with the "proxy" tag
proxy_outbound = next(
(outbound for outbound in converted_config.get("outbounds", [])
if outbound.get("tag") == "proxy"),
None
)
# Skip if no "proxy" outbound is found
if not proxy_outbound:
continue
# Rename the tag and increment the counter
proxy_count += 1
new_tag = f"proxy-{proxy_count}"
proxy_outbound["tag"] = new_tag
# Add to processed proxies and selectors
processed_proxies.append(proxy_outbound)
base_config["burstObservatory"]["subjectSelector"].append(new_tag)
base_config["routing"]["balancers"][0]["selector"].append(new_tag)
except (subprocess.CalledProcessError, json.JSONDecodeError, KeyError):
# Skip this config if an error occurs
continue
# Return an error if no proxies were successfully processed
if not processed_proxies:
return jsonify({"error": "No valid configurations could be processed."}), 400
# Add all processed proxies to the base configuration's outbounds
base_config["outbounds"] = processed_proxies + base_config["outbounds"]
return jsonify({"result": json.dumps(base_config, indent=2)})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8008)