|
| 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