|
|
|
@@ -669,12 +669,23 @@ async function echoLadder(hops) {
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
let directEgress;
|
|
|
|
|
async function directEgressOf() {
|
|
|
|
|
// node fetch never reads HTTP_PROXY/v2ray — this is the machine's true exit.
|
|
|
|
|
if (directEgress === undefined) {
|
|
|
|
|
directEgress = null;
|
|
|
|
|
try { const r = await fetch('http://checkip.amazonaws.com/', { signal: AbortSignal.timeout(7000) }); directEgress = (await r.text()).trim().split(/\s/)[0] || null; } catch { /* offline */ }
|
|
|
|
|
}
|
|
|
|
|
return directEgress;
|
|
|
|
|
}
|
|
|
|
|
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 [e, own] = await Promise.all([echoLadder([h]), directEgressOf()]);
|
|
|
|
|
if (!e) return { ok: false, reason: 'no echo (dead or blocking)', ms: Date.now() - t0 };
|
|
|
|
|
if (own && (e.ip === own || e.ip.startsWith(own.split('.').slice(0, 2).join('.') + '.'))) {
|
|
|
|
|
return { ok: false, reason: `transparent (exit ${e.ip} is your own network — routing through it changes nothing)`, ms: Date.now() - t0 };
|
|
|
|
|
}
|
|
|
|
|
return { ok: true, ip: e.ip, country: e.country, as: e.org, ms: Date.now() - t0 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pipeBoth = (a, b) => {
|
|
|
|
@@ -763,7 +774,7 @@ async function egressVia(hops) {
|
|
|
|
|
async function restartSession() {
|
|
|
|
|
const la = session && session.launchArgs ? session.launchArgs : null;
|
|
|
|
|
if (!session || session.attached || !la) return false;
|
|
|
|
|
const { child, ws, engine } = session;
|
|
|
|
|
const { child, ws, engine, kind } = session;
|
|
|
|
|
// Close the browser itself before dropping the socket: for Firefox the spawn
|
|
|
|
|
// handle is only the Windows launcher, so killing it can orphan the browser
|
|
|
|
|
// and leave Firefox's single BiDi session registered (the next session.new
|
|
|
|
@@ -773,8 +784,24 @@ async function restartSession() {
|
|
|
|
|
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 < 10000) await new Promise((r) => setTimeout(r, 500));
|
|
|
|
|
const port = la.port || effectivePort(la);
|
|
|
|
|
const waitFree = async (ms) => {
|
|
|
|
|
const t0 = Date.now();
|
|
|
|
|
while (await probePort(port) && Date.now() - t0 < ms) await new Promise((r) => setTimeout(r, 400));
|
|
|
|
|
return !(await probePort(port));
|
|
|
|
|
};
|
|
|
|
|
if (!(await waitFree(6000))) {
|
|
|
|
|
// A survivor browser (Edge startup-boost, profile handoff, leftover window)
|
|
|
|
|
// retook the port without our new flags — a fresh spawn would merge into it
|
|
|
|
|
// and silently lose proxy routing. For Chromium, kill instances of this
|
|
|
|
|
// browser kind (same semantics as relaunch:true; logins survive, windows
|
|
|
|
|
// reopen). Firefox refuses: taskkill would close every Firefox window.
|
|
|
|
|
if (kind === 'firefox') return false;
|
|
|
|
|
const exe = kind === 'chrome' ? 'chrome' : 'msedge';
|
|
|
|
|
const killCmd = process.platform === 'win32' ? `taskkill /IM ${exe}.exe /F` : `pkill -9 -x ${exe}`;
|
|
|
|
|
await new Promise((r) => exec(killCmd, () => r()));
|
|
|
|
|
if (!(await waitFree(8000))) return false;
|
|
|
|
|
}
|
|
|
|
|
await doLaunch(la);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
@@ -1340,27 +1367,43 @@ const FETCH_EXTRACT = `(async () => {
|
|
|
|
|
return { url: location.href, status, text: document.body ? document.body.innerText : '' };
|
|
|
|
|
})()`;
|
|
|
|
|
|
|
|
|
|
const SEARCH_EXTRACT = `(() => {
|
|
|
|
|
const SEARCH_EXTRACT = `(async () => {
|
|
|
|
|
const parse = () => {
|
|
|
|
|
const clean = (s) => String(s || '').replace(/\\s+/g, ' ').trim();
|
|
|
|
|
const out = []; const seen = new Set();
|
|
|
|
|
// Engine-agnostic organic parse: a result headline is a heading inside a
|
|
|
|
|
// link on Google and Bing alike — layout class names drift, this doesn't.
|
|
|
|
|
for (const a of document.querySelectorAll('a[href]')) {
|
|
|
|
|
const heading = a.querySelector('h2, h3');
|
|
|
|
|
if (!heading) continue;
|
|
|
|
|
let url = a.href;
|
|
|
|
|
if (url.startsWith('/url?')) { const m = url.match(/[?&]url=([^&]+)/); if (m) { try { url = decodeURIComponent(m[1]); } catch {} } }
|
|
|
|
|
if (!/^https?:/i.test(url) || seen.has(url)) continue;
|
|
|
|
|
const title = clean(heading.innerText);
|
|
|
|
|
const selfUrl = (u) => /^https?:\\/\\/(?:www\\.)?(?:bing|google|duckduckgo)\\./i.test(u);
|
|
|
|
|
// Organic result headlines come in both directions now: heading inside the
|
|
|
|
|
// link (Google: a > h3) AND link inside the heading (Bing: h2 > a). Match
|
|
|
|
|
// both — engine class names drift, the heading/link nesting does not.
|
|
|
|
|
for (const node of document.querySelectorAll('h2 a, h3 a, a h2, a h3')) {
|
|
|
|
|
if (out.length >= 20) break;
|
|
|
|
|
const link = node.matches('a') ? node : node.closest('a');
|
|
|
|
|
if (!link) continue;
|
|
|
|
|
let url = link.href;
|
|
|
|
|
// Engines wrap organic URLs: Google /url?url=..., Bing /ck/a?u=a1<base64>.
|
|
|
|
|
// Unwrap first — then apply the own-domain filter, or the wrappers eat
|
|
|
|
|
// every real result.
|
|
|
|
|
{ const m = url.match(/[?&]u=([^&]+)/); if (m) { let v = m[1]; if (v.startsWith('a1')) v = v.slice(2); try { const d = atob(v.replace(/-/g, '+').replace(/_/g, '/')); if (/^https?:/i.test(d)) url = d; } catch {} } }
|
|
|
|
|
{ const m = url.match(/[?&](?:url|q)=([^&]+)/); if (m) { try { const d = decodeURIComponent(m[1]); if (/^https?:/i.test(d)) url = d; } catch {} } }
|
|
|
|
|
if (!/^https?:/i.test(url) || seen.has(url) || selfUrl(url)) continue;
|
|
|
|
|
const title = clean(link.innerText);
|
|
|
|
|
if (!title || title.length < 4) continue;
|
|
|
|
|
const box = a.closest('li,div.g,div.tF2Cxc,li.b_algo') || a.parentElement;
|
|
|
|
|
const box = link.closest('li,div.g,div.tF2Cxc,li.b_algo,div.b_title,div[class*=result]') || link.parentElement;
|
|
|
|
|
let snippet = box ? clean(box.innerText) : '';
|
|
|
|
|
if (snippet.startsWith(title)) snippet = snippet.slice(title.length).trim();
|
|
|
|
|
seen.add(url);
|
|
|
|
|
out.push({ url, title: title.slice(0, 200), ...(snippet ? { snippet: snippet.slice(0, 400) } : {}) });
|
|
|
|
|
if (out.length >= 20) break;
|
|
|
|
|
}
|
|
|
|
|
return { sources: out, page: { url: location.href, title: clean(document.title) } };
|
|
|
|
|
return { sources: out, page: { url: location.href, title: clean(document.title) } };
|
|
|
|
|
};
|
|
|
|
|
// Engines serving datacenter exits hand back a redirect shell first
|
|
|
|
|
// (Bing: &rdr=1&rdrig=…) and hydrate organics after readyState — poll
|
|
|
|
|
// in-page until real results exist (max ~6s) instead of parsing the shell.
|
|
|
|
|
for (let i = 0; i < 12; i++) {
|
|
|
|
|
const r = parse();
|
|
|
|
|
if (r.sources.length || i >= 11) return r;
|
|
|
|
|
await new Promise((res) => setTimeout(res, 500));
|
|
|
|
|
}
|
|
|
|
|
})()`;
|
|
|
|
|
|
|
|
|
|
const edgeFetchProvider = {
|
|
|
|
@@ -1382,7 +1425,7 @@ const edgeSearchProvider = {
|
|
|
|
|
async search(request, signal) {
|
|
|
|
|
const n = Math.min(Math.max(request.maxResults ?? 10, 1), 20);
|
|
|
|
|
const q = encodeURIComponent(request.query);
|
|
|
|
|
const engine = cfgVal('searchEngine', 'google') === 'bing' ? 'bing' : 'google';
|
|
|
|
|
const engine = cfgVal('searchEngine', 'bing') === 'google' ? 'google' : 'bing';
|
|
|
|
|
const build = (e) => e === 'bing'
|
|
|
|
|
? `https://www.bing.com/search?q=${q}&count=${Math.min(n * 2, 30)}`
|
|
|
|
|
: `https://www.google.com/search?q=${q}&num=${Math.min(n * 2, 20)}`;
|
|
|
|
|