-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl2config.js
258 lines (242 loc) · 10 KB
/
url2config.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const urlparse = require('url-parse');
const querystring = require('querystring');
const { Buffer } = require('buffer');
// import { Buffer } from 'buffer';
const baseConfig = '{"log":{"access":"","error":"","loglevel":"none"},"inbounds":[{"tag":"proxy","port":10808,"listen":"127.0.0.1","protocol":"socks","sniffing":{"enabled":true,"destOverride":["http","tls"]},"settings":{"auth":"noauth","udp":true,"ip":null,"address":null,"clients":null},"streamSettings":null}],"outbounds":[{"tag":"proxy","protocol":"","settings":{"vnext":[{"users":[{"security":"auto"}]}],"servers":null,"response":null},"streamSettings":{"network":"tcp","security":null,"tlsSettings":{"allowInsecure":true,"fingerprint":"randomized"},"kcpSettings":{"mtu":1350,"tti":50,"uplinkCapacity":12,"downlinkCapacity":100,"congestion":false,"readBufferSize":2,"writeBufferSize":2,"header":{"type":"wechat-video"}},"wsSettings":{"connectionReuse":true,"path":"/","headers":{"Host":""}},"httpSettings":{"host":[""],"path":"/"},"quicSettings":{"security":"none","key":"","header":{"type":"none"}},"tcpSettings":{"connectionReuse":true,"header":{"type":"http","request":{"version":"1.1","method":"GET","path":["/"],"headers":{"Host":[""],"User-Agent":["Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36","Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/53.0.2785.109 Mobile/14A456 Safari/601.1.46"],"Accept-Encoding":["gzip, deflate"],"Connection":["keep-alive"],"Pragma":"no-cache"}}}}},"mux":{"enabled":false}}],"stats":{},"dns":{"servers":["1.1.1.1","8.8.8.8"]},"routing":{"domainStrategy":"AsIs","rules":[]}}';
function streamSettings(inp, data) {
var config = {}
config.network = data.net;
if (data.tls) {
config.security = data.tls;
if (data.tls != "none") {
if (data.sni)
inp.tlsSettings.serverName = data.sni;
else if (data.host)
inp.tlsSettings.serverName = data.host;
if (data.alpn)
inp.tlsSettings.alpn = data.alpn.split(",");
if (data.fp)
inp.tlsSettings.fingerprint = data.fp
if (data.pbk)
inp.tlsSettings.publicKey = data.pbk
if (data.sid)
inp.tlsSettings.shortId = data.sid
if (data.spx)
inp.tlsSettings.spiderX = data.spx
config[data.tls + "Settings"] = inp.tlsSettings
}
}
if (data.net === 'kcp') {
const { kcpSettings } = inp;
kcpSettings.header.type = data.type;
config.kcpSettings = kcpSettings
} else if (data.net === 'ws') {
const { wsSettings } = inp;
if (data.host) wsSettings.headers.Host = data.host;
if (data.path) wsSettings.path = data.path;
config.wsSettings = wsSettings
} else if (data.net === 'h2') {
const { httpSettings } = inp;
if (data.host) httpSettings.host = data.host.split(',');
httpSettings.path = data.path;
config.httpSettings = httpSettings
} else if (data.net === 'quic') {
const { quicSettings } = inp;
quicSettings.security = data.host;
quicSettings.key = data.path;
quicSettings.header.type = data.type;
config.quicSettings = quicSettings
} else if (data.net === 'tcp') {
if (data.type === 'http') {
const { tcpSettings } = inp;
tcpSettings.header.request.headers.Host = data.host;
tcpSettings.header.request.path = [data.path];
config.tcpSettings = tcpSettings
}
} else {
}
return config
}
function vmess(config, data) {
const [vnext] = config.settings.vnext;
const [user] = vnext.users;
vnext.address = data.add;
vnext.port = +data.port;
user.id = data.id;
user.security = data.scy;
user.alterId = +data.aid;
config.protocol = data.protocol;
config.tag = data.ps;
}
function vless(config, data) {
const [vnext] = config.settings.vnext;
const [user] = vnext.users;
vnext.address = data.add;
vnext.port = +data.port;
user.id = data.id;
user.security = data.scy;
if (data.enc) user.encryption = data.enc;
if (data.flow) user.flow = data.flow;
config.protocol = data.protocol;
config.tag = data.ps;
}
function trojan(config, data) {
delete config.settings.vnext;
config.settings.servers = [{}]
const [servers] = config.settings.servers;
servers.address = data.add;
servers.port = +data.port;
servers.password = data.id;
servers.method = data.scy;
servers.ota = false;
if (data.enc) servers.encryption = data.enc;
if (data.flow) servers.flow = data.flow;
config.protocol = data.protocol;
config.tag = data.ps;
}
function shadowSocks(config, data) {
delete config.settings.vnext;
config.settings.servers = [{}]
const [servers] = config.settings.servers;
servers.address = data.add;
servers.port = +data.port;
servers.password = data.id;
servers.method = data.scy;
servers.ota = false;
config.protocol = "shadowsocks";
config.tag = data.ps;
}
function socks(config, data) {
delete config.settings.vnext;
if (data.id || data.scy)
config.settings.servers = [{ "users": [{}] }]
else
config.settings.servers = [{}]
const [servers] = config.settings.servers;
servers.address = data.add;
servers.port = +data.port;
if (data.id) servers.users[0].pass = data.id;
if (data.scy) servers.users[0].user = data.scy;
servers.ota = false;
config.protocol = "socks";
config.tag = data.ps;
}
function outbound(data) {
var config = JSON.parse(baseConfig);
const [outbounds] = config.outbounds;
if (data.protocol === 'vmess') {
vmess(outbounds, data);
} else if (data.protocol === 'vless') {
vless(outbounds, data);
} else if (data.protocol === "trojan") {
trojan(outbounds, data);
} else if (data.protocol === "ss") {
shadowSocks(outbounds, data);
} else if (data.protocol === "socks") {
socks(outbounds, data);
}
outbounds.streamSettings = streamSettings(outbounds.streamSettings, data);
return config
}
function parseProtocol(url) {
try {
if (!url) return false;
let temp = url.split("://");
let protocol = temp[0].toLowerCase();
if (protocol === "vmess") {
let vmDec = Buffer.from(url.slice(8), 'base64').toString();
if (!vmDec) return false;
vmDec=JSON.parse(vmDec)
if (!vmDec.path)
vmDec.path = "/"
return { protocol: protocol, ...vmDec };
} else if (protocol === "vless" || protocol === "trojan") {
let parsed_url = urlparse(decodeURIComponent(url));
let netquery = Object.fromEntries(Object.entries(querystring.parse(parsed_url.query)).map(([k, v]) => [k, v.length > 1 ? v : v[0]]));
return {
protocol: protocol,
ps: parsed_url.hash.substring(1) || "none",
add: parsed_url.hostname || "none",
port: Number(parsed_url.port) || 0,
id: parsed_url.auth || "",
scy: "auto",
net: netquery.type || "none",
type: netquery.headerType || "",
host: netquery.host || "",
path: netquery.path || "/",
tls: netquery.security || "",
enc: netquery.encryption || "none",
sni: netquery.sni || "",
flow: netquery.flow || "",
fp: netquery.fp || "",
pbk: netquery.pbk || "",
sid: netquery.sid || "",
spx: netquery.spx || "",
alpn: netquery.alpn
}
} else if (protocol === "ss") {
let parsed_url = urlparse(decodeURIComponent(url));
parsed_url.auth = Buffer.from(parsed_url.auth, 'base64').toString().split(":")
return {
protocol: protocol,
ps: parsed_url.hash.substring(1) || "none",
add: parsed_url.hostname || "none",
port: Number(parsed_url.port) || 0,
id: parsed_url.auth[1] == "null" ? "" : parsed_url.auth[1] || "",
scy: parsed_url.auth[0] == "null" ? "auto" : parsed_url.auth[0] || "auto",
net: "tcp"
}
} else if (protocol === "socks") {
let parsed_url = urlparse(decodeURIComponent(url));
parsed_url.auth = Buffer.from(parsed_url.auth, 'base64').toString().split(":")
return {
protocol: protocol,
ps: parsed_url.hash.substring(1) || "none",
add: parsed_url.hostname || "none",
port: Number(parsed_url.port) || 0,
id: parsed_url.auth[1] == "null" ? "" : parsed_url.auth[1] || "",
scy: parsed_url.auth[0] == "null" ? "" : parsed_url.auth[0] || "",
net: "tcp"
}
}
} catch (error) {
console.log(error)
return false
}
}
function url2config({ config, data, url, port, listen, dns }) {
if (!config) {
if (!data) {
if (!url)
return false;
data = parseProtocol(url);
}
if (!data)
return false;
config = outbound(data);
}
// ====================================
if (!config.inbounds)
config.inbounds = []
var inbound;
for (var i in config.inbounds) {
if (config.inbounds[i].protocol == "socks") {
inbound = config.inbounds[i];
}
}
if (!inbound) {
config.inbounds.push({ "protocol": "socks", "settings": { "auth": "noauth", "udp": true } })
inbound = config.inbounds[0]
}
// ====================================
if (port) inbound.port = port;
if (listen) inbound.listen = listen;
if (!config.dns)
config.dns = {};
if (dns && !config.dns.servers) config.dns.servers = dns;
return config
}
module.exports = {
parse: parseProtocol,
url2config: url2config
};