Skip to content

Commit b3e4547

Browse files
committed
make dns query workable
1 parent ae126f2 commit b3e4547

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

src/worker-vless.js

+36-15
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ async function vlessOverWSHandler(request) {
8282
hasError,
8383
message,
8484
portRemote,
85-
addressRemote,
86-
addressType,
85+
addressRemote = '',
86+
addressType = 2,
8787
rawDataIndex,
8888
vlessVersion = new Uint8Array([0, 0]),
89-
isUDP,
89+
isUDP = false,
9090
} = processVlessHeader(chunk, userID);
91-
address = addressRemote || '';
91+
address = addressRemote;
9292
portWithRandomLog = `${portRemote}--${Math.random()} ${isUDP ? 'udp ' : 'tcp '
9393
} `;
9494
// if UDP but port not DNS port, close it
@@ -110,24 +110,21 @@ async function vlessOverWSHandler(request) {
110110
// get remote address IP
111111
let redirectIp = '';
112112
// 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+
118115
const tcpSocket = connect({
119116
hostname: redirectIp || addressRemote,
120117
port: portRemote,
121118
});
122119
remoteSocket = tcpSocket;
123-
log(`connected`);
120+
log(`connected to ${redirectIp || addressRemote}`);
124121
const writer = tcpSocket.writable.getWriter();
125122
await writer.write(rawClientData); // first write, nomal is tls client hello
126123
writer.releaseLock();
127124

128125
// when remoteSocket is ready, pass to websocket
129126
// remote--> ws
130-
remoteSocketToWS(tcpSocket, webSocket, log, vlessResponseHeader)
127+
remoteSocketToWS(tcpSocket, webSocket, vlessResponseHeader, log)
131128
// let remoteConnectionReadyResolve = null;
132129
// remoteConnectionReadyResolve(tcpSocket);
133130
},
@@ -339,9 +336,10 @@ function processVlessHeader(
339336
*
340337
* @param {import("@cloudflare/workers-types").Socket} remoteSocket
341338
* @param {import("@cloudflare/workers-types").WebSocket} webSocket
339+
* @param {Uint8Array} vlessResponseHeader
342340
* @param {*} log
343341
*/
344-
function remoteSocketToWS(remoteSocket, webSocket, log, vlessResponseHeader) {
342+
function remoteSocketToWS(remoteSocket, webSocket, vlessResponseHeader, log) {
345343
// remote--> ws
346344
let remoteChunkCount = 0;
347345
let chunks = [];
@@ -388,7 +386,7 @@ function remoteSocketToWS(remoteSocket, webSocket, log, vlessResponseHeader) {
388386
}
389387
},
390388
close() {
391-
log(`remoteConnection!.readable is close`);
389+
log(`remoteConnection!.readable is close`);
392390
safeCloseWebSocket(webSocket);
393391
},
394392
abort(reason) {
@@ -431,8 +429,8 @@ function base64ToArrayBuffer(base64Str) {
431429
* @returns
432430
*/
433431
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') || '' : '';
436434
return clientIP;
437435
}
438436

@@ -537,6 +535,29 @@ function stringify(arr, offset = 0) {
537535
return uuid;
538536
}
539537

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+
540561
/**
541562
*
542563
* @param {string} ip

test/cidr.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -6589,4 +6589,8 @@ for (const cidr of cidrList) {
65896589

65906590
console.log(convertIp2Num('162.159.137.0'));
65916591

6592-
console.log(isIPv4InCFCIDR('162.159.137.0'));
6592+
console.log(isIPv4InCFCIDR('162.159.137.0'));
6593+
6594+
console.log(convertIp2Num('1.1.1.1'));
6595+
6596+
console.log(isIPv4InCFCIDR('1.1.1.1'));

0 commit comments

Comments
 (0)