better socks address parser and enhance robustness

This commit is contained in:
cuteist
2023-06-15 23:23:51 +08:00
committed by zizifn
parent 598546d47a
commit 3831c84788

View File

@@ -13,7 +13,6 @@ let proxyIP = '';
// Example: user:pass@host:port or host:port
let socks5Address = '';
if (!isValidUUID(userID)) {
throw new Error('uuid is not valid');
}
@@ -33,13 +32,15 @@ export default {
userID = env.UUID || userID;
proxyIP = env.PROXYIP || proxyIP;
socks5Address = env.SOCKS5 || socks5Address;
try {
parsedSocks5Address = socks5AddressParser(socks5Address);
enableSocks = true;
} catch (err) {
if (socks5Address) {
try {
parsedSocks5Address = socks5AddressParser(socks5Address);
enableSocks = true;
} catch (err) {
/** @type {Error} */ let e = err;
console.log(e.toString());
enableSocks = false;
console.log(e.toString());
enableSocks = false;
}
}
const upgradeHeader = request.headers.get('Upgrade');
if (!upgradeHeader || upgradeHeader !== 'websocket') {
@@ -200,7 +201,7 @@ async function handleTCPOutBound(remoteSocket, addressType, addressRemote, portR
// if the cf connect tcp socket have no incoming data, we retry to redirect ip
async function retry() {
if (enableSocks) {
tcpSocket = await connectAndWrite(addressRemote, portRemote, enableSocks);
tcpSocket = await connectAndWrite(addressRemote, portRemote, true);
} else {
tcpSocket = await connectAndWrite(proxyIP || addressRemote, portRemote);
}