Skip to content

Commit

Permalink
update port handler
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim committed Jan 13, 2020
1 parent ad7f8bc commit 468cfdb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
24 changes: 7 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const createServer = options => {
return;
}

if (!addr) {
if (!addr || !port) {
server.emit(
"error",
`${socket.remoteAddress}: ${socket.remotePort} Unsuported address -- disconnecting`
Expand Down Expand Up @@ -116,22 +116,14 @@ const createServer = options => {
}
}

let request;
const request = net.connect(port, addr, () => {
buffer[1] = constants.REPLIES.SUCCEEDED;

try {
request = net.connect(port, addr, () => {
buffer[1] = constants.REPLIES.SUCCEEDED;

socket.write(buffer, () => {
request.pipe(socket);
socket.pipe(request);
});
socket.write(buffer, () => {
request.pipe(socket);
socket.pipe(request);
});
} catch (err) {
server.emit("error", err);
socket.destroy();
return;
}
});

request.on("connect", () => {
server.emit("connect", { addr, port });
Expand Down Expand Up @@ -174,7 +166,6 @@ const createServer = options => {
constants.REPLIES.NETWORK_UNREACHABLE
]);
socket.end(response);
return;
});
};

Expand Down Expand Up @@ -238,7 +229,6 @@ const createServer = options => {
socket.once("data", handshake);
});


return server;
};

Expand Down
9 changes: 7 additions & 2 deletions lib/handler.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
const { isIPv4 } = require("net");
const { ATYP } = require("./constants");

const getPort = buffer => {
return buffer.readInt16BE(buffer.length - 2);
const port = buffer.readInt16BE(buffer.length - 2);
if (!Number.isInteger(port) || port < 1 || port > 65535) return false;
return port;
};

const getAddress = buffer => {
if (buffer[3] === ATYP.IPV4) {
return `${buffer[4]}.${buffer[5]}.${buffer[6]}.${buffer[7]}`;
const ip = `${buffer[4]}.${buffer[5]}.${buffer[6]}.${buffer[7]}`;
if (!isIPv4(ip)) return false;
return ip;
}
if (buffer[3] === ATYP.DOMAINNAME) {
return buffer.toString("utf8", 5, buffer.length - 2);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "socks-proxy-v5",
"version": "0.7.5",
"version": "0.7.6",
"description": "Implementation of socks 5 version on node js",
"main": "index.js",
"directories": {
Expand Down

0 comments on commit 468cfdb

Please sign in to comment.