-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathparser.js
489 lines (451 loc) · 16 KB
/
parser.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
// @ts-nocheck
// 添加名称解码函数
function decodeNodeName(encodedName, fallback = 'Unnamed') {
if (!encodedName) return fallback;
try {
let decoded = encodedName;
// 1. 第一次 URL 解码
try {
const urlDecoded = decodeURIComponent(decoded);
decoded = urlDecoded;
} catch (e) {}
// 2. 第二次 URL 解码(处理双重编码)
try {
const urlDecoded2 = decodeURIComponent(decoded);
decoded = urlDecoded2;
} catch (e) {}
// 3. 如果看起来是 Base64,尝试 Base64 解码
if (/^[A-Za-z0-9+/=]+$/.test(decoded)) {
try {
const base64Decoded = atob(decoded);
const bytes = new Uint8Array(base64Decoded.length);
for (let i = 0; i < base64Decoded.length; i++) {
bytes[i] = base64Decoded.charCodeAt(i);
}
const text = new TextDecoder('utf-8').decode(bytes);
if (/^[\x20-\x7E\u4E00-\u9FFF]+$/.test(text)) {
decoded = text;
}
} catch (e) {}
}
// 4. 尝试 UTF-8 解码
try {
const utf8Decoded = decodeURIComponent(escape(decoded));
if (utf8Decoded !== decoded) {
decoded = utf8Decoded;
}
} catch (e) {}
return decoded;
} catch (e) {
return encodedName || fallback;
}
}
export default class Parser {
/**
* 解析订阅内容
* @param {string} url - 订阅链接或短链ID
* @param {Env} [env] - KV 环境变量
*/
static async parse(url, env) {
try {
// 检查是否为内部URL格式
if (url.startsWith('http://inner.nodes.secret/id-')) {
const kvId = url.replace('http://inner.nodes.secret/id-', '');
// 从KV读取节点信息
const nodesData = await env.SUBLINK_KV.get(kvId);
if (!nodesData) {
throw new Error('Nodes not found in KV storage');
}
let nodes = [];
// 分割多行内容
const lines = nodesData.split('\n').filter(line => line.trim());
for (const line of lines) {
if (line.startsWith('http')) {
// 如果是URL,解析订阅内容
const subNodes = await this.parse(line, env);
nodes = nodes.concat(subNodes);
} else {
// 如果是节点配置,直接解析
const node = this.parseLine(line.trim());
if (node) {
nodes.push(node);
}
}
}
return nodes;
}
// 处理普通URL
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const content = await response.text();
return this.parseContent(content, env);
} catch (error) {
throw error;
}
}
/**
* 解析订阅内容
* @param {string} content
* @returns {Promise<Array>} 节点列表
*/
static async parseContent(content, env) {
try {
if (!content) return [];
// 尝试 Base64 解码
let decodedContent = this.tryBase64Decode(content);
// 分割成行
const lines = decodedContent.split(/[\n\s]+/).filter(line => line.trim());
let nodes = [];
for (const line of lines) {
if (this.isSubscriptionUrl(line)) {
// 如果是订阅链接,递归解析
const subNodes = await this.parse(line, env);
nodes = nodes.concat(subNodes);
} else {
// 解析单个节点
const node = this.parseLine(line.trim());
if (node) {
nodes.push(node);
}
}
}
return nodes;
} catch (error) {
console.error('Parse error:', error);
return [];
}
}
/**
* 判断是否为订阅链接
* @param {string} line
* @returns {boolean}
*/
static isSubscriptionUrl(line) {
try {
// 1. 检查是否是 UUID 格式(跳过 UUID 格式的字符串)
if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(line)) {
return false;
}
// 2. 检查是否是有效的URL
const url = new URL(line);
// 3. 必须是 http 或 https 协议
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
return false;
}
// 4. 排除已知的节点链接协议
const nodeProtocols = ['vmess://', 'vless://', 'trojan://', 'ss://', 'ssr://', 'hysteria://', 'hysteria2://', 'tuic://'];
if (nodeProtocols.some(protocol => line.toLowerCase().startsWith(protocol))) {
return false;
}
return true;
} catch (error) {
return false;
}
}
/**
* 尝试 Base64 解码
* @param {string} content
* @returns {string}
*/
static tryBase64Decode(content) {
try {
// 1. 检查是否看起来像 Base64
if (!/^[A-Za-z0-9+/=]+$/.test(content.trim())) {
return content;
}
// 2. 尝试 Base64 解码
const decoded = atob(content);
// 3. 验证解码结果是否包含有效的协议前缀
const validProtocols = ['vmess://', 'vless://', 'trojan://', 'ss://', 'ssr://'];
if (validProtocols.some(protocol => decoded.includes(protocol))) {
return decoded;
}
// 4. 如果解码结果不包含有效协议,返回原内容
return content;
} catch {
// 5. 如果解码失败,返回原内容
return content;
}
}
/**
* 解析单行内容
* @param {string} line
* @returns {Object|null}
*/
static parseLine(line) {
if (!line) return null;
try {
// 解析不同类型的节点
if (line.startsWith('vmess://')) {
return this.parseVmess(line);
} else if (line.startsWith('vless://')) {
return this.parseVless(line);
} else if (line.startsWith('trojan://')) {
return this.parseTrojan(line);
} else if (line.startsWith('ss://')) {
return this.parseSS(line);
} else if (line.startsWith('ssr://')) {
return this.parseSSR(line);
} else if (line.startsWith('hysteria://')) {
return this.parseHysteria(line);
} else if (line.startsWith('hysteria2://')) {
return this.parseHysteria2(line);
} else if (line.startsWith('tuic://')) {
return this.parseTuic(line);
}
return null;
} catch (error) {
return null;
}
}
/**
* 解析 VMess 节点
* @param {string} line
* @returns {Object|null}
*/
static parseVmess(line) {
try {
const content = line.slice(8); // 移除 "vmess://"
// 将 URL 安全的 base64 转换为标准 base64
const safeContent = content
.replace(/-/g, '+')
.replace(/_/g, '/')
.replace(/\s+/g, '');
// 添加适当的填充
let paddedContent = safeContent;
const mod4 = safeContent.length % 4;
if (mod4) {
paddedContent += '='.repeat(4 - mod4);
}
const config = JSON.parse(atob(paddedContent));
return {
type: 'vmess',
name: decodeNodeName(config.ps || 'Unnamed'),
server: config.add,
port: parseInt(config.port),
settings: {
id: config.id,
aid: parseInt(config.aid),
net: config.net,
type: config.type,
host: config.host,
path: config.path,
tls: config.tls,
sni: config.sni,
alpn: config.alpn
}
};
} catch (error) {
console.error('Parse VMess error:', error);
return null;
}
}
/**
* 解析 VLESS 节点
* @param {string} line
* @returns {Object|null}
*/
static parseVless(line) {
try {
const url = new URL(line);
const params = new URLSearchParams(url.search);
return {
type: 'vless',
name: decodeNodeName(url.hash.slice(1)),
server: url.hostname,
port: parseInt(url.port),
settings: {
id: url.username,
flow: params.get('flow') || '',
encryption: params.get('encryption') || 'none',
type: params.get('type') || 'tcp',
security: params.get('security') || '',
path: params.get('path') || '',
host: params.get('host') || '',
sni: params.get('sni') || '',
alpn: params.get('alpn') || '',
pbk: params.get('pbk') || '',
fp: params.get('fp') || '',
sid: params.get('sid') || '',
spx: params.get('spx') || ''
}
};
} catch (error) {
console.error('Parse VLESS error:', error);
return null;
}
}
/**
* 解析 Trojan 节点
* @param {string} line
* @returns {Object|null}
*/
static parseTrojan(line) {
try {
const url = new URL(line);
const params = new URLSearchParams(url.search);
return {
type: 'trojan',
name: decodeNodeName(params.get('remarks') || '') || decodeNodeName(url.hash.slice(1)),
server: url.hostname,
port: parseInt(url.port),
settings: {
password: url.username,
type: params.get('type') || 'tcp',
security: params.get('security') || 'tls',
path: params.get('path') || '',
host: params.get('host') || '',
sni: params.get('sni') || '',
alpn: params.get('alpn') || ''
}
};
} catch (error) {
console.error('Parse Trojan error:', error);
return null;
}
}
/**
* 解析 Shadowsocks 节点
* @param {string} line
* @returns {Object|null}
*/
static parseSS(line) {
try {
const content = line.slice(5); // 移除 "ss://"
const [userinfo, serverInfo] = content.split('@');
const [method, password] = atob(userinfo).split(':');
const [server, port] = serverInfo.split(':');
return {
type: 'ss',
name: decodeNodeName(serverInfo || 'Unnamed'),
server,
port: parseInt(port),
settings: {
method,
password
}
};
} catch (error) {
console.error('Parse Shadowsocks error:', error);
return null;
}
}
/**
* 解析 ShadowsocksR 节点
* @param {string} line
* @returns {Object|null}
*/
static parseSSR(line) {
try {
const content = line.slice(6); // 移除 "ssr://"
const decoded = this.tryBase64Decode(content);
const [baseConfig, query] = decoded.split('/?');
const [server, port, protocol, method, obfs, password] = baseConfig.split(':');
const params = new URLSearchParams(query);
return {
type: 'ssr',
name: decodeNodeName(params.get('remarks') || ''),
server,
port: parseInt(port),
settings: {
protocol,
method,
obfs,
password: atob(password),
protocolParam: atob(params.get('protoparam') || ''),
obfsParam: atob(params.get('obfsparam') || '')
}
};
} catch (error) {
console.error('Parse ShadowsocksR error:', error);
return null;
}
}
/**
* 解析 Hysteria 节点
* @param {string} line
* @returns {Object|null}
*/
static parseHysteria(line) {
try {
const url = new URL(line);
const params = new URLSearchParams(url.search);
return {
type: 'hysteria',
name: decodeNodeName(params.get('remarks') || '') || decodeNodeName(url.hash.slice(1)),
server: url.hostname,
port: parseInt(url.port),
settings: {
auth: url.username,
protocol: params.get('protocol') || '',
up: params.get('up') || '',
down: params.get('down') || '',
alpn: params.get('alpn') || '',
obfs: params.get('obfs') || '',
sni: params.get('sni') || ''
}
};
} catch (error) {
console.error('Parse Hysteria error:', error);
return null;
}
}
/**
* 解析 Hysteria2 节点
* @param {string} line
* @returns {Object|null}
*/
static parseHysteria2(line) {
try {
const url = new URL(line);
const params = new URLSearchParams(url.search);
return {
type: 'hysteria2',
name: decodeNodeName(params.get('remarks') || '') || decodeNodeName(url.hash.slice(1)),
server: url.hostname,
port: parseInt(url.port),
settings: {
auth: url.username,
sni: params.get('sni') || '',
obfs: params.get('obfs') || '',
obfsParam: params.get('obfs-password') || ''
}
};
} catch (error) {
console.error('Parse Hysteria2 error:', error);
return null;
}
}
/**
* 解析 TUIC 节点
* @param {string} line
* @returns {Object|null}
*/
static parseTuic(line) {
try {
const url = new URL(line);
const params = new URLSearchParams(url.search);
return {
type: 'tuic',
name: decodeNodeName(url.hash.slice(1)),
server: url.hostname,
port: parseInt(url.port),
settings: {
uuid: url.username,
password: url.password,
congestion_control: params.get('congestion_control') || 'bbr',
udp_relay_mode: params.get('udp_relay_mode') || 'native',
alpn: (params.get('alpn') || '').split(',').filter(Boolean),
reduce_rtt: params.get('reduce_rtt') === '1',
sni: params.get('sni') || '',
disable_sni: params.get('disable_sni') === '1'
}
};
} catch (error) {
console.error('Parse TUIC error:', error);
return null;
}
}
}