mirror of
https://github.com/garrytan/gstack.git
synced 2026-05-12 07:27:26 +08:00
feat(browse): --proxy and --headed flags wire bridge into daemon
Adds the global --proxy <url> and --headed flags to the browse CLI.
Resolves cred policy and routes the daemon launch through the SOCKS5
bridge (or pass-through for HTTP/HTTPS) before chromium.launch().
CLI (cli.ts):
- extractGlobalFlags() strips --proxy/--headed from argv, parses URL via
Node URL class, validates D9 cred-mixing (env BROWSE_PROXY_USER/PASS
+ URL creds → exit 1 with hint), composes canonical proxy URL with
resolved creds, computes a stable configHash for daemon-mismatch
- ensureServer() now reads existing daemon's configHash from state file
and refuses (exit 1 with disconnect hint) if --proxy/--headed mismatch
the existing daemon. No silent restart that would drop tab state.
- All proxy-related stderr lines go through redactProxyUrl
proxy-config.ts (new):
- parseProxyConfig() — URL parser + D9 cred-mixing detector + scheme allowlist
- computeConfigHash() — stable hash of (proxy URL minus creds + headed flag)
- toUpstreamConfig() — map ParsedProxyConfig → socks-bridge.UpstreamConfig
Server (server.ts):
- Reads BROWSE_PROXY_URL at startup; for SOCKS5+auth, runs testUpstream
pre-flight (5s budget, 3 retries, 500ms backoff) and exits 1 on failure
with redacted error
- Spawns startSocksBridge() on 127.0.0.1:<ephemeral> and points
Chromium at it via socks5://127.0.0.1:<port>
- HTTP/HTTPS or unauth SOCKS5 → pass-through to chromium.launch
proxy.server (with username/password if present)
- State file gains optional configHash for daemon-mismatch check
- Bridge tears down via process.on('exit')
Browser manager (browser-manager.ts):
- New setProxyConfig({ server, username, password }) called by server.ts
before launch
- chromium.launch() and both launchPersistentContext sites pass the
proxy config through when set
Tests: 22 new across proxy-config (parse + cred-mixing + hash stability)
and extractGlobalFlags (flag stripping + cred-mixing rejection + cred
rotation hash stability + redaction).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -41,6 +41,9 @@ import { inspectElement, modifyStyle, resetModifications, getModificationHistory
|
||||
// Bun.spawn used instead of child_process.spawn (compiled bun binaries
|
||||
// fail posix_spawn on all executables including /bin/bash)
|
||||
import { safeUnlink, safeUnlinkQuiet, safeKill } from './error-handling';
|
||||
import { startSocksBridge, testUpstream, type BridgeHandle } from './socks-bridge';
|
||||
import { parseProxyConfig, toUpstreamConfig, ProxyConfigError } from './proxy-config';
|
||||
import { redactProxyUrl } from './proxy-redact';
|
||||
import { logTunnelDenial } from './tunnel-denial-log';
|
||||
import {
|
||||
mintSseSessionToken, validateSseSessionToken, extractSseCookie,
|
||||
@@ -1020,6 +1023,70 @@ async function start() {
|
||||
const port = await findPort();
|
||||
LOCAL_LISTEN_PORT = port;
|
||||
|
||||
// ─── Proxy config (D8 + codex F5) ──────────────────────────────
|
||||
// BROWSE_PROXY_URL is set by the CLI when --proxy was passed. For SOCKS5
|
||||
// with auth, we run a local 127.0.0.1 bridge that relays to the
|
||||
// authenticated upstream (Chromium can't do SOCKS5 auth itself). For
|
||||
// HTTP/HTTPS or unauthenticated SOCKS5, we pass the URL directly to
|
||||
// Chromium's proxy.server option.
|
||||
let proxyBridge: BridgeHandle | null = null;
|
||||
const proxyUrl = process.env.BROWSE_PROXY_URL;
|
||||
if (proxyUrl) {
|
||||
let parsed;
|
||||
try {
|
||||
parsed = parseProxyConfig({
|
||||
proxyUrl,
|
||||
envUser: process.env.BROWSE_PROXY_USER,
|
||||
envPass: process.env.BROWSE_PROXY_PASS,
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof ProxyConfigError) {
|
||||
console.error(`[browse] error: ${err.message} (${err.hint})`);
|
||||
process.exit(1);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (parsed.scheme === 'socks5' && parsed.hasAuth) {
|
||||
// Pre-flight: verify upstream accepts our creds before launching
|
||||
// Chromium. 5s budget, 3 retries with 500ms backoff (D4: handles VPN
|
||||
// warm-up race). On failure, exit with redacted error.
|
||||
console.log(`[browse] Testing SOCKS5 upstream ${redactProxyUrl(proxyUrl)}...`);
|
||||
try {
|
||||
const test = await testUpstream({
|
||||
upstream: toUpstreamConfig(parsed),
|
||||
budgetMs: 5000,
|
||||
retries: 3,
|
||||
backoffMs: 500,
|
||||
});
|
||||
console.log(`[browse] [proxy] upstream test ok in ${test.ms}ms (${test.attempts} attempt${test.attempts === 1 ? '' : 's'})`);
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
console.error(`[browse] [proxy] FAIL upstream ${redactProxyUrl(proxyUrl)}: ${msg}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
proxyBridge = await startSocksBridge({ upstream: toUpstreamConfig(parsed) });
|
||||
console.log(`[browse] [proxy] bridge listening on 127.0.0.1:${proxyBridge.port}`);
|
||||
browserManager.setProxyConfig({ server: `socks5://127.0.0.1:${proxyBridge.port}` });
|
||||
} else {
|
||||
// HTTP/HTTPS or unauth SOCKS5 — pass through to Chromium directly.
|
||||
browserManager.setProxyConfig({
|
||||
server: `${parsed.scheme}://${parsed.host}:${parsed.port}`,
|
||||
...(parsed.userId ? { username: parsed.userId } : {}),
|
||||
...(parsed.password ? { password: parsed.password } : {}),
|
||||
});
|
||||
console.log(`[browse] [proxy] using ${redactProxyUrl(proxyUrl)} (pass-through to Chromium)`);
|
||||
}
|
||||
|
||||
// Tear down bridge on shutdown.
|
||||
process.on('exit', () => {
|
||||
if (proxyBridge) {
|
||||
proxyBridge.close().catch(() => { /* shutting down anyway */ });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Launch browser (headless or headed with extension)
|
||||
// BROWSE_HEADLESS_SKIP=1 skips browser launch entirely (for HTTP-only testing)
|
||||
const skipBrowser = process.env.BROWSE_HEADLESS_SKIP === '1';
|
||||
@@ -1998,6 +2065,9 @@ async function start() {
|
||||
serverPath: path.resolve(import.meta.dir, 'server.ts'),
|
||||
binaryVersion: readVersionHash() || undefined,
|
||||
mode: browserManager.getConnectionMode(),
|
||||
// D2 daemon-mismatch detection: CLI computes the same hash from its
|
||||
// resolved flags and refuses if it differs from this stored value.
|
||||
...(process.env.BROWSE_CONFIG_HASH ? { configHash: process.env.BROWSE_CONFIG_HASH } : {}),
|
||||
};
|
||||
const tmpFile = config.stateFile + '.tmp';
|
||||
fs.writeFileSync(tmpFile, JSON.stringify(state, null, 2), { mode: 0o600 });
|
||||
|
||||
Reference in New Issue
Block a user