@certalis/agent-smith 0.1.5 → 0.1.7
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/README.md +51 -5
- package/dist/bin.js +180 -30
- package/dist/post-processor.worker.js +52 -5
- package/dist/server.js +108 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# agent-smith
|
|
2
2
|
|
|
3
|
-
**A proof-of-concept
|
|
3
|
+
**A proof-of-concept proxy and provider pool for Claude Code and Codex.**
|
|
4
4
|
|
|
5
5
|
> [!WARNING]
|
|
6
6
|
> **This is a proof of concept for ideation, not a product.** It exists to
|
|
@@ -15,10 +15,10 @@
|
|
|
15
15
|
> providers' terms.
|
|
16
16
|
> - Not affiliated with, endorsed by or sponsored by Anthropic or OpenAI.
|
|
17
17
|
|
|
18
|
-
Claude Code and Codex keep talking their own protocol. agent-smith sits
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
Claude Code and Codex keep talking their own protocol. agent-smith sits between
|
|
19
|
+
them and the provider: it chooses a provider from the pool you configure, then
|
|
20
|
+
an account inside that provider, watches the quota each provider reports, and
|
|
21
|
+
fails over when one cannot serve.
|
|
22
22
|
|
|
23
23
|
## How it works
|
|
24
24
|
|
|
@@ -85,6 +85,37 @@ startServer({
|
|
|
85
85
|
|
|
86
86
|
Codex works the same way with an OpenAI API key and an `openai-responses` pool.
|
|
87
87
|
|
|
88
|
+
## Connect an authorized caller to a private shared pool
|
|
89
|
+
|
|
90
|
+
The agents service and a laptop can send requests to one proxy with a
|
|
91
|
+
forwarding key. Put the server and callers on a private network first; the
|
|
92
|
+
recommended WireGuard shape is documented in the
|
|
93
|
+
[private-network guide](docs/private-network.md). It keeps the database and
|
|
94
|
+
provider tokens on the server, and exposes the proxy only on the private
|
|
95
|
+
network.
|
|
96
|
+
|
|
97
|
+
This is connectivity only, not team tenancy: the shared forwarding key does
|
|
98
|
+
not identify an individual caller or enforce per-client fairness. Do not
|
|
99
|
+
distribute it team-wide until the B2/W7 tenancy work exists.
|
|
100
|
+
|
|
101
|
+
Copy [`private-network-client.json`](examples/config/private-network-client.json)
|
|
102
|
+
into your private Agent-smith config. Put the forwarding key in the owner-only
|
|
103
|
+
file named by `proxy_key_file`, not in the JSON or a shell profile. `base_url`
|
|
104
|
+
is the private endpoint visible to that machine, not a second pool. The key can
|
|
105
|
+
spend from the pool, but cannot administer it.
|
|
106
|
+
|
|
107
|
+
Then install the normal `claude` and `codex` wrappers once:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
agent-smith install-client
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Open a new terminal and use `claude` or `codex` normally. The wrappers only add
|
|
114
|
+
their own marked `PATH` entry; they do not replace the real binaries or start a
|
|
115
|
+
second proxy. Edit the `client` section and rerun `agent-smith install-client`
|
|
116
|
+
when the private address changes; do not hand-edit the generated wrappers. The
|
|
117
|
+
forwarding key is for inference, not dashboard or management access.
|
|
118
|
+
|
|
88
119
|
## Configuration
|
|
89
120
|
|
|
90
121
|
The config file is JSON, read at startup from:
|
|
@@ -103,6 +134,7 @@ Restart agent-smith after changing `providers`, `provider_pools`, `alerts` or
|
|
|
103
134
|
| `providers.<name>.base_url` | The upstream URL, for an endpoint compatible with one of the strategies. |
|
|
104
135
|
| `providers.<name>.models` | The models a provider serves, as `"client model": "provider model"`. A trailing `*` matches a prefix. Without it, the provider serves every model name unchanged. |
|
|
105
136
|
| `provider_pools.<protocol>` | The ordered providers for `anthropic-messages` or `openai-responses`. Clients reach it at `/v1/pool/<protocol>/*`, and the local wrappers use it automatically. |
|
|
137
|
+
| `client` | The Agent-smith origin and forwarding key used by `agent-smith install-client`. It is for this machine only; it does not define providers or a separate pool. |
|
|
106
138
|
| `team_routing` | Which subscription accounts may share a pool, and their usage caps. See the [configuration guide](docs/configuration.md#team-routing). |
|
|
107
139
|
| `alerts` | A Slack webhook or bot token and/or an email address that hear when a seat spends extra usage and when every account is full. A filled value turns its channel on. See [Alerts](docs/configuration.md#alerts). |
|
|
108
140
|
| `block_when_all_full` | `true` by default: when every account is full, every request is refused until the soonest reset, so none spends extra usage. |
|
|
@@ -157,6 +189,20 @@ to `acme` and are sent as `acme-large`. `acme` serves no other model.
|
|
|
157
189
|
}
|
|
158
190
|
```
|
|
159
191
|
|
|
192
|
+
**A laptop using a private shared pool**
|
|
193
|
+
([`private-network-client.json`](examples/config/private-network-client.json)).
|
|
194
|
+
The address is reachable only through the private network described in the
|
|
195
|
+
[deployment guide](docs/private-network.md).
|
|
196
|
+
|
|
197
|
+
```json
|
|
198
|
+
{
|
|
199
|
+
"client": {
|
|
200
|
+
"base_url": "http://10.76.0.1:8080",
|
|
201
|
+
"proxy_key_file": "~/.config/ccflare/team-pool.key"
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
160
206
|
The Claude Code and Codex subscription examples are in
|
|
161
207
|
[Subscription sign-ins and provider terms](#subscription-sign-ins-and-provider-terms).
|
|
162
208
|
|
package/dist/bin.js
CHANGED
|
@@ -1263,11 +1263,13 @@ var init_paths = __esm(() => {
|
|
|
1263
1263
|
// ../../packages/config/src/index.ts
|
|
1264
1264
|
import { EventEmitter as EventEmitter3 } from "events";
|
|
1265
1265
|
import {
|
|
1266
|
+
chmodSync,
|
|
1266
1267
|
existsSync as existsSync2,
|
|
1267
1268
|
mkdirSync as mkdirSync2,
|
|
1268
1269
|
readFileSync,
|
|
1269
1270
|
renameSync,
|
|
1270
1271
|
rmSync,
|
|
1272
|
+
statSync as statSync2,
|
|
1271
1273
|
writeFileSync
|
|
1272
1274
|
} from "fs";
|
|
1273
1275
|
import { dirname } from "path";
|
|
@@ -1320,6 +1322,12 @@ function sanitizeConfigData(value) {
|
|
|
1320
1322
|
sanitized.provider_pools = providerPools;
|
|
1321
1323
|
continue;
|
|
1322
1324
|
}
|
|
1325
|
+
if (key === "client") {
|
|
1326
|
+
const client = sanitizeClientRouting(entry);
|
|
1327
|
+
if (client)
|
|
1328
|
+
sanitized.client = client;
|
|
1329
|
+
continue;
|
|
1330
|
+
}
|
|
1323
1331
|
if (key === "alerts") {
|
|
1324
1332
|
const alerts = sanitizeAlerts(entry);
|
|
1325
1333
|
if (alerts)
|
|
@@ -1409,6 +1417,23 @@ function sanitizeProviderBaseUrl(value) {
|
|
|
1409
1417
|
return;
|
|
1410
1418
|
}
|
|
1411
1419
|
}
|
|
1420
|
+
function sanitizeClientRouting(value) {
|
|
1421
|
+
if (!isRecord(value))
|
|
1422
|
+
return;
|
|
1423
|
+
const client = {};
|
|
1424
|
+
if (typeof value.base_url === "string") {
|
|
1425
|
+
try {
|
|
1426
|
+
const url = new URL(value.base_url);
|
|
1427
|
+
if ((url.protocol === "http:" || url.protocol === "https:") && url.pathname === "/" && url.search === "" && url.hash === "" && url.username === "" && url.password === "") {
|
|
1428
|
+
client.base_url = url.origin;
|
|
1429
|
+
}
|
|
1430
|
+
} catch {}
|
|
1431
|
+
}
|
|
1432
|
+
if (typeof value.proxy_key_file === "string" && (value.proxy_key_file.startsWith("/") || value.proxy_key_file.startsWith("~/")) && value.proxy_key_file.length <= 1024) {
|
|
1433
|
+
client.proxy_key_file = value.proxy_key_file;
|
|
1434
|
+
}
|
|
1435
|
+
return Object.keys(client).length > 0 ? client : undefined;
|
|
1436
|
+
}
|
|
1412
1437
|
function sanitizeProviderModels(value) {
|
|
1413
1438
|
if (!isRecord(value))
|
|
1414
1439
|
return;
|
|
@@ -1544,17 +1569,30 @@ var init_src4 = __esm(() => {
|
|
|
1544
1569
|
try {
|
|
1545
1570
|
const content = readFileSync(this.configPath, "utf8");
|
|
1546
1571
|
this.data = sanitizeConfigData(JSON.parse(content));
|
|
1547
|
-
if ("default_agent_model" in this.data) {
|
|
1548
|
-
delete this.data.default_agent_model;
|
|
1549
|
-
this.saveConfig();
|
|
1550
|
-
}
|
|
1551
1572
|
} catch (error) {
|
|
1552
1573
|
log2.error(`Failed to parse config file: ${error}`);
|
|
1553
1574
|
this.data = {};
|
|
1575
|
+
return;
|
|
1576
|
+
}
|
|
1577
|
+
try {
|
|
1578
|
+
chmodSync(this.configPath, 384);
|
|
1579
|
+
} catch {
|
|
1580
|
+
let ownerOnly = false;
|
|
1581
|
+
try {
|
|
1582
|
+
ownerOnly = (statSync2(this.configPath).mode & 63) === 0;
|
|
1583
|
+
} catch {}
|
|
1584
|
+
if (!ownerOnly) {
|
|
1585
|
+
throw new Error("Config file must be owner-only");
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
if ("default_agent_model" in this.data) {
|
|
1589
|
+
delete this.data.default_agent_model;
|
|
1590
|
+
this.saveConfig();
|
|
1554
1591
|
}
|
|
1555
1592
|
} else {
|
|
1556
1593
|
const dir = dirname(this.configPath);
|
|
1557
|
-
mkdirSync2(dir, { recursive: true });
|
|
1594
|
+
mkdirSync2(dir, { recursive: true, mode: 448 });
|
|
1595
|
+
chmodSync(dir, 448);
|
|
1558
1596
|
this.data = {
|
|
1559
1597
|
lb_strategy: DEFAULT_STRATEGY
|
|
1560
1598
|
};
|
|
@@ -1685,6 +1723,15 @@ var init_src4 = __esm(() => {
|
|
|
1685
1723
|
resendApiKey: file?.resend_api_key ?? null
|
|
1686
1724
|
};
|
|
1687
1725
|
}
|
|
1726
|
+
getClientRouting() {
|
|
1727
|
+
const client = this.data.client;
|
|
1728
|
+
return {
|
|
1729
|
+
...client?.base_url && { baseUrl: client.base_url },
|
|
1730
|
+
...client?.proxy_key_file && {
|
|
1731
|
+
proxyKeyFile: client.proxy_key_file
|
|
1732
|
+
}
|
|
1733
|
+
};
|
|
1734
|
+
}
|
|
1688
1735
|
getBlockWhenAllFull() {
|
|
1689
1736
|
return this.data.block_when_all_full !== false;
|
|
1690
1737
|
}
|
|
@@ -11637,12 +11684,24 @@ function requestAddress(server, req) {
|
|
|
11637
11684
|
function configuredKey(value) {
|
|
11638
11685
|
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
11639
11686
|
}
|
|
11687
|
+
function configuredBindHosts(value) {
|
|
11688
|
+
if (value === undefined)
|
|
11689
|
+
return;
|
|
11690
|
+
const bindHosts = [
|
|
11691
|
+
...new Set(value.split(",").map((host) => host.trim()).filter(Boolean))
|
|
11692
|
+
];
|
|
11693
|
+
return bindHosts.length > 0 ? bindHosts : undefined;
|
|
11694
|
+
}
|
|
11640
11695
|
function createExposureConfig(config) {
|
|
11641
11696
|
const configuredHost = configuredKey(process.env.CF_HOST) ?? configuredKey(config.get("host"));
|
|
11697
|
+
const bindHosts = configuredBindHosts(process.env.CF_BIND_HOSTS) ?? [
|
|
11698
|
+
configuredHost ?? "127.0.0.1"
|
|
11699
|
+
];
|
|
11642
11700
|
const configuredTrustLoopback = process.env.CF_TRUST_LOOPBACK;
|
|
11643
11701
|
return {
|
|
11644
11702
|
adminKey: configuredKey(process.env.CF_ADMIN_KEY),
|
|
11645
|
-
bindHost:
|
|
11703
|
+
bindHost: bindHosts[0],
|
|
11704
|
+
bindHosts,
|
|
11646
11705
|
proxyKey: configuredKey(process.env.CF_PROXY_KEY),
|
|
11647
11706
|
trustLoopback: configuredTrustLoopback === undefined ? config.get("trust_loopback") !== false : configuredTrustLoopback !== "false"
|
|
11648
11707
|
};
|
|
@@ -11662,12 +11721,23 @@ function safeKeyEqual(candidate, expected) {
|
|
|
11662
11721
|
}
|
|
11663
11722
|
return timingSafeEqual(Buffer.from(candidate), Buffer.from(expected));
|
|
11664
11723
|
}
|
|
11665
|
-
function
|
|
11666
|
-
const
|
|
11667
|
-
|
|
11724
|
+
function bearerToken(req) {
|
|
11725
|
+
const authorization = req.headers.get("authorization");
|
|
11726
|
+
const match = authorization?.match(/^Bearer ([^\s]+)$/);
|
|
11727
|
+
return match?.[1] ?? null;
|
|
11728
|
+
}
|
|
11729
|
+
function isResponsesInferenceRoute(pathname) {
|
|
11730
|
+
return pathname === "/v1/codex/responses" || pathname === "/v1/pool/openai-responses/responses";
|
|
11731
|
+
}
|
|
11732
|
+
function keyRole(req, url, config) {
|
|
11733
|
+
const apiKey = req.headers.get("x-api-key");
|
|
11734
|
+
if (safeKeyEqual(apiKey, config.adminKey)) {
|
|
11668
11735
|
return "admin";
|
|
11669
11736
|
}
|
|
11670
|
-
if (safeKeyEqual(
|
|
11737
|
+
if (safeKeyEqual(apiKey, config.proxyKey)) {
|
|
11738
|
+
return "proxy";
|
|
11739
|
+
}
|
|
11740
|
+
if (isResponsesInferenceRoute(url.pathname) && safeKeyEqual(bearerToken(req), config.proxyKey)) {
|
|
11671
11741
|
return "proxy";
|
|
11672
11742
|
}
|
|
11673
11743
|
return null;
|
|
@@ -11683,7 +11753,7 @@ function hostnameFromHostHeader(host) {
|
|
|
11683
11753
|
function isWildcardHost(host) {
|
|
11684
11754
|
return host === "0.0.0.0" || host === "::";
|
|
11685
11755
|
}
|
|
11686
|
-
function isLocalHostHeader(hostHeader,
|
|
11756
|
+
function isLocalHostHeader(hostHeader, bindHosts) {
|
|
11687
11757
|
if (hostHeader === null) {
|
|
11688
11758
|
return true;
|
|
11689
11759
|
}
|
|
@@ -11691,7 +11761,8 @@ function isLocalHostHeader(hostHeader, bindHost) {
|
|
|
11691
11761
|
if (!hostname) {
|
|
11692
11762
|
return false;
|
|
11693
11763
|
}
|
|
11694
|
-
|
|
11764
|
+
const configuredHosts = typeof bindHosts === "string" ? [bindHosts] : bindHosts;
|
|
11765
|
+
return LOOPBACK_HOSTS.has(hostname) || configuredHosts.some((bindHost) => !isWildcardHost(bindHost) && hostname === bindHost.toLowerCase());
|
|
11695
11766
|
}
|
|
11696
11767
|
function isSameOriginManagementRequest(req, url) {
|
|
11697
11768
|
const fetchSite = req.headers.get("sec-fetch-site");
|
|
@@ -11706,12 +11777,12 @@ function jsonError(status, message) {
|
|
|
11706
11777
|
}
|
|
11707
11778
|
function exposureFailure(req, url, server, config) {
|
|
11708
11779
|
const remote = requestAddress(server, req);
|
|
11709
|
-
const loopbackExempt = config.trustLoopback && (remote.hasRequestIp ? isLoopbackAddress(remote.address) : isLocalHostHeader(url.host, config.
|
|
11710
|
-
const role = keyRole(req, config);
|
|
11780
|
+
const loopbackExempt = config.trustLoopback && (remote.hasRequestIp ? isLoopbackAddress(remote.address) : isLocalHostHeader(url.host, config.bindHosts)) && !isForwardedRequest(req.headers);
|
|
11781
|
+
const role = keyRole(req, url, config);
|
|
11711
11782
|
if (!role && !loopbackExempt) {
|
|
11712
11783
|
return jsonError(401, "A proxy key is required for this request");
|
|
11713
11784
|
}
|
|
11714
|
-
if (!role && !isLocalHostHeader(req.headers.get("host"), config.
|
|
11785
|
+
if (!role && !isLocalHostHeader(req.headers.get("host"), config.bindHosts)) {
|
|
11715
11786
|
return jsonError(403, "Host header is not allowed for a loopback request");
|
|
11716
11787
|
}
|
|
11717
11788
|
if (url.pathname.startsWith("/api/")) {
|
|
@@ -11995,6 +12066,7 @@ function createServerFetchHandler({
|
|
|
11995
12066
|
withDashboard,
|
|
11996
12067
|
exposureConfig = {
|
|
11997
12068
|
bindHost: "127.0.0.1",
|
|
12069
|
+
bindHosts: ["127.0.0.1"],
|
|
11998
12070
|
trustLoopback: true
|
|
11999
12071
|
},
|
|
12000
12072
|
handleProxyRequest = handleProxy,
|
|
@@ -12141,8 +12213,10 @@ async function stopServerRuntime() {
|
|
|
12141
12213
|
serverStopPromise = (async () => {
|
|
12142
12214
|
const errors2 = [];
|
|
12143
12215
|
const activeServer = serverInstance;
|
|
12216
|
+
const activeAdditionalServers = additionalServerInstances;
|
|
12144
12217
|
const activeUnixServer = unixServerInstance;
|
|
12145
12218
|
serverInstance = null;
|
|
12219
|
+
additionalServerInstances = [];
|
|
12146
12220
|
unixServerInstance = null;
|
|
12147
12221
|
try {
|
|
12148
12222
|
await activeUnixServer?.stop(true);
|
|
@@ -12154,6 +12228,13 @@ async function stopServerRuntime() {
|
|
|
12154
12228
|
} catch (error) {
|
|
12155
12229
|
errors2.push(toError("Failed to stop Bun server", error));
|
|
12156
12230
|
}
|
|
12231
|
+
for (const [index, server] of activeAdditionalServers.entries()) {
|
|
12232
|
+
try {
|
|
12233
|
+
await server.stop(true);
|
|
12234
|
+
} catch (error) {
|
|
12235
|
+
errors2.push(toError(`Failed to stop additional Bun server ${index + 1}`, error));
|
|
12236
|
+
}
|
|
12237
|
+
}
|
|
12157
12238
|
stopRetentionMaintenance();
|
|
12158
12239
|
try {
|
|
12159
12240
|
await waitForProxyBackgroundTasks();
|
|
@@ -12232,6 +12313,16 @@ function startServer(options) {
|
|
|
12232
12313
|
fetch: fetchHandler,
|
|
12233
12314
|
websocket: websocketProxyHandler
|
|
12234
12315
|
});
|
|
12316
|
+
const activePort2 = serverInstance.port ?? runtimeConfig2.port;
|
|
12317
|
+
for (const hostname of exposureConfig.bindHosts.slice(1)) {
|
|
12318
|
+
additionalServerInstances.push(serve({
|
|
12319
|
+
hostname,
|
|
12320
|
+
port: activePort2,
|
|
12321
|
+
idleTimeout: NETWORK.IDLE_TIMEOUT_MAX,
|
|
12322
|
+
fetch: fetchHandler,
|
|
12323
|
+
websocket: websocketProxyHandler
|
|
12324
|
+
}));
|
|
12325
|
+
}
|
|
12235
12326
|
} catch (error) {
|
|
12236
12327
|
stopServerRuntime().catch(() => {
|
|
12237
12328
|
lifecycleLog.error("Failed to clean up after listener startup failure");
|
|
@@ -12267,7 +12358,7 @@ async function handleGracefulShutdown(signal) {
|
|
|
12267
12358
|
process.exit(1);
|
|
12268
12359
|
}
|
|
12269
12360
|
}
|
|
12270
|
-
var serverLog, lifecycleLog, serverInstance = null, unixServerInstance = null, stopRetentionJob = null, serverStopPromise = null;
|
|
12361
|
+
var serverLog, lifecycleLog, serverInstance = null, additionalServerInstances, unixServerInstance = null, stopRetentionJob = null, serverStopPromise = null;
|
|
12271
12362
|
var init_src11 = __esm(() => {
|
|
12272
12363
|
init_src9();
|
|
12273
12364
|
init_src2();
|
|
@@ -12285,6 +12376,7 @@ var init_src11 = __esm(() => {
|
|
|
12285
12376
|
lifecycleLog = new Logger("ServerLifecycle", 1 /* INFO */, {
|
|
12286
12377
|
silentConsole: false
|
|
12287
12378
|
});
|
|
12379
|
+
additionalServerInstances = [];
|
|
12288
12380
|
process.on("SIGINT", () => handleGracefulShutdown("SIGINT"));
|
|
12289
12381
|
process.on("SIGTERM", () => handleGracefulShutdown("SIGTERM"));
|
|
12290
12382
|
if (false) {}
|
|
@@ -13820,6 +13912,9 @@ var init_analytics2 = __esm(() => {
|
|
|
13820
13912
|
// src/core/args.ts
|
|
13821
13913
|
import { parseArgs as nodeParseArgs } from "util";
|
|
13822
13914
|
function parseArgs(args) {
|
|
13915
|
+
if (args.length === 1 && args[0] === "install-client") {
|
|
13916
|
+
return { installClient: true };
|
|
13917
|
+
}
|
|
13823
13918
|
if (args[0] === "run") {
|
|
13824
13919
|
const separator = args.indexOf("--");
|
|
13825
13920
|
if (separator === -1) {
|
|
@@ -13895,11 +13990,12 @@ var init_args = () => {};
|
|
|
13895
13990
|
// src/core/local-routing.ts
|
|
13896
13991
|
import {
|
|
13897
13992
|
accessSync,
|
|
13898
|
-
chmodSync,
|
|
13993
|
+
chmodSync as chmodSync2,
|
|
13899
13994
|
constants as constants2,
|
|
13900
13995
|
existsSync as existsSync3,
|
|
13901
13996
|
mkdirSync as mkdirSync4,
|
|
13902
13997
|
readFileSync as readFileSync3,
|
|
13998
|
+
statSync as statSync3,
|
|
13903
13999
|
unlinkSync as unlinkSync2,
|
|
13904
14000
|
writeFileSync as writeFileSync2
|
|
13905
14001
|
} from "fs";
|
|
@@ -13911,25 +14007,56 @@ function shellQuote(value) {
|
|
|
13911
14007
|
function shellDoubleQuote(value) {
|
|
13912
14008
|
return `"${value.replaceAll("\\", "\\\\").replaceAll('"', "\\\"")}"`;
|
|
13913
14009
|
}
|
|
13914
|
-
function proxyBaseUrl(port, directProvider, wire, providerPoolWires) {
|
|
14010
|
+
function proxyBaseUrl(port, configuredBaseUrl, directProvider, wire, providerPoolWires) {
|
|
13915
14011
|
const route = providerPoolWires?.includes(wire) ? `/v1/pool/${wire}` : `/v1/${directProvider}`;
|
|
13916
|
-
return `http://127.0.0.1:${port}${route}`;
|
|
14012
|
+
return `${configuredBaseUrl ?? `http://127.0.0.1:${port}`}${route}`;
|
|
13917
14013
|
}
|
|
13918
|
-
function
|
|
14014
|
+
function validateProxyKeyFile(proxyKeyFile) {
|
|
14015
|
+
const resolvedPath = proxyKeyFile.startsWith("~/") ? join8(homedir2(), proxyKeyFile.slice(2)) : proxyKeyFile;
|
|
14016
|
+
let metadata;
|
|
14017
|
+
try {
|
|
14018
|
+
metadata = statSync3(resolvedPath);
|
|
14019
|
+
} catch {
|
|
14020
|
+
throw new Error("Agent-smith forwarding-key file is not readable");
|
|
14021
|
+
}
|
|
14022
|
+
if (!metadata.isFile()) {
|
|
14023
|
+
throw new Error("Agent-smith forwarding-key path must be a file");
|
|
14024
|
+
}
|
|
14025
|
+
if ((metadata.mode & 63) !== 0) {
|
|
14026
|
+
throw new Error("Agent-smith forwarding-key file must be owner-only");
|
|
14027
|
+
}
|
|
14028
|
+
const key = readFileSync3(resolvedPath, "utf8").trim();
|
|
14029
|
+
if (!/^[\x21-\x7e]{1,512}$/.test(key)) {
|
|
14030
|
+
throw new Error("Agent-smith forwarding-key file is invalid");
|
|
14031
|
+
}
|
|
14032
|
+
return resolvedPath;
|
|
14033
|
+
}
|
|
14034
|
+
function proxyKeySetup(proxyKeyFile) {
|
|
14035
|
+
if (!proxyKeyFile) {
|
|
14036
|
+
return `export AGENT_SMITH_PROXY_KEY=${shellQuote(LOCAL_CLIENT_KEY)}`;
|
|
14037
|
+
}
|
|
14038
|
+
return `if [ ! -r ${shellQuote(proxyKeyFile)} ]; then
|
|
14039
|
+
echo "Agent-smith forwarding-key file is not readable" >&2
|
|
14040
|
+
exit 1
|
|
14041
|
+
fi
|
|
14042
|
+
export AGENT_SMITH_PROXY_KEY=$(cat ${shellQuote(proxyKeyFile)})`;
|
|
14043
|
+
}
|
|
14044
|
+
function claudeWrapper(claudePath, baseUrl, proxyKeyFile) {
|
|
13919
14045
|
return `#!/bin/sh
|
|
13920
14046
|
# Generated by agent-smith. This file delegates to the real Claude Code binary.
|
|
13921
14047
|
unset CF_ADMIN_KEY
|
|
13922
14048
|
unset CF_PROXY_KEY
|
|
13923
|
-
|
|
13924
|
-
export
|
|
14049
|
+
${proxyKeySetup(proxyKeyFile)}
|
|
14050
|
+
export ANTHROPIC_API_KEY="$AGENT_SMITH_PROXY_KEY"
|
|
14051
|
+
export ANTHROPIC_AUTH_TOKEN="$AGENT_SMITH_PROXY_KEY"
|
|
13925
14052
|
export ANTHROPIC_BASE_URL=${shellQuote(baseUrl)}
|
|
13926
14053
|
exec ${shellQuote(claudePath)} "$@"
|
|
13927
14054
|
`;
|
|
13928
14055
|
}
|
|
13929
|
-
function codexWrapper(codexPath, baseUrl) {
|
|
14056
|
+
function codexWrapper(codexPath, baseUrl, proxyKeyFile) {
|
|
13930
14057
|
const config = [
|
|
13931
14058
|
'model_provider="agent_smith"',
|
|
13932
|
-
'model_providers.agent_smith.name="Agent-smith
|
|
14059
|
+
'model_providers.agent_smith.name="Agent-smith"',
|
|
13933
14060
|
`model_providers.agent_smith.base_url="${baseUrl}"`,
|
|
13934
14061
|
'model_providers.agent_smith.env_key="AGENT_SMITH_PROXY_KEY"',
|
|
13935
14062
|
'model_providers.agent_smith.wire_api="responses"',
|
|
@@ -13941,29 +14068,33 @@ function codexWrapper(codexPath, baseUrl) {
|
|
|
13941
14068
|
# Generated by agent-smith. This file delegates to the real Codex binary.
|
|
13942
14069
|
unset CF_ADMIN_KEY
|
|
13943
14070
|
unset CF_PROXY_KEY
|
|
13944
|
-
|
|
14071
|
+
${proxyKeySetup(proxyKeyFile)}
|
|
13945
14072
|
exec ${shellQuote(codexPath)} \\
|
|
13946
14073
|
${overrides} \\
|
|
13947
14074
|
"$@"
|
|
13948
14075
|
`;
|
|
13949
14076
|
}
|
|
13950
14077
|
async function installClientWrappers(options) {
|
|
14078
|
+
if (options.baseUrl && !options.proxyKeyFile) {
|
|
14079
|
+
throw new Error("A configured Agent-smith server requires a forwarding key file");
|
|
14080
|
+
}
|
|
13951
14081
|
mkdirSync4(options.binDir, { recursive: true, mode: 448 });
|
|
14082
|
+
const proxyKeyFile = options.proxyKeyFile ? validateProxyKeyFile(options.proxyKeyFile) : undefined;
|
|
13952
14083
|
if (options.claudePath) {
|
|
13953
14084
|
const claude = join8(options.binDir, "claude");
|
|
13954
|
-
writeFileSync2(claude, claudeWrapper(options.claudePath, proxyBaseUrl(options.port, "claude-code", "anthropic-messages", options.providerPoolWires)), {
|
|
14085
|
+
writeFileSync2(claude, claudeWrapper(options.claudePath, proxyBaseUrl(options.port, options.baseUrl, "claude-code", "anthropic-messages", options.providerPoolWires), proxyKeyFile), {
|
|
13955
14086
|
encoding: "utf8",
|
|
13956
14087
|
mode: 448
|
|
13957
14088
|
});
|
|
13958
|
-
|
|
14089
|
+
chmodSync2(claude, 448);
|
|
13959
14090
|
}
|
|
13960
14091
|
if (options.codexPath) {
|
|
13961
14092
|
const codex = join8(options.binDir, "codex");
|
|
13962
|
-
writeFileSync2(codex, codexWrapper(options.codexPath, proxyBaseUrl(options.port, "codex", "openai-responses", options.providerPoolWires)), {
|
|
14093
|
+
writeFileSync2(codex, codexWrapper(options.codexPath, proxyBaseUrl(options.port, options.baseUrl, "codex", "openai-responses", options.providerPoolWires), proxyKeyFile), {
|
|
13963
14094
|
encoding: "utf8",
|
|
13964
14095
|
mode: 448
|
|
13965
14096
|
});
|
|
13966
|
-
|
|
14097
|
+
chmodSync2(codex, 448);
|
|
13967
14098
|
}
|
|
13968
14099
|
}
|
|
13969
14100
|
function defaultShellProfile() {
|
|
@@ -14050,6 +14181,8 @@ async function enableLocalClientRouting(options) {
|
|
|
14050
14181
|
claudePath,
|
|
14051
14182
|
codexPath,
|
|
14052
14183
|
port: options.port,
|
|
14184
|
+
baseUrl: options.baseUrl,
|
|
14185
|
+
proxyKeyFile: options.proxyKeyFile,
|
|
14053
14186
|
providerPoolWires: options.providerPoolWires ?? Object.keys(new Config().getProviderPools())
|
|
14054
14187
|
});
|
|
14055
14188
|
const existingProfile = existsSync3(shellProfile) ? readFileSync3(shellProfile, "utf8") : "";
|
|
@@ -14059,7 +14192,7 @@ async function enableLocalClientRouting(options) {
|
|
|
14059
14192
|
encoding: "utf8",
|
|
14060
14193
|
mode: 384
|
|
14061
14194
|
});
|
|
14062
|
-
|
|
14195
|
+
chmodSync2(statePath, 384);
|
|
14063
14196
|
return getLocalRoutingStatus({ binDir, shellProfile, statePath });
|
|
14064
14197
|
}
|
|
14065
14198
|
function disableLocalClientRouting(paths2 = {}) {
|
|
@@ -17788,6 +17921,7 @@ ${POC_NOTICE}
|
|
|
17788
17921
|
|
|
17789
17922
|
Usage: agent-smith [options]
|
|
17790
17923
|
agent-smith run [--port <number>] -- [claude arguments]
|
|
17924
|
+
agent-smith install-client
|
|
17791
17925
|
|
|
17792
17926
|
Options:
|
|
17793
17927
|
--serve Start API server with dashboard
|
|
@@ -17817,6 +17951,7 @@ Examples:
|
|
|
17817
17951
|
agent-smith --pause work # Pause account
|
|
17818
17952
|
agent-smith --analyze # Run performance analysis
|
|
17819
17953
|
agent-smith --stats # View stats
|
|
17954
|
+
agent-smith install-client # Configure Claude Code and Codex for this Agent-smith server
|
|
17820
17955
|
`);
|
|
17821
17956
|
process.exit(0);
|
|
17822
17957
|
}
|
|
@@ -17828,6 +17963,21 @@ Examples:
|
|
|
17828
17963
|
await new Promise(() => {});
|
|
17829
17964
|
return;
|
|
17830
17965
|
}
|
|
17966
|
+
if (parsed.installClient) {
|
|
17967
|
+
const config2 = new Config;
|
|
17968
|
+
const client2 = config2.getClientRouting();
|
|
17969
|
+
if (!client2.baseUrl || !client2.proxyKeyFile) {
|
|
17970
|
+
throw new Error("client.base_url and client.proxy_key_file are required before installing client wrappers");
|
|
17971
|
+
}
|
|
17972
|
+
const port2 = config2.getRuntime().port || NETWORK.DEFAULT_PORT;
|
|
17973
|
+
await enableLocalClientRouting({
|
|
17974
|
+
port: port2,
|
|
17975
|
+
baseUrl: client2.baseUrl,
|
|
17976
|
+
proxyKeyFile: client2.proxyKeyFile
|
|
17977
|
+
});
|
|
17978
|
+
console.log(`Claude Code and Codex now route through ${client2.baseUrl}`);
|
|
17979
|
+
return;
|
|
17980
|
+
}
|
|
17831
17981
|
if (parsed.run) {
|
|
17832
17982
|
printPocNotice();
|
|
17833
17983
|
const config2 = new Config;
|
|
@@ -1315,11 +1315,13 @@ var init_paths = __esm(() => {
|
|
|
1315
1315
|
// ../../packages/config/src/index.ts
|
|
1316
1316
|
import { EventEmitter as EventEmitter3 } from "events";
|
|
1317
1317
|
import {
|
|
1318
|
+
chmodSync,
|
|
1318
1319
|
existsSync as existsSync2,
|
|
1319
1320
|
mkdirSync as mkdirSync2,
|
|
1320
1321
|
readFileSync,
|
|
1321
1322
|
renameSync,
|
|
1322
1323
|
rmSync,
|
|
1324
|
+
statSync as statSync2,
|
|
1323
1325
|
writeFileSync
|
|
1324
1326
|
} from "fs";
|
|
1325
1327
|
import { dirname } from "path";
|
|
@@ -1372,6 +1374,12 @@ function sanitizeConfigData(value) {
|
|
|
1372
1374
|
sanitized.provider_pools = providerPools;
|
|
1373
1375
|
continue;
|
|
1374
1376
|
}
|
|
1377
|
+
if (key === "client") {
|
|
1378
|
+
const client = sanitizeClientRouting(entry);
|
|
1379
|
+
if (client)
|
|
1380
|
+
sanitized.client = client;
|
|
1381
|
+
continue;
|
|
1382
|
+
}
|
|
1375
1383
|
if (key === "alerts") {
|
|
1376
1384
|
const alerts = sanitizeAlerts(entry);
|
|
1377
1385
|
if (alerts)
|
|
@@ -1461,6 +1469,23 @@ function sanitizeProviderBaseUrl(value) {
|
|
|
1461
1469
|
return;
|
|
1462
1470
|
}
|
|
1463
1471
|
}
|
|
1472
|
+
function sanitizeClientRouting(value) {
|
|
1473
|
+
if (!isRecord(value))
|
|
1474
|
+
return;
|
|
1475
|
+
const client = {};
|
|
1476
|
+
if (typeof value.base_url === "string") {
|
|
1477
|
+
try {
|
|
1478
|
+
const url = new URL(value.base_url);
|
|
1479
|
+
if ((url.protocol === "http:" || url.protocol === "https:") && url.pathname === "/" && url.search === "" && url.hash === "" && url.username === "" && url.password === "") {
|
|
1480
|
+
client.base_url = url.origin;
|
|
1481
|
+
}
|
|
1482
|
+
} catch {}
|
|
1483
|
+
}
|
|
1484
|
+
if (typeof value.proxy_key_file === "string" && (value.proxy_key_file.startsWith("/") || value.proxy_key_file.startsWith("~/")) && value.proxy_key_file.length <= 1024) {
|
|
1485
|
+
client.proxy_key_file = value.proxy_key_file;
|
|
1486
|
+
}
|
|
1487
|
+
return Object.keys(client).length > 0 ? client : undefined;
|
|
1488
|
+
}
|
|
1464
1489
|
function sanitizeProviderModels(value) {
|
|
1465
1490
|
if (!isRecord(value))
|
|
1466
1491
|
return;
|
|
@@ -1596,17 +1621,30 @@ var init_src4 = __esm(() => {
|
|
|
1596
1621
|
try {
|
|
1597
1622
|
const content = readFileSync(this.configPath, "utf8");
|
|
1598
1623
|
this.data = sanitizeConfigData(JSON.parse(content));
|
|
1599
|
-
if ("default_agent_model" in this.data) {
|
|
1600
|
-
delete this.data.default_agent_model;
|
|
1601
|
-
this.saveConfig();
|
|
1602
|
-
}
|
|
1603
1624
|
} catch (error) {
|
|
1604
1625
|
log2.error(`Failed to parse config file: ${error}`);
|
|
1605
1626
|
this.data = {};
|
|
1627
|
+
return;
|
|
1628
|
+
}
|
|
1629
|
+
try {
|
|
1630
|
+
chmodSync(this.configPath, 384);
|
|
1631
|
+
} catch {
|
|
1632
|
+
let ownerOnly = false;
|
|
1633
|
+
try {
|
|
1634
|
+
ownerOnly = (statSync2(this.configPath).mode & 63) === 0;
|
|
1635
|
+
} catch {}
|
|
1636
|
+
if (!ownerOnly) {
|
|
1637
|
+
throw new Error("Config file must be owner-only");
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
if ("default_agent_model" in this.data) {
|
|
1641
|
+
delete this.data.default_agent_model;
|
|
1642
|
+
this.saveConfig();
|
|
1606
1643
|
}
|
|
1607
1644
|
} else {
|
|
1608
1645
|
const dir = dirname(this.configPath);
|
|
1609
|
-
mkdirSync2(dir, { recursive: true });
|
|
1646
|
+
mkdirSync2(dir, { recursive: true, mode: 448 });
|
|
1647
|
+
chmodSync(dir, 448);
|
|
1610
1648
|
this.data = {
|
|
1611
1649
|
lb_strategy: DEFAULT_STRATEGY
|
|
1612
1650
|
};
|
|
@@ -1737,6 +1775,15 @@ var init_src4 = __esm(() => {
|
|
|
1737
1775
|
resendApiKey: file?.resend_api_key ?? null
|
|
1738
1776
|
};
|
|
1739
1777
|
}
|
|
1778
|
+
getClientRouting() {
|
|
1779
|
+
const client = this.data.client;
|
|
1780
|
+
return {
|
|
1781
|
+
...client?.base_url && { baseUrl: client.base_url },
|
|
1782
|
+
...client?.proxy_key_file && {
|
|
1783
|
+
proxyKeyFile: client.proxy_key_file
|
|
1784
|
+
}
|
|
1785
|
+
};
|
|
1786
|
+
}
|
|
1740
1787
|
getBlockWhenAllFull() {
|
|
1741
1788
|
return this.data.block_when_all_full !== false;
|
|
1742
1789
|
}
|
package/dist/server.js
CHANGED
|
@@ -3834,11 +3834,13 @@ var init_paths = __esm(() => {
|
|
|
3834
3834
|
// ../../packages/config/src/index.ts
|
|
3835
3835
|
import { EventEmitter as EventEmitter3 } from "events";
|
|
3836
3836
|
import {
|
|
3837
|
+
chmodSync,
|
|
3837
3838
|
existsSync as existsSync2,
|
|
3838
3839
|
mkdirSync as mkdirSync2,
|
|
3839
3840
|
readFileSync,
|
|
3840
3841
|
renameSync,
|
|
3841
3842
|
rmSync,
|
|
3843
|
+
statSync as statSync2,
|
|
3842
3844
|
writeFileSync
|
|
3843
3845
|
} from "fs";
|
|
3844
3846
|
import { dirname } from "path";
|
|
@@ -3891,6 +3893,12 @@ function sanitizeConfigData(value) {
|
|
|
3891
3893
|
sanitized.provider_pools = providerPools;
|
|
3892
3894
|
continue;
|
|
3893
3895
|
}
|
|
3896
|
+
if (key === "client") {
|
|
3897
|
+
const client2 = sanitizeClientRouting(entry);
|
|
3898
|
+
if (client2)
|
|
3899
|
+
sanitized.client = client2;
|
|
3900
|
+
continue;
|
|
3901
|
+
}
|
|
3894
3902
|
if (key === "alerts") {
|
|
3895
3903
|
const alerts = sanitizeAlerts(entry);
|
|
3896
3904
|
if (alerts)
|
|
@@ -3980,6 +3988,23 @@ function sanitizeProviderBaseUrl(value) {
|
|
|
3980
3988
|
return;
|
|
3981
3989
|
}
|
|
3982
3990
|
}
|
|
3991
|
+
function sanitizeClientRouting(value) {
|
|
3992
|
+
if (!isRecord(value))
|
|
3993
|
+
return;
|
|
3994
|
+
const client2 = {};
|
|
3995
|
+
if (typeof value.base_url === "string") {
|
|
3996
|
+
try {
|
|
3997
|
+
const url = new URL(value.base_url);
|
|
3998
|
+
if ((url.protocol === "http:" || url.protocol === "https:") && url.pathname === "/" && url.search === "" && url.hash === "" && url.username === "" && url.password === "") {
|
|
3999
|
+
client2.base_url = url.origin;
|
|
4000
|
+
}
|
|
4001
|
+
} catch {}
|
|
4002
|
+
}
|
|
4003
|
+
if (typeof value.proxy_key_file === "string" && (value.proxy_key_file.startsWith("/") || value.proxy_key_file.startsWith("~/")) && value.proxy_key_file.length <= 1024) {
|
|
4004
|
+
client2.proxy_key_file = value.proxy_key_file;
|
|
4005
|
+
}
|
|
4006
|
+
return Object.keys(client2).length > 0 ? client2 : undefined;
|
|
4007
|
+
}
|
|
3983
4008
|
function sanitizeProviderModels(value) {
|
|
3984
4009
|
if (!isRecord(value))
|
|
3985
4010
|
return;
|
|
@@ -4115,17 +4140,30 @@ var init_src7 = __esm(() => {
|
|
|
4115
4140
|
try {
|
|
4116
4141
|
const content = readFileSync(this.configPath, "utf8");
|
|
4117
4142
|
this.data = sanitizeConfigData(JSON.parse(content));
|
|
4118
|
-
if ("default_agent_model" in this.data) {
|
|
4119
|
-
delete this.data.default_agent_model;
|
|
4120
|
-
this.saveConfig();
|
|
4121
|
-
}
|
|
4122
4143
|
} catch (error) {
|
|
4123
4144
|
log6.error(`Failed to parse config file: ${error}`);
|
|
4124
4145
|
this.data = {};
|
|
4146
|
+
return;
|
|
4147
|
+
}
|
|
4148
|
+
try {
|
|
4149
|
+
chmodSync(this.configPath, 384);
|
|
4150
|
+
} catch {
|
|
4151
|
+
let ownerOnly = false;
|
|
4152
|
+
try {
|
|
4153
|
+
ownerOnly = (statSync2(this.configPath).mode & 63) === 0;
|
|
4154
|
+
} catch {}
|
|
4155
|
+
if (!ownerOnly) {
|
|
4156
|
+
throw new Error("Config file must be owner-only");
|
|
4157
|
+
}
|
|
4158
|
+
}
|
|
4159
|
+
if ("default_agent_model" in this.data) {
|
|
4160
|
+
delete this.data.default_agent_model;
|
|
4161
|
+
this.saveConfig();
|
|
4125
4162
|
}
|
|
4126
4163
|
} else {
|
|
4127
4164
|
const dir = dirname(this.configPath);
|
|
4128
|
-
mkdirSync2(dir, { recursive: true });
|
|
4165
|
+
mkdirSync2(dir, { recursive: true, mode: 448 });
|
|
4166
|
+
chmodSync(dir, 448);
|
|
4129
4167
|
this.data = {
|
|
4130
4168
|
lb_strategy: DEFAULT_STRATEGY
|
|
4131
4169
|
};
|
|
@@ -4256,6 +4294,15 @@ var init_src7 = __esm(() => {
|
|
|
4256
4294
|
resendApiKey: file?.resend_api_key ?? null
|
|
4257
4295
|
};
|
|
4258
4296
|
}
|
|
4297
|
+
getClientRouting() {
|
|
4298
|
+
const client2 = this.data.client;
|
|
4299
|
+
return {
|
|
4300
|
+
...client2?.base_url && { baseUrl: client2.base_url },
|
|
4301
|
+
...client2?.proxy_key_file && {
|
|
4302
|
+
proxyKeyFile: client2.proxy_key_file
|
|
4303
|
+
}
|
|
4304
|
+
};
|
|
4305
|
+
}
|
|
4259
4306
|
getBlockWhenAllFull() {
|
|
4260
4307
|
return this.data.block_when_all_full !== false;
|
|
4261
4308
|
}
|
|
@@ -11636,12 +11683,24 @@ function requestAddress(server, req) {
|
|
|
11636
11683
|
function configuredKey(value) {
|
|
11637
11684
|
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
11638
11685
|
}
|
|
11686
|
+
function configuredBindHosts(value) {
|
|
11687
|
+
if (value === undefined)
|
|
11688
|
+
return;
|
|
11689
|
+
const bindHosts = [
|
|
11690
|
+
...new Set(value.split(",").map((host) => host.trim()).filter(Boolean))
|
|
11691
|
+
];
|
|
11692
|
+
return bindHosts.length > 0 ? bindHosts : undefined;
|
|
11693
|
+
}
|
|
11639
11694
|
function createExposureConfig(config) {
|
|
11640
11695
|
const configuredHost = configuredKey(process.env.CF_HOST) ?? configuredKey(config.get("host"));
|
|
11696
|
+
const bindHosts = configuredBindHosts(process.env.CF_BIND_HOSTS) ?? [
|
|
11697
|
+
configuredHost ?? "127.0.0.1"
|
|
11698
|
+
];
|
|
11641
11699
|
const configuredTrustLoopback = process.env.CF_TRUST_LOOPBACK;
|
|
11642
11700
|
return {
|
|
11643
11701
|
adminKey: configuredKey(process.env.CF_ADMIN_KEY),
|
|
11644
|
-
bindHost:
|
|
11702
|
+
bindHost: bindHosts[0],
|
|
11703
|
+
bindHosts,
|
|
11645
11704
|
proxyKey: configuredKey(process.env.CF_PROXY_KEY),
|
|
11646
11705
|
trustLoopback: configuredTrustLoopback === undefined ? config.get("trust_loopback") !== false : configuredTrustLoopback !== "false"
|
|
11647
11706
|
};
|
|
@@ -11661,12 +11720,23 @@ function safeKeyEqual(candidate, expected) {
|
|
|
11661
11720
|
}
|
|
11662
11721
|
return timingSafeEqual(Buffer.from(candidate), Buffer.from(expected));
|
|
11663
11722
|
}
|
|
11664
|
-
function
|
|
11665
|
-
const
|
|
11666
|
-
|
|
11723
|
+
function bearerToken(req) {
|
|
11724
|
+
const authorization = req.headers.get("authorization");
|
|
11725
|
+
const match = authorization?.match(/^Bearer ([^\s]+)$/);
|
|
11726
|
+
return match?.[1] ?? null;
|
|
11727
|
+
}
|
|
11728
|
+
function isResponsesInferenceRoute(pathname) {
|
|
11729
|
+
return pathname === "/v1/codex/responses" || pathname === "/v1/pool/openai-responses/responses";
|
|
11730
|
+
}
|
|
11731
|
+
function keyRole(req, url, config) {
|
|
11732
|
+
const apiKey = req.headers.get("x-api-key");
|
|
11733
|
+
if (safeKeyEqual(apiKey, config.adminKey)) {
|
|
11667
11734
|
return "admin";
|
|
11668
11735
|
}
|
|
11669
|
-
if (safeKeyEqual(
|
|
11736
|
+
if (safeKeyEqual(apiKey, config.proxyKey)) {
|
|
11737
|
+
return "proxy";
|
|
11738
|
+
}
|
|
11739
|
+
if (isResponsesInferenceRoute(url.pathname) && safeKeyEqual(bearerToken(req), config.proxyKey)) {
|
|
11670
11740
|
return "proxy";
|
|
11671
11741
|
}
|
|
11672
11742
|
return null;
|
|
@@ -11682,7 +11752,7 @@ function hostnameFromHostHeader(host) {
|
|
|
11682
11752
|
function isWildcardHost(host) {
|
|
11683
11753
|
return host === "0.0.0.0" || host === "::";
|
|
11684
11754
|
}
|
|
11685
|
-
function isLocalHostHeader(hostHeader,
|
|
11755
|
+
function isLocalHostHeader(hostHeader, bindHosts) {
|
|
11686
11756
|
if (hostHeader === null) {
|
|
11687
11757
|
return true;
|
|
11688
11758
|
}
|
|
@@ -11690,7 +11760,8 @@ function isLocalHostHeader(hostHeader, bindHost) {
|
|
|
11690
11760
|
if (!hostname) {
|
|
11691
11761
|
return false;
|
|
11692
11762
|
}
|
|
11693
|
-
|
|
11763
|
+
const configuredHosts = typeof bindHosts === "string" ? [bindHosts] : bindHosts;
|
|
11764
|
+
return LOOPBACK_HOSTS.has(hostname) || configuredHosts.some((bindHost) => !isWildcardHost(bindHost) && hostname === bindHost.toLowerCase());
|
|
11694
11765
|
}
|
|
11695
11766
|
function isSameOriginManagementRequest(req, url) {
|
|
11696
11767
|
const fetchSite = req.headers.get("sec-fetch-site");
|
|
@@ -11705,12 +11776,12 @@ function jsonError(status, message) {
|
|
|
11705
11776
|
}
|
|
11706
11777
|
function exposureFailure(req, url, server, config) {
|
|
11707
11778
|
const remote = requestAddress(server, req);
|
|
11708
|
-
const loopbackExempt = config.trustLoopback && (remote.hasRequestIp ? isLoopbackAddress(remote.address) : isLocalHostHeader(url.host, config.
|
|
11709
|
-
const role = keyRole(req, config);
|
|
11779
|
+
const loopbackExempt = config.trustLoopback && (remote.hasRequestIp ? isLoopbackAddress(remote.address) : isLocalHostHeader(url.host, config.bindHosts)) && !isForwardedRequest(req.headers);
|
|
11780
|
+
const role = keyRole(req, url, config);
|
|
11710
11781
|
if (!role && !loopbackExempt) {
|
|
11711
11782
|
return jsonError(401, "A proxy key is required for this request");
|
|
11712
11783
|
}
|
|
11713
|
-
if (!role && !isLocalHostHeader(req.headers.get("host"), config.
|
|
11784
|
+
if (!role && !isLocalHostHeader(req.headers.get("host"), config.bindHosts)) {
|
|
11714
11785
|
return jsonError(403, "Host header is not allowed for a loopback request");
|
|
11715
11786
|
}
|
|
11716
11787
|
if (url.pathname.startsWith("/api/")) {
|
|
@@ -11994,6 +12065,7 @@ function createServerFetchHandler({
|
|
|
11994
12065
|
withDashboard,
|
|
11995
12066
|
exposureConfig = {
|
|
11996
12067
|
bindHost: "127.0.0.1",
|
|
12068
|
+
bindHosts: ["127.0.0.1"],
|
|
11997
12069
|
trustLoopback: true
|
|
11998
12070
|
},
|
|
11999
12071
|
handleProxyRequest = handleProxy,
|
|
@@ -12140,8 +12212,10 @@ async function stopServerRuntime() {
|
|
|
12140
12212
|
serverStopPromise = (async () => {
|
|
12141
12213
|
const errors2 = [];
|
|
12142
12214
|
const activeServer = serverInstance;
|
|
12215
|
+
const activeAdditionalServers = additionalServerInstances;
|
|
12143
12216
|
const activeUnixServer = unixServerInstance;
|
|
12144
12217
|
serverInstance = null;
|
|
12218
|
+
additionalServerInstances = [];
|
|
12145
12219
|
unixServerInstance = null;
|
|
12146
12220
|
try {
|
|
12147
12221
|
await activeUnixServer?.stop(true);
|
|
@@ -12153,6 +12227,13 @@ async function stopServerRuntime() {
|
|
|
12153
12227
|
} catch (error) {
|
|
12154
12228
|
errors2.push(toError("Failed to stop Bun server", error));
|
|
12155
12229
|
}
|
|
12230
|
+
for (const [index, server] of activeAdditionalServers.entries()) {
|
|
12231
|
+
try {
|
|
12232
|
+
await server.stop(true);
|
|
12233
|
+
} catch (error) {
|
|
12234
|
+
errors2.push(toError(`Failed to stop additional Bun server ${index + 1}`, error));
|
|
12235
|
+
}
|
|
12236
|
+
}
|
|
12156
12237
|
stopRetentionMaintenance();
|
|
12157
12238
|
try {
|
|
12158
12239
|
await waitForProxyBackgroundTasks();
|
|
@@ -12231,6 +12312,16 @@ function startServer(options) {
|
|
|
12231
12312
|
fetch: fetchHandler,
|
|
12232
12313
|
websocket: websocketProxyHandler
|
|
12233
12314
|
});
|
|
12315
|
+
const activePort2 = serverInstance.port ?? runtimeConfig2.port;
|
|
12316
|
+
for (const hostname of exposureConfig.bindHosts.slice(1)) {
|
|
12317
|
+
additionalServerInstances.push(serve({
|
|
12318
|
+
hostname,
|
|
12319
|
+
port: activePort2,
|
|
12320
|
+
idleTimeout: NETWORK.IDLE_TIMEOUT_MAX,
|
|
12321
|
+
fetch: fetchHandler,
|
|
12322
|
+
websocket: websocketProxyHandler
|
|
12323
|
+
}));
|
|
12324
|
+
}
|
|
12234
12325
|
} catch (error) {
|
|
12235
12326
|
stopServerRuntime().catch(() => {
|
|
12236
12327
|
lifecycleLog.error("Failed to clean up after listener startup failure");
|
|
@@ -12266,7 +12357,7 @@ async function handleGracefulShutdown(signal) {
|
|
|
12266
12357
|
process.exit(1);
|
|
12267
12358
|
}
|
|
12268
12359
|
}
|
|
12269
|
-
var serverLog, lifecycleLog, serverInstance = null, unixServerInstance = null, stopRetentionJob = null, serverStopPromise = null;
|
|
12360
|
+
var serverLog, lifecycleLog, serverInstance = null, additionalServerInstances, unixServerInstance = null, stopRetentionJob = null, serverStopPromise = null;
|
|
12270
12361
|
var init_src11 = __esm(() => {
|
|
12271
12362
|
init_src9();
|
|
12272
12363
|
init_src2();
|
|
@@ -12284,6 +12375,7 @@ var init_src11 = __esm(() => {
|
|
|
12284
12375
|
lifecycleLog = new Logger("ServerLifecycle", 1 /* INFO */, {
|
|
12285
12376
|
silentConsole: false
|
|
12286
12377
|
});
|
|
12378
|
+
additionalServerInstances = [];
|
|
12287
12379
|
process.on("SIGINT", () => handleGracefulShutdown("SIGINT"));
|
|
12288
12380
|
process.on("SIGTERM", () => handleGracefulShutdown("SIGTERM"));
|
|
12289
12381
|
if (false) {}
|
package/package.json
CHANGED