/
e2e.test.ts
92 lines (84 loc) · 2.33 KB
/
e2e.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import retry from 'async-retry';
import { req, json } from 'agent-base';
import { SocksProxyAgent } from '../src';
interface NordVPNServer {
name: string;
hostname: string;
status: string;
locations: {
country: {
code: string;
};
}[];
technologies: {
identifier: string;
}[];
}
jest.setTimeout(30000);
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', () => {
let server: NordVPNServer;
it('should find NordVPN "socks" server', async () => {
server = await retry(
async (): Promise<NordVPNServer> => {
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) {
throw new Error('`NORDVPN_USERNAME` env var is not defined');
}
if (!NORDVPN_PASSWORD) {
throw new Error('`NORDVPN_PASSWORD` env var is not defined');
}
const username = encodeURIComponent(NORDVPN_USERNAME);
const password = encodeURIComponent(NORDVPN_PASSWORD);
const agent = new SocksProxyAgent(
`socks://${username}:${password}@${server.hostname}`
);
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.locations[0].country.code
);
});
});