@0xmaxma/claude-gateway 2.1.3 → 2.1.5
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 +1 -1
- package/dist/agent/dreaming/reviewer.js +1 -1
- package/dist/agent/dreaming/reviewer.js.map +1 -1
- package/dist/agent/knowledge/shared-promote.d.ts +18 -29
- package/dist/agent/knowledge/shared-promote.d.ts.map +1 -1
- package/dist/agent/knowledge/shared-promote.js +26 -46
- package/dist/agent/knowledge/shared-promote.js.map +1 -1
- package/dist/agent/runner.d.ts +4 -1
- package/dist/agent/runner.d.ts.map +1 -1
- package/dist/agent/runner.js +6 -2
- package/dist/agent/runner.js.map +1 -1
- package/dist/agent/skill-learning/index.d.ts.map +1 -1
- package/dist/agent/skill-learning/index.js +2 -9
- package/dist/agent/skill-learning/index.js.map +1 -1
- package/dist/agent/skill-learning/telemetry.js +1 -1
- package/dist/agent/skill-learning/telemetry.js.map +1 -1
- package/dist/agent/skill-learning/trigger.d.ts +0 -1
- package/dist/agent/skill-learning/trigger.d.ts.map +1 -1
- package/dist/agent/skill-learning/trigger.js +1 -5
- package/dist/agent/skill-learning/trigger.js.map +1 -1
- package/dist/agent/skill-learning/types.d.ts +1 -3
- package/dist/agent/skill-learning/types.d.ts.map +1 -1
- package/dist/api/connectors-router.d.ts +4 -0
- package/dist/api/connectors-router.d.ts.map +1 -1
- package/dist/api/connectors-router.js +182 -2
- package/dist/api/connectors-router.js.map +1 -1
- package/dist/api/gateway-router.d.ts.map +1 -1
- package/dist/api/gateway-router.js +8 -0
- package/dist/api/gateway-router.js.map +1 -1
- package/dist/api/packages.js +7 -7
- package/dist/api/packages.js.map +1 -1
- package/dist/api/router.d.ts.map +1 -1
- package/dist/api/router.js +5 -3
- package/dist/api/router.js.map +1 -1
- package/dist/api/voice-call-router.js +1 -1
- package/dist/api/voice-call-router.js.map +1 -1
- package/dist/api/voice-router.js +4 -4
- package/dist/api/voice-router.js.map +1 -1
- package/dist/api/voice-settings-router.d.ts.map +1 -1
- package/dist/api/voice-settings-router.js +18 -4
- package/dist/api/voice-settings-router.js.map +1 -1
- package/dist/build-provenance.json +4 -4
- package/dist/cli/commands/update.d.ts.map +1 -1
- package/dist/cli/commands/update.js +48 -17
- package/dist/cli/commands/update.js.map +1 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +3 -2
- package/dist/cli/index.js.map +1 -1
- package/dist/connectors/custom-connectors-store.d.ts +23 -0
- package/dist/connectors/custom-connectors-store.d.ts.map +1 -1
- package/dist/connectors/custom-connectors-store.js +23 -1
- package/dist/connectors/custom-connectors-store.js.map +1 -1
- package/dist/connectors/custom.d.ts +31 -0
- package/dist/connectors/custom.d.ts.map +1 -1
- package/dist/connectors/custom.js +37 -0
- package/dist/connectors/custom.js.map +1 -1
- package/dist/connectors/exposure.d.ts +23 -0
- package/dist/connectors/exposure.d.ts.map +1 -0
- package/dist/connectors/exposure.js +37 -0
- package/dist/connectors/exposure.js.map +1 -0
- package/dist/connectors/proxy.d.ts +84 -0
- package/dist/connectors/proxy.d.ts.map +1 -0
- package/dist/connectors/proxy.js +652 -0
- package/dist/connectors/proxy.js.map +1 -0
- package/dist/connectors/redirect.d.ts +12 -0
- package/dist/connectors/redirect.d.ts.map +1 -0
- package/dist/connectors/redirect.js +25 -0
- package/dist/connectors/redirect.js.map +1 -0
- package/dist/connectors/resolve.d.ts +11 -0
- package/dist/connectors/resolve.d.ts.map +1 -1
- package/dist/connectors/resolve.js +23 -2
- package/dist/connectors/resolve.js.map +1 -1
- package/dist/connectors/types.d.ts +18 -0
- package/dist/connectors/types.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/orchestration/capabilities.d.ts.map +1 -1
- package/dist/orchestration/capabilities.js +7 -1
- package/dist/orchestration/capabilities.js.map +1 -1
- package/dist/orchestration/config.d.ts +38 -0
- package/dist/orchestration/config.d.ts.map +1 -1
- package/dist/orchestration/config.js +16 -5
- package/dist/orchestration/config.js.map +1 -1
- package/dist/orchestration/display-stream.d.ts +5 -2
- package/dist/orchestration/display-stream.d.ts.map +1 -1
- package/dist/orchestration/display-stream.js +10 -10
- package/dist/orchestration/display-stream.js.map +1 -1
- package/dist/orchestration/progress-review.js +1 -1
- package/dist/orchestration/progress-review.js.map +1 -1
- package/dist/orchestration/report-promise.d.ts +0 -4
- package/dist/orchestration/report-promise.d.ts.map +1 -1
- package/dist/orchestration/report-promise.js +0 -29
- package/dist/orchestration/report-promise.js.map +1 -1
- package/dist/orchestration/response-schema.d.ts +9 -0
- package/dist/orchestration/response-schema.d.ts.map +1 -1
- package/dist/orchestration/response-schema.js +14 -1
- package/dist/orchestration/response-schema.js.map +1 -1
- package/dist/orchestration/runtime.d.ts.map +1 -1
- package/dist/orchestration/runtime.js +22 -15
- package/dist/orchestration/runtime.js.map +1 -1
- package/dist/orchestration/speech.d.ts +11 -2
- package/dist/orchestration/speech.d.ts.map +1 -1
- package/dist/orchestration/speech.js +25 -5
- package/dist/orchestration/speech.js.map +1 -1
- package/dist/orchestration/store.d.ts +2 -0
- package/dist/orchestration/store.d.ts.map +1 -1
- package/dist/orchestration/store.js.map +1 -1
- package/dist/orchestration/unanswered-inputs.d.ts +9 -0
- package/dist/orchestration/unanswered-inputs.d.ts.map +1 -1
- package/dist/orchestration/unanswered-inputs.js +39 -4
- package/dist/orchestration/unanswered-inputs.js.map +1 -1
- package/dist/packages/registry.d.ts +22 -3
- package/dist/packages/registry.d.ts.map +1 -1
- package/dist/packages/registry.js +33 -15
- package/dist/packages/registry.js.map +1 -1
- package/dist/session/codex-process.d.ts +2 -0
- package/dist/session/codex-process.d.ts.map +1 -1
- package/dist/session/codex-process.js +5 -1
- package/dist/session/codex-process.js.map +1 -1
- package/dist/session/container-connectors.d.ts +14 -1
- package/dist/session/container-connectors.d.ts.map +1 -1
- package/dist/session/container-connectors.js +36 -0
- package/dist/session/container-connectors.js.map +1 -1
- package/dist/session/managed-connectors.d.ts +9 -7
- package/dist/session/managed-connectors.d.ts.map +1 -1
- package/dist/session/managed-connectors.js +23 -9
- package/dist/session/managed-connectors.js.map +1 -1
- package/dist/session/process.d.ts +4 -0
- package/dist/session/process.d.ts.map +1 -1
- package/dist/session/process.js +32 -4
- package/dist/session/process.js.map +1 -1
- package/dist/ui/voice-call-client.d.ts +0 -14
- package/dist/ui/voice-call-client.d.ts.map +1 -1
- package/dist/ui/voice-call-client.js +1 -6
- package/dist/ui/voice-call-client.js.map +1 -1
- package/dist/ui/voice-call-ui.d.ts +3 -0
- package/dist/ui/voice-call-ui.d.ts.map +1 -1
- package/dist/ui/voice-call-ui.js +2 -1
- package/dist/ui/voice-call-ui.js.map +1 -1
- package/dist/voice/fast-ack.d.ts +13 -3
- package/dist/voice/fast-ack.d.ts.map +1 -1
- package/dist/voice/fast-ack.js +33 -15
- package/dist/voice/fast-ack.js.map +1 -1
- package/dist/voice/providers/elevenlabs-stt.d.ts +5 -1
- package/dist/voice/providers/elevenlabs-stt.d.ts.map +1 -1
- package/dist/voice/providers/elevenlabs-stt.js +5 -1
- package/dist/voice/providers/elevenlabs-stt.js.map +1 -1
- package/dist/voice/providers/registry.d.ts +5 -2
- package/dist/voice/providers/registry.d.ts.map +1 -1
- package/dist/voice/providers/registry.js +4 -3
- package/dist/voice/providers/registry.js.map +1 -1
- package/dist/voice/session.d.ts +16 -3
- package/dist/voice/session.d.ts.map +1 -1
- package/dist/voice/session.js +78 -15
- package/dist/voice/session.js.map +1 -1
- package/dist/voice/types.d.ts +14 -0
- package/dist/voice/types.d.ts.map +1 -1
- package/dist/voice/types.js +6 -1
- package/dist/voice/types.js.map +1 -1
- package/mcp/connector-bridge.ts +161 -0
- package/mcp/connector-launcher.mjs +44 -0
- package/package.json +2 -1
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Connector secrets stay in the gateway (#635).
|
|
4
|
+
*
|
|
5
|
+
* A connector's resolved mcpServers entry carries its credential: a header for HTTP,
|
|
6
|
+
* env/args for stdio. Writing that entry into an mcp-config file hands the credential
|
|
7
|
+
* to every process the agent runs, since they all run as the file's owner. Instead,
|
|
8
|
+
* the file gets an entry that reaches the connector through this gateway with a
|
|
9
|
+
* per-spawn grant (see connectorSecretRoute in custom.ts):
|
|
10
|
+
*
|
|
11
|
+
* proxy HTTP connectors: Claude Code's HTTP transport talks to the loopback route
|
|
12
|
+
* below, which resolves the real entry per request and forwards upstream.
|
|
13
|
+
* launch stdio/SSE connectors: a stdio launcher (mcp/connector-launcher.mjs) upgrades
|
|
14
|
+
* a gateway connection to a raw stream, and the gateway spawns
|
|
15
|
+
* mcp/connector-bridge.ts with the resolved entry on its stdin.
|
|
16
|
+
*
|
|
17
|
+
* Legacy app-agent containers cannot reach the host loopback address. They launch
|
|
18
|
+
* every connector through a unix socket the gateway binds in the bind-mounted
|
|
19
|
+
* workspace, and the bridge runs inside the same container with `docker exec`, so
|
|
20
|
+
* stdio servers and upstream URLs see the container's filesystem and network as before.
|
|
21
|
+
*
|
|
22
|
+
* A revoked grant, or a disconnected/disabled/paused/out-of-scope connector, fails
|
|
23
|
+
* closed on the next request or MCP operation.
|
|
24
|
+
*/
|
|
25
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
26
|
+
if (k2 === undefined) k2 = k;
|
|
27
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
28
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
29
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
30
|
+
}
|
|
31
|
+
Object.defineProperty(o, k2, desc);
|
|
32
|
+
}) : (function(o, m, k, k2) {
|
|
33
|
+
if (k2 === undefined) k2 = k;
|
|
34
|
+
o[k2] = m[k];
|
|
35
|
+
}));
|
|
36
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
37
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
38
|
+
}) : function(o, v) {
|
|
39
|
+
o["default"] = v;
|
|
40
|
+
});
|
|
41
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
42
|
+
var ownKeys = function(o) {
|
|
43
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
44
|
+
var ar = [];
|
|
45
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
46
|
+
return ar;
|
|
47
|
+
};
|
|
48
|
+
return ownKeys(o);
|
|
49
|
+
};
|
|
50
|
+
return function (mod) {
|
|
51
|
+
if (mod && mod.__esModule) return mod;
|
|
52
|
+
var result = {};
|
|
53
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
54
|
+
__setModuleDefault(result, mod);
|
|
55
|
+
return result;
|
|
56
|
+
};
|
|
57
|
+
})();
|
|
58
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
59
|
+
exports.LAUNCH_PROTOCOL = exports.CONNECTOR_PROXY_PATH = void 0;
|
|
60
|
+
exports.setConnectorProxyOrigin = setConnectorProxyOrigin;
|
|
61
|
+
exports.agentFacingConnectors = agentFacingConnectors;
|
|
62
|
+
exports.sweepStaleSockets = sweepStaleSockets;
|
|
63
|
+
exports.revokeConnectorGrant = revokeConnectorGrant;
|
|
64
|
+
exports.createConnectorProxyRouter = createConnectorProxyRouter;
|
|
65
|
+
exports.handleConnectorUpgrade = handleConnectorUpgrade;
|
|
66
|
+
const crypto_1 = require("crypto");
|
|
67
|
+
const child_process_1 = require("child_process");
|
|
68
|
+
const fs = __importStar(require("fs"));
|
|
69
|
+
const http = __importStar(require("node:http"));
|
|
70
|
+
const https = __importStar(require("node:https"));
|
|
71
|
+
const os_1 = require("os");
|
|
72
|
+
const path = __importStar(require("path"));
|
|
73
|
+
const node_stream_1 = require("node:stream");
|
|
74
|
+
const express_1 = __importStar(require("express"));
|
|
75
|
+
const resolve_1 = require("./resolve");
|
|
76
|
+
const custom_1 = require("./custom");
|
|
77
|
+
const child_env_1 = require("../jev/child-env");
|
|
78
|
+
const exposure_1 = require("./exposure");
|
|
79
|
+
const redirect_1 = require("./redirect");
|
|
80
|
+
exports.CONNECTOR_PROXY_PATH = '/internal/mcp/connectors';
|
|
81
|
+
/** Runs the bridge module that arrives as the first stdin line (a JSON string), then
|
|
82
|
+
* leaves the rest of stdin to it. */
|
|
83
|
+
const CONTAINER_BRIDGE_LOADER = "let b=Buffer.alloc(0);const on=c=>{b=Buffer.concat([b,c]);const i=b.indexOf(10);if(i<0)return;process.stdin.off('data',on);process.stdin.pause();const code=JSON.parse(b.subarray(0,i).toString('utf8'));process.stdin.unshift(b.subarray(i+1));const M=require('module');const m=new M('/gateway-connector-bridge.cjs');m.filename='/gateway-connector-bridge.cjs';m.paths=[];m._compile(code,m.filename);};process.stdin.on('data',on);";
|
|
84
|
+
exports.LAUNCH_PROTOCOL = 'gateway-connector-stdio';
|
|
85
|
+
const LAUNCHER = path.resolve(__dirname, '../../mcp/connector-launcher.mjs');
|
|
86
|
+
const BRIDGE = path.resolve(__dirname, '../../mcp/connector-bridge.ts');
|
|
87
|
+
/** sun_path is 108 bytes on Linux, terminator included. */
|
|
88
|
+
const MAX_SOCKET_PATH = 107;
|
|
89
|
+
/** Request bodies kept for a same-origin 307/308 replay; MCP messages are far smaller. */
|
|
90
|
+
const MAX_REPLAY_BYTES = 4 * 1024 * 1024;
|
|
91
|
+
const grants = new Map();
|
|
92
|
+
/** Only non-secret runtime basics from the launching agent reach a connector.
|
|
93
|
+
* Connector-specific credentials belong in that connector's explicit `env`. */
|
|
94
|
+
const CONNECTOR_INHERITED_ENV = new Set([
|
|
95
|
+
'PATH', 'HOME', 'USER', 'LOGNAME', 'SHELL', 'TMPDIR', 'TMP', 'TEMP',
|
|
96
|
+
'LANG', 'LC_ALL', 'LC_CTYPE', 'TERM', 'SSL_CERT_FILE', 'SSL_CERT_DIR',
|
|
97
|
+
'NODE_EXTRA_CA_CERTS', 'SYSTEMROOT', 'WINDIR', 'COMSPEC', 'PATHEXT',
|
|
98
|
+
]);
|
|
99
|
+
/** Client headers the MCP streamable-HTTP transport needs; everything else, including
|
|
100
|
+
* the grant's own Authorization, is dropped so a caller cannot add or override one. */
|
|
101
|
+
const FORWARDED_REQUEST_HEADERS = ['accept', 'content-type', 'content-length', 'mcp-session-id', 'mcp-protocol-version', 'last-event-id'];
|
|
102
|
+
const FORWARDED_RESPONSE_HEADERS = ['content-type', 'content-encoding', 'mcp-session-id', 'cache-control'];
|
|
103
|
+
let listenOrigin;
|
|
104
|
+
/** Records where the gateway's HTTP server listens, so a proxy entry never points at
|
|
105
|
+
* an address nothing serves (a specific GATEWAY_BIND, `::1`) or at a GATEWAY_API_URL
|
|
106
|
+
* meant for another network. Set from the bind config before any agent starts, then
|
|
107
|
+
* from the bound address once listening. */
|
|
108
|
+
function setConnectorProxyOrigin(address) {
|
|
109
|
+
if (!address || typeof address === 'string') {
|
|
110
|
+
listenOrigin = undefined;
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const bound = address.address.trim();
|
|
114
|
+
const host = !bound || bound === '0.0.0.0' || bound === '::' ? '127.0.0.1'
|
|
115
|
+
: bound.includes(':') && !bound.startsWith('[') ? `[${bound}]` : bound;
|
|
116
|
+
listenOrigin = `http://${host}:${address.port}`;
|
|
117
|
+
}
|
|
118
|
+
function proxyBaseUrl() {
|
|
119
|
+
return listenOrigin ?? (process.env.GATEWAY_API_URL ?? `http://127.0.0.1:${process.env.PORT ?? '10850'}`).replace(/\/+$/, '');
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Replace every secret-bearing connector in `resolved` with an entry that reaches it
|
|
123
|
+
* through the gateway, and mint one grant covering them. `resolved` itself stays the
|
|
124
|
+
* in-memory source of truth for restart fingerprints; only the returned `servers` may
|
|
125
|
+
* be written to disk. The caller owns the grant and must revoke it when the spawn
|
|
126
|
+
* that uses it ends.
|
|
127
|
+
*
|
|
128
|
+
* `container` is set for a legacy app-agent session whose container can run the
|
|
129
|
+
* launcher (see prepareContainerLaunch). `containerUnavailable` marks one whose
|
|
130
|
+
* container cannot: it keeps the resolved entries, since nothing else in the container
|
|
131
|
+
* can reach the gateway. Either fallback is recorded for the status API, which then
|
|
132
|
+
* reports this agent's secrets as agent-readable (connectors/exposure.ts).
|
|
133
|
+
*/
|
|
134
|
+
function agentFacingConnectors(agent, gateway, resolved, options = {}) {
|
|
135
|
+
const entries = gateway.gateway?.customConnectors ?? {};
|
|
136
|
+
const entryOf = (id) => Object.prototype.hasOwnProperty.call(entries, id) ? entries[id] : undefined;
|
|
137
|
+
const servers = { ...resolved };
|
|
138
|
+
const routes = new Map();
|
|
139
|
+
for (const id of Object.keys(resolved)) {
|
|
140
|
+
const route = (0, custom_1.connectorSecretRoute)(entryOf(id));
|
|
141
|
+
// A container cannot reach the loopback proxy, so its HTTP connectors launch too.
|
|
142
|
+
if (route === 'proxy')
|
|
143
|
+
routes.set(id, options.container ? 'launch' : 'proxy');
|
|
144
|
+
else if (route === 'launch')
|
|
145
|
+
routes.set(id, 'launch');
|
|
146
|
+
}
|
|
147
|
+
const fallBack = () => {
|
|
148
|
+
// A direct-entry fallback is still owned by this live session. Keep its
|
|
149
|
+
// exposure until that session ends; a later discovery or spawn is independent.
|
|
150
|
+
const exposureGrant = (0, crypto_1.randomBytes)(32).toString('base64url');
|
|
151
|
+
grants.set(exposureGrant, {
|
|
152
|
+
agentId: agent.id,
|
|
153
|
+
routes: new Map(),
|
|
154
|
+
resolve: () => undefined,
|
|
155
|
+
cleanEnv: env => env,
|
|
156
|
+
bridges: new Set(),
|
|
157
|
+
});
|
|
158
|
+
(0, exposure_1.recordRuntimeExposure)(exposureGrant, agent.id, routes.keys());
|
|
159
|
+
return { servers, grant: exposureGrant };
|
|
160
|
+
};
|
|
161
|
+
if (!routes.size)
|
|
162
|
+
return { servers };
|
|
163
|
+
if (options.containerUnavailable && !options.container) {
|
|
164
|
+
console.warn(`connector-proxy: agent=${agent.id} container cannot launch connectors; ${[...routes.keys()].join(', ')} keep their resolved entries`);
|
|
165
|
+
return fallBack();
|
|
166
|
+
}
|
|
167
|
+
const grant = (0, crypto_1.randomBytes)(32).toString('base64url');
|
|
168
|
+
const record = {
|
|
169
|
+
agentId: agent.id,
|
|
170
|
+
routes,
|
|
171
|
+
// Re-resolved per request, so a disconnected connector (its secret deleted) or a
|
|
172
|
+
// removed entry fails on the next call. The agent config object is shared with
|
|
173
|
+
// its runner, so current per-agent scope changes are checked in-flight too.
|
|
174
|
+
resolve: id => {
|
|
175
|
+
if (agent.isolateMemory)
|
|
176
|
+
return undefined;
|
|
177
|
+
const current = gateway.gateway?.customConnectors ?? {};
|
|
178
|
+
if (!Object.prototype.hasOwnProperty.call(current, id))
|
|
179
|
+
return undefined;
|
|
180
|
+
return (0, resolve_1.resolveEnabledConnectors)(agent, { [id]: current[id] }, gateway.gateway?.connectorsDefaultEnabled ?? true)[id];
|
|
181
|
+
},
|
|
182
|
+
container: options.container,
|
|
183
|
+
cleanEnv: env => options.sanitizeEnv
|
|
184
|
+
? (0, child_env_1.sanitizeJevChildEnv)(Object.fromEntries(Object.entries(env).filter(([name]) => CONNECTOR_INHERITED_ENV.has(name.toUpperCase()))), gateway.gateway?.jev)
|
|
185
|
+
: env,
|
|
186
|
+
bridges: new Set(),
|
|
187
|
+
};
|
|
188
|
+
let socketName;
|
|
189
|
+
if (options.container) {
|
|
190
|
+
socketName = `${(0, crypto_1.randomBytes)(6).toString('hex')}.sock`;
|
|
191
|
+
const file = path.join(options.container.hostDir, socketName);
|
|
192
|
+
if (Buffer.byteLength(file) > MAX_SOCKET_PATH) {
|
|
193
|
+
console.warn(`connector-proxy: agent=${agent.id} socket path ${file} is too long; ${[...routes.keys()].join(', ')} keep their resolved entries`);
|
|
194
|
+
return fallBack();
|
|
195
|
+
}
|
|
196
|
+
record.socket = { server: listenOnSocket(file), file };
|
|
197
|
+
}
|
|
198
|
+
grants.set(grant, record);
|
|
199
|
+
const base = proxyBaseUrl();
|
|
200
|
+
for (const [id, route] of routes) {
|
|
201
|
+
const config = entryOf(id).config;
|
|
202
|
+
const kept = (0, custom_1.agentFacingFields)(config);
|
|
203
|
+
if (route === 'proxy') {
|
|
204
|
+
servers[id] = { ...kept, type: config.type, url: `${base}${exports.CONNECTOR_PROXY_PATH}/${encodeURIComponent(id)}`, headers: { Authorization: `Bearer ${grant}` } };
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
const launchPath = `${exports.CONNECTOR_PROXY_PATH}/${encodeURIComponent(id)}/stdio`;
|
|
208
|
+
servers[id] = options.container
|
|
209
|
+
? { ...kept, command: 'node', args: [`${options.container.containerDir}/connector-launcher.mjs`], env: { GATEWAY_CONNECTOR_SOCKET: `${options.container.containerDir}/${socketName}`, GATEWAY_CONNECTOR_PATH: launchPath, GATEWAY_CONNECTOR_GRANT: grant } }
|
|
210
|
+
: { ...kept, command: process.execPath, args: [LAUNCHER], env: { GATEWAY_CONNECTOR_URL: `${base}${launchPath}`, GATEWAY_CONNECTOR_GRANT: grant } };
|
|
211
|
+
}
|
|
212
|
+
return { servers, grant };
|
|
213
|
+
}
|
|
214
|
+
/** A container's private channel for launcher upgrades and live authorization checks. */
|
|
215
|
+
function listenOnSocket(file) {
|
|
216
|
+
try {
|
|
217
|
+
fs.rmSync(file, { force: true });
|
|
218
|
+
}
|
|
219
|
+
catch { /* bind reports it */ }
|
|
220
|
+
const app = (0, express_1.default)();
|
|
221
|
+
app.use(createConnectorProxyRouter());
|
|
222
|
+
const server = http.createServer(app);
|
|
223
|
+
server.on('upgrade', (req, socket, head) => { if (!handleConnectorUpgrade(req, socket, head))
|
|
224
|
+
socket.destroy(); });
|
|
225
|
+
server.on('error', err => console.error(`connector-proxy: socket ${file} failed: ${err.message}`));
|
|
226
|
+
server.listen(file, () => { try {
|
|
227
|
+
fs.chmodSync(file, 0o600);
|
|
228
|
+
}
|
|
229
|
+
catch { /* removed already */ } });
|
|
230
|
+
return server;
|
|
231
|
+
}
|
|
232
|
+
/** Remove launcher sockets in `dir` no live grant holds: a gateway that exited
|
|
233
|
+
* without revoking (crash, SIGKILL) leaves them in the agent's workspace. */
|
|
234
|
+
function sweepStaleSockets(dir) {
|
|
235
|
+
const live = new Set([...grants.values()].map(g => g.socket?.file));
|
|
236
|
+
let names;
|
|
237
|
+
try {
|
|
238
|
+
names = fs.readdirSync(dir);
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
for (const name of names) {
|
|
244
|
+
const file = path.join(dir, name);
|
|
245
|
+
if (!name.endsWith('.sock') || live.has(file))
|
|
246
|
+
continue;
|
|
247
|
+
try {
|
|
248
|
+
fs.rmSync(file, { force: true });
|
|
249
|
+
}
|
|
250
|
+
catch (err) {
|
|
251
|
+
console.warn(`connector-proxy: cannot remove stale socket ${file}: ${err.message}`);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
function revokeConnectorGrant(grant) {
|
|
256
|
+
if (!grant)
|
|
257
|
+
return;
|
|
258
|
+
const record = grants.get(grant);
|
|
259
|
+
grants.delete(grant);
|
|
260
|
+
(0, exposure_1.clearRuntimeExposure)(grant);
|
|
261
|
+
if (!record)
|
|
262
|
+
return;
|
|
263
|
+
record.revoked = true;
|
|
264
|
+
// Ending stdin lets a server exit on its own (a container bridge only sees EOF);
|
|
265
|
+
// the signal covers one that does not.
|
|
266
|
+
for (const bridge of record.bridges) {
|
|
267
|
+
bridge.stdin?.end();
|
|
268
|
+
setTimeout(() => bridge.kill('SIGTERM'), 2000).unref();
|
|
269
|
+
}
|
|
270
|
+
if (record.socket) {
|
|
271
|
+
record.socket.server.close();
|
|
272
|
+
try {
|
|
273
|
+
fs.rmSync(record.socket.file, { force: true });
|
|
274
|
+
}
|
|
275
|
+
catch { /* already gone */ }
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/** Loopback, or a same-host client that reached a non-loopback bind address. A
|
|
279
|
+
* same-host reverse proxy also connects from loopback, so a request it forwarded
|
|
280
|
+
* (it adds a forwarding header; an agent's MCP client never does) is refused too. */
|
|
281
|
+
function isSameHost(req) {
|
|
282
|
+
if (req.headers['x-forwarded-for'] !== undefined || req.headers['forwarded'] !== undefined)
|
|
283
|
+
return false;
|
|
284
|
+
const remote = req.socket.remoteAddress;
|
|
285
|
+
// Only a unix socket has no remote address: a container's launcher socket.
|
|
286
|
+
if (remote === undefined)
|
|
287
|
+
return true;
|
|
288
|
+
return remote === req.socket.localAddress || /^(127\.|::1$|::ffff:127\.)/.test(remote);
|
|
289
|
+
}
|
|
290
|
+
function upstreamOf(server) {
|
|
291
|
+
if (!server || typeof server !== 'object')
|
|
292
|
+
return undefined;
|
|
293
|
+
const { type, url, headers } = server;
|
|
294
|
+
if (!(0, custom_1.isStreamableHttpType)(type) || typeof url !== 'string')
|
|
295
|
+
return undefined;
|
|
296
|
+
let parsed;
|
|
297
|
+
try {
|
|
298
|
+
parsed = new URL(url);
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:')
|
|
304
|
+
return undefined;
|
|
305
|
+
const out = {};
|
|
306
|
+
if (headers !== undefined) {
|
|
307
|
+
if (!headers || typeof headers !== 'object' || Array.isArray(headers))
|
|
308
|
+
return undefined;
|
|
309
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
310
|
+
if (typeof value !== 'string')
|
|
311
|
+
return undefined;
|
|
312
|
+
out[name.toLowerCase()] = value;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return { url: parsed, headers: out };
|
|
316
|
+
}
|
|
317
|
+
/** Mounted on the root app BEFORE the JSON body parser: the request body is streamed
|
|
318
|
+
* to the upstream unread, so no size limit applies that a direct connection lacked. */
|
|
319
|
+
function createConnectorProxyRouter() {
|
|
320
|
+
const router = (0, express_1.Router)();
|
|
321
|
+
router.post(`${exports.CONNECTOR_PROXY_PATH}/:id/authorize`, (req, res) => {
|
|
322
|
+
if (!isSameHost(req)) {
|
|
323
|
+
res.status(403).end();
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
const match = /^Bearer (\S+)$/.exec(req.headers.authorization ?? '');
|
|
327
|
+
const grant = match ? grants.get(match[1]) : undefined;
|
|
328
|
+
if (!grant) {
|
|
329
|
+
res.status(401).end();
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const id = req.params['id'] ?? '';
|
|
333
|
+
if (grant.routes.get(id) !== 'launch') {
|
|
334
|
+
res.status(403).end();
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
let resolved;
|
|
338
|
+
try {
|
|
339
|
+
resolved = grant.resolve(id);
|
|
340
|
+
}
|
|
341
|
+
catch (err) {
|
|
342
|
+
console.error(`connector-proxy: connector=${id} authorization check failed: ${err.message}`);
|
|
343
|
+
}
|
|
344
|
+
if (!bridgeable(resolved)) {
|
|
345
|
+
res.status(403).end();
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
res.status(204).end();
|
|
349
|
+
});
|
|
350
|
+
router.all(`${exports.CONNECTOR_PROXY_PATH}/:id`, (req, res) => {
|
|
351
|
+
if (!isSameHost(req)) {
|
|
352
|
+
res.status(403).json({ error: 'Forbidden' });
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const match = /^Bearer (\S+)$/.exec(req.headers.authorization ?? '');
|
|
356
|
+
const grant = match ? grants.get(match[1]) : undefined;
|
|
357
|
+
if (!grant) {
|
|
358
|
+
res.status(401).json({ error: 'Unauthorized' });
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const id = req.params['id'] ?? '';
|
|
362
|
+
if (grant.routes.get(id) !== 'proxy') {
|
|
363
|
+
res.status(403).json({ error: 'Forbidden' });
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (!['GET', 'POST', 'DELETE'].includes(req.method)) {
|
|
367
|
+
res.status(405).json({ error: 'Method not allowed' });
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
let upstream;
|
|
371
|
+
try {
|
|
372
|
+
const resolved = grant.resolve(id);
|
|
373
|
+
upstream = upstreamOf(resolved);
|
|
374
|
+
// Disconnected/disabled resolves to nothing, which is normal; a resolved entry
|
|
375
|
+
// the proxy cannot use is a configuration error worth a line.
|
|
376
|
+
if (resolved !== undefined && !upstream)
|
|
377
|
+
console.error(`connector-proxy: connector=${id} has no usable http url/headers`);
|
|
378
|
+
}
|
|
379
|
+
catch (err) {
|
|
380
|
+
console.error(`connector-proxy: connector=${id} resolve failed: ${err.message}`);
|
|
381
|
+
}
|
|
382
|
+
if (!upstream) {
|
|
383
|
+
res.status(403).json({ error: 'Connector is not available' });
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const headers = {};
|
|
387
|
+
for (const name of FORWARDED_REQUEST_HEADERS) {
|
|
388
|
+
const value = req.headers[name];
|
|
389
|
+
if (typeof value === 'string')
|
|
390
|
+
headers[name] = value;
|
|
391
|
+
}
|
|
392
|
+
Object.assign(headers, upstream.headers);
|
|
393
|
+
// Kept for replaying the body to a same-origin 307/308 target; dropped past the cap.
|
|
394
|
+
let body = [];
|
|
395
|
+
let bodyBytes = 0;
|
|
396
|
+
req.on('data', (chunk) => {
|
|
397
|
+
if (!body)
|
|
398
|
+
return;
|
|
399
|
+
bodyBytes += chunk.length;
|
|
400
|
+
if (bodyBytes > MAX_REPLAY_BYTES)
|
|
401
|
+
body = undefined;
|
|
402
|
+
else
|
|
403
|
+
body.push(chunk);
|
|
404
|
+
});
|
|
405
|
+
const method = req.method;
|
|
406
|
+
let outbound;
|
|
407
|
+
let clientGone = false;
|
|
408
|
+
const send = (target, hops) => {
|
|
409
|
+
let request;
|
|
410
|
+
try {
|
|
411
|
+
// node:http rather than fetch: undici's default 300s headers/body timeouts
|
|
412
|
+
// would cut long tool calls and idle SSE streams the agent's client allows.
|
|
413
|
+
request = (target.protocol === 'https:' ? https : http).request(target, { method, headers }, incoming => {
|
|
414
|
+
const status = incoming.statusCode ?? 502;
|
|
415
|
+
const location = incoming.headers.location;
|
|
416
|
+
if (status >= 300 && status < 400 && location !== undefined) {
|
|
417
|
+
incoming.resume();
|
|
418
|
+
// Same-origin only (see redirect.ts), and only where the request can be
|
|
419
|
+
// repeated unchanged: a bodyless method, or a 307/308 with its body kept.
|
|
420
|
+
const next = (0, redirect_1.sameOriginRedirect)(target, location);
|
|
421
|
+
const repeatable = method !== 'POST' || ((status === 307 || status === 308) && body !== undefined);
|
|
422
|
+
if (!next || hops >= redirect_1.MAX_REDIRECTS || !repeatable || clientGone) {
|
|
423
|
+
if (!next)
|
|
424
|
+
console.error(`connector-proxy: connector=${id} refused a redirect to another origin`);
|
|
425
|
+
if (!res.headersSent)
|
|
426
|
+
res.status(502).json({ error: 'Upstream redirect refused' });
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
const follow = () => {
|
|
430
|
+
if (clientGone)
|
|
431
|
+
return;
|
|
432
|
+
outbound = send(next, hops + 1);
|
|
433
|
+
outbound?.end(method === 'POST' ? Buffer.concat(body) : undefined);
|
|
434
|
+
};
|
|
435
|
+
if (req.readableEnded)
|
|
436
|
+
follow();
|
|
437
|
+
else
|
|
438
|
+
req.once('end', follow);
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
res.status(status);
|
|
442
|
+
for (const name of FORWARDED_RESPONSE_HEADERS) {
|
|
443
|
+
const value = incoming.headers[name];
|
|
444
|
+
if (value !== undefined)
|
|
445
|
+
res.setHeader(name, value);
|
|
446
|
+
}
|
|
447
|
+
res.flushHeaders();
|
|
448
|
+
// pipeline, not pipe: an upstream that drops mid-response must end the
|
|
449
|
+
// agent's stream too, or its call hangs on a response that never finishes.
|
|
450
|
+
(0, node_stream_1.pipeline)(incoming, res, err => {
|
|
451
|
+
// `res.errored` tells an upstream failure from the agent cancelling the call.
|
|
452
|
+
if (err && res.errored)
|
|
453
|
+
console.error(`connector-proxy: connector=${id} response stream failed: ${err.message}`);
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
catch (err) {
|
|
458
|
+
// An invalid header value in the pasted config throws synchronously.
|
|
459
|
+
console.error(`connector-proxy: connector=${id} request rejected: ${err.message}`);
|
|
460
|
+
if (!res.headersSent)
|
|
461
|
+
res.status(502).json({ error: 'Upstream request failed' });
|
|
462
|
+
return undefined;
|
|
463
|
+
}
|
|
464
|
+
request.on('error', err => {
|
|
465
|
+
// Destroyed below because the agent cancelled first: nothing failed upstream.
|
|
466
|
+
if (clientGone)
|
|
467
|
+
return;
|
|
468
|
+
console.error(`connector-proxy: connector=${id} upstream failed: ${err.message}`);
|
|
469
|
+
if (!res.headersSent)
|
|
470
|
+
res.status(502).json({ error: 'Upstream request failed' });
|
|
471
|
+
else
|
|
472
|
+
res.destroy();
|
|
473
|
+
});
|
|
474
|
+
return request;
|
|
475
|
+
};
|
|
476
|
+
outbound = send(upstream.url, 0);
|
|
477
|
+
if (!outbound)
|
|
478
|
+
return;
|
|
479
|
+
// The agent's client went away (cancelled call, closed SSE stream): stop upstream too.
|
|
480
|
+
res.on('close', () => {
|
|
481
|
+
clientGone = !res.writableFinished;
|
|
482
|
+
outbound?.destroy();
|
|
483
|
+
});
|
|
484
|
+
(0, node_stream_1.pipeline)(req, outbound, () => { });
|
|
485
|
+
});
|
|
486
|
+
return router;
|
|
487
|
+
}
|
|
488
|
+
const MAX_PREAMBLE_BYTES = 1024 * 1024;
|
|
489
|
+
/** Whether the bridge can drive this resolved entry (mirrors connector-bridge.ts). */
|
|
490
|
+
function bridgeable(server) {
|
|
491
|
+
if (!server || typeof server !== 'object')
|
|
492
|
+
return false;
|
|
493
|
+
const { command, url, type } = server;
|
|
494
|
+
if (typeof command === 'string')
|
|
495
|
+
return true;
|
|
496
|
+
return typeof url === 'string' && (type === 'sse' || (0, custom_1.isStreamableHttpType)(type));
|
|
497
|
+
}
|
|
498
|
+
function stringEnv(value) {
|
|
499
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
500
|
+
return {};
|
|
501
|
+
return Object.fromEntries(Object.entries(value).filter((pair) => typeof pair[1] === 'string'));
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* A launcher's upgrade request (mcp/connector-launcher.mjs). Returns false when the
|
|
505
|
+
* request is not for this route, so the server's other upgrade handlers can run.
|
|
506
|
+
* After the 101 the socket carries one JSON line (the launcher's cwd and env), then
|
|
507
|
+
* the MCP stdio stream in both directions, piped to and from a bridge process.
|
|
508
|
+
*/
|
|
509
|
+
function handleConnectorUpgrade(req, socket, head) {
|
|
510
|
+
const match = /^\/internal\/mcp\/connectors\/([^/?]+)\/stdio(?:\?.*)?$/.exec(req.url ?? '');
|
|
511
|
+
if (!match)
|
|
512
|
+
return false;
|
|
513
|
+
const refuse = (status) => { socket.end(`HTTP/1.1 ${status}\r\nConnection: close\r\n\r\n`); return true; };
|
|
514
|
+
socket.on('error', () => { });
|
|
515
|
+
if (String(req.headers.upgrade ?? '').toLowerCase() !== exports.LAUNCH_PROTOCOL)
|
|
516
|
+
return refuse('400 Bad Request');
|
|
517
|
+
if (!isSameHost(req))
|
|
518
|
+
return refuse('403 Forbidden');
|
|
519
|
+
const auth = /^Bearer (\S+)$/.exec(req.headers.authorization ?? '');
|
|
520
|
+
const grant = auth ? grants.get(auth[1]) : undefined;
|
|
521
|
+
if (!auth || !grant)
|
|
522
|
+
return refuse('401 Unauthorized');
|
|
523
|
+
let id;
|
|
524
|
+
try {
|
|
525
|
+
id = decodeURIComponent(match[1]);
|
|
526
|
+
}
|
|
527
|
+
catch {
|
|
528
|
+
return refuse('400 Bad Request');
|
|
529
|
+
}
|
|
530
|
+
if (!grant.routes.has(id))
|
|
531
|
+
return refuse('403 Forbidden');
|
|
532
|
+
let config;
|
|
533
|
+
try {
|
|
534
|
+
config = grant.resolve(id);
|
|
535
|
+
if (config !== undefined && !bridgeable(config))
|
|
536
|
+
console.error(`connector-proxy: connector=${id} has no usable command or url`);
|
|
537
|
+
}
|
|
538
|
+
catch (err) {
|
|
539
|
+
console.error(`connector-proxy: connector=${id} resolve failed: ${err.message}`);
|
|
540
|
+
}
|
|
541
|
+
if (!bridgeable(config))
|
|
542
|
+
return refuse('403 Forbidden');
|
|
543
|
+
const server = config;
|
|
544
|
+
socket.write(`HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: ${exports.LAUNCH_PROTOCOL}\r\n\r\n`);
|
|
545
|
+
let pending = head;
|
|
546
|
+
const onData = (chunk) => {
|
|
547
|
+
pending = Buffer.concat([pending, chunk]);
|
|
548
|
+
const end = pending.indexOf(10);
|
|
549
|
+
if (end < 0) {
|
|
550
|
+
if (pending.length > MAX_PREAMBLE_BYTES)
|
|
551
|
+
socket.destroy();
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
socket.off('data', onData);
|
|
555
|
+
socket.pause();
|
|
556
|
+
let preamble = {};
|
|
557
|
+
try {
|
|
558
|
+
preamble = JSON.parse(pending.subarray(0, end).toString('utf8'));
|
|
559
|
+
}
|
|
560
|
+
catch {
|
|
561
|
+
socket.destroy();
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
startBridge(grant, auth[1], id, server, preamble, pending.subarray(end + 1), socket);
|
|
565
|
+
};
|
|
566
|
+
socket.on('data', onData);
|
|
567
|
+
if (head.length)
|
|
568
|
+
onData(Buffer.alloc(0));
|
|
569
|
+
return true;
|
|
570
|
+
}
|
|
571
|
+
/** Per-bridge cap on stderr copied to the gateway log; a server stuck in a log
|
|
572
|
+
* loop must not fill the disk. */
|
|
573
|
+
const MAX_STDERR_LOG_BYTES = 256 * 1024;
|
|
574
|
+
const MAX_STDERR_LINE = 4096;
|
|
575
|
+
/**
|
|
576
|
+
* Copy a launched server's stderr (and the bridge's own) to the gateway log, line by
|
|
577
|
+
* line as it arrives. Claude Code used to keep it in its per-server MCP log; with the
|
|
578
|
+
* server running under the gateway, this log is where it now goes.
|
|
579
|
+
*/
|
|
580
|
+
function logBridgeStderr(bridge, prefix) {
|
|
581
|
+
let partial = '';
|
|
582
|
+
let logged = 0;
|
|
583
|
+
const emit = (line) => {
|
|
584
|
+
if (!line.trim() || logged > MAX_STDERR_LOG_BYTES)
|
|
585
|
+
return;
|
|
586
|
+
logged += line.length;
|
|
587
|
+
console.error(`${prefix} stderr: ${line.slice(0, MAX_STDERR_LINE)}`);
|
|
588
|
+
if (logged > MAX_STDERR_LOG_BYTES)
|
|
589
|
+
console.error(`${prefix} stderr: further output suppressed`);
|
|
590
|
+
};
|
|
591
|
+
bridge.stderr.on('data', (chunk) => {
|
|
592
|
+
const lines = (partial + chunk.toString('utf8')).split('\n');
|
|
593
|
+
partial = lines.pop() ?? '';
|
|
594
|
+
if (partial.length > MAX_STDERR_LINE) {
|
|
595
|
+
emit(partial);
|
|
596
|
+
partial = '';
|
|
597
|
+
}
|
|
598
|
+
for (const line of lines)
|
|
599
|
+
emit(line);
|
|
600
|
+
});
|
|
601
|
+
bridge.stderr.on('end', () => { emit(partial); partial = ''; });
|
|
602
|
+
}
|
|
603
|
+
function startBridge(grant, grantToken, id, server, preamble, rest, socket) {
|
|
604
|
+
if (grant.revoked) {
|
|
605
|
+
socket.destroy();
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
const clean = grant.cleanEnv;
|
|
609
|
+
// Connector env is explicit connector configuration and remains available. Only
|
|
610
|
+
// the launcher's inherited environment is unrelated agent/process state.
|
|
611
|
+
const config = server;
|
|
612
|
+
const authPath = `${exports.CONNECTOR_PROXY_PATH}/${encodeURIComponent(id)}/authorize`;
|
|
613
|
+
const spec = {
|
|
614
|
+
config,
|
|
615
|
+
cwd: typeof preamble.cwd === 'string' && preamble.cwd.startsWith('/') ? preamble.cwd : undefined,
|
|
616
|
+
env: clean(stringEnv(preamble.env)),
|
|
617
|
+
authorization: grant.container
|
|
618
|
+
? { socketPath: path.join(grant.container.containerDir, path.basename(grant.socket?.file ?? '')), path: authPath, grant: grantToken }
|
|
619
|
+
: { url: `${proxyBaseUrl()}${authPath}`, grant: grantToken },
|
|
620
|
+
};
|
|
621
|
+
const bridge = grant.container
|
|
622
|
+
? (0, child_process_1.spawn)('docker', ['exec', '-i', '--user', String((0, os_1.userInfo)().uid), grant.container.container, 'node', '-e', CONTAINER_BRIDGE_LOADER], { stdio: ['pipe', 'pipe', 'pipe'] })
|
|
623
|
+
// Its own environment carries nothing of the gateway's: the server's comes from the spec.
|
|
624
|
+
: (0, child_process_1.spawn)('bun', [BRIDGE], { env: { PATH: process.env.PATH ?? '', HOME: process.env.HOME ?? '' }, stdio: ['pipe', 'pipe', 'pipe'] });
|
|
625
|
+
grant.bridges.add(bridge);
|
|
626
|
+
logBridgeStderr(bridge, `connector-proxy: agent=${grant.agentId} connector=${id}`);
|
|
627
|
+
bridge.stdin.on('error', () => { });
|
|
628
|
+
if (grant.container)
|
|
629
|
+
bridge.stdin.write(JSON.stringify(grant.container.bridgeCode) + '\n');
|
|
630
|
+
bridge.stdin.write(JSON.stringify(spec) + '\n');
|
|
631
|
+
if (rest.length)
|
|
632
|
+
bridge.stdin.write(rest);
|
|
633
|
+
socket.pipe(bridge.stdin);
|
|
634
|
+
socket.resume();
|
|
635
|
+
bridge.stdout.pipe(socket);
|
|
636
|
+
bridge.on('error', err => { console.error(`connector-proxy: connector=${id} bridge failed: ${err.message}`); socket.destroy(); });
|
|
637
|
+
bridge.on('exit', (code, signal) => {
|
|
638
|
+
grant.bridges.delete(bridge);
|
|
639
|
+
if (code)
|
|
640
|
+
console.error(`connector-proxy: agent=${grant.agentId} connector=${id} exited code=${code}`);
|
|
641
|
+
else if (signal && signal !== 'SIGTERM')
|
|
642
|
+
console.error(`connector-proxy: connector=${id} killed by ${signal}`);
|
|
643
|
+
});
|
|
644
|
+
bridge.stdout.on('end', () => socket.end());
|
|
645
|
+
// The launcher went away (its client stopped the server): end the bridge the same way.
|
|
646
|
+
socket.on('close', () => {
|
|
647
|
+
bridge.stdin.end();
|
|
648
|
+
setTimeout(() => { if (bridge.exitCode === null)
|
|
649
|
+
bridge.kill('SIGTERM'); }, 2000).unref();
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
//# sourceMappingURL=proxy.js.map
|