From 40f67371830c5171e01ff6c7b7d57ab7dacf6371 Mon Sep 17 00:00:00 2001 From: 8ball Date: Sun, 8 Nov 2026 20:33:15 +0330 Subject: [PATCH] v3.6.0: proxy engine - auto-test/replace free proxies (echo ladder), local CONNECT-chain forwarder, browser exit verified live --- README.md | 11 ++ index.js | 269 +++++++++++++++++++++++++++++++++++++- package.json | 2 +- test/proxy-chain.test.mjs | 91 +++++++++++++ 4 files changed, 367 insertions(+), 6 deletions(-) create mode 100644 test/proxy-chain.test.mjs diff --git a/README.md b/README.md index d5c85ed..61a6f83 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,17 @@ Per-browser profiles: the user's real profile by default (`realProfile`), or iso `launch` · `attach` · `status` · `tabs` · `newTab` · `switchTab` · `closeTab` · `goto` · `back` · `forward` · `reload` · `see` · `click` · `clickAt` · `drag` · `video` · `type` · `press` · `scroll` · `waitFor` · `eval` · `screenshot` · `wait` · `close` +## Proxy support + +`proxy {op:auto|set|status|clear, proxyChain:"ip:port[,ip:port,...]", chainTo?}`: + +- **auto** — probes every candidate by fetching an IP echo **through** it (ladder: checkip.amazonaws.com → ifconfig.me → ipinfo.io, so a single rate-limited echo service can't fake a failure), picks the fastest working proxy, restarts the browser onto it, and reports the verified exit IP/country. +- **set** — installs a list verbatim. One hop is handed to the browser directly (`--proxy-server`); two or more are **chained** through an in-kit local CONNECT forwarder (browser → 127.0.0.1 → hop1 → hop2 → … → origin), verified end-to-end before the switch — a rejected chain falls back to the fastest single hop (or errors, in `set`). +- **status** — live re-probe of the current route; **clear** — direct traffic again. `chainTo` adds one chained hop behind the auto-pick. +- The `proxyChain` setting preloads a chain into every launch; the native web seam (`web_fetch` / `web_search`) rides the same route. + +A proxy counts as *working* only when an echo comes back through it — no blind trust in proxy-list sites. + ## Troubleshooting - **`Maximum number of active sessions` (Firefox)** — a zombie Firefox is holding the BiDi session slot; terminate `firefox.exe` and relaunch. diff --git a/index.js b/index.js index e452ddb..98514b7 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,7 @@ */ import os from 'node:os'; import path from 'node:path'; +import net from 'node:net'; import { spawn, exec } from 'node:child_process'; import { promises as fs } from 'node:fs'; // schemastery ships inside the app bundle; a profile-linked package cannot @@ -59,6 +60,9 @@ export const Config = z && z.object({ browser: z.string() .description('Which browser the kit launches by default: "edge", "chrome", or "firefox". The launch action can still pick another per call.') .default('edge').volatile(), + proxyChain: z.string() + .description('Proxy applied to every launch: "ip:port", or comma-separated hops "ip:port,ip:port" — two or more chain through an in-kit local forwarder. The proxy action can test candidates and swap this live.') + .volatile(), fetchTextCap: z.number() .description('Maximum characters of rendered page text returned by native web_fetch (it reads pages through your browser, so JavaScript sites work and bot walls pass).') .step(1).min(1000).max(60000).default(20000).volatile(), @@ -80,6 +84,7 @@ const settingsSnapshot = () => ({ executablePath: cfgVal('executablePath', ''), seeTextCap: cfgVal('seeTextCap', 2200), browser: cfgVal('browser', 'edge'), + proxyChain: cfgVal('proxyChain', ''), }); const DEFAULT_PORT = 9444; @@ -400,11 +405,13 @@ async function doLaunch(args) { : (useReal && realFor ? realFor : cfgVal('profileDir', '') || kitDir); await fs.mkdir(profileDir, { recursive: true }); const headless = args.headless ?? cfgVal('headless', false); + const proxyFlag = await launchProxyFlag(args); const child = spawn(executable, kind === 'firefox' ? [ `-remote-debugging-port=${port}`, '-profile', profileDir, '-no-remote', ...(headless ? ['-headless'] : []), + ...(proxyFlag ? ['-proxy-server', proxyFlag] : []), 'about:blank', ] : [ `--remote-debugging-port=${port}`, @@ -417,6 +424,7 @@ async function doLaunch(args) { '--disable-backgrounding-occluded-windows', '--disable-renderer-backgrounding', ...(headless ? ['--headless=new'] : []), + ...(proxyFlag ? [`--proxy-server=${proxyFlag}`] : []), 'about:blank', ], { stdio: 'ignore', detached: false }); if (!(await waitForPort(port, 25000))) { @@ -424,7 +432,7 @@ async function doLaunch(args) { throw new Error('browser started but the DevTools port never opened. If your everyday browser is running without a debug port it owns this profile: pass relaunch:true (closes it and reopens it debuggable), or use a different profileDir.'); } const { ws, info } = await connect(port); - session = { ws, port, child, headless, profileDir, attached: false, kind, engine: info.bidi ? 'bidi' : 'cdp' }; + session = { ws, port, child, headless, profileDir, attached: false, kind, engine: info.bidi ? 'bidi' : 'cdp', launchArgs: { browser: kind, executablePath: executable, profileDir, headless, port } }; await attachToPage(); return { ok: true, launched: true, headless, port, profileDir, realProfile: profileDir === realFor, browser: kind, executable }; } @@ -439,6 +447,202 @@ async function ensureActive(args) { await doLaunch(args ?? {}); } +/* ── Proxy engine: routes all browser traffic (including the web seam) through + * free proxies. One hop is handed straight to the browser; two or more chain + * through an in-kit local CONNECT forwarder (browser → 127.0.0.1 → hop1 → + * hop2 → … → origin). Candidates are verified by fetching an IP echo through + * them, so "working" means the exit IP literally proves the route. */ +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}`; +let chainHolder = null; // multi-hop mode: { hops, server, endpoint, raw } +let activeProxy = null; // single-hop mode: 'host:port' given to the browser + +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 ${host}:${port} 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})/.exec(buf); + resolve({ ok: !!m && m[1] === '200', rest: buf.slice(i + 4), reason: m ? `status ${m[1]}` : '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 = 9000) { + 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(`proxy ${chainLabel(hops[0])}: CONNECT ${th}:${tp} -> ${r.reason}`); } + carry = r.rest; + } + return { sock, carry }; +} + +async function proxyHttpGet(hops, url, timeoutMs = 9000) { + const m = /^https?:\/\/([^/:]+)(?::(\d+))?(\/[^\s]*)?$/i.exec(url); + if (!m) throw new Error('bad proxy-probe url'); + const host = m[1]; + const port = m[2] ? Number(m[2]) : 80; + const path = m[3] || '/'; + const direct = hops.length === 1 && port === 80; + let sock, carry = ''; + if (direct) sock = await netConnect(hops[0].host, hops[0].port, timeoutMs); + else ({ sock, carry } = await chainTunnel(hops, host, port, timeoutMs)); + return await new Promise((resolve, reject) => { + let buf = ''; + 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] }); + }; + let done = false; + sock.setTimeout(timeoutMs, () => fin(new Error('proxy read timeout'))); + sock.on('data', (d) => { buf += d.toString('latin1'); }); + sock.on('end', () => fin()); sock.on('close', () => fin()); sock.on('error', (e) => fin(e)); + // Single hop over http: absolute-form forward GET — every HTTP proxy does + // this, including the free ones that refuse CONNECT (common on IR lists). + // Chaining uses the CONNECT nesting chainTunnel speaks. + sock.write(`${direct ? `GET ${url}` : `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 ECHOES = ['http://checkip.amazonaws.com/', 'http://ifconfig.me/ip', 'http://ipinfo.io/json']; +async function echoLadder(hops) { + // Free proxies share exit IPs, so any one echo service can rate-limit (429); + // the ladder keeps verification honest as long as one echo answers. + for (const url of ECHOES) { + try { + const r = await proxyHttpGet(hops, url); + if (r.status !== 200) continue; + let ip = null, country = null, org = null; + try { const j = JSON.parse(r.body); ip = j.ip; country = j.country; org = j.org; } catch { if (/^[0-9a-fA-F:.]{4,45}$/.test(r.body)) ip = r.body; } + if (ip) return { ip, country, org }; + } catch { /* next echo */ } + } + return null; +} +async function probeCandidate(h) { + const t0 = Date.now(); + const e = await echoLadder([h]); + return e + ? { ok: true, ip: e.ip, country: e.country, as: e.org, ms: Date.now() - t0 } + : { ok: false, reason: 'no echo (dead or blocking)', ms: Date.now() - t0 }; +} + +const pipeBoth = (a, b) => { + a.on('data', (c) => { if (!b.write(c)) a.pause(); }); + b.on('drain', () => a.resume()); + a.on('end', () => { try { b.end(); } catch { /* gone */ } }); + a.on('error', () => { try { b.destroy(); } catch { /* gone */ } }); + a.on('close', () => { try { b.destroy(); } catch { /* gone */ } }); +}; + +function startChainServer(hops) { + return new Promise((resolve, reject) => { + const server = net.createServer((client) => { + client.on('error', () => { try { client.destroy(); } catch { /* gone */ } }); + let buf = ''; + const onData = (d) => { + buf += d.toString('latin1'); + const i = buf.indexOf('\r\n\r\n'); + if (i < 0) { if (buf.length > 65536) client.destroy(); return; } + client.removeListener('data', onData); + handle(client, buf.slice(0, i), buf.slice(i + 4)); + }; + client.on('data', onData); + }); + const handle = async (client, head, rest) => { + const [method, target] = head.split('\r\n')[0].split(' '); + let host, port, rewritten = null; + if (method === 'CONNECT') { + const p = target.split(':'); host = p[0]; port = Number(p[1]); + if (!host || !port) throw new Error('bad CONNECT target'); + } else { + const m = /^(?:https?:)?\/\/([^/:?#]+)(?::(\d+))?(\/[^\s]*)?/i.exec(target) || /^(?:https?:)?\/\/([^/:?#]+)(?::(\d+))?$/i.exec(target); + if (!m) throw new Error('bad forward target'); + host = m[1]; port = m[2] ? Number(m[2]) : 80; + rewritten = `${method} ${m[3] || '/'} HTTP/1.1${head.slice(head.indexOf('\r\n'))}\r\n\r\n`; + } + const { sock, carry } = await chainTunnel(hops, host, port, 10000); + client.write('HTTP/1.1 200 Connection Established\r\n\r\n'); + if (rewritten) sock.write(Buffer.concat([Buffer.from(rewritten, 'latin1'), Buffer.from(rest, 'latin1')])); + else if (carry) sock.write(Buffer.from(carry, 'latin1')); + pipeBoth(client, sock); pipeBoth(sock, client); + }; + server.on('error', reject); + server.listen(0, '127.0.0.1', () => resolve(server)); + }); +} + +const stopChain = () => { + if (chainHolder) { try { chainHolder.server.close(); } catch { /* gone */ } chainHolder = null; } +}; +async function ensureHolder(hops) { + stopChain(); activeProxy = null; + if (hops.length < 2) { activeProxy = chainLabel(hops[0]); return null; } + const server = await startChainServer(hops); + chainHolder = { hops, server, mode: 'chain', raw: hops.map(chainLabel).join(','), endpoint: `http://127.0.0.1:${server.address().port}` }; + return chainHolder; +} +async function launchProxyFlag(args) { + if (chainHolder) return chainHolder.endpoint; + if (activeProxy) return `http://${activeProxy}`; + const raw = typeof args.proxyChain === 'string' && args.proxyChain.trim() + ? args.proxyChain.trim() + : String(cfgVal('proxyChain', '') || '').trim(); + if (!raw) return null; + const hops = raw.split(',').map(parseProxy).filter(Boolean); + if (!hops.length) return null; + const held = await ensureHolder(hops); + return held ? held.endpoint : `http://${activeProxy}`; +} +const activeProxyView = () => chainHolder + ? { chain: chainHolder.hops.map(chainLabel), mode: 'chain', endpoint: chainHolder.endpoint } + : (activeProxy ? { chain: [activeProxy], mode: 'single' } : null); +async function egressVia(hops) { + const t0 = Date.now(); + const e = await echoLadder(hops); + return e ? { ok: true, ip: e.ip, country: e.country, as: e.org, ms: Date.now() - t0 } : { ok: false, error: 'no echo back', ms: Date.now() - t0 }; +} +async function restartSession() { + const la = session && session.launchArgs ? session.launchArgs : null; + if (!session || session.attached || !la) return false; + const { child, ws } = session; + session = null; + try { ws.close(); } catch { /* gone */ } + if (child && child.exitCode === null) child.kill(); + const t0 = Date.now(); + while (await probePort(la.port || effectivePort(la)) && Date.now() - t0 < 6000) await new Promise((r) => setTimeout(r, 500)); + await doLaunch(la); + return true; +} + async function mouseClick(x, y, clickCount = 1) { for (const type of ['mousePressed', 'mouseReleased']) { await send('Input.dispatchMouseEvent', { type, x, y, button: 'left', clickCount, buttons: type === 'mousePressed' ? 1 : 0 }, session.sessionId); @@ -631,7 +835,7 @@ async function run(args) { case 'status': { const settings = settingsSnapshot(); if (session && session.ws.readyState === 1) { - return { ok: true, running: true, liveMode: session.attached, port: session.port, headless: session.headless, childPid: session.child?.pid ?? null, profileDir: session.profileDir, settings, ...await pageInfo() }; + return { ok: true, running: true, liveMode: session.attached, port: session.port, headless: session.headless, childPid: session.child?.pid ?? null, profileDir: session.profileDir, proxy: activeProxyView(), settings, ...await pageInfo() }; } const occupied = !!(await probePort(effectivePort(args))); return { ok: true, running: false, portOccupied: occupied, settings }; @@ -837,6 +1041,55 @@ async function run(args) { await new Promise((r) => setTimeout(r, ms)); return { ok: true, waited: ms }; } + case 'proxy': { + const raw = String(args.proxyChain || '').trim(); + const entries = raw.split(/[\s,;]+/).map(parseProxy).filter(Boolean); + const op = ['auto', 'set', 'status', 'clear'].includes(args.op) ? args.op : (entries.length ? 'auto' : 'status'); + if (op === 'clear') { + stopChain(); activeProxy = null; + const relaunched = await restartSession(); + return { ok: true, cleared: true, relaunched, note: 'proxy removed; relaunch to route traffic direct' }; + } + if (op === 'status') { + const view = activeProxyView(); + const live = view ? await egressVia(view.mode === 'chain' ? chainHolder.hops : [parseProxy(activeProxy)]) : null; + return { ok: true, active: view && { ...view, exit: live }, settings: settingsSnapshot() }; + } + if (!entries.length) return { ok: false, error: 'proxy: pass proxyChain "ip:port[,ip:port,...]" (auto tests candidates; set installs the list as the chain)' }; + const tested = await Promise.all(entries.map(async (h) => ({ hop: h, ...(await probeCandidate(h)) }))); + const working = tested.filter((t) => t.ok).sort((a, b) => a.ms - b.ms); + if (!working.length) return { ok: false, tested: entries.length, results: tested.map((t) => ({ proxy: chainLabel(t.hop), reason: t.reason, ms: t.ms })) }; + const pick = working[0]; + let hops; + if (op === 'set') hops = entries; + else { + hops = [pick.hop]; + if (args.chainTo) { const second = parseProxy(args.chainTo); if (second) hops.push(second); } + } + let note = null; + let exit = null; + if (hops.length > 1) { + exit = await egressVia(hops); + if (!exit.ok) { + if (op === 'set') return { ok: false, error: `chain ${hops.map(chainLabel).join(' -> ')} rejected: ${exit.error || 'no echo'} (fastest single hop: ${chainLabel(pick.hop)}, exit ${pick.ip})` }; + note = `chain ${hops.map(chainLabel).join(' -> ')} rejected (${exit.error || 'no echo'}) — applied the fastest single hop instead`; + hops = [pick.hop]; + } + } + await ensureHolder(hops); + const relaunched = await restartSession(); + const view = activeProxyView(); + if (!exit) exit = await egressVia(hops); + return { + ok: true, + ...(note ? { note } : {}), + active: { ...(view || {}), exit }, + chosen: { proxy: chainLabel(pick.hop), ip: pick.ip, country: pick.country, ms: pick.ms }, + relaunched, + tested: entries.length, + working: working.map((w) => ({ proxy: chainLabel(w.hop), ip: w.ip, country: w.country, ms: w.ms })).slice(0, 12), + }; + } case 'close': { if (!session) return { ok: true, note: 'no active session' }; const { child, attached, ws } = session; @@ -1001,7 +1254,10 @@ export function apply(ctx, config) { 'page: text + numbered elements, shadow-DOM aware) then act: click {index|text|selector}, ' + 'clickAt {x,y}, drag {x,y,toX,toY}, type {into,text,submit?}, press {key,times}, scroll; ' + 'understand video: video {video?, op:status|play|pause|seek|rate, to?, shots?, every?} ' + - 'returns playback state and PNG frames of the player; verify with waitFor {text|selector} ' + + 'returns playback state and PNG frames of the player; route traffic: proxy ' + + '{op:auto|set|status|clear, proxyChain:"ip:port[,ip:port]", chainTo?} verifies candidates with an ' + + 'IP echo, moves the browser onto the fastest working proxy, and chains hops through a local ' + + 'forwarder (exit IP reported); verify with waitFor {text|selector} ' + 'or screenshot; navigate with goto/back/forward/reload; organize with tabs/newTab/' + 'switchTab/closeTab. Session: launch (browser: edge|chrome|firefox; live-attaches to a ' + 'running debuggable browser first; realProfile:true keeps your logins; relaunch:true ' + @@ -1012,7 +1268,7 @@ export function apply(ctx, config) { properties: { action: { type: 'string', - enum: ['launch', 'attach', 'status', 'tabs', 'newTab', 'switchTab', 'closeTab', 'goto', 'back', 'forward', 'reload', 'see', 'click', 'clickAt', 'drag', 'video', 'type', 'press', 'scroll', 'waitFor', 'eval', 'screenshot', 'wait', 'close'], + enum: ['launch', 'attach', 'status', 'tabs', 'newTab', 'switchTab', 'closeTab', 'goto', 'back', 'forward', 'reload', 'see', 'click', 'clickAt', 'drag', 'video', 'type', 'press', 'scroll', 'waitFor', 'eval', 'screenshot', 'wait', 'proxy', 'close'], description: 'What to do.', }, url: { type: 'string', description: 'goto/newTab: address to open.' }, @@ -1043,10 +1299,12 @@ export function apply(ctx, config) { toY: { type: 'integer', description: 'drag: destination y.' }, times: { type: 'integer', description: 'press: repeat the key this many times (games).' }, video: { type: 'integer', description: 'video: which video element on the page (0 = first).' }, - op: { type: 'string', enum: ['status', 'play', 'pause', 'seek', 'rate'], description: 'video: what to do.' }, + op: { type: 'string', enum: ['status', 'play', 'pause', 'seek', 'rate', 'auto', 'set', 'clear'], description: 'video: what to do. proxy: auto (test list, use fastest working), set (use list as-is), status, clear.' }, to: { type: 'number', description: 'video seek: seconds; video rate: playback speed.' }, shots: { type: 'integer', description: 'video: also capture this many PNG frames (max 12).' }, every: { type: 'integer', description: 'video: milliseconds between frames (default 1000).' }, + proxyChain: { type: 'string', description: 'proxy: comma-separated ip:port list — candidates for op "auto", or the exact chain for op "set" (2+ hops chain through the in-kit local forwarder).' }, + chainTo: { type: 'string', description: 'proxy auto: chain this extra ip:port hop behind the winning proxy.' }, }, additionalProperties: false, }, @@ -1065,6 +1323,7 @@ export function apply(ctx, config) { offSearch(); offFetch(); dispose(); + stopChain(); }; }); } diff --git a/package.json b/package.json index 41e8ef4..8af4a2d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@local/edge-kit-easy", - "version": "3.5.6", + "version": "3.6.0", "private": true, "type": "module", "description": "Edge Kit Browser — human-style browsing for agents in Edge, Chrome, or Firefox: see, click, type, video understanding, gameplay; live-attach-first over CDP/BiDi with a native settings page and the native web seam", diff --git a/test/proxy-chain.test.mjs b/test/proxy-chain.test.mjs new file mode 100644 index 0000000..7202366 --- /dev/null +++ b/test/proxy-chain.test.mjs @@ -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)}`); } + } +}