refactor code

This commit is contained in:
Emo-Damage 2023-05-28 01:45:48 +08:00
parent 990f001d1b
commit 3b9737b7ba

View File

@ -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;