From 3b9737b7ba6fb780828bfe2f0e95746dfccfe232 Mon Sep 17 00:00:00 2001 From: Emo-Damage <75520940+Emo-Damage@users.noreply.github.com> Date: Sun, 28 May 2023 01:45:48 +0800 Subject: [PATCH] refactor code --- src/worker-vless.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/worker-vless.js b/src/worker-vless.js index 6d9bbda..dffc492 100644 --- a/src/worker-vless.js +++ b/src/worker-vless.js @@ -61,8 +61,7 @@ async function vlessOverWSHandler(request) { const earlyDataHeader = request.headers.get('sec-websocket-protocol') || ''; // only try to get client ip as redirect ip when client is not in China - const isCN = request.headers.get('cf-ipcountry')?.toUpperCase() !== 'CN'; - const clientIP = isCN ? request.headers.get('cf-connecting-ip') || '' : ''; + const clientIP = getClientIp(request); const readableWebSocketStream = makeReadableWebSocketStream(webSocket, earlyDataHeader, log); @@ -148,6 +147,7 @@ async function vlessOverWSHandler(request) { }); } + /** * * @param {import("@cloudflare/workers-types").WebSocket} webSocketServer @@ -382,7 +382,6 @@ function remoteSocketToWS(remoteSocket, webSocket, log, vlessResponseHeader) { await delay(1); } webSocket.send(chunk); - console.log(chunk.byteLength); } else { controller.error( 'webSocket.readyState is not open, maybe close' @@ -427,6 +426,17 @@ function base64ToArrayBuffer(base64Str) { } } +/** + * + * @param {import("@cloudflare/workers-types").Request} request + * @returns + */ +function getClientIp(request) { + const isCN = request.headers.get('cf-ipcountry')?.toUpperCase() !== 'CN'; + const clientIP = isCN ? request.headers.get('cf-connecting-ip') || '' : ''; + return clientIP; +} + /** * // 1--> ipv4 addressLength =4 * // 2--> domain name addressLength=addressBuffer[1] @@ -450,7 +460,7 @@ async function isCloudFlareIP(addressType, addressRemote) { if(addressType === 2) { const domainIP = await doh(addressRemote); - console.log(' domainIP ', domainIP); + // console.log(' domainIP ', domainIP); return isIPv4InCFCIDR(domainIP); } return false;