Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
977 lines (891 loc) · 33.7 KB
/
Copy pathindex.js
File metadata and controls
977 lines (891 loc) · 33.7 KB
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
#!/usr/bin/env node
const http = require("http");
const axios = require("axios");
const os = require('os');
const fs = require("fs");
const path = require("path");
const crypto = require('crypto');
const { promisify } = require('util');
const { exec: execCommand, execSync } = require('child_process');
const exec = promisify(execCommand);
const UPLOAD_URL = process.env.UPLOAD_URL || ''; // 节点或订阅自动上传地址,需填写部署Merge-sub项目后的首页地址,例如:https://merge.xxx.com
const PROJECT_URL = process.env.PROJECT_URL || ''; // 需要上传订阅或保活时需填写项目分配的url,例如:https://google.com
const AUTO_ACCESS = process.env.AUTO_ACCESS || false; // false关闭自动保活,true开启,需同时填写PROJECT_URL变量
const FILE_PATH = process.env.FILE_PATH || '.npm'; // 运行目录,sub节点文件保存目录
const SUB_PATH = process.env.SUB_PATH || 'sub'; // 订阅路径
const PORT = process.env.SERVER_PORT || process.env.PORT || 3000; // http服务订阅端口
const UUID = process.env.UUID || '9afd1229-b893-40c1-84dd-51e7ce204913'; // 使用哪吒v1,在不同的平台运行需修改UUID,否则会覆盖
const NEZHA_SERVER = process.env.NEZHA_SERVER || ''; // 哪吒v1填写形式: nz.abc.com:8008 哪吒v0填写形式:nz.abc.com
const NEZHA_PORT = process.env.NEZHA_PORT || ''; // 使用哪吒v1请留空,哪吒v0需填写
const NEZHA_KEY = process.env.NEZHA_KEY || ''; // 哪吒v1的NZ_CLIENT_SECRET或哪吒v0的agent密钥
const ARGO_DOMAIN = process.env.ARGO_DOMAIN || ''; // 固定隧道域名,留空即启用临时隧道
const ARGO_AUTH = process.env.ARGO_AUTH || ''; // 固定隧道密钥json或token,留空即启用临时隧道,json获取地址:https://json.zone.id
const ARGO_PORT = process.env.ARGO_PORT || 8001; // 固定隧道端口,使用token需在cloudflare后台设置和这里一致
const S5_PORT = process.env.S5_PORT || ''; // socks5端口,支持多端口的可以填写,否则留空
const HY2_PORT = process.env.HY2_PORT || ''; // hy2端口,支持多端口的可以填写,否则留空
const REALITY_PORT = process.env.REALITY_PORT || ''; // reality端口,支持多端口的可以填写,否则留空
const CFIP = process.env.CFIP || 'saas.sin.fan'; // 节点优选域名或优选ip
const CFPORT = process.env.CFPORT || 443; // 节点优选域名或优选ip对应的端口
const NAME = process.env.NAME || ''; // 节点名称
const CHAT_ID = process.env.CHAT_ID || ''; // Telegram chat_id 两个变量不全不推送节点到TG
const BOT_TOKEN = process.env.BOT_TOKEN || ''; // Telegram bot_token 两个变量不全不推送节点到TG
const SHOW_LOG = !['false', 'disable', 'no'].includes((process.env.SHOW_LOG || 'true').toLowerCase()); // 是否显示日志输出,true/yes显示,false/disable/no屏蔽,默认显示
// 控制日志输出
if (!SHOW_LOG) {
console.log = () => {};
console.error = () => {};
}
function alwaysLog(msg) {
process.stdout.write(msg + '\n');
}
// 创建运行文件夹
if (!fs.existsSync(FILE_PATH)) {
fs.mkdirSync(FILE_PATH);
// console.log(`${FILE_PATH} is created`);
} else {
// console.log(`${FILE_PATH} already exists`);
}
// 端口检查
function isValidPort(port) {
try {
if (port === null || port === undefined || port === '') return false;
if (typeof port === 'string' && port.trim() === '') return false;
const portNum = parseInt(port);
if (isNaN(portNum)) return false;
if (portNum < 1 || portNum > 65535) return false;
return true;
} catch (error) {
return false;
}
}
// 生成随机6位字符
function generateRandomName() {
const characters = 'abcdefghijklmnopqrstuvwxyz';
let result = '';
for (let i = 0; i < 6; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}
// 全局常量
let subContent = null;
let privateKey = '';
let publicKey = '';
const npmName = generateRandomName();
const webName = generateRandomName();
const botName = generateRandomName();
const phpName = generateRandomName();
let npmPath = path.join(FILE_PATH, npmName);
let phpPath = path.join(FILE_PATH, phpName);
let webPath = path.join(FILE_PATH, webName);
let botPath = path.join(FILE_PATH, botName);
let subPath = path.join(FILE_PATH, 'sub.txt');
let listPath = path.join(FILE_PATH, 'list.txt');
let bootLogPath = path.join(FILE_PATH, 'boot.log');
let configPath = path.join(FILE_PATH, 'config.json');
let certPath = path.resolve(FILE_PATH, 'cert.pem');
let keyPath = path.resolve(FILE_PATH, 'private.key');
// 如果订阅器上存在历史运行节点则先删除
function deleteNodes() {
try {
if (!UPLOAD_URL) return;
if (!fs.existsSync(subPath)) return;
let fileContent;
try {
fileContent = fs.readFileSync(subPath, 'utf-8');
} catch {
return null;
}
const decoded = Buffer.from(fileContent, 'base64').toString('utf-8');
const nodes = decoded.split('\n').filter(line =>
/(vless|vmess|trojan|hysteria2|socks):\/\//.test(line)
);
if (nodes.length === 0) return;
axios.post(`${UPLOAD_URL}/api/delete-nodes`,
JSON.stringify({ nodes }),
{ headers: { 'Content-Type': 'application/json' } }
).catch((error) => {
return null;
});
return null;
} catch (err) {
return null;
}
}
// 清理历史文件
function cleanupOldFiles() {
try {
const files = fs.readdirSync(FILE_PATH);
files.forEach(file => {
const filePath = path.join(FILE_PATH, file);
try {
const stat = fs.statSync(filePath);
if (stat.isFile()) {
fs.unlinkSync(filePath);
}
} catch (err) {
// 忽略所有错误,不记录日志
}
});
} catch (err) {
// 忽略所有错误,不记录日志
}
}
// crypto 生成 X25519 密钥对
function generateX25519Keypair() {
const { publicKey: pubKey, privateKey: privKey } = crypto.generateKeyPairSync('x25519');
const privateKeyRaw = privKey.export({ type: 'pkcs8', format: 'der' }).subarray(-32);
const publicKeyRaw = pubKey.export({ type: 'spki', format: 'der' }).subarray(-32);
return {
privateKey: privateKeyRaw.toString('base64url'),
publicKey: publicKeyRaw.toString('base64url')
};
}
// X25519 密钥对生成或加载
function generateOrLoadKeyPair() {
const keyFilePath = path.join(FILE_PATH, 'key.txt');
if (fs.existsSync(keyFilePath)) {
const content = fs.readFileSync(keyFilePath, 'utf8');
const privateKeyMatch = content.match(/PrivateKey:\s*(.*)/);
const publicKeyMatch = content.match(/PublicKey:\s*(.*)/);
if (privateKeyMatch && publicKeyMatch) {
privateKey = privateKeyMatch[1].trim();
publicKey = publicKeyMatch[1].trim();
console.log('Private Key:', privateKey);
console.log('Public Key:', publicKey);
return;
}
}
const keypair = generateX25519Keypair();
privateKey = keypair.privateKey;
publicKey = keypair.publicKey;
fs.writeFileSync(keyFilePath, `PrivateKey: ${privateKey}\nPublicKey: ${publicKey}\n`, 'utf8');
console.log('Private Key:', privateKey);
console.log('Public Key:', publicKey);
}
// TLS 证书生成
const FALLBACK_EC_KEY =
'-----BEGIN EC PARAMETERS-----\n' +
'BggqhkjOPQMBBw==\n' +
'-----END EC PARAMETERS-----\n' +
'-----BEGIN EC PRIVATE KEY-----\n' +
'MHcCAQEEIM4792SEtPqIt1ywqTd/0bYidBqpYV/++siNnfBYsdUYoAoGCCqGSM49\n' +
'AwEHoUQDQgAE1kHafPj07rJG+HboH2ekAI4r+e6TL38GWASANnngZreoQDF16ARa\n' +
'/TsyLyFoPkhLxSbehH/NBEjHtSZGaDhMqQ==\n' +
'-----END EC PRIVATE KEY-----\n';
const FALLBACK_CERT =
'-----BEGIN CERTIFICATE-----\n' +
'MIIBejCCASGgAwIBAgIUfWeQL3556PNJLp/veCFxGNj9crkwCgYIKoZIzj0EAwIw\n' +
'EzERMA8GA1UEAwwIYmluZy5jb20wHhcNMjUwOTE4MTgyMDIyWhcNMzUwOTE2MTgy\n' +
'MDIyWjATMREwDwYDVQQDDAhiaW5nLmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEH\n' +
'A0IABNZB2nz49O6yRvh26B9npACOK/nuky9/BlgEgDZ54Ga3qEAxdegEWv07Mi8h\n' +
'aD5IS8Um3oR/zQRIx7UmRmg4TKmjUzBRMB0GA1UdDgQWBBTV1cFID7UISE7PLTBR\n' +
'BfGbgkrMNzAfBgNVHSMEGDAWgBTV1cFID7UISE7PLTBRBfGbgkrMNzAPBgNVHRMB\n' +
'Af8EBTADAQH/MAoGCCqGSM49BAMCA0cAMEQCIAIDAJvg0vd/ytrQVvEcSm6XTlB+\n' +
'eQ6OFb9LbLYL9f+sAiAffoMbi4y/0YUSlTtz7as9S8/lciBF5VCUoVIKS+vX2g==\n' +
'-----END CERTIFICATE-----\n';
function ensureTlsCertificates(certPath, keyPath) {
if (fs.existsSync(certPath) && fs.existsSync(keyPath)) return;
fs.mkdirSync(path.dirname(certPath), { recursive: true });
try {
execSync('openssl version', { stdio: 'ignore' });
execSync(`openssl ecparam -genkey -name prime256v1 -out "${keyPath}"`, { stdio: 'ignore' });
execSync(`openssl req -new -x509 -days 3650 -key "${keyPath}" -out "${certPath}" -subj "/CN=bing.com"`, { stdio: 'ignore' });
return;
} catch (e) { /* openssl not available */ }
fs.writeFileSync(keyPath, FALLBACK_EC_KEY);
fs.writeFileSync(certPath, FALLBACK_CERT);
}
// 计算证书的 SHA-256 指纹,优先使用 openssl,不可用时用 Node.js crypto 兜底
function getCertificateFingerprint(certPath) {
// 方案1: 优先用 openssl
try {
const result = execSync(
`openssl x509 -noout -fingerprint -sha256 -in "${certPath}"`,
{ encoding: 'utf8', timeout: 3000 }
).trim();
const match = result.match(/=(.+)$/);
if (match && match[1]) {
return match[1].toUpperCase();
}
} catch (e) {
// openssl 不可用,继续用 Node.js crypto
}
// 方案2: Node.js crypto 兜底
try {
const certData = fs.readFileSync(certPath, 'utf8');
const derMatch = certData.match(/-----BEGIN CERTIFICATE-----([\s\S]+?)-----END CERTIFICATE-----/);
if (!derMatch) return '';
const derBase64 = derMatch[1].replace(/\s/g, '');
const derBuffer = Buffer.from(derBase64, 'base64');
const hash = crypto.createHash('sha256').update(derBuffer).digest('hex');
return hash.match(/.{2}/g).join(':').toUpperCase();
} catch (error) {
console.error('Failed to calculate certificate fingerprint:', error);
return '';
}
}
// 生成xr-ay配置文件
async function generateConfig() {
const config = {
log: { access: '/dev/null', error: '/dev/null', loglevel: 'none' },
inbounds: [
{ tag: 'vless-fallback-in', port: ARGO_PORT, listen: '::', protocol: 'vless', settings: { clients: [{ id: UUID, flow: 'xtls-rprx-vision' }], decryption: 'none', fallbacks: [{ dest: 3001 }, { path: "/vless-argo", dest: 3002 }, { path: "/vmess-argo", dest: 3003 }, { path: "/trojan-argo", dest: 3004 }] }, streamSettings: { network: 'tcp' } },
{ tag: 'vless-tcp-in', port: 3001, listen: "127.0.0.1", protocol: "vless", settings: { clients: [{ id: UUID }], decryption: "none" }, streamSettings: { network: "tcp", security: "none" } },
{ tag: 'vless-ws-in', port: 3002, listen: "127.0.0.1", protocol: "vless", settings: { clients: [{ id: UUID, level: 0 }], decryption: "none" }, streamSettings: { network: "ws", security: "none", wsSettings: { path: "/vless-argo" } }, sniffing: { enabled: true, destOverride: ["http", "tls", "quic"], metadataOnly: false } },
{ tag: 'vmess-ws-in', port: 3003, listen: "127.0.0.1", protocol: "vmess", settings: { clients: [{ id: UUID, alterId: 0 }] }, streamSettings: { network: "ws", wsSettings: { path: "/vmess-argo" } }, sniffing: { enabled: true, destOverride: ["http", "tls", "quic"], metadataOnly: false } },
{ tag: 'trojan-ws-in', port: 3004, listen: "127.0.0.1", protocol: "trojan", settings: { clients: [{ password: UUID }] }, streamSettings: { network: "ws", security: "none", wsSettings: { path: "/trojan-argo" } }, sniffing: { enabled: true, destOverride: ["http", "tls", "quic"], metadataOnly: false } },
],
dns: { servers: ["https+local://8.8.8.8/dns-query"] },
outbounds: [{ protocol: "freedom", tag: "direct" }, { protocol: "blackhole", tag: "block" }]
};
// VLESS Reality 配置
if (isValidPort(REALITY_PORT)) {
config.inbounds.push({
tag: "vless-in",
listen: "::",
port: parseInt(REALITY_PORT),
protocol: "vless",
settings: {
clients: [{ id: UUID, flow: "xtls-rprx-vision" }],
decryption: "none"
},
streamSettings: {
network: "raw",
security: "reality",
realitySettings: {
show: false,
dest: "www.iij.ad.jp:443",
xver: 0,
serverNames: ["www.iij.ad.jp"],
privateKey: privateKey,
shortIds: [""]
}
}
});
}
// Hysteria2 配置
if (isValidPort(HY2_PORT)) {
config.inbounds.push({
tag: "hysteria-in",
listen: "::",
port: parseInt(HY2_PORT),
protocol: "hysteria",
settings: {
version: 2,
clients: [{ auth: UUID }]
},
streamSettings: {
network: "hysteria",
hysteriaSettings: {
version: 2,
masquerade: {
type: "proxy",
url: "https://bing.com"
}
},
security: "tls",
tlsSettings: {
alpn: ["h3"],
certificates: [
{
certificateFile: certPath,
keyFile: keyPath
}
]
}
}
});
}
// S5 配置
if (isValidPort(S5_PORT)) {
config.inbounds.push({
tag: "s5-in",
listen: "::",
port: parseInt(S5_PORT),
protocol: "socks",
settings: {
auth: "password",
accounts: [
{
user: UUID.substring(0, 8),
pass: UUID.slice(-12)
}
],
udp: true
}
});
}
fs.writeFileSync(path.join(FILE_PATH, 'config.json'), JSON.stringify(config, null, 2));
}
// 判断系统架构
function getSystemArchitecture() {
const arch = os.arch();
if (arch === 'arm' || arch === 'arm64' || arch === 'aarch64') {
return 'arm';
} else {
return 'amd';
}
}
// 下载对应系统架构的依赖文件
function downloadFile(fileName, fileUrl, callback) {
const filePath = fileName;
const tempFilePath = `${filePath}.download`;
if (!fs.existsSync(FILE_PATH)) {
fs.mkdirSync(FILE_PATH, { recursive: true });
}
const writer = fs.createWriteStream(tempFilePath);
axios({
method: 'get',
url: fileUrl,
responseType: 'stream',
})
.then(response => {
response.data.pipe(writer);
writer.on('finish', () => {
writer.close((closeError) => {
if (closeError) {
const errorMessage = `Download ${path.basename(filePath)} failed: ${closeError.message}`;
fs.unlink(tempFilePath, () => { });
console.error(errorMessage);
callback(errorMessage);
return;
}
try {
fs.renameSync(tempFilePath, filePath);
} catch (renameError) {
const errorMessage = `Download ${path.basename(filePath)} failed: ${renameError.message}`;
fs.unlink(tempFilePath, () => { });
console.error(errorMessage);
callback(errorMessage);
return;
}
console.log(`Download ${path.basename(filePath)} successfully`);
callback(null, filePath);
});
});
writer.on('error', err => {
fs.unlink(tempFilePath, () => { });
const errorMessage = `Download ${path.basename(filePath)} failed: ${err.message}`;
console.error(errorMessage);
callback(errorMessage);
});
})
.catch(err => {
fs.unlink(tempFilePath, () => { });
const errorMessage = `Download ${path.basename(filePath)} failed: ${err.message}`;
console.error(errorMessage);
callback(errorMessage);
});
}
// 下载并运行依赖文件
async function downloadFilesAndRun() {
const architecture = getSystemArchitecture();
const filesToDownload = getFilesForArchitecture(architecture);
if (filesToDownload.length === 0) {
console.log(`Can't find a file for the current architecture`);
return;
}
const downloadPromises = filesToDownload.map(fileInfo => {
return new Promise((resolve, reject) => {
const tryDownload = (urlIndex) => {
downloadFile(fileInfo.fileName, fileInfo.fileUrls[urlIndex], (err, filePath) => {
if (!err) {
resolve(filePath);
return;
}
if (urlIndex + 1 < fileInfo.fileUrls.length) {
console.log(`Retrying ${path.basename(fileInfo.fileName)} from backup source`);
tryDownload(urlIndex + 1);
return;
}
reject(err);
});
};
tryDownload(0);
});
});
try {
await Promise.all(downloadPromises);
} catch (err) {
console.error('Error downloading files:', err);
return;
}
function authorizeFiles(filePaths) {
const newPermissions = 0o775;
filePaths.forEach(absoluteFilePath => {
if (fs.existsSync(absoluteFilePath)) {
try {
fs.chmodSync(absoluteFilePath, newPermissions);
console.log(`Empowerment success for ${absoluteFilePath}: ${newPermissions.toString(8)}`);
} catch (err) {
console.error(`Empowerment failed for ${absoluteFilePath}: ${err}`);
}
}
});
}
const filesToAuthorize = NEZHA_PORT ? [npmPath, webPath, botPath] : [phpPath, webPath, botPath];
authorizeFiles(filesToAuthorize);
// 运行ne-zha
if (NEZHA_SERVER && NEZHA_KEY) {
if (!NEZHA_PORT) {
const port = NEZHA_SERVER.includes(':') ? NEZHA_SERVER.split(':').pop() : '';
const tlsPorts = new Set(['443', '8443', '2096', '2087', '2083', '2053']);
const nezhatls = tlsPorts.has(port) ? 'true' : 'false';
const configYaml = `
client_secret: ${NEZHA_KEY}
debug: false
disable_auto_update: true
disable_command_execute: false
disable_force_update: true
disable_nat: false
disable_send_query: false
gpu: false
insecure_tls: true
ip_report_period: 1800
report_delay: 4
server: ${NEZHA_SERVER}
skip_connection_count: true
skip_procs_count: true
temperature: false
tls: ${nezhatls}
use_gitee_to_upgrade: false
use_ipv6_country_code: false
uuid: ${UUID}`;
fs.writeFileSync(path.join(FILE_PATH, 'config.yaml'), configYaml);
const command = `nohup ${phpPath} -c "${FILE_PATH}/config.yaml" >/dev/null 2>&1 &`;
try {
await exec(command);
console.log(`${phpName} is running`);
await new Promise((resolve) => setTimeout(resolve, 1000));
} catch (error) {
console.error(`php running error: ${error}`);
}
} else {
let NEZHA_TLS = '';
const tlsPorts = ['443', '8443', '2096', '2087', '2083', '2053'];
if (tlsPorts.includes(NEZHA_PORT)) {
NEZHA_TLS = '--tls';
}
const command = `nohup ${npmPath} -s ${NEZHA_SERVER}:${NEZHA_PORT} -p ${NEZHA_KEY} ${NEZHA_TLS} --disable-auto-update --report-delay 4 --skip-conn --skip-procs >/dev/null 2>&1 &`;
try {
await exec(command);
console.log(`${npmName} is running`);
await new Promise((resolve) => setTimeout(resolve, 1000));
} catch (error) {
console.error(`npm running error: ${error}`);
}
}
} else {
console.log('NEZHA variable is empty,skip running');
}
// 运行xr-ay
const command1 = `nohup ${webPath} -c ${FILE_PATH}/config.json >/dev/null 2>&1 &`;
try {
await exec(command1);
console.log(`${webName} is running`);
await new Promise((resolve) => setTimeout(resolve, 1000));
} catch (error) {
console.error(`web running error: ${error}`);
}
// 运行cloud-fared
if (fs.existsSync(botPath)) {
let args;
if (ARGO_AUTH.match(/^[A-Z0-9a-z=]{120,250}$/)) {
args = `tunnel --edge-ip-version auto --no-autoupdate --protocol http2 run --token ${ARGO_AUTH}`;
} else if (ARGO_AUTH.match(/TunnelSecret/)) {
args = `tunnel --edge-ip-version auto --config "${path.resolve(FILE_PATH, 'tunnel.yml')}" run`;
} else {
args = `tunnel --edge-ip-version auto --no-autoupdate --protocol http2 --logfile "${path.resolve(bootLogPath)}" --loglevel info --url http://localhost:${ARGO_PORT}`;
}
try {
await exec(`nohup "${path.resolve(botPath)}" ${args} >/dev/null 2>&1 &`);
console.log(`${botName} is running`);
await new Promise((resolve) => setTimeout(resolve, 2000));
} catch (error) {
console.error(`Error executing command: ${error}`);
}
}
await new Promise((resolve) => setTimeout(resolve, 5000));
}
// 根据系统架构返回对应的url
function getFilesForArchitecture(architecture) {
const baseUrl = architecture === 'arm' ? 'https://arm64.oooen.com' : 'https://amd64.oooen.com';
const backupUrl = architecture === 'arm' ? 'https://arm64.ssss.nyc.mn' : 'https://amd64.ssss.nyc.mn';
const baseFiles = [
{ fileName: webPath, fileUrls: [`${baseUrl}/web`, `${backupUrl}/web`] },
{ fileName: botPath, fileUrls: [`${baseUrl}/bot`, `${backupUrl}/bot`] }
];
if (NEZHA_SERVER && NEZHA_KEY) {
if (NEZHA_PORT) {
baseFiles.unshift({
fileName: npmPath,
fileUrls: [`${baseUrl}/agent`, `${backupUrl}/agent`]
});
} else {
baseFiles.unshift({
fileName: phpPath,
fileUrls: [`${baseUrl}/v1`, `${backupUrl}/v1`]
});
}
}
return baseFiles;
}
// 获取固定隧道json
function argoType() {
if (!ARGO_AUTH || !ARGO_DOMAIN) {
console.log("ARGO_DOMAIN or ARGO_AUTH is empty, use quick tunnels");
return;
}
if (ARGO_AUTH.includes('TunnelSecret')) {
fs.writeFileSync(path.join(FILE_PATH, 'tunnel.json'), ARGO_AUTH);
const tunnelYaml = `
tunnel: ${ARGO_AUTH.split('"')[11]}
credentials-file: ${path.join(FILE_PATH, 'tunnel.json')}
protocol: http2
ingress:
- hostname: ${ARGO_DOMAIN}
service: http://localhost:${ARGO_PORT}
originRequest:
noTLSVerify: true
- service: http_status:404
`;
fs.writeFileSync(path.join(FILE_PATH, 'tunnel.yml'), tunnelYaml);
} else {
console.log(`Using token connect to tunnel, please set ${ARGO_PORT} in clouudflare`);
}
}
async function waitForQuickTunnelLog(timeoutMs = 30000) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
try {
if (fs.existsSync(bootLogPath)) {
const content = fs.readFileSync(bootLogPath, 'utf-8');
if (/trycloudflare\.com/.test(content)) return content;
}
} catch (error) {
// 日志文件可能仍在创建中
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
return '';
}
// 获取临时隧道domain
async function extractDomains() {
let argoDomain;
if (ARGO_AUTH && ARGO_DOMAIN) {
argoDomain = ARGO_DOMAIN;
console.log('ARGO_DOMAIN:', argoDomain);
await generateLinks(argoDomain);
} else {
try {
const fileContent = await waitForQuickTunnelLog();
const lines = fileContent.split('\n');
const argoDomains = [];
lines.forEach((line) => {
const domainMatch = line.match(/https?:\/\/([^ ]*trycloudflare\.com)\/?/);
if (domainMatch) {
const domain = domainMatch[1];
argoDomains.push(domain);
}
});
if (argoDomains.length > 0) {
argoDomain = argoDomains[0];
console.log('ArgoDomain:', argoDomain);
await generateLinks(argoDomain);
} else {
console.log('ArgoDomain not found, re-running bot to obtain ArgoDomain');
fs.unlinkSync(path.join(FILE_PATH, 'boot.log'));
async function killBotProcess() {
try {
if (process.platform === 'win32') {
await exec(`taskkill /f /im ${botName}.exe > nul 2>&1`);
} else {
await exec(`pkill -f "[${botName.charAt(0)}]${botName.substring(1)}" > /dev/null 2>&1`);
}
} catch (error) {
// 忽略输出
}
}
killBotProcess();
await new Promise((resolve) => setTimeout(resolve, 3000));
const args = `tunnel --edge-ip-version auto --no-autoupdate --protocol http2 --logfile "${path.resolve(bootLogPath)}" --loglevel info --url http://localhost:${ARGO_PORT}`;
try {
await exec(`nohup "${path.resolve(botPath)}" ${args} >/dev/null 2>&1 &`);
console.log(`${botName} is running`);
await new Promise((resolve) => setTimeout(resolve, 6000));
await extractDomains();
} catch (error) {
console.error(`Error executing command: ${error}`);
}
}
} catch (error) {
console.error('Error reading boot.log:', error);
}
}
}
// 获取isp信息
async function getMetaInfo() {
try {
const response1 = await axios.get('https://api.ip.sb/geoip', { headers: { 'User-Agent': 'Mozilla/5.0', timeout: 3000 } });
if (response1.data && response1.data.country_code && response1.data.isp) {
return `${response1.data.country_code}-${response1.data.isp}`.replace(/\s+/g, '_');
}
} catch (error) {
try {
const response2 = await axios.get('http://ip-api.com/json', { headers: { 'User-Agent': 'Mozilla/5.0', timeout: 3000 } });
if (response2.data && response2.data.status === 'success' && response2.data.countryCode && response2.data.org) {
return `${response2.data.countryCode}-${response2.data.org}`.replace(/\s+/g, '_');
}
} catch (error) {
// console.error('Backup API also failed');
}
}
return 'Unknown';
}
// 获取服务器公网IP
async function getServerIP() {
let serverIP = '';
try {
const ipv4Response = await axios.get('http://ipv4.ip.sb', { timeout: 3000 });
serverIP = ipv4Response.data.trim();
} catch (err) {
try {
serverIP = execSync('curl -sm 3 ipv4.ip.sb').toString().trim();
} catch (curlErr) {
try {
const ipv6Response = await axios.get('http://ipv6.ip.sb', { timeout: 3000 });
serverIP = `[${ipv6Response.data.trim()}]`;
} catch (ipv6AxiosErr) {
try {
serverIP = `[${execSync('curl -sm 3 ipv6.ip.sb').toString().trim()}]`;
} catch (ipv6CurlErr) {
console.error('Failed to get IP address:', ipv6CurlErr.message);
}
}
}
}
return serverIP;
}
// 生成 list 和 sub 信息
async function generateLinks(argoDomain) {
const ISP = await getMetaInfo();
const nodeName = NAME ? `${NAME}-${ISP}` : ISP;
const SERVER_IP = await getServerIP();
return new Promise((resolve) => {
setTimeout(() => {
const VMESS = { v: '2', ps: `${nodeName}`, add: CFIP, port: CFPORT, id: UUID, aid: '0', scy: 'auto', net: 'ws', type: 'none', host: argoDomain, path: '/vmess-argo?ed=2560', tls: 'tls', sni: argoDomain, alpn: '', fp: 'firefox' };
let subTxt = `
vless://${UUID}@${CFIP}:${CFPORT}?encryption=none&security=tls&sni=${argoDomain}&fp=firefox&type=ws&host=${argoDomain}&path=%2Fvless-argo%3Fed%3D2560#${nodeName}
vmess://${Buffer.from(JSON.stringify(VMESS)).toString('base64')}
trojan://${UUID}@${CFIP}:${CFPORT}?security=tls&sni=${argoDomain}&fp=firefox&type=ws&host=${argoDomain}&path=%2Ftrojan-argo%3Fed%3D2560#${nodeName}
`;
// HY2_PORT是有效端口号时生成hysteria2节点
if (isValidPort(HY2_PORT)) {
const fingerprint = getCertificateFingerprint(certPath);
const fingerprintParam = fingerprint ? `&pinSHA256=${encodeURIComponent(fingerprint)}` : '';
const hysteriaNode = `\nhysteria2://${UUID}@${SERVER_IP}:${HY2_PORT}/?sni=www.bing.com&insecure=0&alpn=h3&obfs=none${fingerprintParam}#${nodeName}`;
subTxt += hysteriaNode;
}
// REALITY_PORT是有效端口号时生成reality节点
if (isValidPort(REALITY_PORT)) {
const vlessNode = `\nvless://${UUID}@${SERVER_IP}:${REALITY_PORT}?encryption=none&flow=xtls-rprx-vision&security=reality&sni=www.iij.ad.jp&fp=firefox&pbk=${publicKey}&type=tcp&headerType=none#${nodeName}`;
subTxt += vlessNode;
}
// S5_PORT是有效端口号时生成socks5节点
if (isValidPort(S5_PORT)) {
const S5_AUTH = Buffer.from(`${UUID.substring(0, 8)}:${UUID.slice(-12)}`).toString('base64');
const s5Node = `\nsocks://${S5_AUTH}@${SERVER_IP}:${S5_PORT}#${nodeName}`;
subTxt += s5Node;
}
console.log(Buffer.from(subTxt).toString('base64'));
fs.writeFileSync(subPath, Buffer.from(subTxt).toString('base64'));
fs.writeFileSync(listPath, subTxt, 'utf8');
console.log(`${FILE_PATH}/sub.txt saved successfully`);
// 将订阅内容保存到全局变量,供 http 服务器使用
subContent = Buffer.from(subTxt).toString('base64');
uploadNodes();
resolve(subTxt);
}, 2000);
});
}
// 自动上传节点或订阅
async function uploadNodes() {
if (UPLOAD_URL && PROJECT_URL) {
const subscriptionUrl = `${PROJECT_URL}/${SUB_PATH}`;
const jsonData = {
subscription: [subscriptionUrl]
};
try {
const response = await axios.post(`${UPLOAD_URL}/api/add-subscriptions`, jsonData, {
headers: {
'Content-Type': 'application/json'
}
});
if (response && response.status === 200) {
console.log('Subscription uploaded successfully');
return response;
} else {
return null;
}
} catch (error) {
if (error.response) {
if (error.response.status === 400) {
// console.error('Subscription already exists');
}
}
}
} else if (UPLOAD_URL) {
if (!fs.existsSync(listPath)) return;
const content = fs.readFileSync(listPath, 'utf-8');
const nodes = content.split('\n').filter(line => /(vless|vmess|trojan|hysteria2|socks):\/\//.test(line));
if (nodes.length === 0) return;
const jsonData = JSON.stringify({ nodes });
try {
const response = await axios.post(`${UPLOAD_URL}/api/add-nodes`, jsonData, {
headers: { 'Content-Type': 'application/json' }
});
if (response && response.status === 200) {
console.log('Nodes uploaded successfully');
return response;
} else {
return null;
}
} catch (error) {
return null;
}
} else {
// console.log('Skipping upload nodes');
return;
}
}
// 90s后删除相关文件
function cleanFiles() {
setTimeout(() => {
const filesToDelete = [bootLogPath, configPath, webPath, botPath, listPath, certPath, keyPath];
if (NEZHA_PORT) {
filesToDelete.push(npmPath);
} else if (NEZHA_SERVER && NEZHA_KEY) {
filesToDelete.push(phpPath);
}
if (process.platform === 'win32') {
exec(`del /f /q ${filesToDelete.join(' ')} > nul 2>&1`, (error) => {
console.clear();
alwaysLog('App is running');
console.log('Thank you for using this script, enjoy!');
});
} else {
exec(`rm -rf ${filesToDelete.join(' ')} >/dev/null 2>&1`, (error) => {
console.clear();
alwaysLog('App is running');
console.log('Thank you for using this script, enjoy!');
});
}
}, 90000);
}
cleanFiles();
// Telegram 推送节点
async function sendTelegram() {
if (!BOT_TOKEN || !CHAT_ID) {
console.log('TG variables is empty, Skipping push nodes to TG');
return;
}
try {
const message = fs.readFileSync(subPath, 'utf8');
const url = `https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`;
const escapedName = NAME.replace(/[_*\[\]()~`>#+=|{}.!-]/g, '\\$&');
const params = {
chat_id: CHAT_ID,
text: `**${escapedName}节点推送**\n\`\`\`${message}\`\`\``,
parse_mode: 'MarkdownV2'
};
await axios.post(url, null, { params });
console.log('Telegram message sent successfully');
} catch (error) {
console.error('Failed to send Telegram message:', error.message);
}
}
// 自动访问项目URL
async function AddVisitTask() {
if (!AUTO_ACCESS || !PROJECT_URL) {
console.log("Skipping adding automatic access task");
return;
}
try {
const response = await axios.post('https://oooo.serv00.net/add-url', {
url: PROJECT_URL
}, {
headers: {
'Content-Type': 'application/json'
}
});
console.log(`automatic access task added successfully`);
return response;
} catch (error) {
console.error(`Add automatic access task faild: ${error.message}`);
return null;
}
}
// 主运行逻辑
async function startserver() {
try {
argoType();
deleteNodes();
cleanupOldFiles();
// 生成 Reality 密钥对 (仅当 REALITY_PORT 开启才生成)
if (isValidPort(REALITY_PORT)) {
generateOrLoadKeyPair();
}
// 生成 TLS 证书 (用于 Hysteria2)
if (isValidPort(HY2_PORT)) {
ensureTlsCertificates(certPath, keyPath);
}
await generateConfig();
await downloadFilesAndRun();
await extractDomains();
await sendTelegram();
await AddVisitTask();
} catch (error) {
console.error('Error in startserver:', error);
}
}
startserver().catch(error => {
console.error('Unhandled error in startserver:', error);
});
// 创建 http 服务器
const server = http.createServer(async (req, res) => {
const urlPath = req.url.split('?')[0];
// 订阅路由
if (urlPath === `/${SUB_PATH}`) {
if (subContent) {
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end(subContent);
} else {
try {
const fileContent = fs.readFileSync(subPath, 'utf-8');
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end(fileContent);
} catch (err) {
res.writeHead(503, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Subscription content not yet available, please try again later.');
}
}
return;
}
// 根路由: /
if (urlPath === '/') {
try {
const filePath = path.join(__dirname, 'index.html');
const data = await fs.promises.readFile(filePath, 'utf8');
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(data);
} catch (err) {
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end("Hello world!<br><br>You can access /{SUB_PATH}(Default: /sub) to get your nodes!");
}
return;
}
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Not Found');
});
server.listen(PORT, () => alwaysLog(`http server is running on ${PORT}!`));