Files
magpie/internal/plugin/testdata/cliproxy/index.js
T
yetone f0c63264e5 plugin: a Node program a plugin starts takes magpie's proxy, as Node's fetch skips HTTPS_PROXY without NODE_USE_ENV_PROXY=1
larchsis on X (Windows 11, a proxy set in magpie, none in the system):
the "our free model" plugin's sign-in said 原渠道包下载失败. The plugin
downloads its channel pack in a Node program it starts. The host hands
such a program magpie's proxy as HTTPS_PROXY/HTTP_PROXY (since the grok
login fix), but Node's built-in fetch ignores those unless
NODE_USE_ENV_PROXY=1, so the download went direct and failed behind the
wall.

host.js's proxied() now also sets NODE_USE_ENV_PROXY=1 whenever it
hands the program a proxy, unless the plugin's env already names it
(a plugin's own value, 0 included, is kept). It is not lowercased.

Not changed: the Go side's netproxy.Env, which starts npm, Claude Code,
grok and devin. Those read *_PROXY themselves, and the proxy there may be
socks5://, which Node's env proxy does not take.

Run:
- TestPluginNodeTakesTheProxy (new) fails on the old host.js and passes
  now, on macOS and on the Windows box.
- go test ./internal/plugin/... passes.
- GOOS=windows and GOOS=linux go vet pass; gofmt is clean.
- End to end through the real host, on the Mac (Node 22.22) and on the
  Windows box (Node 22.22.2), with a throwaway plugin (not committed)
  that starts `node` to fetch https://raw.githubusercontent.com/ while
  magpie's proxy points at a fake 502 proxy:
  - old host.js: direct200, so it went past the proxy;
  - fixed: fetch failed at the proxy.
2026-10-10 18:11:00 +08:00

46 lines
1.5 KiB
JavaScript

// A plugin that signs in with a CLI of the vendor's, as the Grok plugin
// runs `grok login`: the link it hands on says the proxy the CLI was
// given, started with the host's environment and with a copy of it, and
// whether Node's fetch was told to take it (NODE_USE_ENV_PROXY).
import { spawn } from "node:child_process"
function proxyOf(env, name = "HTTPS_PROXY") {
return new Promise((resolve) => {
const child = spawn(process.execPath, ["-e", `process.stdout.write(process.env.${name} || 'none')`], {
env,
stdio: ["ignore", "pipe", "pipe"],
})
let out = ""
child.stdout.on("data", (b) => (out += b))
child.on("close", () => resolve(out))
})
}
export const CLIProxyPlugin = async () => ({
auth: {
provider: "cliproxy",
methods: [
{
type: "oauth",
label: "CLI",
authorize: async () => {
const inherited = await proxyOf(undefined)
const copied = await proxyOf({ ...process.env })
const node = {
inheritedNode: await proxyOf(undefined, "NODE_USE_ENV_PROXY"),
copiedNode: await proxyOf({ ...process.env }, "NODE_USE_ENV_PROXY"),
ownNode: await proxyOf({ ...process.env, NODE_USE_ENV_PROXY: "0" }, "NODE_USE_ENV_PROXY"),
}
const q = new URLSearchParams({ inherited, copied, ...node })
return {
url: `https://cliproxy.invalid/?${q}`,
instructions: "",
method: "code",
callback: async () => ({ type: "failed" }),
}
},
},
],
},
})