Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tor support #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ let net = require('net');
let tls = require('tls');
const TIMEOUT = 5000;

const socks = require('socks');

const TlsSocketWrapper = require('./TlsSocketWrapper.js');
const EventEmitter = require('events').EventEmitter;
const util = require('./util');
Expand Down Expand Up @@ -72,7 +74,16 @@ class Client {
return Promise.resolve();
}
this.status = 1;
return this.connectSocket(this.conn, this.port, this.host);
if (this._options && this._options.proxy && this._options.proxy.port) {
this.conn = socks.SocksClient.createConnection({command: "connect", destination: {port: this.port, host: this.host }, ...this._options}).then(client => {
return this.initSocksProxy(client)
});

return this.conn;
} else {
return this.connectSocket(this.conn, this.port, this.host);
}

}

connectSocket(conn, port, host) {
Expand All @@ -89,6 +100,29 @@ class Client {
});
}

initSocksProxy(client) {
client.socket.setTimeout(TIMEOUT);
client.socket.setEncoding('utf8');
client.socket.setKeepAlive(true, 0);
client.socket.setNoDelay(true);
client.socket.on('connect', () => {
client.socket.setTimeout(0);
this.onConnect();
});
client.socket.on('close', e => {
this.onClose(e);
});
client.socket.on('data', chunk => {
client.socket.setTimeout(0);
this.onRecv(chunk);
});
client.socket.on('error', e => {
this.onError(e);
});

return client.socket;
}

close() {
if (this.status === 0) {
return;
Expand All @@ -106,7 +140,14 @@ class Client {
const id = ++this.id;
const content = util.makeRequest(method, params, id);
this.callback_message_queue[id] = util.createPromiseResult(resolve, reject);
this.conn.write(content + '\n');
if (this._options && this._options.proxy && this._options.proxy.port) {
this.conn.then(socket => {
socket.write(content + '\n');
socket.resume();
});
} else {
this.conn.write(content + '\n');
}
});
}

Expand All @@ -128,8 +169,15 @@ class Client {
}
const content = '[' + contents.join(',') + ']';
this.callback_message_queue[this.id] = util.createPromiseResultBatch(resolve, reject, arguments_far_calls);
// callback will exist only for max id
this.conn.write(content + '\n');
if (this._options && this._options.proxy && this._options.proxy.port) {
this.conn.then(socket => {
socket.write(content + '\n');
socket.resume();
});
} else {
// callback will exist only for max id
this.conn.write(content + '\n');
}
});
}

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {},
"dependencies": {
"socks": "^2.6.1"
},
"devDependencies": {},
"repository": {
"type": "git",
Expand Down