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 socks proxy #234

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions envs/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const Transport = require('./transport');
const getRandomBytes = require('./get-random-bytes');
const getLocalStorage = require('./get-local-storage');

function createTransport(dc, crypto) {
return new Transport(dc, crypto);
function createTransport(dc, crypto, proxy) {
return new Transport(dc, crypto, proxy);
}

const MTProto = makeMTProto({
Expand Down
36 changes: 28 additions & 8 deletions envs/node/transport.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const net = require('net');
const SocksClient = require('socks').SocksClient;
const Obfuscated = require('../../src/transport/obfuscated');
const baseDebug = require('../../src/utils/common/base-debug');

class Transport extends Obfuscated {
constructor(dc, crypto) {
constructor(dc, crypto, proxy) {
super();

this.dc = dc;
this.debug = baseDebug.extend(`transport-${this.dc.id}`);
this.crypto = crypto;
this.proxy = proxy;

this.connect();
}
Expand All @@ -17,14 +19,32 @@ class Transport extends Obfuscated {
return this.socket.writable;
}

connect() {
async connect() {
this.stream = new Uint8Array();

this.socket = net.connect(
this.dc.port,
this.dc.ip,
this.handleConnect.bind(this)
);
if (this.proxy && this.proxy.host && this.proxy.port && this.proxy.type) {
let options = {
proxy: {
host: this.proxy.host, // ipv4 or ipv6 or hostname
port: this.proxy.port,
type: this.proxy.type // Proxy version (4 or 5)
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add authorization.

command: 'connect', // SOCKS command (createConnection factory function only supports the connect command
destination: {
host: this.dc.ip,
port: this.dc.port
}
};
const proxyClient = await SocksClient.createConnection(options)
this.socket = proxyClient.socket;
this.handleConnect()
} else {
this.socket = net.connect(
this.dc.port,
this.dc.ip,
this.handleConnect.bind(this)
);
}


this.socket.on('data', this.handleData.bind(this));
this.socket.on('error', this.handleError.bind(this));
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"events": "3.3.0",
"leemon": "6.2.0",
"lodash.debounce": "4.0.8",
"pako": "2.0.4"
"pako": "2.0.4",
"socks": "^2.6.1"
},
"devDependencies": {
"jest": "27.4.5",
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ function makeMTProto(envMethods) {

return class {
constructor(options) {
const { api_id, api_hash, storageOptions } = options;
const { api_id, api_hash, storageOptions, proxy } = options;

this.api_id = api_id;
this.api_hash = api_hash;
this.proxy = proxy;

this.initConnectionParams = {};

Expand Down Expand Up @@ -166,7 +167,7 @@ function makeMTProto(envMethods) {
return;
}

const transport = this.envMethods.createTransport(dc, this.crypto);
const transport = this.envMethods.createTransport(dc, this.crypto, this.proxy);

const rpc = new RPC({
dc,
Expand Down