Skip to content

Instantly share code, notes, and snippets.

@longbill
Last active March 28, 2024 03:37
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save longbill/d321fc371e6dda9acf40decdb923c048 to your computer and use it in GitHub Desktop.
Save longbill/d321fc371e6dda9acf40decdb923c048 to your computer and use it in GitHub Desktop.
Socks5 proxy server in pure javascript
const net = require('net')
net.createServer(client => {
client.once('data', data => {
client.write(Buffer.from([5, 0]));
client.once('data', data => {
data = [...data];
let ver = data.shift();
let cmd = data.shift(); //1: connect, 2: bind, 3: udp
let rsv = data.shift();
let atyp = data.shift(); //1: ipv4(4bytes followed), 3: domain, 4: ipv6(16 bytes followed)
let dstAddr, dstPort, serverStr = '';
if (atyp === 1) {
dstAddr = data.splice(0, 4);
serverStr = dstAddr.join('.');
} else if (atyp === 3) {
let domainLength = data.shift();
dstAddr = data.splice(0, domainLength);
serverStr = Buffer.from(dstAddr).toString();
dstAddr.unshift(domainLength);
} else if (atyp === 4) {
dstAddr = data.splice(0, 16);
serverStr = [];
for(let i = 0; i < 8; i++) {
serverStr.push(Buffer.from(dstAddr.slice(i * 2, 2)).toString('hex'));
}
serverStr = serverStr.join(':');
} else {
client.destroy();
return;
}
dstPort = data.splice(0, 2);
const portNum = dstPort[0] * 256 + dstPort[1];
const serverName = `${serverStr}:${portNum}`;
console.log('>', serverName);
const dstServer = net.connect({
host: serverStr,
port: portNum
});
dstServer.on('error', err => {
console.log(`!Server Error(${serverName}):`, err.message);
client.destroy();
});
client.on('error', err=>{
console.log(`!Client Error(${serverName}):`, err.message);
dstServer.destroy();
});
if (data && data.length > 0) {
dstServer.write(Buffer.from(data));
}
client.write(Buffer.concat([
Buffer.from([ver, 0, 0, atyp]),
Buffer.from(dstAddr),
Buffer.from(dstPort)
]));
dstServer.pipe(client);
client.pipe(dstServer);
});
});
}).listen(8787, '127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment