Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Runs a simple end to end test against NordVPN servers, for `https-proxy-agent` and `socks-proxy-agent`.
- Loading branch information
1 parent
de70a45
commit 4b3e591
Showing
12 changed files
with
268 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import retry from 'async-retry'; | ||
import { req, json } from 'agent-base'; | ||
import { HttpsProxyAgent } 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.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<string> { | ||
const res = await req('https://dump.n8.io'); | ||
const body = await json(res); | ||
return body.request.headers['x-real-ip']; | ||
} | ||
|
||
describe('HttpsProxyAgent', () => { | ||
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 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` | ||
); | ||
|
||
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 | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"include": ["test.ts"] | ||
"include": ["*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"include": ["*.ts"] | ||
} |
Oops, something went wrong.