v3.6.0: proxy engine - auto-test/replace free proxies (echo ladder), local CONNECT-chain forwarder, browser exit verified live
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
// Round 3: find working proxy PAIRS/CHAINS among the 15 verified survivors,
|
||||
// with reasons, and try a 3-hop chain over the best pair.
|
||||
import net from 'node:net';
|
||||
const parseProxy = (s) => { const m = /^(?:https?:\/\/)?([\w.-]+):(\d{1,5})$/.exec(String(s).trim()); return m ? { host: m[1], port: Number(m[2]) } : null; };
|
||||
const chainLabel = (h) => `${h.host}:${h.port}`;
|
||||
function netConnect(host, port, timeoutMs) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const s = net.connect({ host, port });
|
||||
const t = setTimeout(() => { s.destroy(); reject(new Error(`connect timeout`)); }, timeoutMs);
|
||||
s.once('connect', () => { clearTimeout(t); resolve(s); });
|
||||
s.once('error', (e) => { clearTimeout(t); reject(e); });
|
||||
});
|
||||
}
|
||||
function hopConnect200(sock, carry, host, port, timeoutMs) {
|
||||
return new Promise((resolve) => {
|
||||
let buf = carry || '';
|
||||
const cleanup = () => { clearTimeout(timer); sock.removeListener('data', onData); sock.removeListener('error', onError); };
|
||||
const timer = setTimeout(() => { cleanup(); resolve({ ok: false, rest: '', reason: 'timeout' }); }, timeoutMs);
|
||||
const onData = (d) => {
|
||||
buf += d.toString('latin1');
|
||||
const i = buf.indexOf('\r\n\r\n');
|
||||
if (i < 0) { if (buf.length > 16384) { cleanup(); resolve({ ok: false, rest: '', reason: 'bad headers' }); } return; }
|
||||
cleanup();
|
||||
const m = /^HTTP\/1\.[01] (\d{3})([^\r\n]*)/.exec(buf);
|
||||
resolve({ ok: !!m && m[1] === '200', rest: buf.slice(i + 4), reason: m ? `status ${m[1]}${m[2]}` : 'not http' });
|
||||
};
|
||||
const onError = () => { cleanup(); resolve({ ok: false, rest: '', reason: 'socket error' }); };
|
||||
sock.on('data', onData);
|
||||
sock.on('error', onError);
|
||||
sock.write(`CONNECT ${host}:${port} HTTP/1.1\r\nHost: ${host}:${port}\r\nProxy-Connection: keep-alive\r\n\r\n`);
|
||||
});
|
||||
}
|
||||
async function chainTunnel(hops, targetHost, targetPort, timeoutMs = 8000) {
|
||||
const sock = await netConnect(hops[0].host, hops[0].port, timeoutMs);
|
||||
let carry = '';
|
||||
for (const [th, tp] of [...hops.slice(1).map((h) => [h.host, h.port]), [targetHost, targetPort]]) {
|
||||
const r = await hopConnect200(sock, carry, th, tp, timeoutMs);
|
||||
carry = '';
|
||||
if (!r.ok) { sock.destroy(); throw new Error(`hop ${chainLabel(hops[0])} CONNECT ${th}:${tp} -> ${r.reason}`); }
|
||||
carry = r.rest;
|
||||
}
|
||||
return { sock, carry };
|
||||
}
|
||||
async function proxyHttpGet(hops, url, timeoutMs = 8000) {
|
||||
const m = /^https?:\/\/([^/:]+)(?::(\d+))?(\/[^\s]*)?$/i.exec(url);
|
||||
const host = m[1]; const port = m[2] ? Number(m[2]) : 80; const path = m[3] || '/';
|
||||
const { sock, carry } = await chainTunnel(hops, host, port, timeoutMs);
|
||||
return await new Promise((resolve, reject) => {
|
||||
let buf = ''; let done = false;
|
||||
const fin = (err) => {
|
||||
if (done) return; done = true;
|
||||
if (err) { sock.destroy(); reject(err); return; }
|
||||
const im = /^HTTP\/1\.[01] (\d{3})/.exec(buf);
|
||||
const bi = buf.indexOf('\r\n\r\n');
|
||||
resolve({ status: im ? Number(im[1]) : 0, body: (bi >= 0 ? buf.slice(bi + 4) : buf).trim().split(/\r?\n/)[0] });
|
||||
};
|
||||
sock.setTimeout(timeoutMs, () => fin(new Error('read timeout')));
|
||||
sock.on('data', (d) => { buf += d.toString('latin1'); });
|
||||
sock.on('end', () => fin()); sock.on('close', () => fin()); sock.on('error', (e) => fin(e));
|
||||
sock.write(`GET ${path} HTTP/1.1\r\nHost: ${host}\r\nUser-Agent: edge-kit/3.6\r\nAccept: */*\r\nConnection: close\r\n\r\n`);
|
||||
if (carry) sock.write(Buffer.from(carry, 'latin1'));
|
||||
});
|
||||
}
|
||||
|
||||
const W = ('190.115.15.100:8080 123.253.145.165:8080 34.43.46.91:80 157.10.97.37:8080 165.99.151.252:8080 194.55.234.58:8080 144.79.181.33:8080 153.56.246.169:8080')
|
||||
.split(' ').map(parseProxy);
|
||||
const combos = [];
|
||||
for (const a of W) for (const b of W) if (a !== b) combos.push([a, b]);
|
||||
const out = await Promise.all(combos.map(async ([a, b]) => {
|
||||
const t0 = Date.now();
|
||||
try {
|
||||
const r = await proxyHttpGet([a, b], 'http://checkip.amazonaws.com/', 8000);
|
||||
return { a: chainLabel(a), b: chainLabel(b), status: r.status, ip: r.status === 200 ? r.body : null, ms: Date.now() - t0 };
|
||||
} catch (e) { return { a: chainLabel(a), b: chainLabel(b), status: 0, err: String(e.message).slice(0, 100), ip: null, ms: Date.now() - t0 }; }
|
||||
}));
|
||||
const win = out.filter((r) => r.ip);
|
||||
console.log(`2-hop attempts: ${out.length}, working: ${win.length}`);
|
||||
for (const r of win.slice(0, 6)) console.log(`CHAIN OK ${r.a} -> ${r.b}: exit ${r.ip} (${r.ms}ms)`);
|
||||
for (const r of out.filter((x) => !x.ip).slice(0, 6)) console.log(`chain fail ${r.a} -> ${r.b}: ${r.err || 'status ' + r.status}`);
|
||||
if (win.length) {
|
||||
const used = new Set([win[0].a, win[0].b]);
|
||||
const third = W.find((h) => !used.has(chainLabel(h)));
|
||||
if (third) {
|
||||
const hops = [parseProxy(win[0].a), parseProxy(win[0].b), third];
|
||||
const t0 = Date.now();
|
||||
try {
|
||||
const r = await proxyHttpGet(hops, 'http://checkip.amazonaws.com/', 9000);
|
||||
console.log(`3-HOP CHAIN ${hops.map(chainLabel).join(' -> ')}: ${r.status === 200 ? 'exit ' + r.body : 'status ' + r.status} (${Date.now() - t0}ms)`);
|
||||
} catch (e) { console.log(`3-HOP CHAIN fail: ${String(e.message).slice(0, 130)}`); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user