solrouter 0.4.5 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/proxy.js CHANGED
@@ -119,6 +119,26 @@ async function main() {
119
119
  return;
120
120
  }
121
121
 
122
+ case "install": {
123
+ const { install } = await import("../src/install.js");
124
+ const r = await install();
125
+ if (!r.ok) { console.error("Install failed:", r.error); process.exit(1); }
126
+ console.log(`\n✓ SolRouter installed as a background service on http://127.0.0.1:${r.port}`);
127
+ console.log(` EVERY Claude Code session now routes through the proxy automatically —`);
128
+ console.log(` no special window, no special command. Just open Claude Code.`);
129
+ console.log(` (auto-starts at login, restarts on crash)\n`);
130
+ console.log(` Heads up: all Claude Code now bills to your API key, not your Max subscription`);
131
+ console.log(` — that's required to use a custom endpoint. Undo anytime: solrouter uninstall\n`);
132
+ return;
133
+ }
134
+
135
+ case "uninstall": {
136
+ const { uninstall } = await import("../src/install.js");
137
+ uninstall();
138
+ console.log("✓ SolRouter uninstalled. Claude Code goes straight to Anthropic again.");
139
+ return;
140
+ }
141
+
122
142
  // `solrouter code [claude args...]` — the one-command path: start the proxy
123
143
  // in THIS process, then launch Claude Code already pointed at it, in the same
124
144
  // tab. When Claude Code exits, so does the proxy. (`claude` is the alias.)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solrouter",
3
- "version": "0.4.5",
3
+ "version": "0.5.0",
4
4
  "description": "Local privacy router for coding tools: pseudonymises every request before it leaves your machine, and sends work a frontier model would refuse to an uncensored open-weight model you control.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/install.js ADDED
@@ -0,0 +1,113 @@
1
+ // install.js — make the proxy the default for EVERY Claude Code session, so there's
2
+ // no "which window routes through it" footgun. Two pieces:
3
+ // 1. a macOS LaunchAgent that keeps the proxy running on a fixed port (auto-start
4
+ // at login, auto-restart on crash);
5
+ // 2. ~/.claude/settings.json `env` pointing all Claude Code at that port (+ an API
6
+ // key, which Claude Code requires to use a custom base URL — see the README).
7
+ //
8
+ // `uninstall` reverses both, touching only the keys we added.
9
+
10
+ import { homedir } from "node:os";
11
+ import { join } from "node:path";
12
+ import { execSync } from "node:child_process";
13
+ import { existsSync, mkdirSync, writeFileSync, readFileSync, rmSync, realpathSync } from "node:fs";
14
+ import { loadConfig, HOME_DIR } from "./config.js";
15
+
16
+ const LABEL = "com.solrouter.proxy";
17
+ const PLIST = join(homedir(), "Library", "LaunchAgents", `${LABEL}.plist`);
18
+ const CLAUDE_SETTINGS = join(homedir(), ".claude", "settings.json");
19
+ const MARK = "__solrouter"; // marks which env keys we own, for a clean uninstall
20
+
21
+ async function freePort(start) {
22
+ const net = await import("node:net");
23
+ for (let p = start; p < start + 25; p++) {
24
+ const ok = await new Promise(res => {
25
+ const s = net.createServer();
26
+ s.once("error", () => res(false));
27
+ s.once("listening", () => s.close(() => res(true)));
28
+ s.listen(p, "127.0.0.1");
29
+ });
30
+ if (ok) return p;
31
+ }
32
+ return start;
33
+ }
34
+
35
+ function plistXml({ node, binPath, port }) {
36
+ const log = join(HOME_DIR, "daemon.log");
37
+ const args = [node, binPath, "start", "--port", String(port)]
38
+ .map(a => ` <string>${a}</string>`).join("\n");
39
+ return `<?xml version="1.0" encoding="UTF-8"?>
40
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
41
+ <plist version="1.0">
42
+ <dict>
43
+ <key>Label</key><string>${LABEL}</string>
44
+ <key>ProgramArguments</key>
45
+ <array>
46
+ ${args}
47
+ </array>
48
+ <key>RunAtLoad</key><true/>
49
+ <key>KeepAlive</key><true/>
50
+ <key>StandardOutPath</key><string>${log}</string>
51
+ <key>StandardErrorPath</key><string>${log}</string>
52
+ </dict>
53
+ </plist>
54
+ `;
55
+ }
56
+
57
+ /** Merge our env into ~/.claude/settings.json, preserving everything else. */
58
+ function writeClaudeEnv(port, key) {
59
+ let s = {};
60
+ try { s = JSON.parse(readFileSync(CLAUDE_SETTINGS, "utf8")); } catch {}
61
+ if (!existsSync(join(homedir(), ".claude"))) mkdirSync(join(homedir(), ".claude"), { recursive: true });
62
+ s.env = s.env || {};
63
+ s.env.ANTHROPIC_BASE_URL = `http://127.0.0.1:${port}`;
64
+ s.env.ANTHROPIC_API_KEY = key;
65
+ s.env[MARK] = "1"; // our marker
66
+ writeFileSync(CLAUDE_SETTINGS, JSON.stringify(s, null, 2));
67
+ }
68
+
69
+ function removeClaudeEnv() {
70
+ let s;
71
+ try { s = JSON.parse(readFileSync(CLAUDE_SETTINGS, "utf8")); } catch { return; }
72
+ if (s.env?.[MARK]) {
73
+ delete s.env.ANTHROPIC_BASE_URL;
74
+ delete s.env.ANTHROPIC_API_KEY;
75
+ delete s.env[MARK];
76
+ if (Object.keys(s.env).length === 0) delete s.env;
77
+ writeFileSync(CLAUDE_SETTINGS, JSON.stringify(s, null, 2));
78
+ }
79
+ }
80
+
81
+ /** Resolve a stable path to this proxy's bin (npx caches are ephemeral → warn). */
82
+ function resolveBin() {
83
+ const p = realpathSync(process.argv[1]);
84
+ return { binPath: p, ephemeral: /[/\\]_npx[/\\]/.test(p) };
85
+ }
86
+
87
+ export async function install() {
88
+ const cfg = loadConfig();
89
+ const key = cfg.frontier.apiKey || process.env.ANTHROPIC_API_KEY;
90
+ if (!key) return { ok: false, error: "No Anthropic API key configured. Run `solrouter setup` first." };
91
+
92
+ const { binPath, ephemeral } = resolveBin();
93
+ if (ephemeral) return { ok: false, error: "Run `npm i -g solrouter` first — an npx cache path can't back a persistent service." };
94
+
95
+ const port = await freePort(cfg.port || 8787);
96
+ if (!existsSync(HOME_DIR)) mkdirSync(HOME_DIR, { recursive: true });
97
+ if (!existsSync(join(homedir(), "Library", "LaunchAgents"))) mkdirSync(join(homedir(), "Library", "LaunchAgents"), { recursive: true });
98
+
99
+ writeFileSync(PLIST, plistXml({ node: process.execPath, binPath, port }));
100
+ try { execSync(`launchctl unload "${PLIST}" 2>/dev/null`); } catch {}
101
+ execSync(`launchctl load "${PLIST}"`);
102
+ writeClaudeEnv(port, key);
103
+ return { ok: true, port, plist: PLIST };
104
+ }
105
+
106
+ export function uninstall() {
107
+ try { execSync(`launchctl unload "${PLIST}" 2>/dev/null`); } catch {}
108
+ if (existsSync(PLIST)) rmSync(PLIST);
109
+ removeClaudeEnv();
110
+ return { ok: true };
111
+ }
112
+
113
+ export default { install, uninstall };