Skip to content

Commit 6352745

Browse files
Bug fix createConnectionChain - creating a tunnel (#88)
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.
1 parent 6ad8587 commit 6352745

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/client/socksclient.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,14 @@ class SocksClient extends EventEmitter implements SocksClient {
167167
}
168168
}
169169

170-
let sock: net.Socket;
171-
172170
// Shuffle proxies
173171
if (options.randomizeChain) {
174172
shuffleArray(options.proxies);
175173
}
176174

177175
try {
176+
let sock: net.Socket;
177+
178178
for (let i = 0; i < options.proxies.length; i++) {
179179
const nextProxy = options.proxies[i];
180180

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

200200
// If sock is undefined, assign it here.
201-
if (!sock) {
202-
sock = result.socket;
203-
}
201+
sock = sock || result.socket;
204202
}
205203

206204
if (typeof callback === 'function') {

0 commit comments

Comments
 (0)