Skip to content

Commit

Permalink
Add socks-proxy-agent E2E test and randomize
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Apr 29, 2023
1 parent b4b70a5 commit 452a986
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/https-proxy-agent/test/e2e.test.ts
Expand Up @@ -20,14 +20,15 @@ const findNordVpnServer = () =>
throw new Error(`Status code: ${res.statusCode}`);
}
const body = await json(res);
const server = (body as NordVPNServer[]).find(
const servers = (body as NordVPNServer[]).filter(
(s) => s.features.proxy_ssl
);
if (!server) {
if (servers.length === 0) {
throw new Error(
'Could not find `https` proxy server from NordVPN'
);
}
const server = servers[Math.floor(Math.random() * servers.length)];
return server;
},
{
Expand Down
5 changes: 5 additions & 0 deletions packages/socks-proxy-agent/jest.config.js
@@ -0,0 +1,5 @@
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
9 changes: 8 additions & 1 deletion packages/socks-proxy-agent/package.json
Expand Up @@ -114,12 +114,18 @@
"socks": "^2.7.1"
},
"devDependencies": {
"@types/async-retry": "^1.4.5",
"@types/debug": "^4.1.7",
"@types/jest": "^29.5.1",
"@types/node": "^14.18.43",
"async-retry": "^1.3.3",
"cacheable-lookup": "^6.1.0",
"dns2": "^2.1.0",
"jest": "^29.5.0",
"mocha": "^9.2.2",
"proxy": "workspace:*",
"socksv5": "github:TooTallNate/socksv5#fix/dstSock-close-event",
"ts-jest": "^29.1.0",
"tsconfig": "workspace:*",
"typescript": "^5.0.4"
},
Expand All @@ -128,7 +134,8 @@
},
"scripts": {
"build": "tsc",
"test": "mocha --reporter spec",
"test": "mocha --reporter spec test/test.js",
"test-e2e": "jest --env node --verbose --bail test/e2e.test.ts",
"lint": "eslint . --ext .ts",
"prepublishOnly": "npm run build"
},
Expand Down
82 changes: 82 additions & 0 deletions packages/socks-proxy-agent/test/e2e.test.ts
@@ -0,0 +1,82 @@
import retry from 'async-retry';
import { req, json } from 'agent-base';
import { SocksProxyAgent } from '../src';

interface NordVPNServer {
name: string;
domain: string;
flag: string;
features: { [key: string]: boolean };
}

jest.setTimeout(30000);

const findNordVpnServer = () =>
retry(
async (): Promise<NordVPNServer> => {
const res = await req('https://nordvpn.com/api/server');
if (res.statusCode !== 200) {
res.socket.destroy();
throw new Error(`Status code: ${res.statusCode}`);
}
const body = await json(res);
const servers = (body as NordVPNServer[]).filter(
(s) => s.features.socks
);
if (servers.length === 0) {
throw new Error(
'Could not find `socks` proxy server from NordVPN'
);
}
const server = servers[Math.floor(Math.random() * servers.length)];
return server;
},
{
retries: 5,
onRetry(err, attempt) {
console.log(
`Failed to get NordVPN servers. Retrying… (attempt #${attempt}, ${err.message})`
);
},
}
);

async function getRealIP(): Promise<string> {
const res = await req('https://dump.n8.io');
const body = await json(res);
return body.request.headers['x-real-ip'];
}

describe('SocksProxyAgent', () => {
it('should work over NordVPN proxy', async () => {
const { NORDVPN_USERNAME, NORDVPN_PASSWORD } = process.env;
if (!NORDVPN_USERNAME) {
throw new Error('`NORDVPN_USERNAME` env var is not defined');
}
if (!NORDVPN_PASSWORD) {
throw new Error('`NORDVPN_PASSWORD` env var is not defined');
}

const [realIp, server] = await Promise.all([
getRealIP(),
findNordVpnServer(),
]);
console.log(
`Using NordVPN SOCKS proxy server: ${server.name} (${server.domain})`
);

const username = encodeURIComponent(NORDVPN_USERNAME);
const password = encodeURIComponent(NORDVPN_PASSWORD);

const agent = new SocksProxyAgent(
`socks://${username}:${password}@${server.domain}`
);

const res = await req('https://dump.n8.io', { agent });
const body = await json(res);
expect(body.request.headers['x-real-ip']).not.toEqual(realIp);
expect(body.request.headers['x-vercel-ip-country']).toEqual(
server.flag
);
});
});
4 changes: 4 additions & 0 deletions packages/socks-proxy-agent/test/tsconfig.json
@@ -0,0 +1,4 @@
{
"extends": "../tsconfig.json",
"include": ["*.ts"]
}
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 452a986

Please sign in to comment.