Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
socks-proxy-agent
E2E test and randomize
- Loading branch information
1 parent
b4b70a5
commit 452a986
Showing
6 changed files
with
120 additions
and
3 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
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"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.