forked from eooce/Cloudflare-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnippets.js
More file actions
628 lines (567 loc) · 31.5 KB
/
snippets.js
File metadata and controls
628 lines (567 loc) · 31.5 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
import { connect } from 'cloudflare:sockets';
let proxyIP = 'proxy.xxxxxxxx.tk:50001'; // proxyIP,也可以是socks5或http
let yourUUID = '93bf61d9-3796-44c2-9b3a-49210ece2585'; // UUID
// CDN
let cfip = [ // 格式:优选域名:端口#备注名称、优选IP:端口#备注名称、[ipv6优选]:端口#备注名称、优选域名#备注
'mfa.gov.ua#SG', 'saas.sin.fan#HK', 'store.ubi.com#JP','cf.130519.xyz#KR','cf.008500.xyz#HK',
'cf.090227.xyz#SG', 'cf.877774.xyz#HK','cdns.doon.eu.org#JP','sub.danfeng.eu.org#TW','cf.zhetengsha.eu.org#HK'
]; // 在此感谢各位大佬维护的优选域名
function getHomePageHTML(currentDomain) {
return `<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Snippets</title><style>body{font-family:Arial,sans-serif;margin:0;padding:40px 20px;background:linear-gradient(135deg,#667eea 0%,#18800e 100%);min-height:100vh;display:flex;align-items:center;justify-content:center}.container{max-width:600px;background:#fff;padding:40px;border-radius:10px;box-shadow:0 10px 40px rgba(0,0,0,.3);text-align:center}h1{color:#667eea;margin-bottom:20px}.info{font-size:18px;color:#666;margin:20px 0}.link{display:inline-block;background:#667eea;color:#fff;padding:12px 30px;border-radius:5px;text-decoration:none;margin-top:20px}.link:hover{background:#5568d3}.footer{margin-top:30px;padding-top:20px;border-top:1px solid #eee;font-size:14px;color:#999}.footer a{color:#667eea;text-decoration:none;margin:0 10px}.footer a:hover{text-decoration:underline}</style></head><body><div class="container"><h1>Hello Snippets</h1><div class="info">请访问: <strong>https://${currentDomain}/你的UUID</strong><br><br>查看订阅和使用说明</div><div class="footer"><a href="https://github.com/eooce/CF-Workers-and-Snip-VLESS" target="_blank">GitHub</a>|<a href="https://t.me/eooceu" target="_blank">TG群组</a></div></div></body></html>`;
}
function getSubPageHTML(currentDomain) {
const v2raySubLink = `https://${currentDomain}/sub/${yourUUID}`;
const clashSubLink = `https://sublink.eooce.com/clash?config=https://${currentDomain}/sub/${yourUUID}`;
const singboxSubLink = `https://sublink.eooce.com/singbox?config=https://${currentDomain}/sub/${yourUUID}`;
return `<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>订阅链接</title><style>*{margin:0;padding:0;box-sizing:border-box}body{font-family:Arial,sans-serif;background:linear-gradient(135deg,#667eea 0%,#18800e 100%);min-height:100vh;padding:20px}.container{max-width:900px;margin:0 auto;background:#fff;border-radius:15px;padding:30px;box-shadow:0 20px 60px rgba(0,0,0,.3)}h1{color:#667eea;margin-bottom:10px;font-size:2rem;text-align:center}.section{margin-bottom:25px}.section-title{color:#667eea;font-size:16px;font-weight:600;margin-bottom:12px;padding-bottom:6px;border-bottom:2px solid #667eea}.link-box{background:#f7f9fc;border:1px solid #e1e8ed;border-radius:8px;padding:12px;margin-bottom:10px}.link-label{font-size:16px;color:#666;margin-bottom:6px;font-weight:700}.link-content{display:flex;gap:8px}.link-text{flex:1;background:#fff;padding:8px 12px;border-radius:5px;border:1px solid #ddd;font-size:.8rem;word-break:break-all;font-family:monospace}.copy-btn{background:#667eea;color:#fff;border:none;padding:8px 16px;border-radius:5px;cursor:pointer;font-size:13px;white-space:nowrap}.copy-btn:hover{background:#5568d3}.copy-btn.copied{background:#48c774}.usage-section{background:#fff9e6;border-left:4px solid #ffc107;padding:15px;border-radius:5px;margin-top:25px}.usage-title{color:#f57c00;font-size:1.2rem;font-weight:600;margin-bottom:12px}.usage-item{margin-bottom:12px;font-size:13px;line-height:1.6}.usage-item strong{color:#333;display:block;margin-bottom:4px}.usage-item code{background:#fff;padding:2px 6px;border-radius:3px;color:#e91e63;font-size:13px;border:1px solid #ddd;word-wrap:break-word;word-break:break-all;display:inline-block;max-width:100%}.example{color:#666;font-size:14px;margin-left:8px}.footer{margin-top:30px;padding-top:20px;border-top:1px solid #e1e8ed;text-align:center;font-size:14px;color:#999}.footer a{color:#667eea;text-decoration:none;margin:0 10px}.footer a:hover{text-decoration:underline}@media (max-width:768px){.container{padding:20px}.link-content{flex-direction:column}.copy-btn{width:100%}}</style></head><body><div class="container"><h1>Snippets 订阅中心</h1><div class="section"><div class="section-title">🔗 通用订阅</div><div class="link-box"><div class="link-label">v2rayN / Loon / Shadowrocket / Karing</div><div class="link-content"><div class="link-text" id="v2ray-link">${v2raySubLink}</div><button class="copy-btn" onclick="copyToClipboard('v2ray-link',this)">复制</button></div></div></div><div class="section"><div class="section-title">😺 Clash 系列订阅</div><div class="link-box"><div class="link-label">Mihomo / FlClash / Clash Meta</div><div class="link-content"><div class="link-text" id="clash-link">${clashSubLink}</div><button class="copy-btn" onclick="copyToClipboard('clash-link',this)">复制</button></div></div></div><div class="section"><div class="section-title">📦 Sing-box 系列订阅</div><div class="link-box"><div class="link-label">Sing-box / SFI / SFA</div><div class="link-content"><div class="link-text" id="singbox-link">${singboxSubLink}</div><button class="copy-btn" onclick="copyToClipboard('singbox-link',this)">复制</button></div></div></div><div class="usage-section"><div class="usage-title">⚙️ 自定义路径(节点里的path)使用说明</div><div class="usage-item"><strong>1. 默认路径</strong><code>/?ed=2560</code><div class="example">使用代码里设置的默认proxyip</div></div><div class="usage-item"><strong>2. 带端口的proxyip</strong><code>/proxyip=210.61.97.241:81</code><br><code>/proxyip=proxy.xxxxxxxx.tk:50001</code><br><code>/?ed=2560&proxyip=210.61.97.241:81</code><br><code>/?ed=2560&proxyip=proxy.xxxxxxxx.tk:50001</code></div><div class="usage-item"><strong>3. 域名proxyip</strong><code>/proxyip=jp.yutian.nyc.mn</code><br><code>/?ed=2560&proxyip=jp.yutian.nyc.mn</code></div><div class="usage-item"><strong>4. 全局SOCKS5</strong><code>/proxyip=socks://user:password@host:port</code><br><code>/proxyip=socks5://user:password@host:port</code><br><code>/?ed=2560&proxyip=socks://user:password@host:port</code><br><code>/?ed=2560&proxyip=socks5://user:password@host:port</code></div><div class="usage-item"><strong>5. 全局HTTP/HTTPS</strong><code>/proxyip=http://user:password@host:port</code><br><code>/proxyip=https://user:password@host:port</code><br><code>/?ed=2560&proxyip=http://user:password@host:port</code><br><code>/?ed=2560&proxyip=https://user:password@host:port</code></div></div><div class="footer"><a href="https://github.com/eooce/CF-Workers-and-Snip-VLESS" target="_blank">GitHub 项目</a>|<a href="https://t.me/eooceu" target="_blank">Telegram 群组</a>|<a href="https://check-proxyip.ssss.nyc.mn" target="_blank">ProxyIP 检测服务</a></div></div><script>function copyToClipboard(e,t){const n=document.getElementById(e).textContent;navigator.clipboard&&navigator.clipboard.writeText?navigator.clipboard.writeText(n).then(()=>{showCopySuccess(t)}).catch(()=>{fallbackCopy(n,t)}):fallbackCopy(n,t)}function fallbackCopy(e,t){const n=document.createElement("textarea");n.value=e,n.style.position="fixed",n.style.left="-999999px",document.body.appendChild(n),n.select();try{document.execCommand("copy"),showCopySuccess(t)}catch(e){alert("复制失败,请手动复制")}document.body.removeChild(n)}function showCopySuccess(e){const t=e.textContent;e.textContent="已复制",e.classList.add("copied"),setTimeout(()=>{e.textContent=t,e.classList.remove("copied")},2e3)}</script></body></html>`;
}
async function handleHomePage(request) {
const url = new URL(request.url);
const currentDomain = url.hostname;
return new Response(getHomePageHTML(currentDomain), {
headers: {
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
},
});
}
async function handleSubtionPage(request) {
const url = new URL(request.url);
const currentDomain = url.hostname;
return new Response(getSubPageHTML(currentDomain), {
headers: {
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
},
});
}
function formatIdentifier(arr, offset = 0) {
const hex = [...arr.slice(offset, offset + 16)].map(b => b.toString(16).padStart(2, '0')).join('');
return `${hex.substring(0,8)}-${hex.substring(8,12)}-${hex.substring(12,16)}-${hex.substring(16,20)}-${hex.substring(20)}`;
}
function base64ToArray(b64Str) {
if (!b64Str) return { error: null };
try {
const binaryString = atob(b64Str.replace(/-/g, '+').replace(/_/g, '/'));
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return { earlyData: bytes.buffer, error: null };
} catch (error) {
return { error };
}
}
function closeSocketQuietly(socket) {
try {
if (socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CLOSING) {
socket.close();
}
} catch (error) {}
}
function isSpeedTestSite(hostname) {
const speedTestDomains = ['speedtest.net','fast.com','speedtest.cn','speed.cloudflare.com','ovo.speedtestcustom.com'];
if (speedTestDomains.includes(hostname)) {
return true;
}
for (const domain of speedTestDomains) {
if (hostname.endsWith('.' + domain) || hostname === domain) {
return true;
}
}
return false;
}
function parseProxyAddress(proxyStr) {
if (!proxyStr) return null;
proxyStr = proxyStr.trim();
if (proxyStr.startsWith('socks://') || proxyStr.startsWith('socks5://')) {
const urlStr = proxyStr.replace(/^socks:\/\//, 'socks5://');
try {
const url = new URL(urlStr);
return {
type: 'socks5',
host: url.hostname,
port: parseInt(url.port) || 1080,
username: url.username ? decodeURIComponent(url.username) : '',
password: url.password ? decodeURIComponent(url.password) : ''
};
} catch (e) {
return null;
}
}
if (proxyStr.startsWith('http://') || proxyStr.startsWith('https://')) {
try {
const url = new URL(proxyStr);
return {
type: 'http',
host: url.hostname,
port: parseInt(url.port) || (proxyStr.startsWith('https://') ? 443 : 80),
username: url.username ? decodeURIComponent(url.username) : '',
password: url.password ? decodeURIComponent(url.password) : ''
};
} catch (e) {
return null;
}
}
if (proxyStr.startsWith('[')) {
const closeBracket = proxyStr.indexOf(']');
if (closeBracket > 0) {
const host = proxyStr.substring(1, closeBracket);
const rest = proxyStr.substring(closeBracket + 1);
if (rest.startsWith(':')) {
const port = parseInt(rest.substring(1), 10);
if (!isNaN(port) && port > 0 && port <= 65535) {
return { type: 'direct', host, port };
}
}
return { type: 'direct', host, port: 443 };
}
}
const lastColonIndex = proxyStr.lastIndexOf(':');
if (lastColonIndex > 0) {
const host = proxyStr.substring(0, lastColonIndex);
const portStr = proxyStr.substring(lastColonIndex + 1);
const port = parseInt(portStr, 10);
if (!isNaN(port) && port > 0 && port <= 65535) {
return { type: 'direct', host, port };
}
}
return { type: 'direct', host: proxyStr, port: 443 };
}
export default {
async fetch(request, env, ctx) {
try {
const url = new URL(request.url);
const pathname = url.pathname;
let pathProxyIP = null;
if (pathname.startsWith('/proxyip=')) {
try {
pathProxyIP = decodeURIComponent(pathname.substring(9)).trim();
} catch (e) {
// 忽略错误
}
if (pathProxyIP && !request.headers.get('Upgrade')) {
proxyIP = pathProxyIP;
return new Response(`set proxyIP to: ${proxyIP}\n\n`, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
},
});
}
}
if (request.headers.get('Upgrade') === 'websocket') {
let wsPathProxyIP = null;
if (pathname.startsWith('/proxyip=')) {
try {
wsPathProxyIP = decodeURIComponent(pathname.substring(9)).trim();
} catch (e) {
// 忽略错误
}
}
const customProxyIP = wsPathProxyIP || url.searchParams.get('proxyip') || request.headers.get('proxyip');
return await handleVlsRequest(request, customProxyIP);
} else if (request.method === 'GET') {
if (url.pathname === '/') {
return handleHomePage(request);
}
if (url.pathname === `/${yourUUID}`) {
return handleSubtionPage(request);
}
if (url.pathname.toLowerCase().includes(`/sub/${yourUUID}`)) {
const currentDomain = url.hostname;
const header = 'v' + 'l' + 'e' + 's' + 's';
const nodeLinks = cfip.map(cdnItem => {
let host, port = 443, nodeName = '';
if (cdnItem.includes('#')) {
const parts = cdnItem.split('#');
cdnItem = parts[0];
nodeName = parts[1];
}
if (cdnItem.startsWith('[') && cdnItem.includes(']:')) {
const ipv6End = cdnItem.indexOf(']:');
host = cdnItem.substring(0, ipv6End + 1);
const portStr = cdnItem.substring(ipv6End + 2);
port = parseInt(portStr) || 443;
} else if (cdnItem.includes(':')) {
const parts = cdnItem.split(':');
host = parts[0];
port = parseInt(parts[1]) || 443;
} else {
host = cdnItem;
}
if (!nodeName) {
nodeName = `Snippets-${header}`;
}
return `${header}://${yourUUID}@${host}:${port}?encryption=none&security=tls&sni=${currentDomain}&fp=firefox&allowInsecure=0&type=ws&host=${currentDomain}&path=%2F%3Fed%3D2560#${nodeName}`;
});
const linksText = nodeLinks.join('\n');
const base64Content = btoa(unescape(encodeURIComponent(linksText)));
return new Response(base64Content, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
},
});
}
}
return new Response('Not Found', { status: 404 });
} catch (err) {
return new Response('Internal Server Error', { status: 500 });
}
},
};
async function handleVlsRequest(request, customProxyIP) {
const wsPair = new WebSocketPair();
const [clientSock, serverSock] = Object.values(wsPair);
serverSock.accept();
let remoteConnWrapper = { socket: null };
let isDnsQuery = false;
const earlyData = request.headers.get('sec-websocket-protocol') || '';
const readable = makeReadableStream(serverSock, earlyData);
readable.pipeTo(new WritableStream({
async write(chunk) {
if (isDnsQuery) return await forwardUDP(chunk, serverSock, null);
if (remoteConnWrapper.socket) {
const writer = remoteConnWrapper.socket.writable.getWriter();
await writer.write(chunk);
writer.releaseLock();
return;
}
const { hasError, message, addressType, port, hostname, rawIndex, version, isUDP } = parseWsPacketHeader(chunk, yourUUID);
if (hasError) throw new Error(message);
if (isSpeedTestSite(hostname)) {
throw new Error('Speedtest site is blocked');
}
if (isUDP) {
if (port === 53) isDnsQuery = true;
else throw new Error('UDP is not supported');
}
const respHeader = new Uint8Array([version[0], 0]);
const rawData = chunk.slice(rawIndex);
if (isDnsQuery) return forwardUDP(rawData, serverSock, respHeader);
await forwardTCP(addressType, hostname, port, rawData, serverSock, respHeader, remoteConnWrapper, customProxyIP);
},
})).catch((err) => {
});
return new Response(null, { status: 101, webSocket: clientSock });
}
async function connect2Socks5(proxyConfig, targetHost, targetPort, initialData) {
const { host, port, username, password } = proxyConfig;
let socket;
try {
socket = connect({ hostname: host, port: port });
const writer = socket.writable.getWriter();
const reader = socket.readable.getReader();
try {
const authMethods = username && password ?
new Uint8Array([0x05, 0x02, 0x00, 0x02]) :
new Uint8Array([0x05, 0x01, 0x00]);
await writer.write(authMethods);
const methodResponse = await reader.read();
if (methodResponse.done || methodResponse.value.byteLength < 2) {
throw new Error('S5 method selection failed');
}
const selectedMethod = new Uint8Array(methodResponse.value)[1];
if (selectedMethod === 0x02) {
if (!username || !password) {
throw new Error('S5 requires authentication');
}
const userBytes = new TextEncoder().encode(username);
const passBytes = new TextEncoder().encode(password);
const authPacket = new Uint8Array(3 + userBytes.length + passBytes.length);
authPacket[0] = 0x01;
authPacket[1] = userBytes.length;
authPacket.set(userBytes, 2);
authPacket[2 + userBytes.length] = passBytes.length;
authPacket.set(passBytes, 3 + userBytes.length);
await writer.write(authPacket);
const authResponse = await reader.read();
if (authResponse.done || new Uint8Array(authResponse.value)[1] !== 0x00) {
throw new Error('S5 authentication failed');
}
} else if (selectedMethod !== 0x00) {
throw new Error(`S5 unsupported auth method: ${selectedMethod}`);
}
const hostBytes = new TextEncoder().encode(targetHost);
const connectPacket = new Uint8Array(7 + hostBytes.length);
connectPacket[0] = 0x05;
connectPacket[1] = 0x01;
connectPacket[2] = 0x00;
connectPacket[3] = 0x03;
connectPacket[4] = hostBytes.length;
connectPacket.set(hostBytes, 5);
new DataView(connectPacket.buffer).setUint16(5 + hostBytes.length, targetPort, false);
await writer.write(connectPacket);
const connectResponse = await reader.read();
if (connectResponse.done || new Uint8Array(connectResponse.value)[1] !== 0x00) {
throw new Error('S5 connection failed');
}
await writer.write(initialData);
writer.releaseLock();
reader.releaseLock();
return socket;
} catch (error) {
writer.releaseLock();
reader.releaseLock();
throw error;
}
} catch (error) {
if (socket) {
try {
socket.close();
} catch (e) {
// throw e;
}
}
throw error;
}
}
async function connect2Http(proxyConfig, targetHost, targetPort, initialData) {
const { host, port, username, password } = proxyConfig;
let socket;
try {
socket = connect({ hostname: host, port: port });
const writer = socket.writable.getWriter();
const reader = socket.readable.getReader();
try {
let connectRequest = `CONNECT ${targetHost}:${targetPort} HTTP/1.1\r\n`;
connectRequest += `Host: ${targetHost}:${targetPort}\r\n`;
if (username && password) {
const auth = btoa(`${username}:${password}`);
connectRequest += `Proxy-Authorization: Basic ${auth}\r\n`;
}
connectRequest += `User-Agent: Mozilla/5.0\r\n`;
connectRequest += `Connection: keep-alive\r\n`;
connectRequest += '\r\n';
await writer.write(new TextEncoder().encode(connectRequest));
let responseBuffer = new Uint8Array(0);
let headerEndIndex = -1;
let bytesRead = 0;
const maxHeaderSize = 8192;
const startTime = Date.now();
const timeoutMs = 10000;
while (headerEndIndex === -1 && bytesRead < maxHeaderSize) {
if (Date.now() - startTime > timeoutMs) {
throw new Error('connection timeout');
}
const { done, value } = await reader.read();
if (done) {
throw new Error('Connection closed before receiving HTTP response');
}
const newBuffer = new Uint8Array(responseBuffer.length + value.length);
newBuffer.set(responseBuffer);
newBuffer.set(value, responseBuffer.length);
responseBuffer = newBuffer;
bytesRead = responseBuffer.length;
for (let i = 0; i < responseBuffer.length - 3; i++) {
if (responseBuffer[i] === 0x0d && responseBuffer[i + 1] === 0x0a &&
responseBuffer[i + 2] === 0x0d && responseBuffer[i + 3] === 0x0a) {
headerEndIndex = i + 4;
break;
}
}
}
if (headerEndIndex === -1) {
throw new Error('Invalid HTTP response or response too large');
}
const headerText = new TextDecoder().decode(responseBuffer.slice(0, headerEndIndex));
const statusLine = headerText.split('\r\n')[0];
const statusMatch = statusLine.match(/HTTP\/\d\.\d\s+(\d+)/);
if (!statusMatch) {
throw new Error(`Invalid response: ${statusLine}`);
}
const statusCode = parseInt(statusMatch[1]);
if (statusCode < 200 || statusCode >= 300) {
throw new Error(`Connection failed with status ${statusCode}: ${statusLine}`);
}
await writer.write(initialData);
writer.releaseLock();
reader.releaseLock();
return socket;
} catch (error) {
try {
writer.releaseLock();
} catch (e) {}
try {
reader.releaseLock();
} catch (e) {}
throw error;
}
} catch (error) {
// 确保套接字被正确关闭
if (socket) {
try {
socket.close();
} catch (e) {
// 忽略关闭错误
}
}
throw error;
}
}
async function forwardTCP(addrType, host, portNum, rawData, ws, respHeader, remoteConnWrapper, customProxyIP) {
async function connectDirect(address, port, data) {
const remoteSock = connect({ hostname: address, port: port });
const writer = remoteSock.writable.getWriter();
await writer.write(data);
writer.releaseLock();
return remoteSock;
}
let proxyConfig = null;
let shouldUseProxy = false;
if (customProxyIP) {
proxyConfig = parseProxyAddress(customProxyIP);
if (proxyConfig && (proxyConfig.type === 'socks5' || proxyConfig.type === 'http' || proxyConfig.type === 'https')) {
shouldUseProxy = true;
} else if (!proxyConfig) {
proxyConfig = parseProxyAddress(proxyIP) || { type: 'direct', host: proxyIP, port: 443 };
}
} else {
proxyConfig = parseProxyAddress(proxyIP) || { type: 'direct', host: proxyIP, port: 443 };
if (proxyConfig.type === 'socks5' || proxyConfig.type === 'http' || proxyConfig.type === 'https') {
shouldUseProxy = true;
}
}
async function connectWithProxy() {
let newSocket;
if (proxyConfig.type === 'socks5') {
newSocket = await connect2Socks5(proxyConfig, host, portNum, rawData);
} else if (proxyConfig.type === 'http' || proxyConfig.type === 'https') {
newSocket = await connect2Http(proxyConfig, host, portNum, rawData);
} else {
newSocket = await connectDirect(proxyConfig.host, proxyConfig.port, rawData);
}
remoteConnWrapper.socket = newSocket;
newSocket.closed.catch(() => {}).finally(() => closeSocketQuietly(ws));
connectStreams(newSocket, ws, respHeader, null);
}
if (shouldUseProxy) {
try {
await connectWithProxy();
} catch (err) {
throw err;
}
} else {
try {
const initialSocket = await connectDirect(host, portNum, rawData);
remoteConnWrapper.socket = initialSocket;
connectStreams(initialSocket, ws, respHeader, connectWithProxy);
} catch (err) {
await connectWithProxy();
}
}
}
function parseWsPacketHeader(chunk, token) {
if (chunk.byteLength < 24) return { hasError: true, message: 'Invalid data' };
const version = new Uint8Array(chunk.slice(0, 1));
if (formatIdentifier(new Uint8Array(chunk.slice(1, 17))) !== token) return { hasError: true, message: 'Invalid uuid' };
const optLen = new Uint8Array(chunk.slice(17, 18))[0];
const cmd = new Uint8Array(chunk.slice(18 + optLen, 19 + optLen))[0];
let isUDP = false;
if (cmd === 1) {} else if (cmd === 2) { isUDP = true; } else { return { hasError: true, message: 'Invalid cmd' }; }
const portIdx = 19 + optLen;
const port = new DataView(chunk.slice(portIdx, portIdx + 2)).getUint16(0);
let addrIdx = portIdx + 2, addrLen = 0, addrValIdx = addrIdx + 1, hostname = '';
const addressType = new Uint8Array(chunk.slice(addrIdx, addrValIdx))[0];
switch (addressType) {
case 1:
addrLen = 4;
hostname = new Uint8Array(chunk.slice(addrValIdx, addrValIdx + addrLen)).join('.');
break;
case 2:
addrLen = new Uint8Array(chunk.slice(addrValIdx, addrValIdx + 1))[0];
addrValIdx += 1;
hostname = new TextDecoder().decode(chunk.slice(addrValIdx, addrValIdx + addrLen));
break;
case 3:
addrLen = 16;
const ipv6 = [];
const ipv6View = new DataView(chunk.slice(addrValIdx, addrValIdx + addrLen));
for (let i = 0; i < 8; i++) ipv6.push(ipv6View.getUint16(i * 2).toString(16));
hostname = ipv6.join(':');
break;
default:
return { hasError: true, message: `Invalid address type: ${addressType}` };
}
if (!hostname) return { hasError: true, message: `Invalid address: ${addressType}` };
return { hasError: false, addressType, port, hostname, isUDP, rawIndex: addrValIdx + addrLen, version };
}
function makeReadableStream(socket, earlyDataHeader) {
let cancelled = false;
return new ReadableStream({
start(controller) {
socket.addEventListener('message', (event) => {
if (!cancelled) controller.enqueue(event.data);
});
socket.addEventListener('close', () => {
if (!cancelled) {
closeSocketQuietly(socket);
controller.close();
}
});
socket.addEventListener('error', (err) => controller.error(err));
const { earlyData, error } = base64ToArray(earlyDataHeader);
if (error) controller.error(error);
else if (earlyData) controller.enqueue(earlyData);
},
cancel() {
cancelled = true;
closeSocketQuietly(socket);
}
});
}
async function connectStreams(remoteSocket, webSocket, headerData, retryFunc) {
let header = headerData, hasData = false;
await remoteSocket.readable.pipeTo(
new WritableStream({
async write(chunk, controller) {
hasData = true;
if (webSocket.readyState !== WebSocket.OPEN) controller.error('ws.readyState is not open');
if (header) {
const response = new Uint8Array(header.length + chunk.byteLength);
response.set(header, 0);
response.set(chunk, header.length);
webSocket.send(response.buffer);
header = null;
} else {
webSocket.send(chunk);
}
},
abort() {},
})
).catch((err) => {
console.error('Stream pipe error:', err);
closeSocketQuietly(webSocket);
});
if (!hasData && retryFunc) {
console.log('No data received, retrying...');
await retryFunc();
}
}
async function forwardUDP(udpChunk, webSocket, respHeader) {
try {
const tcpSocket = connect({ hostname: '8.8.4.4', port: 53 });
let vlessHeader = respHeader;
const writer = tcpSocket.writable.getWriter();
await writer.write(udpChunk);
writer.releaseLock();
await tcpSocket.readable.pipeTo(new WritableStream({
async write(chunk) {
if (webSocket.readyState === WebSocket.OPEN) {
if (vlessHeader) {
const response = new Uint8Array(vlessHeader.length + chunk.byteLength);
response.set(vlessHeader, 0);
response.set(chunk, vlessHeader.length);
webSocket.send(response.buffer);
vlessHeader = null;
} else {
webSocket.send(chunk);
}
}
},
}));
} catch (error) {
// console.error('UDP forward error:', error);
}
}