Skip to content

Commit

Permalink
test and refactoring all code
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim committed Dec 8, 2019
1 parent 8db4746 commit 8f0c73f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 116 deletions.
23 changes: 0 additions & 23 deletions examples/client.js

This file was deleted.

19 changes: 19 additions & 0 deletions examples/createClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { createClient } = require("../index");

const setting = {
host: "127.0.0.1", // your socks 5 verison server
port: "1080", // port your server
login: "foo",
password: "bar"
};

const server = createClient(setting);
server.listen(2080);

server.on("listening", () => {
console.log(
`server listening ${server.address().address}:${server.address().port}`
);
});

server.on("error", err => console.error(`Client Server Error ---> ${err}`));
33 changes: 8 additions & 25 deletions examples/createServer.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
const socks5 = require("../index");

// const setAddr = new Set(["tools.ietf.org"]);

const server = socks5.createServer({
authenticate(name, password) {
// verify name/password
if (name !== "lexa12" || password !== "darksouls3") {
console.log("Пшел на хуй", name, password);
// respond with auth failure (can be any error)
return true;
}
// return successful authentication
return false;
}
// filter(addr) {
// console.log(addr);
// return setAddr.has(addr);
// }
});
const { createServer } = require("../index");


const server = createServer();

server.listen(5550); // 5550 for prod, 1080 for dev
server.listen(1080);

server.on("connect", info =>
console.log(`connected to remote server at ${info.addr}:${info.port}`)
Expand All @@ -32,12 +16,11 @@ server.on("listening", () => {
});

server.on("connection", socket => {
console.log("connection", socket.remoteAddress, socket.remotePort);
console.log("new socks connection", socket.remoteAddress, socket.remotePort);
});

// server.on("data", data => console.log(data));
server.on("data", data => console.log(data));

server.on("error", err => {
console.error("server ERROR");
console.error(err);
console.error(`server ERROR ---> ${err}`);
});
32 changes: 13 additions & 19 deletions examples/createServerWithAuth.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
const socks5 = require("../index");
const { createServer } = require("../index");

// const setAddr = new Set(["tools.ietf.org"]);

const server = socks5.createServer({
authenticate(name, password) {
const server = createServer({
authenticate(login, password) {
// verify name/password
if (name !== "lexa12" || password !== "darksouls3") {
console.log("Пшел на хуй", name, password);
// respond with auth failure (can be any error)
return true;
if (login !== "foo" || password !== "bar") {
console.log("authentication failed", login);
return false;
// authentication failed
}
console.log(`user ${login} connect`);
// return successful authentication
return false;
return true;
}
// filter(addr) {
// console.log(addr);
// return setAddr.has(addr);
// }
});

server.listen(5550); // 5550 for prod, 1080 for dev
server.listen(1080);

server.on("connect", info =>
console.log(`connected to remote server at ${info.addr}:${info.port}`)
Expand All @@ -32,12 +27,11 @@ server.on("listening", () => {
});

server.on("connection", socket => {
console.log("connection", socket.remoteAddress, socket.remotePort);
console.log("new socks connection", socket.remoteAddress, socket.remotePort);
});

// server.on("data", data => console.log(data));
server.on("data", data => console.log(data));

server.on("error", err => {
console.error("server ERROR");
console.error(err);
console.error(`server ERROR ---> ${err}`);
});
30 changes: 10 additions & 20 deletions examples/createServerWithFilter.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
const socks5 = require("../index");

// const setAddr = new Set(["tools.ietf.org"]);
const setAddr = new Set(["tools.ietf.org", "github.com", "2ip.ru"]);

const server = socks5.createServer({
authenticate(name, password) {
// verify name/password
if (name !== "lexa12" || password !== "darksouls3") {
console.log("Пшел на хуй", name, password);
// respond with auth failure (can be any error)
return true;
}
// return successful authentication
return false;
filter(addr) {
const result = !setAddr.has(addr);
if(!result) console.log(`host ${addr} unreachable`);
return result;
}
// filter(addr) {
// console.log(addr);
// return setAddr.has(addr);
// }
});

server.listen(5550); // 5550 for prod, 1080 for dev
server.listen(1080);

server.on("connect", info =>
console.log(`connected to remote server at ${info.addr}:${info.port}`)
Expand All @@ -32,12 +23,11 @@ server.on("listening", () => {
});

server.on("connection", socket => {
console.log("connection", socket.remoteAddress, socket.remotePort);
console.log("new socks connection", socket.remoteAddress, socket.remotePort);
});

// server.on("data", data => console.log(data));
server.on("data", data => console.log(data));

server.on("error", err => {
console.error("server ERROR");
console.error(err);
});
console.error(`server ERROR ---> ${err}`);
});
77 changes: 48 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const constants = require("./lib/constants");
const { getAddress, getPort } = require("./lib/handler");

const createServer = options => {
options = options || {};
const server = net.createServer(socket => {
const authenticate = buffer => {
// +----+------+----------+------+----------+
Expand All @@ -17,22 +18,28 @@ const createServer = options => {

// check socks version
if (buffer[0] !== constants.AUTH_VERSION) {
console.log(`auth wrong socks version: ${buffer[0]}`);
server.emit(
"error",
`${socket.remoteAddress} :
${socket.remotePort} auth wrong socks version: ${buffer[0]}`
);
const response = Buffer.from([
constants.AUTH_VERSION,
constants.AUTH_REPLIES.GENERAL_FAILURE
]);
socket.end(response);
return;
}

const auth = options.authenticate(name, password);

if (auth) {
if (!auth) {
const response = Buffer.from([
constants.AUTH_VERSION,
constants.AUTH_REPLIES.GENERAL_FAILURE
]);
socket.end(response);
return;
}

const response = Buffer.from([
Expand All @@ -58,38 +65,53 @@ const createServer = options => {
const port = getPort(buffer);

if (buffer[0] !== constants.VERSION) {
console.error(`socks5 connect: wrong socks version: ${buffer[0]}`);
server.emit(
"error",
`${socket.remoteAddress}:
${socket.remotePort} socks5 connect: wrong socks version:
${buffer[0]}`
);
const response = Buffer.from([0x05, constants.REPLIES.GENERAL_FAILURE]);
socket.end(response);
return;
}

if (!addr) {
console.log("Unsuported address -- disconnecting");
server.emit(
"error",
`${socket.remoteAddress}: ${socket.remotePort} Unsuported address -- disconnecting`
);
const response = Buffer.from([
0x05,
constants.REPLIES.ADDRESS_TYPE_NOT_SUPPORTED
]);
socket.end(response);
return;
}

if (cmd !== constants.COMMANDS.CONNECT) {
// Unsuported udp and bind metod
console.log("Unsuported metod command");
server.emit(
"error",
`${socket.remoteAddress}: ${socket.remotePort} Unsuported metod command`
);
const response = Buffer.from([
0x05,
constants.REPLIES.COMMAND_NOT_SUPPORTED
]);
socket.end(response);
return;
}

if (typeof options.filter === "function") {
const result = options.filter(addr);
if (result) {
if (!result) {
const response = Buffer.from([
constants.VERSION,
constants.REPLIES.HOST_UNREACHABLE
]);
socket.end(response);
return;
}
}

Expand Down Expand Up @@ -122,6 +144,7 @@ const createServer = options => {
constants.REPLIES.HOST_UNREACHABLE
]);
socket.end(response);
return;
}

if (err.code === "ECONNREFUSED") {
Expand All @@ -130,6 +153,7 @@ const createServer = options => {
constants.REPLIES.CONNECTION_REFUSED
]);
socket.end(response);
return;
}
const response = Buffer.from([
0x05,
Expand All @@ -148,9 +172,14 @@ const createServer = options => {

// SOCKS Version 5 is the only support version
if (buffer[0] !== constants.VERSION) {
console.error(`wrong socks version: ${buffer[0]}`);
server.emit(
"error",
`${socket.remoteAddress}: ${socket.remotePort} wrong socks version:
${buffer[0]}`
);
const response = Buffer.from([0x05, constants.REPLIES.GENERAL_FAILURE]);
socket.end(response);
return;
}

const auth = typeof options.authenticate === "function";
Expand All @@ -166,12 +195,15 @@ const createServer = options => {
response[1] = constants.METHODS.NO_AUTHENTICATION;
next = connect;
} else {
console.log(
"Unsuported authentication method -- disconnecting, method",
buffer[2]
server.emit(
"error",
`${socket.remoteAddress}: ${socket.remotePort}
Unsuported authentication method -- disconnecting, method
${buffer[2]}`
);
response[1] = constants.METHODS.NO_ACCEPTABLE_METHODS;
socket.end(response);
return;
}

socket.write(response, () => {
Expand All @@ -190,34 +222,29 @@ const createServer = options => {
};

const createClient = options => {
console.log(options);
const { name, password } = options;
const { login, password } = options;

const server = net.createServer(socket => {
const handshakeClient = data => {
const handshakeClient = () => {
socket.write(
Buffer.from([constants.VERSION, constants.METHODS.NO_AUTHENTICATION])
);
console.log("Browser data ", data);

socket.once("data", connectClient);
};

const connectServer = (mainServ, buffer) => {
mainServ.once("data", data => {
console.log("mainServ data ", data);
mainServ.once("data", () => {
const res = Buffer.from([
constants.AUTH_VERSION,
name.length,
...Buffer.from(name, "utf8"),
login.length,
...Buffer.from(login, "utf8"),
password.length,
...Buffer.from(password, "utf8")
]);

mainServ.write(res, () => {
mainServ.once("data", data => {
console.log("mainserv 2 data ", data);

mainServ.once("data", () => {
mainServ.write(buffer, () => {
mainServ.pipe(socket);
socket.pipe(mainServ);
Expand All @@ -228,14 +255,6 @@ const createClient = options => {
};

const connectClient = buffer => {
const cmd = buffer[1];
const atyp = buffer[3];
const addr = getAddress(buffer);
const port = getPort(buffer);

console.log("Browser 2 data", buffer[0], cmd, atyp, addr, port);
console.log("Browser 2 data buffer ", buffer);

const mainServ = net.connect(options);

mainServ.on("error", err => {
Expand Down

0 comments on commit 8f0c73f

Please sign in to comment.