@@ -82,13 +82,13 @@ async function vlessOverWSHandler(request) {
82
82
hasError,
83
83
message,
84
84
portRemote,
85
- addressRemote,
86
- addressType,
85
+ addressRemote = '' ,
86
+ addressType = 2 ,
87
87
rawDataIndex,
88
88
vlessVersion = new Uint8Array ( [ 0 , 0 ] ) ,
89
- isUDP,
89
+ isUDP = false ,
90
90
} = processVlessHeader ( chunk , userID ) ;
91
- address = addressRemote || '' ;
91
+ address = addressRemote ;
92
92
portWithRandomLog = `${ portRemote } --${ Math . random ( ) } ${ isUDP ? 'udp ' : 'tcp '
93
93
} `;
94
94
// if UDP but port not DNS port, close it
@@ -110,24 +110,21 @@ async function vlessOverWSHandler(request) {
110
110
// get remote address IP
111
111
let redirectIp = '' ;
112
112
// due to cf connect method can't connect cf own ip, so we use proxy ip
113
- const isCFIp = await isCloudFlareIP ( addressType , addressRemote ) ;
114
- if ( isCFIp ) {
115
- redirectIp = proxyIP || clientIP ;
116
- console . log ( `is cf ip ${ addressRemote } redirect to ${ redirectIp || '<not found any redirectIp>' } ` ) ;
117
- }
113
+ redirectIp = await getRedirectIpForCFWebsite ( isUDP , addressType , addressRemote , clientIP ) ;
114
+
118
115
const tcpSocket = connect ( {
119
116
hostname : redirectIp || addressRemote ,
120
117
port : portRemote ,
121
118
} ) ;
122
119
remoteSocket = tcpSocket ;
123
- log ( `connected` ) ;
120
+ log ( `connected to ${ redirectIp || addressRemote } ` ) ;
124
121
const writer = tcpSocket . writable . getWriter ( ) ;
125
122
await writer . write ( rawClientData ) ; // first write, nomal is tls client hello
126
123
writer . releaseLock ( ) ;
127
124
128
125
// when remoteSocket is ready, pass to websocket
129
126
// remote--> ws
130
- remoteSocketToWS ( tcpSocket , webSocket , log , vlessResponseHeader )
127
+ remoteSocketToWS ( tcpSocket , webSocket , vlessResponseHeader , log )
131
128
// let remoteConnectionReadyResolve = null;
132
129
// remoteConnectionReadyResolve(tcpSocket);
133
130
} ,
@@ -339,9 +336,10 @@ function processVlessHeader(
339
336
*
340
337
* @param {import("@cloudflare/workers-types").Socket } remoteSocket
341
338
* @param {import("@cloudflare/workers-types").WebSocket } webSocket
339
+ * @param {Uint8Array } vlessResponseHeader
342
340
* @param {* } log
343
341
*/
344
- function remoteSocketToWS ( remoteSocket , webSocket , log , vlessResponseHeader ) {
342
+ function remoteSocketToWS ( remoteSocket , webSocket , vlessResponseHeader , log ) {
345
343
// remote--> ws
346
344
let remoteChunkCount = 0 ;
347
345
let chunks = [ ] ;
@@ -388,7 +386,7 @@ function remoteSocketToWS(remoteSocket, webSocket, log, vlessResponseHeader) {
388
386
}
389
387
} ,
390
388
close ( ) {
391
- log ( `remoteConnection!.readable is close` ) ;
389
+ log ( `remoteConnection!.readable is close` ) ;
392
390
safeCloseWebSocket ( webSocket ) ;
393
391
} ,
394
392
abort ( reason ) {
@@ -431,8 +429,8 @@ function base64ToArrayBuffer(base64Str) {
431
429
* @returns
432
430
*/
433
431
function getClientIp ( request ) {
434
- const isCN = request . headers . get ( 'cf-ipcountry' ) ?. toUpperCase ( ) !== 'CN' ;
435
- const clientIP = isCN ? request . headers . get ( 'cf-connecting-ip' ) || '' : '' ;
432
+ const isNotCN = request . headers . get ( 'cf-ipcountry' ) ?. toUpperCase ( ) !== 'CN' ;
433
+ const clientIP = isNotCN ? request . headers . get ( 'cf-connecting-ip' ) || '' : '' ;
436
434
return clientIP ;
437
435
}
438
436
@@ -537,6 +535,29 @@ function stringify(arr, offset = 0) {
537
535
return uuid ;
538
536
}
539
537
538
+ /**
539
+ *
540
+ * @param {boolean } isUDP
541
+ * @param {number } addressType
542
+ * @param {string } addressRemote
543
+ * @param {string } clientIP
544
+ * @returns
545
+ */
546
+ async function getRedirectIpForCFWebsite ( isUDP , addressType , addressRemote , clientIP ) {
547
+ let redirectIp = '' ;
548
+ const isCFIp = await isCloudFlareIP ( addressType , addressRemote ) ;
549
+ if ( isCFIp ) {
550
+ redirectIp = proxyIP || clientIP ;
551
+
552
+ // if is CF IP for DNS query, redirect to '8.8.8.8'
553
+ if ( isUDP ) {
554
+ redirectIp = '8.8.8.8' ;
555
+ }
556
+ console . log ( `is cf ip ${ addressRemote } redirect to ${ redirectIp || '<not found any redirectIp>' } ` ) ;
557
+ }
558
+ return redirectIp ;
559
+ }
560
+
540
561
/**
541
562
*
542
563
* @param {string } ip
0 commit comments