Skip to content

Commit

Permalink
Bug fix createConnectionChain - creating a tunnel (#88)
Browse files Browse the repository at this point in the history
The current implementation returns a socket to the first proxy in the chain - this is the bug (reproduce: the example in the README does not work). The method "createConnectionChain" should return a socket to the final destination which is tunnelled through the given proxies. Therefore each iteration over the array of proxies must carry forward the socket. This PR is implementing this and hence fixing the bug.
  • Loading branch information
diva-exchange authored Oct 2, 2022
1 parent 6ad8587 commit 6352745
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/client/socksclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ class SocksClient extends EventEmitter implements SocksClient {
}
}

let sock: net.Socket;

// Shuffle proxies
if (options.randomizeChain) {
shuffleArray(options.proxies);
}

try {
let sock: net.Socket;

for (let i = 0; i < options.proxies.length; i++) {
const nextProxy = options.proxies[i];

Expand All @@ -194,13 +194,11 @@ class SocksClient extends EventEmitter implements SocksClient {
command: 'connect',
proxy: nextProxy,
destination: nextDestination,
// Initial connection ignores this as sock is undefined. Subsequent connections re-use the first proxy socket to form a chain.
existing_socket: sock,
});

// If sock is undefined, assign it here.
if (!sock) {
sock = result.socket;
}
sock = sock || result.socket;
}

if (typeof callback === 'function') {
Expand Down

0 comments on commit 6352745

Please sign in to comment.