-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathapp.js
594 lines (547 loc) · 20.9 KB
/
app.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
* Author: ivwv
* Created: April 12, 2024
* Description: A NodeJS script to Convert ChromeGo Proxies
* Website: blog.ivwv.site
* Email: [email protected]
*/
const yaml = require("js-yaml");
const fs = require("fs");
const geoip = require("geoip-lite");
const { promisify } = require("util");
const axios = require("axios");
const readFileAsync = promisify(fs.readFile);
const writeFileAsync = promisify(fs.writeFile);
let extractedProxies = [];
let serversList = [];
async function processUrls(urlsFile, processFunction) {
try {
const urls = fs
.readFileSync(urlsFile, "utf8")
.split("\n")
.filter((url) => url.trim() !== "");
for (let index = 0; index < urls.length; index++) {
const url = urls[index];
try {
// const response = await axios.get(
// url.replace("raw.githubusercontent.com", "p.ivwv.site/raw.githubusercontent.com")
// );
const response = await axios.get(url);
console.log(url);
const data = response.data;
await processFunction(data, index);
} catch (error) {
console.error(`处理 ${url} 时遇到错误: ${error}`);
}
}
} catch (error) {
console.error(`读取 ${urlsFile} 时遇到错误: ${error}`);
}
}
async function processClashMeta(data, index) {
try {
const content = yaml.load(data);
const proxies = content.proxies || [];
for (let i = 0; i < proxies.length; i++) {
const proxy = proxies[i];
if (proxy.network === "ws") {
if (
`${proxy.server}:${proxy.port}-${proxy["ws-opts"]["headers"]["host"]}-ws` in serversList
) {
continue;
}
} else if (`${proxy.server}:${proxy.port}-${proxy.type}` in serversList) {
continue;
}
proxy.name = `${await getPhysicalLocation(proxy.server)}-${proxy.type} | ${index}-${i + 1}`;
extractedProxies.push(proxy);
serversList.push(`${proxy.server}:${proxy.port}-${proxy.type}`);
}
} catch (e) {
console.error(`处理Clash Meta配置${index}时遇到错误: ${e}`);
}
}
async function processHysteria(data, index) {
try {
const content = data;
const auth = content.auth_str;
const [server, ports] = content.server.split(":");
const [serverPort, mport] = ports.split(",");
const fastOpen = content.fast_open || true;
const insecure = content.insecure;
const sni = content.server_name;
const location = await getPhysicalLocation(server);
const name = `${location}-Hysteria | ${index}-0`;
const proxy = {
name,
type: "hysteria",
server,
port: parseInt(serverPort),
ports: parseInt(mport) || parseInt(serverPort),
"auth-str": auth,
up: 80,
down: 100,
"fast-open": fastOpen,
protocol: content.protocol,
sni,
"skip-cert-verify": insecure,
alpn: content.alpn ? [content.alpn] : [],
};
if (`${proxy.server}:${proxy.port}-hysteria` in serversList) {
return;
}
extractedProxies.push(proxy);
serversList.push(`${proxy.server}:${proxy.port}-hysteria`);
} catch (e) {
console.error(`处理Hysteria配置${index}时遇到错误: ${e}`);
}
}
async function processHysteria2(data, index) {
try {
const content = data;
const auth = content["auth"];
const serverPortsSlt = content["server"].split(":");
const server = serverPortsSlt[0];
const ports = serverPortsSlt[1];
const portsSlt = ports.split(",");
const serverPort = parseInt(portsSlt[0]);
const insecure = content["tls"]["insecure"];
const sni = content["tls"]["sni"];
const location = await getPhysicalLocation(server);
const name = `${location}-Hysteria2 | ${index}-0`;
const proxy = {
name: name,
type: "hysteria2",
server: server,
port: serverPort,
password: auth,
sni: sni,
"skip-cert-verify": insecure,
};
if (!serversList.some((item) => item === `${proxy["server"]}:${proxy["port"]}-hysteria2`)) {
extractedProxies.push(proxy);
serversList.push(`${proxy["server"]}:${proxy["port"]}-hysteria2`);
} else {
return;
}
} catch (error) {
console.error(`处理Hysteria2配置${index}时遇到错误: ${error}`);
return;
}
}
async function processXray(data, index) {
try {
const content = data;
const outbounds = content["outbounds"];
const pendingProxy = outbounds[0];
const type = pendingProxy["protocol"];
if (type === "vmess") {
const server = pendingProxy["settings"]["vnext"][0]["address"];
const port = pendingProxy["settings"]["vnext"][0]["port"];
const uuid = pendingProxy["settings"]["vnext"][0]["users"][0]["id"];
const alterId = pendingProxy["settings"]["vnext"][0]["users"][0]["alterId"];
const cipher = pendingProxy["settings"]["vnext"][0]["users"][0]["security"];
const network = pendingProxy["streamSettings"]["network"];
const security = pendingProxy["streamSettings"]["security"] || "none";
const location = await getPhysicalLocation(server);
const name = `${location}-${type} | ${index}-0`;
const tls = security !== "none";
const sni = pendingProxy["streamSettings"]["tlsSettings"]
? pendingProxy["streamSettings"]["tlsSettings"]["serverName"]
: "";
const allowInsecure = pendingProxy["streamSettings"]["tlsSettings"]
? pendingProxy["streamSettings"]["tlsSettings"]["allowInsecure"]
: false;
let ws_path = "";
let ws_headers = {};
let grpc_serviceName = "";
let h2_path = "";
let h2_host = [];
if (network === "tcp" || network === "ws" || network === "grpc" || network === "h2") {
ws_path = pendingProxy["streamSettings"]["wsSettings"]
? pendingProxy["streamSettings"]["wsSettings"]["path"]
: "";
ws_headers = pendingProxy["streamSettings"]["wsSettings"]
? pendingProxy["streamSettings"]["wsSettings"]["headers"]
: {};
grpc_serviceName = pendingProxy["streamSettings"]["grpcSettings"]
? pendingProxy["streamSettings"]["grpcSettings"]["serviceName"]
: "/";
h2_path = pendingProxy["streamSettings"]["httpSettings"]
? pendingProxy["streamSettings"]["httpSettings"]["path"]
: "/";
h2_host = pendingProxy["streamSettings"]["httpSettings"]
? pendingProxy["streamSettings"]["httpSettings"]["host"]
: [];
} else {
console.error(`处理Xray配置${index}时遇到错误: 不支持的VMess传输协议: ${network}`);
return;
}
const proxy = {
name: name,
type: "vmess",
server: server,
port: port,
uuid: uuid,
alterId: alterId,
cipher: cipher,
tls: tls,
servername: sni,
"skip-cert-verify": allowInsecure,
network: network,
"ws-opts": {
path: ws_path,
headers: ws_headers,
},
"grpc-opts": {
serviceName: grpc_serviceName,
},
"h2-opts": {
path: h2_path,
host: h2_host,
},
};
if (!serversList.includes(`${proxy.server}:${proxy.port}-${proxy.type}`)) {
extractedProxies.push(proxy);
serversList.push(`${proxy.server}:${proxy.port}-${proxy.type}`);
} else {
return;
}
} else if (type === "vless") {
const server = pendingProxy["settings"]["vnext"][0]["address"];
const port = pendingProxy["settings"]["vnext"][0]["port"];
const uuid = pendingProxy["settings"]["vnext"][0]["users"][0]["id"];
const flow = pendingProxy["settings"]["vnext"][0]["users"][0]["flow"] || "";
const security = pendingProxy["streamSettings"]["security"] || "none";
const network = pendingProxy["streamSettings"]["network"];
const location = await getPhysicalLocation(server);
const name = `${location}-${type} | ${index}-0`;
const tls = security !== "none";
let sni = "";
let fingerprint = "";
let publicKey = "";
let shortId = "";
let grpc_serviceName = "";
if (security === "reality") {
const realitySettings = pendingProxy["streamSettings"]["realitySettings"] || {};
sni = realitySettings["serverName"] || "";
shortId = realitySettings["shortId"] || "";
publicKey = realitySettings["publicKey"];
fingerprint = realitySettings["fingerprint"];
grpc_serviceName = pendingProxy["streamSettings"]["grpcSettings"]
? pendingProxy["streamSettings"]["grpcSettings"]["serviceName"]
: "/";
} else {
if (network === "tcp" || network === "ws" || network === "grpc") {
sni = pendingProxy["streamSettings"]["tlsSettings"]
? pendingProxy["streamSettings"]["tlsSettings"]["serverName"]
: "";
const allowInsecure = pendingProxy["streamSettings"]["tlsSettings"]
? pendingProxy["streamSettings"]["tlsSettings"]["allowInsecure"]
: false;
const ws_path = pendingProxy["streamSettings"]["wsSettings"]
? pendingProxy["streamSettings"]["wsSettings"]["path"]
: "";
const ws_headers = pendingProxy["streamSettings"]["wsSettings"]
? pendingProxy["streamSettings"]["wsSettings"]["headers"]
: {};
grpc_serviceName = pendingProxy["streamSettings"]["grpcSettings"]
? pendingProxy["streamSettings"]["grpcSettings"]["serviceName"]
: "/";
const proxy = {
name: name,
type: "vless",
server: server,
port: port,
uuid: uuid,
tls: tls,
servername: sni,
"skip-cert-verify": allowInsecure,
network: network,
"ws-opts": {
path: ws_path,
headers: ws_headers,
},
"grpc-opts": {
serviceName: grpc_serviceName,
},
};
if (!serversList.includes(`${proxy.server}:${proxy.port}-${proxy.type}`)) {
extractedProxies.push(proxy);
serversList.push(`${proxy.server}:${proxy.port}-${proxy.type}`);
} else {
return;
}
} else {
console.error(`处理Xray配置${index}时遇到错误: 不支持的VLESS传输协议: ${network}`);
return;
}
}
const proxy = {
name: name,
type: "vless",
server: server,
port: port,
uuid: uuid,
flow: flow,
tls: tls,
servername: sni,
network: network,
"client-fingerprint": fingerprint,
"grpc-opts": {
"grpc-service-name": grpc_serviceName,
},
"reality-opts": {
"public-key": publicKey,
"short-id": shortId,
},
};
if (!serversList.includes(`${proxy.server}:${proxy.port}-${proxy.type}`)) {
extractedProxies.push(proxy);
serversList.push(`${proxy.server}:${proxy.port}-${proxy.type}`);
} else {
return;
}
} else {
console.error(`处理Xray配置${index}时遇到错误: 不支持的传输协议: ${type}`);
return;
}
} catch (error) {
console.error(`处理Xray配置${index}时遇到错误: ${error}`);
}
}
async function getPhysicalLocation(address) {
try {
const geo = geoip.lookup(address);
const country = geo ? geo.country : "CloudFlare";
return `${country} ${getFlagEmoji(country)}`;
} catch (e) {
console.error(`区域代码获取失败: ${e}`);
return "🏳 CloudFlare";
}
}
function getFlagEmoji(countryCode) {
// 将国家/地区代码转换为大写,以匹配 ISO 3166-1 alpha-2 格式
const countryCodeUpper = countryCode.toUpperCase();
// 计算国旗 emoji 的 Unicode 编码
// 每个国家/地区的 Unicode 编码范围是从 'A' 到 'Z',并且与国家/地区代码对应
const unicodeOffset = 127397; // 国旗 emoji 的 Unicode 偏移量
const firstLetter = countryCodeUpper.charCodeAt(0);
const secondLetter = countryCodeUpper.charCodeAt(1);
const emoji = String.fromCodePoint(firstLetter + unicodeOffset, secondLetter + unicodeOffset);
return emoji;
}
async function writeClashMetaProfile(templateFile, outputFile, extractedProxies) {
try {
const template = await readFileAsync(templateFile, "utf-8");
const profile = yaml.load(template);
if (!profile.proxies || profile.proxies.length === 0) {
profile.proxies = extractedProxies;
} else {
profile.proxies.push(...extractedProxies);
}
for (const group of profile["proxy-groups"]) {
if (["🚀 节点选择", "♻️ 自动选择", "⚖ 负载均衡"].includes(group.name)) {
if (!group.proxies || group.proxies.length === 0) {
group.proxies = extractedProxies.map((proxy) => proxy.name);
} else {
group.proxies.push(...extractedProxies.map((proxy) => proxy.name));
}
}
}
await writeFileAsync(outputFile, yaml.dump(profile));
} catch (e) {
console.error(`写入${outputFile}时遇到错误: ${e}`);
}
}
async function writeProxyUrlsFile(outputFile, proxies) {
let proxyUrls = [];
proxies.forEach((proxy) => {
try {
let proxyUrl;
if (proxy.type === "vless") {
let name = proxy.name;
let server = proxy.server;
let port = proxy.port;
let uuid = proxy.uuid;
let tls = parseInt(proxy.tls || 0);
let network = proxy.network;
let flow = proxy.flow || "";
let grpcServiceName = proxy?.["grpc-opts"]?.["grpc-service-name"] || "";
let wsPath = proxy?.["ws-opts"]?.["path"] || "";
let wsHeadersHost =
proxy?.["ws-opts"]?.["headers"]?.["host"] ||
proxy?.["ws-opts"]?.["headers"]?.["Host"] ||
"";
let sni = proxy.servername || "";
let publicKey = proxy?.["reality-opts"]?.["public-key"] || "";
let shortId = proxy?.["reality-opts"]?.["short-id"] || "";
let fingerprint = proxy["client-fingerprint"] || "";
let insecure = proxy["skip-cert-verify"] ? 1 : 0;
if (tls === 0) {
proxyUrl = `vless://${uuid}@${server}:${port}?encryption=none&flow=${flow}&security=none&type=${network}&serviceName=${grpcServiceName}&host=${wsHeadersHost}&path=${wsPath}#${name}`;
} else {
if (publicKey !== "") {
proxyUrl = `vless://${uuid}@${server}:${port}?encryption=none&flow=${flow}&security=reality&sni=${sni}&fp=${fingerprint}&pbk=${publicKey}&sid=${shortId}&type=${network}&serviceName=${grpcServiceName}&host=${wsHeadersHost}&path=${wsPath}#${name}`;
} else {
proxyUrl = `vless://${uuid}@${server}:${port}?encryption=none&flow=${flow}&security=tls&sni=${sni}&fp=${fingerprint}&insecure=${insecure}&type=${network}&serviceName=${grpcServiceName}&host=${wsHeadersHost}&path=${wsPath}#${name}`;
}
}
} else if (proxy.type === "vmess") {
let name = proxy.name;
let server = proxy.server;
let port = proxy.port;
let uuid = proxy.uuid;
let alterId = proxy.alterId;
let tls = parseInt(proxy.tls || 0) === 1 ? "tls" : "";
let sni = proxy.servername || "";
let network = proxy.network;
let type, path, host;
if (network === "tcp") {
type = "none";
path = "";
host = "";
} else if (network === "ws") {
type = "none";
path = proxy?.["ws-opts"]?.path || "";
host =
proxy?.["ws-opts"]?.["headers"]?.host || proxy?.["ws-opts"]?.["headers"]?.Host || "";
} else if (network === "grpc") {
type = "gun";
path = proxy?.["grpc-opts"]?.["grpc-service-name"] || "";
host = "";
} else if (network === "h2") {
type = "none";
path = proxy?.["h2-opts"]?.path || "";
host = proxy?.["h2-opts"]?.["host"]?.join(",") || "";
} else {
return;
}
let vmessMeta = {
v: "2",
ps: name,
add: server,
port: port,
id: uuid,
aid: alterId,
net: network,
type: type,
host: host,
path: path,
tls: tls,
sni: sni,
alpn: "",
};
vmessMeta = Buffer.from(JSON.stringify(vmessMeta)).toString("base64");
proxyUrl = "vmess://" + vmessMeta;
} else if (proxy.type === "ss") {
let name = proxy.name;
let server = proxy.server;
let port = proxy.port;
let password = proxy.password;
let cipher = proxy.cipher;
let ssMeta = Buffer.from(`${cipher}:${password}`).toString("base64");
proxyUrl = `ss://${ssMeta}@${server}:${port}#${name}`;
} else if (proxy.type === "hysteria") {
let name = proxy.name;
let server = proxy.server;
let port = proxy.port;
let protocol = proxy.protocol || "udp";
let insecure = parseInt(proxy["skip-cert-verify"] || 0);
let peer = proxy.sni || "";
let auth = proxy["auth-str"] || proxy["auth_str"] || "";
let upmbps = proxy.up || "11";
let downmbps = proxy.down || "55";
let alpn = (proxy.alpn || []).join(",");
let obfs = proxy.obfs || "";
proxyUrl = `hysteria://${server}:${port}/?protocol=${protocol}&insecure=${insecure}&peer=${peer}&auth=${auth}&upmbps=${upmbps}&downmbps=${downmbps}&alpn=${alpn}&obfs=${obfs}#${name}`;
} else if (proxy.type === "hysteria2") {
let name = proxy.name;
let server = proxy.server;
let port = proxy.port;
let auth = proxy.password || "";
let sni = proxy.sni || "";
let insecure = parseInt(proxy["skip-cert-verify"] || 0);
let obfs = proxy.obfs || "";
let obfsPassword = proxy["obfs-password"] || "";
if ("obfs" in proxy && proxy.obfs !== "") {
proxyUrl = `hysteria2://${auth}@${server}:${port}/?sni=${sni}&insecure=${insecure}&obfs=${obfs}&obfs-password=${obfsPassword}#${name}`;
} else {
proxyUrl = `hysteria2://${auth}@${server}:${port}/?sni=${sni}&insecure=${insecure}#${name}`;
}
} else if (proxy.type === "tuic") {
let name = proxy.name;
let server = proxy.server;
let port = proxy.port;
let uuid = proxy.uuid;
let password = proxy.password || "";
let congestionController = proxy["congestion-controller"] || "bbr";
let udpRelayMode = proxy["udp-relay-mode"] || "naive";
let sni = proxy.sni || "";
let alpn = (proxy.alpn || []).join(",");
let allowInsecure = parseInt(proxy["skip-cert-verify"] || 1);
let disableSni = parseInt(proxy["disable-sni"] || 0);
proxyUrl = `tuic://${uuid}:${password}@${server}:${port}/?congestion_controller=${congestionController}&udp_relay_mode=${udpRelayMode}&sni=${sni}&alpn=${alpn}&allow_insecure=${allowInsecure}&disable_sni=${disableSni}#${name}`;
} else {
console.error(`处理 ${proxy.name} 时遇到问题: 不支持的协议: ${proxy.type}`);
return;
}
proxyUrls.push(proxyUrl);
} catch (error) {
console.error(`处理 ${proxy.name} 时遇到问题: ${error}`);
return;
}
});
// 将 proxyUrls 写入 outputFile
await fs.promises.writeFile(outputFile, proxyUrls.join("\n"), "utf-8");
}
async function writeBase64File(outputFile, proxyUrlsFile) {
// 读取 proxyUrlsFile 文件中的内容
const proxyUrls = fs.readFileSync(proxyUrlsFile, "utf-8");
// 对代理 URL 进行编码
// const encodedProxyUrls = encodeURIComponent(proxyUrls);
// 将编码后的内容进行 Base64 编码
const base64EncodedProxyUrls = Buffer.from(proxyUrls).toString("base64");
// 将编码后的内容写入 outputFile
fs.writeFileSync(outputFile, base64EncodedProxyUrls, "utf-8");
}
function deduplicateByUUID(arr) {
return new Promise((resolve, reject) => {
try {
const uniqueItems = new Map();
arr.forEach(item => {
if (!uniqueItems.has(item.uuid)) {
uniqueItems.set(item.uuid, item);
}
});
resolve(Array.from(uniqueItems.values()));
} catch (error) {
reject(error);
}
});
}
(async () => {
// 处理clash meta urls
await processUrls("./urls/clash_meta_urls.txt", processClashMeta);
// // 处理hysteria urls
await processUrls("./urls/hysteria_urls.txt", processHysteria);
await processUrls("./urls/hysteria2_urls.txt", processHysteria2);
// 处理xray urls
await processUrls("./urls/xray_urls.txt", processXray);
// const uniqueExtractedProxies = await deduplicateByUUID(extractedProxies);
const uniqueExtractedProxies = extractedProxies;
// 写入clash meta配置
await writeClashMetaProfile(
"./templates/clash_meta_warp.yaml",
"./outputs/clash_meta_warp.yaml",
uniqueExtractedProxies
);
await writeClashMetaProfile(
"./templates/clash_meta.yaml",
"./outputs/clash_meta.yaml",
uniqueExtractedProxies
);
await writeProxyUrlsFile("./outputs/proxy-urls.txt", uniqueExtractedProxies);
// 写入base64文件
await writeBase64File("./outputs/base64.txt", "./outputs/proxy-urls.txt");
})();