File size: 2,346 Bytes
58461df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const http = require('http');
const https = require('https');

async function testProxy(protocol, targetHost, targetPath) {
    const proxy = 'http://zlet9i3y:oZJQ0o4V@157.10.49.215:44128';
    const targetUrl = `${protocol}://${targetHost}${targetPath}`;

    console.log(`Testing Proxy: ${proxy}`);
    console.log(`Target: ${targetUrl}`);

    const url = new URL(proxy);
    const auth = 'Basic ' + Buffer.from(url.username + ':' + url.password).toString('base64');

    if (protocol === 'http') {
        return new Promise((resolve) => {
            const options = {
                host: url.hostname,
                port: url.port,
                path: targetUrl,
                headers: {
                    'Proxy-Authorization': auth,
                    'Host': targetHost
                }
            };
            http.get(options, (res) => {
                let data = '';
                res.on('data', chunk => data += chunk);
                res.on('end', () => {
                    console.log(`HTTP Status: ${res.statusCode}`);
                    resolve(true);
                });
            }).on('error', e => {
                console.error(`HTTP Proxy Error: ${e.message}`);
                resolve(false);
            });
        });
    } else {
        // HTTPS CONNECT
        return new Promise((resolve) => {
            const req = http.request({
                host: url.hostname,
                port: url.port,
                method: 'CONNECT',
                path: `${targetHost}:443`,
                headers: {
                    'Proxy-Authorization': auth
                }
            });

            req.on('connect', (res, socket, head) => {
                console.log('HTTPS CONNECT established');
                socket.destroy();
                resolve(true);
            });

            req.on('error', e => {
                console.error(`HTTPS Proxy Error (CONNECT): ${e.message}`);
                resolve(false);
            });

            req.end();
        });
    }
}

async function run() {
    console.log('--- Step 1: HTTP Test ---');
    await testProxy('http', 'httpbin.org', '/ip');
    console.log('\n--- Step 2: HTTPS Test ---');
    await testProxy('https', 'labs.google', '/');
}

run();