-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifyProxy.ts
58 lines (35 loc) · 1.31 KB
/
verifyProxy.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
import ProxyVerifier from 'proxy-verifier'
const CHECK_TIMEOUT = 1000 * 5;
import { HttpsProxyAgent } from 'https-proxy-agent';
import fetch from 'node-fetch'
export async function testProxy(proxyUrl: string): Promise<boolean> {
try {
// Target website URL
const targetUrl = 'https://ident.me/ip';
// Create a new Proxy Agent
const proxyAgent = new HttpsProxyAgent(proxyUrl);
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), CHECK_TIMEOUT)
// Fetch the target website using the proxy agent
const response = await fetch(targetUrl, { agent: proxyAgent, signal:controller.signal });
return response.ok;
} catch (e) {
return false;
}
}
export function verifyProxy(proxyString: string): Promise<boolean> {
let proxy = {
ipAddress: proxyString.split(':')[0],
port: parseInt(proxyString.split(':')[1]),
protocol: 'http',
}
return Promise.race<boolean>([new Promise(res => {
ProxyVerifier.testAll(proxy, {}, function (error, result) {
// console.log(result)
let ok = result?.protocols?.http?.ok;
res(ok);
});
}), new Promise(res => {
setTimeout(_ => res(false), CHECK_TIMEOUT)
})])
}