Skip to content

Commit 1ef5588

Browse files
authored
feat: add tests for socks, README improvements (#541)
1 parent a586b3e commit 1ef5588

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ The proxy-chain package is developed by [Apify](https://apify.com/), the full-st
1616
which provides an easy access to a large pool of datacenter and residential IP addresses all around the world. The proxy-chain package is also used by [Crawlee](https://crawlee.dev/),
1717
the world's most popular web craling library for Node.js.
1818

19-
Note that the proxy-chain package currently only supports HTTP forwarding and HTTP CONNECT tunneling to forward arbitrary protocols such as HTTPS or FTP ([learn more](https://blog.apify.com/tunneling-arbitrary-protocols-over-http-proxy-with-static-ip-address-b3a2222191ff)). The SOCKS protocol is not supported yet.
20-
Also, proxy-chain only supports the Basic [Proxy-Authorization](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization).
19+
The proxy-chain package currently supports HTTP/SOCKS forwarding and HTTP CONNECT tunneling to forward arbitrary protocols such as HTTPS or FTP ([learn more](https://blog.apify.com/tunneling-arbitrary-protocols-over-http-proxy-with-static-ip-address-b3a2222191ff)). The HTTP CONNECT tunneling also supports the SOCKS protocol. Also, proxy-chain only supports the Basic [Proxy-Authorization](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization).
2120

2221
## Run a simple HTTP/HTTPS proxy server
2322

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "proxy-chain",
3-
"version": "2.5.0",
3+
"version": "2.5.1",
44
"description": "Node.js implementation of a proxy server (think Squid) with support for SSL, authentication, upstream proxy chaining, and protocol tunneling.",
55
"main": "dist/index.js",
66
"keywords": [
@@ -69,6 +69,7 @@
6969
"rimraf": "^4.1.2",
7070
"sinon": "^13.0.2",
7171
"sinon-stub-promise": "^4.0.0",
72+
"socksv5": "^0.0.6",
7273
"through": "^2.3.8",
7374
"ts-node": "^10.2.1",
7475
"typescript": "^4.4.3",

test/socks.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const portastic = require('portastic');
2+
const socksv5 = require('socksv5');
3+
const { gotScraping } = require('got-scraping');
4+
const { expect } = require('chai');
5+
const ProxyChain = require('../src/index');
6+
7+
describe('SOCKS protocol', () => {
8+
let socksServer;
9+
let proxyServer;
10+
11+
afterEach(() => {
12+
if (socksServer) socksServer.close();
13+
if (proxyServer) proxyServer.close();
14+
});
15+
16+
it('works without auth', (done) => {
17+
portastic.find({ min: 50000, max: 50250 }).then((ports) => {
18+
const [socksPort, proxyPort] = ports;
19+
socksServer = socksv5.createServer((info, accept) => {
20+
accept();
21+
});
22+
socksServer.listen(socksPort, 'localhost');
23+
socksServer.useAuth(socksv5.auth.None());
24+
25+
proxyServer = new ProxyChain.Server({
26+
port: proxyPort,
27+
prepareRequestFunction() {
28+
return {
29+
upstreamProxyUrl: `socks://localhost:${socksPort}`,
30+
};
31+
},
32+
});
33+
proxyServer.listen();
34+
35+
gotScraping.get({ url: 'https://example.com', proxyUrl: `http://127.0.0.1:${proxyPort}` })
36+
.then((response) => {
37+
expect(response.body).to.contain('Example Domain');
38+
done();
39+
})
40+
.catch(done);
41+
});
42+
}).timeout(5 * 1000);
43+
44+
it('work with auth', (done) => {
45+
portastic.find({ min: 50250, max: 50500 }).then((ports) => {
46+
const [socksPort, proxyPort] = ports;
47+
socksServer = socksv5.createServer((info, accept) => {
48+
accept();
49+
});
50+
socksServer.listen(socksPort, 'localhost');
51+
socksServer.useAuth(socksv5.auth.UserPassword((user, password, cb) => {
52+
cb(user === 'proxy-chain' && password === 'rules!');
53+
}));
54+
55+
proxyServer = new ProxyChain.Server({
56+
port: proxyPort,
57+
prepareRequestFunction() {
58+
return {
59+
upstreamProxyUrl: `socks://proxy-chain:rules!@localhost:${socksPort}`,
60+
};
61+
},
62+
});
63+
proxyServer.listen();
64+
65+
gotScraping.get({ url: 'https://example.com', proxyUrl: `http://127.0.0.1:${proxyPort}` })
66+
.then((response) => {
67+
expect(response.body).to.contain('Example Domain');
68+
done();
69+
})
70+
.catch(done);
71+
});
72+
}).timeout(5 * 1000);
73+
});

0 commit comments

Comments
 (0)