Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 41c6bb133e | |||
| e072aadcb8 |
@@ -2,4 +2,3 @@ node_modules/
|
||||
*.log
|
||||
*.tgz
|
||||
.DS_Store
|
||||
midas/tool-def.json
|
||||
|
||||
@@ -63,45 +63,12 @@ Browser discovery is cross-platform — Windows, Linux (apt **and** snap paths,
|
||||
|
||||
- **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. Switching routes force-closes any survivor browser process (Edge startup-boost / profile handoff) before relaunch, so the new proxy state can never be silently lost — logins survive, windows reopen.
|
||||
- **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.
|
||||
- **Fully built in** — no system/v2ray proxy anywhere in the path: probes use raw sockets, and launched browsers get explicit proxy state (`--proxy-server` when routing; `--no-proxy-server` / `network.proxy.type:0` when direct, with `HTTP(S)_PROXY`/`ALL_PROXY` stripped from the launch environment). A local v2ray can never silently capture the browser.
|
||||
- A candidate whose echo comes back **with your own egress IP** is rejected as *transparent* — a route that doesn't change your exit is a no-op, not a proxy. `setup.mjs` also disables Edge's startup boost (registry policy) so background Edge processes can't re-grab the debug port with stale, proxy-less flags.
|
||||
|
||||
A proxy counts as *working* only when an echo comes back through it — no blind trust in proxy-list sites.
|
||||
|
||||
## Running under MIDAS
|
||||
|
||||
MIDAS loads plugins but has no Cordis runtime, so `midas/` supplies one:
|
||||
|
||||
- **`midas/kitd.mjs`** — the host. Builds the smallest `ctx` the plugin asks for
|
||||
(`tools.register`, `web.registerSearchProvider`, `web.registerFetchProvider`,
|
||||
`effect`), applies the kit with a plain-object config, and republishes the kit's
|
||||
tool and both web providers over a loopback HTTP seam on `127.0.0.1:7789`
|
||||
(`/health`, `/tool-def`, `/tool`, `/search`, `/fetch`, `/settings`, `/shutdown`).
|
||||
It runs as its own process on purpose: the launched browser is the expensive
|
||||
part, and a warm profile survives MIDAS restarting.
|
||||
- **`midas/kit.settings.json`** — host defaults (`browser`, `headless`, `port`,
|
||||
`searchEngine`, caps, `chromiumArgs`). Edit and restart the host to apply.
|
||||
- **`midas/plugin.js`** — the MIDAS plugin. Starts the host on demand, then registers
|
||||
`edge_kit` (the full action surface), `edge_kit_search` and `edge_kit_fetch`.
|
||||
MIDAS reserves the built-in `websearch` / `webfetch` ids, so a plugin tool cannot
|
||||
take those names; instead the plugin hooks `tool.execute.after` and upgrades a
|
||||
thin built-in `webfetch` result to the rendered page. Register it in
|
||||
`~/.config/midas/midas.json`:
|
||||
|
||||
```json
|
||||
{ "plugin": ["file:///root/gitea/8ball/edge-kit/midas/plugin.js"] }
|
||||
```
|
||||
|
||||
Env overrides: `EDGE_KIT_REPO` (checkout), `EDGE_KIT_HTTP` (seam URL),
|
||||
`EDGE_KIT_NODE` (node binary — `process.execPath` inside MIDAS is the MIDAS
|
||||
binary, so the plugin looks for a real node itself).
|
||||
|
||||
Headless Linux launches needed two additions to `index.js`, both harmless
|
||||
elsewhere: `--no-sandbox` is appended automatically when running as uid 0, and the
|
||||
new `chromiumArgs` setting appends whatever extra flags a container wants.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
- **`Maximum number of active sessions` (Firefox)** — a zombie Firefox is holding the BiDi session slot; terminate `firefox.exe` and relaunch.
|
||||
|
||||
@@ -97,9 +97,6 @@ export const Config = z && z.object({
|
||||
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(),
|
||||
chromiumArgs: z.string()
|
||||
.description('Extra flags appended to every Edge/Chrome launch, space-separated (e.g. "--disable-dev-shm-usage" in containers). Root sessions get --no-sandbox automatically.')
|
||||
.volatile(),
|
||||
});
|
||||
|
||||
let cfg = null; // bound by apply(); volatile fields are read through .get()
|
||||
@@ -550,10 +547,6 @@ async function doLaunch(args) {
|
||||
'--disable-renderer-backgrounding',
|
||||
...(headless ? ['--headless=new'] : []),
|
||||
...(proxyFlag ? [`--proxy-server=${proxyFlag}`] : ['--no-proxy-server']),
|
||||
// Containers and the default WSL user run as root, where the setuid
|
||||
// sandbox is unavailable and Chrome refuses to start without this.
|
||||
...(typeof process.getuid === 'function' && process.getuid() === 0 ? ['--no-sandbox'] : []),
|
||||
...String(cfgVal('chromiumArgs', '') || '').split(/\s+/).filter(Boolean),
|
||||
'about:blank',
|
||||
], { stdio: 'ignore', detached: false, env: childEnv });
|
||||
if (!(await waitForPort(port, 25000))) {
|
||||
@@ -676,23 +669,12 @@ 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, 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 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) => {
|
||||
@@ -781,7 +763,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, kind } = session;
|
||||
const { child, ws, engine } = 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
|
||||
@@ -791,24 +773,8 @@ async function restartSession() {
|
||||
session = null;
|
||||
try { ws.close(); } catch { /* gone */ }
|
||||
if (child && child.exitCode === null) child.kill();
|
||||
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;
|
||||
}
|
||||
const t0 = Date.now();
|
||||
while (await probePort(la.port || effectivePort(la)) && Date.now() - t0 < 10000) await new Promise((r) => setTimeout(r, 500));
|
||||
await doLaunch(la);
|
||||
return true;
|
||||
}
|
||||
@@ -1374,43 +1340,27 @@ const FETCH_EXTRACT = `(async () => {
|
||||
return { url: location.href, status, text: document.body ? document.body.innerText : '' };
|
||||
})()`;
|
||||
|
||||
const SEARCH_EXTRACT = `(async () => {
|
||||
const parse = () => {
|
||||
const SEARCH_EXTRACT = `(() => {
|
||||
const clean = (s) => String(s || '').replace(/\\s+/g, ' ').trim();
|
||||
const out = []; const seen = new Set();
|
||||
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);
|
||||
// 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);
|
||||
if (!title || title.length < 4) continue;
|
||||
const box = link.closest('li,div.g,div.tF2Cxc,li.b_algo,div.b_title,div[class*=result]') || link.parentElement;
|
||||
const box = a.closest('li,div.g,div.tF2Cxc,li.b_algo') || a.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) } };
|
||||
};
|
||||
// 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));
|
||||
}
|
||||
return { sources: out, page: { url: location.href, title: clean(document.title) } };
|
||||
})()`;
|
||||
|
||||
const edgeFetchProvider = {
|
||||
@@ -1432,7 +1382,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', 'bing') === 'google' ? 'google' : 'bing';
|
||||
const engine = cfgVal('searchEngine', 'google') === 'bing' ? 'bing' : 'google';
|
||||
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)}`;
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
{
|
||||
"browser": "chrome",
|
||||
"headless": true,
|
||||
"realProfile": false,
|
||||
"port": 9445,
|
||||
"searchEngine": "bing",
|
||||
"seeTextCap": 4000,
|
||||
"fetchTextCap": 20000,
|
||||
"proxyChain": "",
|
||||
"profileDir": "",
|
||||
"chromiumArgs": "--disable-dev-shm-usage"
|
||||
}
|
||||
-184
@@ -1,184 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/* Edge Kit host for MIDAS.
|
||||
*
|
||||
* index.js is a Cordis plugin: it expects a harness that hands it a `ctx` with
|
||||
* `tools.register`, `web.registerSearchProvider` and `web.registerFetchProvider`.
|
||||
* MIDAS has no Cordis runtime, so this file is the missing host: it builds the
|
||||
* smallest ctx that satisfies the plugin, applies it, and republishes what the
|
||||
* kit registered over a local HTTP seam that the MIDAS plugin (midas/plugin.js)
|
||||
* drives. Keeping the kit in its own process also keeps the browser warm across
|
||||
* MIDAS restarts, which is the whole point of a signed-in browsing profile.
|
||||
*
|
||||
* node midas/kitd.mjs [--port 7789] [--headless true|false] [--engine bing|google]
|
||||
*
|
||||
* Endpoints (127.0.0.1 only):
|
||||
* GET /health { ok, version, browser }
|
||||
* GET /tool-def the tool definition the kit registered
|
||||
* POST /tool { action, ...kit args } -> kit result
|
||||
* POST /search { query, maxResults } -> kit web_search provider
|
||||
* POST /fetch { url } -> kit web_fetch provider
|
||||
* POST /shutdown
|
||||
*/
|
||||
import http from 'node:http';
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
||||
const ROOT = path.resolve(HERE, '..');
|
||||
const arg = (name, dflt) => {
|
||||
const i = process.argv.indexOf('--' + name);
|
||||
return i >= 0 && process.argv[i + 1] !== undefined ? process.argv[i + 1] : dflt;
|
||||
};
|
||||
|
||||
const PORT = Number(arg('port', process.env.EDGE_KIT_HTTP_PORT || 7789));
|
||||
const HOST = '127.0.0.1';
|
||||
|
||||
/* Persistent host-side defaults; the kit re-reads volatile fields per call, so
|
||||
* editing this file and POSTing /reload is enough for most tuning. */
|
||||
const SETTINGS_FILE = process.env.EDGE_KIT_SETTINGS || path.join(HERE, 'kit.settings.json');
|
||||
const DEFAULT_SETTINGS = {
|
||||
browser: 'chrome',
|
||||
headless: true,
|
||||
realProfile: false,
|
||||
port: 9445,
|
||||
searchEngine: arg('engine', 'bing'),
|
||||
seeTextCap: 4000,
|
||||
fetchTextCap: 20000,
|
||||
proxyChain: '',
|
||||
profileDir: '',
|
||||
chromiumArgs: '',
|
||||
};
|
||||
let settings = { ...DEFAULT_SETTINGS };
|
||||
try { settings = { ...settings, ...JSON.parse(await fs.readFile(SETTINGS_FILE, 'utf8')) }; } catch { /* defaults */ }
|
||||
|
||||
const kit = await import(path.join(ROOT, 'index.js'));
|
||||
|
||||
/* ── the ctx the kit asks for ───────────────────────────────────────────── */
|
||||
const cleanups = [];
|
||||
const state = { tool: null, search: null, fetch: null };
|
||||
|
||||
const web = {
|
||||
registerSearchProvider(p) {
|
||||
state.search = p;
|
||||
const off = () => { if (state.search === p) state.search = null; };
|
||||
cleanups.push(off);
|
||||
return off;
|
||||
},
|
||||
registerFetchProvider(p) {
|
||||
state.fetch = p;
|
||||
const off = () => { if (state.fetch === p) state.fetch = null; };
|
||||
cleanups.push(off);
|
||||
return off;
|
||||
},
|
||||
// The kit's own activation probe calls these; route them back through the
|
||||
// providers it just registered so the probe measures this host end to end.
|
||||
search: async (req, signal) => {
|
||||
if (!state.search) throw new Error('no search provider registered');
|
||||
return state.search.search(req, signal);
|
||||
},
|
||||
fetch: async (req, signal) => {
|
||||
if (!state.fetch) throw new Error('no fetch provider registered');
|
||||
return state.fetch.fetch(req, signal);
|
||||
},
|
||||
};
|
||||
|
||||
const ctx = {
|
||||
// Cordis runs an effect body immediately and calls its return value on
|
||||
// teardown; the kit registers its providers inside that effect.
|
||||
effect(fn) {
|
||||
const off = fn();
|
||||
if (typeof off === 'function') cleanups.push(off);
|
||||
return off;
|
||||
},
|
||||
web,
|
||||
tools: {
|
||||
register(def) {
|
||||
state.tool = def;
|
||||
return () => { if (state.tool === def) state.tool = null; };
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
/* cfg is handed to the kit as a plain object: cfgVal() accepts raw values and
|
||||
* volatile `{get}` boxes alike, so getters give us live re-reading for free. */
|
||||
const liveConfig = Object.fromEntries(Object.keys(DEFAULT_SETTINGS).map((k) => [k, { get: () => settings[k] }]));
|
||||
kit.apply(ctx, liveConfig);
|
||||
|
||||
const writeToolDef = async () => {
|
||||
if (!state.tool) return;
|
||||
const def = { name: state.tool.name, description: state.tool.description, parameters: state.tool.parameters };
|
||||
await fs.writeFile(path.join(HERE, 'tool-def.json'), JSON.stringify(def, null, 2));
|
||||
};
|
||||
await writeToolDef();
|
||||
|
||||
/* ── HTTP seam ──────────────────────────────────────────────────────────── */
|
||||
const readBody = (req) => new Promise((resolve, reject) => {
|
||||
let raw = '';
|
||||
req.on('data', (c) => { raw += c; if (raw.length > 4e6) req.destroy(); });
|
||||
req.on('end', () => { try { resolve(raw ? JSON.parse(raw) : {}); } catch (e) { reject(e); } });
|
||||
req.on('error', reject);
|
||||
});
|
||||
|
||||
const json = (res, code, obj) => {
|
||||
const body = JSON.stringify(obj);
|
||||
res.writeHead(code, { 'content-type': 'application/json', 'content-length': Buffer.byteLength(body) });
|
||||
res.end(body);
|
||||
};
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const url = new URL(req.url, 'http://' + HOST);
|
||||
try {
|
||||
if (url.pathname === '/health') {
|
||||
let browser = null;
|
||||
try { browser = await state.tool.execute({ action: 'status' }); } catch { /* no session yet */ }
|
||||
return json(res, 200, {
|
||||
ok: true, name: 'edge-kit', version: (await import(path.join(ROOT, 'package.json'), { with: { type: 'json' } })).default.version,
|
||||
providers: { search: !!state.search, fetch: !!state.fetch }, settings, browser,
|
||||
});
|
||||
}
|
||||
if (url.pathname === '/tool-def') return json(res, 200, state.tool ?? {});
|
||||
|
||||
if (req.method === 'POST' && url.pathname === '/tool') {
|
||||
const args = await readBody(req);
|
||||
if (!state.tool) throw new Error('kit did not register a tool');
|
||||
return json(res, 200, { ok: true, result: await state.tool.execute(args) });
|
||||
}
|
||||
if (req.method === 'POST' && url.pathname === '/search') {
|
||||
const b = await readBody(req);
|
||||
const r = await web.search({ query: String(b.query ?? ''), maxResults: b.maxResults ?? 8 });
|
||||
return json(res, 200, { ok: true, result: r });
|
||||
}
|
||||
if (req.method === 'POST' && url.pathname === '/fetch') {
|
||||
const b = await readBody(req);
|
||||
const r = await web.fetch({ url: String(b.url ?? '') });
|
||||
return json(res, 200, { ok: true, result: r });
|
||||
}
|
||||
if (req.method === 'POST' && url.pathname === '/settings') {
|
||||
const b = await readBody(req);
|
||||
settings = { ...settings, ...b };
|
||||
return json(res, 200, { ok: true, settings });
|
||||
}
|
||||
if (req.method === 'POST' && url.pathname === '/shutdown') {
|
||||
json(res, 200, { ok: true });
|
||||
setTimeout(() => shutdown('shutdown requested'), 50);
|
||||
return;
|
||||
}
|
||||
return json(res, 404, { ok: false, error: 'no such endpoint' });
|
||||
} catch (e) {
|
||||
return json(res, 500, { ok: false, error: String((e && e.message) || e) });
|
||||
}
|
||||
});
|
||||
|
||||
async function shutdown(reason) {
|
||||
server.close();
|
||||
// The kit's effect cleanup closes (or detaches from) the browser it owns.
|
||||
while (cleanups.length) { try { await cleanups.pop()(); } catch { /* gone */ } }
|
||||
process.exit(0);
|
||||
}
|
||||
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
||||
process.on('SIGINT', () => shutdown('SIGINT'));
|
||||
|
||||
server.listen(PORT, HOST, () => {
|
||||
process.stdout.write(`edge-kit host listening on http://${HOST}:${PORT} (browser=${settings.browser} headless=${settings.headless} port=${settings.port})\n`);
|
||||
});
|
||||
-238
@@ -1,238 +0,0 @@
|
||||
/* Edge Kit Browser plugin for MIDAS.
|
||||
*
|
||||
* MIDAS has no Cordis runtime, so the kit (index.js) is hosted by
|
||||
* midas/kitd.mjs, which speaks the ctx the plugin expects and republishes the
|
||||
* kit's tool and web providers over a loopback HTTP seam. This file is that
|
||||
* seam's client, and it makes the kit the browser MIDAS actually browses with:
|
||||
*
|
||||
* edge_kit the kit's full action surface (see/click/type/video/proxy…)
|
||||
* websearch kit-backed replacement for the built-in provider
|
||||
* webfetch upgrades the built-in plain HTTP fetch to page text that
|
||||
* the browser already rendered, so JS sites and bot walls
|
||||
* stop returning empty or challenge pages
|
||||
*
|
||||
* kitd is started on demand and left running: the browser is the expensive
|
||||
* part, and a warm signed-in profile is the point of the kit.
|
||||
*/
|
||||
import { spawn } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
||||
// The plugin is reached from either the repo (midas/plugin.js) or a link in
|
||||
// ~/.midas/plugin, so locate the checkout by trying the plausible roots.
|
||||
const REPO = process.env.EDGE_KIT_REPO
|
||||
|| ([
|
||||
path.resolve(HERE, '..', '..', 'gitea', '8ball', 'edge-kit'),
|
||||
path.resolve(HERE, '..', 'gitea', '8ball', 'edge-kit'),
|
||||
path.join(os.homedir(), 'gitea', '8ball', 'edge-kit'),
|
||||
].find((c) => fs.existsSync(path.join(c, 'midas', 'kitd.mjs'))) || null);
|
||||
const KITD = process.env.EDGE_KIT_D || (REPO ? path.join(REPO, 'midas', 'kitd.mjs') : null);
|
||||
const HTTP = process.env.EDGE_KIT_HTTP || 'http://127.0.0.1:7789';
|
||||
const START_TIMEOUT_MS = Number(process.env.EDGE_KIT_START_MS || 30000);
|
||||
|
||||
const call = async (endpoint, body, timeoutMs = 120000) => {
|
||||
const res = await fetch(HTTP + endpoint, {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify(body ?? {}),
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || data.ok === false) throw new Error(`edge-kit ${endpoint}: ${data.error ?? res.status}`);
|
||||
return data.result;
|
||||
};
|
||||
|
||||
const alive = async () => {
|
||||
try {
|
||||
const r = await fetch(HTTP + '/health', { signal: AbortSignal.timeout(1500) });
|
||||
return r.ok;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const log = (msg) => {
|
||||
try { process.stderr.write('[edge-kit] ' + msg + '\n'); } catch { /* no terminal */ }
|
||||
};
|
||||
|
||||
/* process.execPath inside MIDAS is the midas binary, so the host needs a real
|
||||
* node. Prefer an explicit override, then a node on the usual paths, then the
|
||||
* current executable if it genuinely is node (host running under plain node). */
|
||||
const NODE = (() => {
|
||||
if (process.env.EDGE_KIT_NODE && fs.existsSync(process.env.EDGE_KIT_NODE)) return process.env.EDGE_KIT_NODE;
|
||||
for (const c of ['/usr/bin/node', '/usr/local/bin/node', path.join(path.dirname(process.execPath), 'node')]) {
|
||||
if (fs.existsSync(c)) return c;
|
||||
}
|
||||
return /node/.test(path.basename(process.execPath)) ? process.execPath : null;
|
||||
})();
|
||||
|
||||
const startKitd = async () => {
|
||||
if (await alive()) return true;
|
||||
if (!KITD) {
|
||||
log(`checkout not found — set EDGE_KIT_REPO to the edge-kit directory`);
|
||||
return false;
|
||||
}
|
||||
if (!NODE) {
|
||||
log('no node runtime found — set EDGE_KIT_NODE, or run node midas/kitd.mjs by hand');
|
||||
return false;
|
||||
}
|
||||
log(`starting host: ${NODE} ${KITD}`);
|
||||
const child = spawn(NODE, [KITD], { detached: true, stdio: 'ignore' });
|
||||
child.unref();
|
||||
const deadline = Date.now() + START_TIMEOUT_MS;
|
||||
while (Date.now() < deadline) {
|
||||
if (await alive()) return log('host is up'), true;
|
||||
await new Promise((r) => setTimeout(r, 400));
|
||||
}
|
||||
log('host did not come up in time — falling back to MIDAS built-in web tools');
|
||||
return false;
|
||||
};
|
||||
|
||||
/* ── tool definitions ───────────────────────────────────────────────────── */
|
||||
const ACTIONS = ['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'];
|
||||
|
||||
// args values are plain JSON Schema objects: MIDAS accepts those directly, so
|
||||
// the plugin needs no zod and no @midas/plugin import at runtime.
|
||||
const S = (type, description) => ({ type, description });
|
||||
|
||||
const edge_kit = {
|
||||
description:
|
||||
'Edge Kit Browser — human-style browsing in a real Chrome/Edge/Firefox driven by the edge-kit ' +
|
||||
'host. Flow: see (page text + numbered actionable elements) then act: click {index|text|selector}, ' +
|
||||
'clickAt {x,y}, drag {x,y,toX,toY}, type {into,text,submit}, press {key,times}, scroll; navigate ' +
|
||||
'with goto/back/forward/reload; verify with waitFor or screenshot; organize with tabs/newTab/' +
|
||||
'switchTab/closeTab; understand players with video {op:status|play|pause|seek|rate,shots}; ' +
|
||||
'route traffic with proxy {op:auto|set|status|clear,proxyChain}. Start with {action:"status"}.',
|
||||
args: {
|
||||
action: { type: 'string', enum: ACTIONS, description: 'What to do.' },
|
||||
url: S('string', 'goto/newTab/fetch: address to open.'),
|
||||
index: S('integer', 'click: element number from the last see. switchTab/closeTab: tab number.'),
|
||||
text: S('string', 'click: visible text. type/waitFor: text.'),
|
||||
into: S('string', 'type: human label of the field.'),
|
||||
selector: S('string', 'click/waitFor: CSS selector.'),
|
||||
script: S('string', 'eval: JS, await allowed.'),
|
||||
key: S('string', 'press: key name (default Enter).'),
|
||||
submit: S('boolean', 'type: press Enter afterwards.'),
|
||||
x: S('integer', 'clickAt/drag: x.'),
|
||||
y: S('integer', 'clickAt/drag: y.'),
|
||||
toX: S('integer', 'drag: destination x.'),
|
||||
toY: S('integer', 'drag: destination y.'),
|
||||
direction: { type: 'string', enum: ['up', 'down'], description: 'scroll direction.' },
|
||||
amount: S('integer', 'scroll pixels.'),
|
||||
times: S('integer', 'press: repeat count.'),
|
||||
ms: S('integer', 'wait: milliseconds.'),
|
||||
timeoutMs: S('integer', 'waitFor: milliseconds.'),
|
||||
maxText: S('integer', 'see: page-text cap.'),
|
||||
saveTo: S('string', 'screenshot: absolute PNG path.'),
|
||||
browser: { type: 'string', enum: ['edge', 'chrome', 'firefox'], description: 'launch: which browser.' },
|
||||
headless: S('boolean', 'launch: start hidden.'),
|
||||
relaunch: S('boolean', 'launch: restart a profile-owning browser so it is debuggable.'),
|
||||
realProfile: S('boolean', 'launch: use the everyday signed-in profile.'),
|
||||
profileDir: S('string', 'launch: user-data-dir override.'),
|
||||
executablePath: S('string', 'launch: browser binary override.'),
|
||||
port: S('integer', 'DevTools port.'),
|
||||
targetId: S('string', 'switchTab/closeTab: exact target id.'),
|
||||
video: S('integer', 'video: which video element (0 = first).'),
|
||||
op: { type: 'string', enum: ['status', 'play', 'pause', 'seek', 'rate', 'auto', 'set', 'clear'], description: 'video op, or proxy op.' },
|
||||
to: S('number', 'video seek seconds / rate multiplier.'),
|
||||
shots: S('integer', 'video: capture N PNG frames.'),
|
||||
every: S('integer', 'video: milliseconds between frames.'),
|
||||
proxyChain: S('string', 'proxy: comma-separated ip:port list.'),
|
||||
chainTo: S('string', 'proxy auto: extra chained hop.'),
|
||||
},
|
||||
execute: async (args) => {
|
||||
const out = await call('/tool', args);
|
||||
return { output: JSON.stringify(out, null, 2), metadata: { action: args.action } };
|
||||
},
|
||||
};
|
||||
|
||||
const websearch = {
|
||||
description:
|
||||
'Search the web through edge-kit: a real browser runs the query on Bing/Google and the organic ' +
|
||||
'results are parsed out of the rendered page, so results survive JavaScript rendering and bot ' +
|
||||
'walls. Returns numbered sources with url, title and snippet. Use this instead of guessing from ' +
|
||||
'memory for anything current. Then use webfetch to actually read a source.',
|
||||
args: {
|
||||
query: S('string', 'What to search for.'),
|
||||
count: S('integer', 'Results to return, 1-20 (default 8).'),
|
||||
engine: { type: 'string', enum: ['bing', 'google', 'auto'], description: 'Engine override.' },
|
||||
},
|
||||
execute: async (args) => {
|
||||
const r = await call('/search', { query: args.query, maxResults: args.count ?? 8 });
|
||||
const sources = r.sources ?? [];
|
||||
const lines = sources.map((s, i) => `${i + 1}. ${s.title}\n ${s.url}${s.snippet ? '\n ' + s.snippet : ''}`);
|
||||
const text = lines.length ? lines.join('\n') : (r.content ?? 'no results');
|
||||
return {
|
||||
output: `${sources.length} results for "${args.query}" via edge-kit\n\n${text}`,
|
||||
metadata: { provider: 'edge-kit', count: sources.length },
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
const webfetch = {
|
||||
description:
|
||||
'Read a URL through edge-kit: the page is loaded in a real browser and returned as rendered text, ' +
|
||||
'so JavaScript pages, signed-in content and bot-walled sites come back with content where a plain ' +
|
||||
'HTTP fetch returns an empty shell. Slower than a raw GET — use it when a page needs rendering.',
|
||||
args: { url: S('string', 'The URL to read.') },
|
||||
execute: async (args) => {
|
||||
const r = await call('/fetch', { url: args.url });
|
||||
const body = r.body ?? {};
|
||||
return {
|
||||
output: String(body.content ?? ''),
|
||||
metadata: { url: r.url, statusCode: r.statusCode, truncated: !!r.truncated, via: 'edge-kit' },
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
/* ── the plugin ─────────────────────────────────────────────────────────── */
|
||||
export default async () => {
|
||||
const ready = await startKitd();
|
||||
let definition = null;
|
||||
if (ready) {
|
||||
try {
|
||||
const r = await fetch(HTTP + '/tool-def', { signal: AbortSignal.timeout(3000) });
|
||||
definition = await r.json();
|
||||
} catch { /* keep the local copy */ }
|
||||
log(`attached to host; kit ${definition?.name ?? 'edge_kit_browser'} available`);
|
||||
}
|
||||
|
||||
return {
|
||||
// MIDAS reserves the built-in ids, so a plugin tool named websearch/webfetch
|
||||
// is dropped rather than overriding. Register under the kit's own names (the
|
||||
// aliases are kept in case a host has no built-in of that id), and upgrade
|
||||
// the built-in webfetch from the hook below.
|
||||
tool: {
|
||||
edge_kit,
|
||||
edge_kit_search: websearch,
|
||||
edge_kit_fetch: webfetch,
|
||||
websearch,
|
||||
webfetch,
|
||||
},
|
||||
|
||||
// When the built-in plain-HTTP webfetch runs (a call into a tool of the same
|
||||
// name is ours, so this only fires for hosts that kept the built-in), swap
|
||||
// its body for the rendered one when it looks thin.
|
||||
'tool.execute.after': async (input, output) => {
|
||||
if (!ready || input.tool !== 'webfetch' || !output?.output) return;
|
||||
if (String(output.metadata?.via ?? '') === 'edge-kit') return;
|
||||
if (String(output.output).length > 400) return; // already got real content
|
||||
const url = input.args?.url;
|
||||
if (!url) return;
|
||||
try {
|
||||
const r = await call('/fetch', { url });
|
||||
const rendered = String(r.body?.content ?? '');
|
||||
if (rendered.length > String(output.output).length) {
|
||||
output.output = rendered;
|
||||
output.metadata = { ...output.metadata, via: 'edge-kit-rendered' };
|
||||
}
|
||||
} catch { /* keep the plain fetch result */ }
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@local/edge-kit-easy",
|
||||
"version": "3.6.8",
|
||||
"version": "3.6.1",
|
||||
"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",
|
||||
|
||||
@@ -10,7 +10,6 @@ import fs from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import net from 'node:net';
|
||||
import path from 'node:path';
|
||||
import { execSync } from 'node:child_process';
|
||||
|
||||
const P = process.platform;
|
||||
const HOME = os.homedir();
|
||||
@@ -77,21 +76,6 @@ for (const port of [9444, 9445, 9446]) {
|
||||
else warn(`debug port ${port} busy — a browser already holds it; the kit will attach to it (relaunch:true takes over)`);
|
||||
}
|
||||
|
||||
/* 4b. Edge startup-boost survivor background processes re-spawn with stale
|
||||
* command lines and silently swallow the kit's proxy flag on relaunch.
|
||||
* The enterprise policy kills them for good. */
|
||||
if (P === 'win32') {
|
||||
try {
|
||||
execSync('reg query "HKCU\\Software\\Policies\\Microsoft\\Edge" /v StartupBoostEnabled', { stdio: 'pipe' });
|
||||
ok('Edge startup-boost already governed by policy');
|
||||
} catch {
|
||||
try {
|
||||
execSync('reg add "HKCU\\Software\\Policies\\Microsoft\\Edge" /v StartupBoostEnabled /t REG_DWORD /d 0 /f', { stdio: 'pipe' });
|
||||
ok('disabled Edge startup boost (policy) — survivor processes can no longer drop proxy flags');
|
||||
} catch { warn('could not write Edge StartupBoostEnabled policy — restarts may race a background Edge'); }
|
||||
}
|
||||
}
|
||||
|
||||
/* 5. Direct egress — proves there is an internet path WITHOUT any system/v2ray
|
||||
* proxy (node fetch never reads HTTP_PROXY). Geo-restricted hosts (Iran)
|
||||
* often have no open HTTP echo without VPN but can reach proxies directly
|
||||
|
||||
Reference in New Issue
Block a user