diff --git a/.changeset/cool-coins-march.md b/.changeset/cool-coins-march.md new file mode 100644 index 00000000..a845151c --- /dev/null +++ b/.changeset/cool-coins-march.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/https-proxy-agent/test/e2e.test.ts b/packages/https-proxy-agent/test/e2e.test.ts index 29d3fcc3..7e1215e0 100644 --- a/packages/https-proxy-agent/test/e2e.test.ts +++ b/packages/https-proxy-agent/test/e2e.test.ts @@ -4,43 +4,21 @@ import { HttpsProxyAgent } from '../src'; interface NordVPNServer { name: string; - domain: string; - flag: string; - features: { [key: string]: boolean }; + hostname: string; + status: string; + locations: { + country: { + code: string; + } + + }[]; + technologies: { + identifier: string; + }[]; } jest.setTimeout(30000); -const findNordVpnServer = () => - retry( - async (): Promise => { - 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.proxy_ssl - ); - 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; - }, - { - retries: 5, - onRetry(err, attempt) { - console.log( - `Failed to get NordVPN servers. Retrying… (attempt #${attempt}, ${err.message})` - ); - }, - } - ); - async function getRealIP(): Promise { const res = await req('https://dump.n8.io'); const body = await json(res); @@ -48,6 +26,45 @@ async function getRealIP(): Promise { } describe('HttpsProxyAgent', () => { + let server: NordVPNServer; + + it('should find NordVPN "proxy_ssl" server', async () => { + server = await retry( + async (): Promise => { + const res = await req( + 'https://api.nordvpn.com/v1/servers?limit=0' + ); + 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.status === 'online' && + s.technologies.find((t) => t.identifier === 'proxy_ssl') + ); + if (servers.length === 0) { + throw new Error( + 'Could not find `https` proxy server from NordVPN' + ); + } + return servers[Math.floor(Math.random() * servers.length)]; + }, + { + retries: 5, + onRetry(err, attempt) { + console.log( + `Failed to get NordVPN servers. Retrying… (attempt #${attempt}, ${err.message})` + ); + }, + } + ); + console.log( + `Using NordVPN "proxy_ssl" server: ${server.name} (${server.hostname})` + ); + }); + it('should work over NordVPN proxy', async () => { const { NORDVPN_USERNAME, NORDVPN_PASSWORD } = process.env; if (!NORDVPN_USERNAME) { @@ -57,28 +74,23 @@ describe('HttpsProxyAgent', () => { throw new Error('`NORDVPN_PASSWORD` env var is not defined'); } - const [realIp, server] = await Promise.all([ - getRealIP(), - findNordVpnServer(), - ]); - console.log( - `Using NordVPN HTTPS proxy server: ${server.name} (${server.domain})` - ); - const username = encodeURIComponent(NORDVPN_USERNAME); const password = encodeURIComponent(NORDVPN_PASSWORD); // NordVPN runs their HTTPS proxy servers on port 89 // https://www.reddit.com/r/nordvpn/comments/hvz48h/nordvpn_https_proxy/ const agent = new HttpsProxyAgent( - `https://${username}:${password}@${server.domain}:89` + `https://${username}:${password}@${server.hostname}:89` ); - const res = await req('https://dump.n8.io', { agent }); + const [res, realIp] = await Promise.all([ + req('https://dump.n8.io', { agent }), + getRealIP(), + ]); 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 + server.locations[0].country.code ); }); }); diff --git a/packages/socks-proxy-agent/test/e2e.test.ts b/packages/socks-proxy-agent/test/e2e.test.ts index dd4a695e..4fa3f857 100644 --- a/packages/socks-proxy-agent/test/e2e.test.ts +++ b/packages/socks-proxy-agent/test/e2e.test.ts @@ -4,43 +4,20 @@ import { SocksProxyAgent } from '../src'; interface NordVPNServer { name: string; - domain: string; - flag: string; - features: { [key: string]: boolean }; + hostname: string; + status: string; + locations: { + country: { + code: string; + }; + }[]; + technologies: { + identifier: string; + }[]; } jest.setTimeout(30000); -const findNordVpnServer = () => - retry( - async (): Promise => { - 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 { const res = await req('https://dump.n8.io'); const body = await json(res); @@ -48,6 +25,45 @@ async function getRealIP(): Promise { } describe('SocksProxyAgent', () => { + let server: NordVPNServer; + + it('should find NordVPN "socks" server', async () => { + server = await retry( + async (): Promise => { + const res = await req( + 'https://api.nordvpn.com/v1/servers?limit=0' + ); + 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.status === 'online' && + s.technologies.find((t) => t.identifier === 'socks') + ); + if (servers.length === 0) { + throw new Error( + 'Could not find `https` proxy server from NordVPN' + ); + } + return servers[Math.floor(Math.random() * servers.length)]; + }, + { + retries: 5, + onRetry(err, attempt) { + console.log( + `Failed to get NordVPN servers. Retrying… (attempt #${attempt}, ${err.message})` + ); + }, + } + ); + console.log( + `Using NordVPN "socks" server: ${server.name} (${server.hostname})` + ); + }); + it('should work over NordVPN proxy', async () => { const { NORDVPN_USERNAME, NORDVPN_PASSWORD } = process.env; if (!NORDVPN_USERNAME) { @@ -57,26 +73,20 @@ describe('SocksProxyAgent', () => { 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}` + `socks://${username}:${password}@${server.hostname}` ); - const res = await req('https://dump.n8.io', { agent }); + const [res, realIp] = await Promise.all([ + req('https://dump.n8.io', { agent }), + getRealIP(), + ]); 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 + server.locations[0].country.code ); }); });