123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- const { Provider } = require('nconf');
- const nconf = new Provider();
- const getPort = require('get-port');
- const { HttpAgent, auth } = require('socksv5');
- const { assert } = require('chai');
- const SocksProxyAgent = require('socks-proxy-agent');
- const _ = require('lodash');
- const { TorPool, SOCKSServer, TorProcess } = require('../');
- const { WAIT_FOR_CREATE, PAGE_LOAD_TIME, RETRY_DELAY, RETRY_STRATEGY, MAX_ATTEMPTS } = require('./constants');
- const request = require('requestretry').defaults({
- promiseFactory: ((resolver) => new Promise(resolver)),
- maxAttempts: MAX_ATTEMPTS,
- retryStrategy: RETRY_STRATEGY,
- retryDelay: RETRY_DELAY
- });
- nconf.use('memory');
- require(`${__dirname}/../src/nconf_load_env.js`)(nconf);
- nconf.defaults(require(`${__dirname}/../src/default_config.js`));
- describe('SOCKSServer', function () {
- describe('#handleConnection(socket)', function () {
- let socksPort;
- let socksServerTorPool;
- let socksServer;
- before('start up server', async function (){
- socksServerTorPool = new TorPool(nconf.get('torPath'), {}, nconf.get('parentDataDirectory'), 'round_robin', null);
- socksServer = new SOCKSServer(socksServerTorPool, null, false);
- this.timeout(WAIT_FOR_CREATE);
-
- await socksServerTorPool.create(1);
- socksPort = await getPort();
-
- await socksServer.listen(socksPort);
- });
- it('should service a request for example.com', async function () {
- this.timeout(PAGE_LOAD_TIME);
- await request({
- url: 'http://example.com',
- agent: new SocksProxyAgent(`socks5h://127.0.0.1:${socksPort}`)
- });
- });
- it('should emit the "instance_connection" event', function (done) {
- this.timeout(PAGE_LOAD_TIME);
- let connectionHandler = (instance, source) => {
- assert.instanceOf(instance, TorProcess);
- assert.isObject(source);
-
- socksServer.removeAllListeners('instance_connection');;
- done();
- };
- socksServer.on('instance_connection', connectionHandler);
- request({
- url: 'http://example.com',
- agent: new SocksProxyAgent(`socks5h://127.0.0.1:${socksPort}`)
- })
- .catch(done)
- });
- after('shutdown server and shutdown tor pool', async function () {
- socksServer.close();
- await socksServerTorPool.exit();
- });
- });
- describe('#authenticate_user(username, password)', function () {
- let socksPort;
- let socksServerTorPool;
- let socksServer;
- let instance_def = {
- Name: 'instance-3',
- Group: "foo"
- };
- before('start up server', async function (){
- socksServerTorPool = new TorPool(nconf.get('torPath'), {}, nconf.get('parentDataDirectory'), 'round_robin', null);
- socksServer = new SOCKSServer(socksServerTorPool, null, { deny_unidentified_users: true, mode: "individual" });
-
- this.timeout(WAIT_FOR_CREATE * 3);
-
- await socksServerTorPool.create(2);
- await socksServerTorPool.add(instance_def);
- socksPort = await getPort();
-
- await socksServer.listen(socksPort);
- });
- it(`should service a request for example.com through ${instance_def.Name}`, function (done) {
- this.timeout(PAGE_LOAD_TIME);
- let connectionHandler = (instance, source) => {
- assert.equal(instance.instance_name, instance_def.Name);
- assert.isTrue(source.by_name);
- socksServer.removeAllListeners('instance_connection');
- done();
- };
- socksServer.on('instance_connection', connectionHandler);
- request({
- url: 'http://example.com',
- agent: new SocksProxyAgent(`socks5h://${instance_def.Name}:doesnotmatter@127.0.0.1:${socksPort}`)
- })
- .catch(done);
- });
- return
- it(`four requests made to example.com through the group named "foo" should come from the instances in "foo"`, function (done) {
- (async () => {
- this.timeout(PAGE_LOAD_TIME + (WAIT_FOR_CREATE));
- await socksServerTorPool.add([
- {
- Name: 'instance-4',
- Group: 'foo'
- }
- ]);
- })()
- .then(async () => {
- socksServer.proxy_by_name.mode = "group";
- let names_requested = [];
- let connectionHandler = (instance, source) => {
- names_requested.push(instance.instance_name);
- if (names_requested.length === socksServerTorPool.instances.length) {
- names_requested = _.uniq(names_requested).sort();
- let names_in_group = socksServerTorPool.instances_by_group('foo').map((i) => i.instance_name).sort()
- assert.deepEqual(names_requested, names_in_group);
- socksServer.removeAllListeners('instance_connection');
- done();
- }
- };
- socksServer.on('instance_connection', connectionHandler);
- let i = 0;
- while (i < socksServerTorPool.instances.length) {
- await request({
- url: 'http://example.com',
- agent: new SocksProxyAgent(`socks5h://foo:doesnotmatter@127.0.0.1:${socksPort}`)
- });
- i++;
- }
- })
- .then(async () => {
- await socksServerTorPool.remove_by_name('instance-4');
- socksServer.proxy_by_name.mode = "individual";
- })
- .catch(done);
- });
- // it(`shouldn't be able to send a request without a username`, async function() {
- // let f = () => {};
- // try {
- // await request({
- // url: 'http://example.com',
- // agent: new HttpAgent({
- // proxyHost: '127.0.0.1',
- // proxyPort: socksPort,
- // localDNS: false,
- // auths: [ auth.None() ]
- // }),
- // timeout: PAGE_LOAD_TIME
- // });
- // } catch (error) {
- // f = () => { throw error };
- // } finally {
- // assert.throws(f);
- // }
- // });
- // it(`shouldn't be able to send a request with an incorrect username`, async function() {
- // let f = () => {};
- // try {
- // await request({
- // url: 'http://example.com',
- // agent: new HttpAgent({
- // proxyHost: '127.0.0.1',
- // proxyPort: socksPort,
- // localDNS: false,
- // auths: [ auth.UserPassword("foo", "bar") ]
- // }),
- // timeout: PAGE_LOAD_TIME
- // });
- // } catch (error) {
- // f = () => { throw error };
- // } finally {
- // assert.throws(f);
- // }
- // });
- after('shutdown server and shutdown tor pool', async function () {
- socksServer.close();
- await socksServerTorPool.exit();
- });
- });
-
- });
|