forked from 3Kmfi6HP/EDtunnel
-
Notifications
You must be signed in to change notification settings - Fork 185
/
_worker_custom.js
840 lines (835 loc) · 26.5 KB
/
_worker_custom.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
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
import { connect } from 'cloudflare:sockets';
const listProxy = [
{ path: '/akamai', proxy: '172.232.238.169' },
{ path: '/kr', proxy: '52.141.25.42'},
{ path: '/us', proxy: '91.186.208.191'},
{ path: '/gcp', proxy: '34.34.217.201' },
{ path: '/do', proxy: '188.166.255.195' },
{ path: '/do2', proxy: '143.198.213.197' },
{ path: '/incapsula', proxy: '45.60.186.91' },
{ path: '/ovh', proxy: '15.235.162.49' },
];
let proxyIP;
export default {
async fetch(request, ctx) {
try {
proxyIP = proxyIP;
const url = new URL(request.url);
const upgradeHeader = request.headers.get('Upgrade');
for (const entry of listProxy) {
if (url.pathname === entry.path) {
proxyIP = entry.proxy;
break;
}
}
if (upgradeHeader === 'websocket' && proxyIP) {
return await vlessOverWSHandler(request);
}
const allConfig = await getAllConfigVless(request.headers.get('Host'));
return new Response(allConfig, {
status: 200,
headers: { "Content-Type": "text/html;charset=utf-8" },
});
} catch (err) {
return new Response(err.toString(), { status: 500 });
}
},
};
async function getAllConfigVless(hostName) {
try {
let vlessConfigs = '';
let clashConfigs = '';
for (const entry of listProxy) {
const { path, proxy } = entry;
const response = await fetch(`http://ip-api.com/json/${proxy}`);
const data = await response.json();
const pathFixed = encodeURIComponent(path);
const vlessTls = `vless://${generateUUIDv4()}\u0040${hostName}:443?encryption=none&security=tls&sni=${hostName}&type=ws&host=${hostName}&path=${pathFixed}#${data.isp} (${data.countryCode})`;
const vlessNtls = `vless://${generateUUIDv4()}\u0040${hostName}:80?path=${pathFixed}&security=none&encryption=none&host=${hostName}&type=ws&sni=${hostName}#${data.isp} (${data.countryCode})`;
const vlessTlsFixed = vlessTls.replace(/ /g, '+');
const vlessNtlsFixed = vlessNtls.replace(/ /g, '+');
const clashConfTls =
`- name: ${data.isp} (${data.countryCode})
server: ${hostName}
port: 443
type: vless
uuid: ${generateUUIDv4()}
cipher: auto
tls: true
skip-cert-verify: true
network: ws
servername: ${hostName}
ws-opts:
path: ${path}
headers:
Host: ${hostName}
udp: true`;
const clashConfNtls =
`- name: ${data.isp} (${data.countryCode})
server: ${hostName}
port: 80
type: vless
uuid: ${generateUUIDv4()}
cipher: auto
tls: false
skip-cert-verify: true
network: ws
ws-opts:
path: ${path}
headers:
Host: ${hostName}
udp: true`;
clashConfigs += `
<div style="display: none;">
<textarea id="clashTls${path}">${clashConfTls}</textarea>
</div>
<div style="display: none;">
<textarea id="clashNtls${path}">${clashConfNtls}</textarea>
</div>
<div class="config-section" style="background-color: rgba(10, 10, 10, 0.8); color: #00ff00; border: 2px solid #00ff00;">
<p style="color: #00ff00;"><strong>ISP:</strong> ${data.isp} (${data.countryCode})</p>
<hr style="border-color: #00ff00; width: 100%; margin-left: auto; margin-right: auto;" />
<div class="config-toggle">
<button class="button" onclick="toggleConfig(this, 'Tap Here To Show Configurations', 'Tap Here To Hide')">Tap Here To Show Configurations</button>
<div class="config-content">
<div class="config-block" style="background-color: rgba(0, 0, 0, 0.3);">
<h3 style="color: #00ff00;">TLS:</h3>
<p class="config">${clashConfTls}</p>
<button class="button" onclick='copyClash("clashTls${path}")'><i class="fa fa-clipboard"></i>Copy</button>
</div>
<hr style="border-color: #00ff00; width: 75%; margin-left: auto; margin-right: auto;" />
<div class="config-block" style="background-color: rgba(0, 0, 0, 0.3);">
<h3 style="color: #00ff00;">NTLS:</h3>
<p class="config">${clashConfNtls}</p>
<button class="button" onclick='copyClash("clashNtls${path}")'><i class="fa fa-clipboard"></i>Copy</button>
</div>
</div>
</div>
</div>
<hr class="config-divider" style="background: linear-gradient(to right, transparent, #00ff00, transparent); margin: 40px 0;" />
`;
vlessConfigs += `
<div class="config-section" style="background-color: rgba(10, 10, 10, 0.8); color: #00ff00; border: 2px solid #00ff00;">
<p style="color: #00ff00;"><strong>ISP:</strong> ${data.isp} (${data.countryCode})</p>
<hr style="border-color: #00ff00; width: 100%; margin-left: auto; margin-right: auto;" />
<div class="config-toggle">
<button class="button" onclick="toggleConfig(this, 'Tap Here To Show Account', 'Tap Here To Hide')">Tap Here To Show Account</button>
<div class="config-content">
<div class="config-block" style="background-color: rgba(0, 0, 0, 0.3);">
<h3 style="color: #00ff00;">TLS:</h3>
<p class="config" style="color: #00ff00;">${vlessTlsFixed}</p>
<button class="button" onclick='copyToClipboard("${vlessTlsFixed}")'><i class="fa fa-clipboard"></i>Copy</button>
</div>
<hr style="border-color: #00ff00; width: 75%; margin-left: auto; margin-right: auto;" />
<div class="config-block" style="background-color: rgba(0, 0, 0, 0.3);">
<h3 style="color: #00ff00;">NTLS:</h3>
<p class="config" style="color: #00ff00;">${vlessNtlsFixed}</p>
<button class="button" onclick='copyToClipboard("${vlessNtlsFixed}")'><i class="fa fa-clipboard"></i>Copy</button>
</div>
</div>
</div>
</div>
<hr class="config-divider" style="background: linear-gradient(to right, transparent, #00ff00, transparent); margin: 40px 0;" />
`;
}
const htmlConfigs = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CLOUDFLARE PROXY</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-Fo3rlrZj/k7ujTnHg4C+6PCWJ+8zzHcXQjXGp6n5Yh9rX0x5fOdPaOqO+e2X4R5C1aE/BSqPIG+8y3O6APa8w==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');
body {
margin: 0;
padding: 0;
font-family: 'Roboto', monospace;
background-color: #0c0c0c;
color: #00ff00;
display: flex;
flex-direction: column;
height: 100vh;
}
.container {
height: 100%;
width: 100%;
background: rgba(0, 0, 0, 0.5);
border-radius: 0;
padding: 30px;
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
animation: fadeIn 1s ease-in-out;
overflow-y: auto;
box-sizing: border-box;
}
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
z-index: -1;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.header {
text-align: center;
margin-bottom: 20px;
}
.profile-container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
.profile-pic {
width: 200px;
height: 200px;
border-radius: 50%;
box-shadow: 0 0 10px rgba(0, 255, 0, 0.5);
margin: 0;
}
.profile-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100%;
}
.profile-name {
color: #00ff00;
font-size: 20px;
text-align: center;
margin-top: 30px;
margin-bottom: 3px;
text-shadow: 0 0 5px rgba(0, 255, 0, 0.7);
}
.header h1 {
font-size: 24px;
text-align: center;
text-transform: uppercase;
margin: 0;
text-shadow: 0 0 10px #00ff00;
}
.nav-buttons {
display: flex;
justify-content: center;
margin-bottom: 30px;
gap: 20px;
}
.nav-buttons .button {
background-color: transparent;
border: 2px solid #00ff00;
color: #00ff00;
padding: 6px 12px;
font-size: 10px;
border-radius: 0;
cursor: pointer;
transition: all 0.3s ease;
text-transform: uppercase;
letter-spacing: 1px;
}
.nav-buttons .button:hover {
background-color: #00ff00;
color: black;
transform: scale(1.05);
}
.content {
display: none;
}
.content.active {
display: block;
}
.config-section {
background: rgba(255, 255, 255, 0.1);
padding: 10px;
margin-bottom: 20px;
position: relative;
animation: slideIn 0.5s ease-in-out;
}
@keyframes slideIn {
from { transform: translateX(-30px); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
.config-section h3 {
margin-top: 0;
color: #00ff00;
font-size: 28px;
}
.config-section p {
color: #00ff00;
font-size: 16px;
}
.config-toggle {
margin-bottom: 20px;
}
.config-content {
display: none;
}
.config-content.active {
display: block;
}
.config-block {
margin-bottom: 20px;
padding: 15px;
background-color: rgba(0, 0, 0, 0.3);
transition: background-color 0.3s ease;
}
.config-block h4 {
margin-bottom: 8px;
color: #00ff00;
font-size: 22px;
font-weight: 600;
}
.config {
background-color: rgba(0, 0, 0, 0.4);
padding: 10px;
border-radius: 0;
border: 1px solid #00ff00;
color: #f5f5f5;
word-wrap: break-word;
white-space: pre-wrap;
font-family: 'Courier New', Courier, monospace;
font-size: 15px;
}
.button {
background-color: transparent;
color: #00ff00;
border: 2px solid #00ff00;
padding: 10px 20px;
margin: 10px;
cursor: pointer;
transition: all 0.3s ease;
}
.button i {
margin-right: 3px;
}
.button:hover {
background-color: #00ff00;
color: black;
}
.config-divider {
border: none;
height: 1px;
background: linear-gradient(to right, transparent, #00ff00, transparent);
margin: 40px 0;
}
@media (max-width: 768px) {
.header h1 {
font-size: 28px;
}
.config-section h3 {
font-size: 24px;
}
.config-block h4 {
font-size: 20px;
}
}
</style>
</head>
<body>
<div class="overlay"></div>
<div class="container">
<div class="header">
<div class="profile-container">
<img src="https://avatars.githubusercontent.com/u/61716582?v=4" alt="VLESS CLOUDFLARE" class="profile-pic">
<h2 class="profile-name">SONZAIX VLESS</h2>
</div>
</div>
<div class="nav-buttons">
<button class="button" onclick="showContent('vless')">VLESS MENU</button>
<button class="button" onclick="showContent('clash')">CLASH MENU</button>
<button class="button" onclick="showContent('info')">INFO</button>
</div>
<div id="vless" class="content active">
${vlessConfigs}
</div>
<div id="clash" class="content">
${clashConfigs}
</div>
<div id="info" class="content">
<p>Workers ini baru support wildcard <strong>support.zoom.us</strong></p>
<p>Jika kalian ingin menambah domain agar support wildcard di vless ini, silakan chat di Telegram: <a href="https://t.me/November2k" target="_blank" style="color: #00ff00; text-decoration: none;">Sonzai X シ</a></p>
<!-- Tambahkan tombol CHAT di sini -->
<a href="https://t.me/november2k" target="_blank">
<button class="button" style="margin-top: 10px;">CHAT</button>
</a>
</div>
</div>
<script>
function showContent(contentId) {
const contents = document.querySelectorAll('.content');
contents.forEach(content => {
content.classList.remove('active');
});
document.getElementById(contentId).classList.add('active');
}
function salinTeks() {
var teks = document.getElementById('teksAsli');
teks.select();
document.execCommand('copy');
alert('Teks telah disalin.');
}
function copyClash(elementId) {
const text = document.getElementById(elementId).textContent;
navigator.clipboard.writeText(text)
.then(() => {
const alertBox = document.createElement('div');
alertBox.textContent = "Copied to clipboard!";
alertBox.style.position = 'fixed';
alertBox.style.bottom = '20px';
alertBox.style.right = '20px';
alertBox.style.backgroundColor = 'green';
alertBox.style.color = '#fff';
alertBox.style.padding = '10px 20px';
alertBox.style.borderRadius = '5px';
alertBox.style.boxShadow = '0 4px 6px rgba(0,0,0,0.1)';
alertBox.style.opacity = '0';
alertBox.style.transition = 'opacity 0.5s ease-in-out';
document.body.appendChild(alertBox);
setTimeout(() => {
alertBox.style.opacity = '1';
}, 100);
setTimeout(() => {
alertBox.style.opacity = '0';
setTimeout(() => {
document.body.removeChild(alertBox);
}, 500);
}, 2000);
})
.catch((err) => {
console.error("Failed to copy to clipboard:", err);
});
}
function copyToClipboard(text) {
navigator.clipboard.writeText(text)
.then(() => {
const alertBox = document.createElement('div');
alertBox.textContent = "Copied to clipboard!";
alertBox.style.position = 'fixed';
alertBox.style.bottom = '20px';
alertBox.style.right = '20px';
alertBox.style.backgroundColor = 'green';
alertBox.style.color = '#fff';
alertBox.style.padding = '10px 20px';
alertBox.style.borderRadius = '5px';
alertBox.style.boxShadow = '0 4px 6px rgba(0,0,0,0.1)';
alertBox.style.opacity = '0';
alertBox.style.transition = 'opacity 0.5s ease-in-out';
document.body.appendChild(alertBox);
setTimeout(() => {
alertBox.style.opacity = '1';
}, 100);
setTimeout(() => {
alertBox.style.opacity = '0';
setTimeout(() => {
document.body.removeChild(alertBox);
}, 500);
}, 2000);
})
.catch((err) => {
console.error("Failed to copy to clipboard:", err);
});
}
function toggleConfig(button, show, hide) {
const configContent = button.nextElementSibling;
if (configContent.classList.contains('active')) {
configContent.classList.remove('active');
button.textContent = show;
} else {
configContent.classList.add('active');
button.textContent = hide;
}
}
</script>
</body>
</html>`;
return htmlConfigs;
} catch (error) {
return `An error occurred while generating the VLESS configurations. ${error}`;
}
}
function generateUUIDv4() {
const randomValues = crypto.getRandomValues(new Uint8Array(16));
randomValues[6] = (randomValues[6] & 0x0f) | 0x40;
randomValues[8] = (randomValues[8] & 0x3f) | 0x80;
return [
randomValues[0].toString(16).padStart(2, '0'),
randomValues[1].toString(16).padStart(2, '0'),
randomValues[2].toString(16).padStart(2, '0'),
randomValues[3].toString(16).padStart(2, '0'),
randomValues[4].toString(16).padStart(2, '0'),
randomValues[5].toString(16).padStart(2, '0'),
randomValues[6].toString(16).padStart(2, '0'),
randomValues[7].toString(16).padStart(2, '0'),
randomValues[8].toString(16).padStart(2, '0'),
randomValues[9].toString(16).padStart(2, '0'),
randomValues[10].toString(16).padStart(2, '0'),
randomValues[11].toString(16).padStart(2, '0'),
randomValues[12].toString(16).padStart(2, '0'),
randomValues[13].toString(16).padStart(2, '0'),
randomValues[14].toString(16).padStart(2, '0'),
randomValues[15].toString(16).padStart(2, '0')
].join('').replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, '$1-$2-$3-$4-$5');
}
async function vlessOverWSHandler(request) {
const webSocketPair = new WebSocketPair();
const [client, webSocket] = Object.values(webSocketPair);
webSocket.accept();
let address = '';
let portWithRandomLog = '';
const log = (info, event) => {
console.log(`[${address}:${portWithRandomLog}] ${info}`, event || '');
};
const earlyDataHeader = request.headers.get('sec-websocket-protocol') || '';
const readableWebSocketStream = makeReadableWebSocketStream(webSocket, earlyDataHeader, log);
let remoteSocketWapper = {
value: null,
};
let udpStreamWrite = null;
let isDns = false;
readableWebSocketStream.pipeTo(new WritableStream({
async write(chunk, controller) {
if (isDns && udpStreamWrite) {
return udpStreamWrite(chunk);
}
if (remoteSocketWapper.value) {
const writer = remoteSocketWapper.value.writable.getWriter()
await writer.write(chunk);
writer.releaseLock();
return;
}
const {
hasError,
message,
portRemote = 443,
addressRemote = '',
rawDataIndex,
vlessVersion = new Uint8Array([0, 0]),
isUDP,
} = processVlessHeader(chunk);
address = addressRemote;
portWithRandomLog = `${portRemote}--${Math.random()} ${isUDP ? 'udp ' : 'tcp '
} `;
if (hasError) {
throw new Error(message);
return;
}
if (isUDP) {
if (portRemote === 53) {
isDns = true;
} else {
throw new Error('UDP proxy only enable for DNS which is port 53');
return;
}
}
const vlessResponseHeader = new Uint8Array([vlessVersion[0], 0]);
const rawClientData = chunk.slice(rawDataIndex);
if (isDns) {
const { write } = await handleUDPOutBound(webSocket, vlessResponseHeader, log);
udpStreamWrite = write;
udpStreamWrite(rawClientData);
return;
}
handleTCPOutBound(remoteSocketWapper, addressRemote, portRemote, rawClientData, webSocket, vlessResponseHeader, log);
},
close() {
log(`readableWebSocketStream is close`);
},
abort(reason) {
log(`readableWebSocketStream is abort`, JSON.stringify(reason));
},
})).catch((err) => {
log('readableWebSocketStream pipeTo error', err);
});
return new Response(null, {
status: 101,
webSocket: client,
});
}
async function handleTCPOutBound(remoteSocket, addressRemote, portRemote, rawClientData, webSocket, vlessResponseHeader, log,) {
async function connectAndWrite(address, port) {
const tcpSocket = connect({
hostname: address,
port: port,
});
remoteSocket.value = tcpSocket;
log(`connected to ${address}:${port}`);
const writer = tcpSocket.writable.getWriter();
await writer.write(rawClientData);
writer.releaseLock();
return tcpSocket;
}
async function retry() {
const tcpSocket = await connectAndWrite(proxyIP || addressRemote, portRemote)
tcpSocket.closed.catch(error => {
console.log('retry tcpSocket closed error', error);
}).finally(() => {
safeCloseWebSocket(webSocket);
})
remoteSocketToWS(tcpSocket, webSocket, vlessResponseHeader, null, log);
}
const tcpSocket = await connectAndWrite(addressRemote, portRemote);
remoteSocketToWS(tcpSocket, webSocket, vlessResponseHeader, retry, log);
}
function makeReadableWebSocketStream(webSocketServer, earlyDataHeader, log) {
let readableStreamCancel = false;
const stream = new ReadableStream({
start(controller) {
webSocketServer.addEventListener('message', (event) => {
if (readableStreamCancel) {
return;
}
const message = event.data;
controller.enqueue(message);
});
webSocketServer.addEventListener('close', () => {
safeCloseWebSocket(webSocketServer);
if (readableStreamCancel) {
return;
}
controller.close();
}
);
webSocketServer.addEventListener('error', (err) => {
log('webSocketServer has error');
controller.error(err);
}
);
const { earlyData, error } = base64ToArrayBuffer(earlyDataHeader);
if (error) {
controller.error(error);
} else if (earlyData) {
controller.enqueue(earlyData);
}
},
pull(controller) {
},
cancel(reason) {
if (readableStreamCancel) {
return;
}
log(`ReadableStream was canceled, due to ${reason}`)
readableStreamCancel = true;
safeCloseWebSocket(webSocketServer);
}
});
return stream;
}
function processVlessHeader(
vlessBuffer
) {
if (vlessBuffer.byteLength < 24) {
return {
hasError: true,
message: 'invalid data',
};
}
const version = new Uint8Array(vlessBuffer.slice(0, 1));
let isValidUser = true;
let isUDP = false;
if (!isValidUser) {
return {
hasError: true,
message: 'invalid user',
};
}
const optLength = new Uint8Array(vlessBuffer.slice(17, 18))[0];
const command = new Uint8Array(
vlessBuffer.slice(18 + optLength, 18 + optLength + 1)
)[0];
if (command === 1) {
} else if (command === 2) {
isUDP = true;
} else {
return {
hasError: true,
message: `command ${command} is not support, command 01-tcp,02-udp,03-mux`,
};
}
const portIndex = 18 + optLength + 1;
const portBuffer = vlessBuffer.slice(portIndex, portIndex + 2);
const portRemote = new DataView(portBuffer).getUint16(0);
let addressIndex = portIndex + 2;
const addressBuffer = new Uint8Array(
vlessBuffer.slice(addressIndex, addressIndex + 1)
);
const addressType = addressBuffer[0];
let addressLength = 0;
let addressValueIndex = addressIndex + 1;
let addressValue = '';
switch (addressType) {
case 1:
addressLength = 4;
addressValue = new Uint8Array(
vlessBuffer.slice(addressValueIndex, addressValueIndex + addressLength)
).join('.');
break;
case 2:
addressLength = new Uint8Array(
vlessBuffer.slice(addressValueIndex, addressValueIndex + 1)
)[0];
addressValueIndex += 1;
addressValue = new TextDecoder().decode(
vlessBuffer.slice(addressValueIndex, addressValueIndex + addressLength)
);
break;
case 3:
addressLength = 16;
const dataView = new DataView(
vlessBuffer.slice(addressValueIndex, addressValueIndex + addressLength)
);
const ipv6 = [];
for (let i = 0; i < 8; i++) {
ipv6.push(dataView.getUint16(i * 2).toString(16));
}
addressValue = ipv6.join(':');
break;
default:
return {
hasError: true,
message: `invild addressType is ${addressType}`,
};
}
if (!addressValue) {
return {
hasError: true,
message: `addressValue is empty, addressType is ${addressType}`,
};
}
return {
hasError: false,
addressRemote: addressValue,
addressType,
portRemote,
rawDataIndex: addressValueIndex + addressLength,
vlessVersion: version,
isUDP,
};
}
async function remoteSocketToWS(remoteSocket, webSocket, vlessResponseHeader, retry, log) {
let remoteChunkCount = 0;
let chunks = [];
let vlessHeader = vlessResponseHeader;
let hasIncomingData = false;
await remoteSocket.readable
.pipeTo(
new WritableStream({
start() {
},
async write(chunk, controller) {
hasIncomingData = true;
if (webSocket.readyState !== WS_READY_STATE_OPEN) {
controller.error(
'webSocket.readyState is not open, maybe close'
);
}
if (vlessHeader) {
webSocket.send(await new Blob([vlessHeader, chunk]).arrayBuffer());
vlessHeader = null;
} else {
webSocket.send(chunk);
}
},
close() {
log(`remoteConnection!.readable is close with hasIncomingData is ${hasIncomingData}`);
},
abort(reason) {
console.error(`remoteConnection!.readable abort`, reason);
},
})
)
.catch((error) => {
console.error(
`remoteSocketToWS has exception `,
error.stack || error
);
safeCloseWebSocket(webSocket);
});
if (hasIncomingData === false && retry) {
log(`retry`)
retry();
}
}
function base64ToArrayBuffer(base64Str) {
if (!base64Str) {
return { error: null };
}
try {
base64Str = base64Str.replace(/-/g, '+').replace(/_/g, '/');
const decode = atob(base64Str);
const arryBuffer = Uint8Array.from(decode, (c) => c.charCodeAt(0));
return { earlyData: arryBuffer.buffer, error: null };
} catch (error) {
return { error };
}
}
const WS_READY_STATE_OPEN = 1;
const WS_READY_STATE_CLOSING = 2;
function safeCloseWebSocket(socket) {
try {
if (socket.readyState === WS_READY_STATE_OPEN || socket.readyState === WS_READY_STATE_CLOSING) {
socket.close();
}
} catch (error) {
console.error('safeCloseWebSocket error', error);
}
}
async function handleUDPOutBound(webSocket, vlessResponseHeader, log) {
let isVlessHeaderSent = false;
const transformStream = new TransformStream({
start(controller) {
},
transform(chunk, controller) {
for (let index = 0; index < chunk.byteLength;) {
const lengthBuffer = chunk.slice(index, index + 2);
const udpPakcetLength = new DataView(lengthBuffer).getUint16(0);
const udpData = new Uint8Array(
chunk.slice(index + 2, index + 2 + udpPakcetLength)
);
index = index + 2 + udpPakcetLength;
controller.enqueue(udpData);
}
},
flush(controller) {
}
});
transformStream.readable.pipeTo(new WritableStream({
async write(chunk) {
const resp = await fetch('https://1.1.1.1/dns-query',
{
method: 'POST',
headers: {
'content-type': 'application/dns-message',
},
body: chunk,
})
const dnsQueryResult = await resp.arrayBuffer();
const udpSize = dnsQueryResult.byteLength;
const udpSizeBuffer = new Uint8Array([(udpSize >> 8) & 0xff, udpSize & 0xff]);
if (webSocket.readyState === WS_READY_STATE_OPEN) {
log(`doh success and dns message length is ${udpSize}`);
if (isVlessHeaderSent) {
webSocket.send(await new Blob([udpSizeBuffer, dnsQueryResult]).arrayBuffer());
} else {
webSocket.send(await new Blob([vlessResponseHeader, udpSizeBuffer, dnsQueryResult]).arrayBuffer());
isVlessHeaderSent = true;
}
}
}
})).catch((error) => {
log('dns udp has error' + error)
});
const writer = transformStream.writable.getWriter();
return {
write(chunk) {
writer.write(chunk);
}
};
}