@lobu/gateway 3.0.5 → 3.0.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.
Files changed (175) hide show
  1. package/package.json +2 -2
  2. package/src/__tests__/agent-config-routes.test.ts +254 -0
  3. package/src/__tests__/agent-history-routes.test.ts +72 -0
  4. package/src/__tests__/agent-routes.test.ts +68 -0
  5. package/src/__tests__/agent-schedules-routes.test.ts +59 -0
  6. package/src/__tests__/agent-settings-store.test.ts +323 -0
  7. package/src/__tests__/chat-instance-manager-slack.test.ts +204 -0
  8. package/src/__tests__/chat-response-bridge.test.ts +131 -0
  9. package/src/__tests__/config-memory-plugins.test.ts +92 -0
  10. package/src/__tests__/config-request-store.test.ts +127 -0
  11. package/src/__tests__/connection-routes.test.ts +144 -0
  12. package/src/__tests__/core-services-store-selection.test.ts +92 -0
  13. package/src/__tests__/docker-deployment.test.ts +1211 -0
  14. package/src/__tests__/embedded-deployment.test.ts +342 -0
  15. package/src/__tests__/grant-store.test.ts +148 -0
  16. package/src/__tests__/http-proxy.test.ts +281 -0
  17. package/src/__tests__/instruction-service.test.ts +37 -0
  18. package/src/__tests__/link-buttons.test.ts +112 -0
  19. package/src/__tests__/lobu.test.ts +32 -0
  20. package/src/__tests__/mcp-config-service.test.ts +347 -0
  21. package/src/__tests__/mcp-proxy.test.ts +696 -0
  22. package/src/__tests__/message-handler-bridge.test.ts +17 -0
  23. package/src/__tests__/model-selection.test.ts +172 -0
  24. package/src/__tests__/oauth-templates.test.ts +39 -0
  25. package/src/__tests__/platform-adapter-slack-send.test.ts +114 -0
  26. package/src/__tests__/platform-helpers-model-resolution.test.ts +253 -0
  27. package/src/__tests__/provider-inheritance.test.ts +212 -0
  28. package/src/__tests__/routes/cli-auth.test.ts +337 -0
  29. package/src/__tests__/routes/interactions.test.ts +121 -0
  30. package/src/__tests__/secret-proxy.test.ts +85 -0
  31. package/src/__tests__/session-manager.test.ts +572 -0
  32. package/src/__tests__/setup.ts +133 -0
  33. package/src/__tests__/skill-and-mcp-registry.test.ts +203 -0
  34. package/src/__tests__/slack-routes.test.ts +161 -0
  35. package/src/__tests__/system-config-resolver.test.ts +75 -0
  36. package/src/__tests__/system-message-limiter.test.ts +89 -0
  37. package/src/__tests__/system-skills-service.test.ts +362 -0
  38. package/src/__tests__/transcription-service.test.ts +222 -0
  39. package/src/__tests__/utils/rate-limiter.test.ts +102 -0
  40. package/src/__tests__/worker-connection-manager.test.ts +497 -0
  41. package/src/__tests__/worker-job-router.test.ts +722 -0
  42. package/src/api/index.ts +1 -0
  43. package/src/api/platform.ts +292 -0
  44. package/src/api/response-renderer.ts +157 -0
  45. package/src/auth/agent-metadata-store.ts +168 -0
  46. package/src/auth/api-auth-middleware.ts +69 -0
  47. package/src/auth/api-key-provider-module.ts +213 -0
  48. package/src/auth/base-provider-module.ts +201 -0
  49. package/src/auth/chatgpt/chatgpt-oauth-module.ts +185 -0
  50. package/src/auth/chatgpt/device-code-client.ts +218 -0
  51. package/src/auth/chatgpt/index.ts +1 -0
  52. package/src/auth/claude/oauth-module.ts +280 -0
  53. package/src/auth/cli/token-service.ts +249 -0
  54. package/src/auth/external/client.ts +560 -0
  55. package/src/auth/external/device-code-client.ts +225 -0
  56. package/src/auth/mcp/config-service.ts +392 -0
  57. package/src/auth/mcp/proxy.ts +1088 -0
  58. package/src/auth/mcp/string-substitution.ts +17 -0
  59. package/src/auth/mcp/tool-cache.ts +90 -0
  60. package/src/auth/oauth/base-client.ts +267 -0
  61. package/src/auth/oauth/client.ts +153 -0
  62. package/src/auth/oauth/credentials.ts +7 -0
  63. package/src/auth/oauth/providers.ts +69 -0
  64. package/src/auth/oauth/state-store.ts +150 -0
  65. package/src/auth/oauth-templates.ts +179 -0
  66. package/src/auth/provider-catalog.ts +220 -0
  67. package/src/auth/provider-model-options.ts +41 -0
  68. package/src/auth/settings/agent-settings-store.ts +565 -0
  69. package/src/auth/settings/auth-profiles-manager.ts +216 -0
  70. package/src/auth/settings/index.ts +12 -0
  71. package/src/auth/settings/model-preference-store.ts +52 -0
  72. package/src/auth/settings/model-selection.ts +135 -0
  73. package/src/auth/settings/resolved-settings-view.ts +298 -0
  74. package/src/auth/settings/template-utils.ts +44 -0
  75. package/src/auth/settings/token-service.ts +88 -0
  76. package/src/auth/system-env-store.ts +98 -0
  77. package/src/auth/user-agents-store.ts +68 -0
  78. package/src/channels/binding-service.ts +214 -0
  79. package/src/channels/index.ts +4 -0
  80. package/src/cli/gateway.ts +1304 -0
  81. package/src/cli/index.ts +74 -0
  82. package/src/commands/built-in-commands.ts +80 -0
  83. package/src/commands/command-dispatcher.ts +94 -0
  84. package/src/commands/command-reply-adapters.ts +27 -0
  85. package/src/config/file-loader.ts +618 -0
  86. package/src/config/index.ts +588 -0
  87. package/src/config/network-allowlist.ts +71 -0
  88. package/src/connections/chat-instance-manager.ts +1284 -0
  89. package/src/connections/chat-response-bridge.ts +618 -0
  90. package/src/connections/index.ts +7 -0
  91. package/src/connections/interaction-bridge.ts +831 -0
  92. package/src/connections/message-handler-bridge.ts +415 -0
  93. package/src/connections/platform-auth-methods.ts +15 -0
  94. package/src/connections/types.ts +84 -0
  95. package/src/gateway/connection-manager.ts +291 -0
  96. package/src/gateway/index.ts +700 -0
  97. package/src/gateway/job-router.ts +201 -0
  98. package/src/gateway-main.ts +200 -0
  99. package/src/index.ts +41 -0
  100. package/src/infrastructure/queue/index.ts +12 -0
  101. package/src/infrastructure/queue/queue-producer.ts +148 -0
  102. package/src/infrastructure/queue/redis-queue.ts +361 -0
  103. package/src/infrastructure/queue/types.ts +133 -0
  104. package/src/infrastructure/redis/system-message-limiter.ts +94 -0
  105. package/src/interactions/config-request-store.ts +198 -0
  106. package/src/interactions.ts +363 -0
  107. package/src/lobu.ts +311 -0
  108. package/src/metrics/prometheus.ts +159 -0
  109. package/src/modules/module-system.ts +179 -0
  110. package/src/orchestration/base-deployment-manager.ts +900 -0
  111. package/src/orchestration/deployment-utils.ts +98 -0
  112. package/src/orchestration/impl/docker-deployment.ts +620 -0
  113. package/src/orchestration/impl/embedded-deployment.ts +268 -0
  114. package/src/orchestration/impl/index.ts +8 -0
  115. package/src/orchestration/impl/k8s/deployment.ts +1061 -0
  116. package/src/orchestration/impl/k8s/helpers.ts +610 -0
  117. package/src/orchestration/impl/k8s/index.ts +1 -0
  118. package/src/orchestration/index.ts +333 -0
  119. package/src/orchestration/message-consumer.ts +584 -0
  120. package/src/orchestration/scheduled-wakeup.ts +704 -0
  121. package/src/permissions/approval-policy.ts +36 -0
  122. package/src/permissions/grant-store.ts +219 -0
  123. package/src/platform/file-handler.ts +66 -0
  124. package/src/platform/link-buttons.ts +57 -0
  125. package/src/platform/renderer-utils.ts +44 -0
  126. package/src/platform/response-renderer.ts +84 -0
  127. package/src/platform/unified-thread-consumer.ts +187 -0
  128. package/src/platform.ts +318 -0
  129. package/src/proxy/http-proxy.ts +752 -0
  130. package/src/proxy/proxy-manager.ts +81 -0
  131. package/src/proxy/secret-proxy.ts +402 -0
  132. package/src/proxy/token-refresh-job.ts +143 -0
  133. package/src/routes/internal/audio.ts +141 -0
  134. package/src/routes/internal/device-auth.ts +566 -0
  135. package/src/routes/internal/files.ts +226 -0
  136. package/src/routes/internal/history.ts +69 -0
  137. package/src/routes/internal/images.ts +127 -0
  138. package/src/routes/internal/interactions.ts +84 -0
  139. package/src/routes/internal/middleware.ts +23 -0
  140. package/src/routes/internal/schedule.ts +226 -0
  141. package/src/routes/internal/types.ts +22 -0
  142. package/src/routes/openapi-auto.ts +239 -0
  143. package/src/routes/public/agent-access.ts +23 -0
  144. package/src/routes/public/agent-config.ts +675 -0
  145. package/src/routes/public/agent-history.ts +422 -0
  146. package/src/routes/public/agent-schedules.ts +296 -0
  147. package/src/routes/public/agent.ts +1086 -0
  148. package/src/routes/public/agents.ts +373 -0
  149. package/src/routes/public/channels.ts +191 -0
  150. package/src/routes/public/cli-auth.ts +883 -0
  151. package/src/routes/public/connections.ts +574 -0
  152. package/src/routes/public/landing.ts +16 -0
  153. package/src/routes/public/oauth.ts +147 -0
  154. package/src/routes/public/settings-auth.ts +104 -0
  155. package/src/routes/public/slack.ts +173 -0
  156. package/src/routes/shared/agent-ownership.ts +101 -0
  157. package/src/routes/shared/token-verifier.ts +34 -0
  158. package/src/services/core-services.ts +1053 -0
  159. package/src/services/image-generation-service.ts +257 -0
  160. package/src/services/instruction-service.ts +318 -0
  161. package/src/services/mcp-registry.ts +94 -0
  162. package/src/services/platform-helpers.ts +287 -0
  163. package/src/services/session-manager.ts +262 -0
  164. package/src/services/settings-resolver.ts +74 -0
  165. package/src/services/system-config-resolver.ts +90 -0
  166. package/src/services/system-skills-service.ts +229 -0
  167. package/src/services/transcription-service.ts +684 -0
  168. package/src/session.ts +110 -0
  169. package/src/spaces/index.ts +1 -0
  170. package/src/spaces/space-resolver.ts +17 -0
  171. package/src/stores/in-memory-agent-store.ts +403 -0
  172. package/src/stores/redis-agent-store.ts +279 -0
  173. package/src/utils/public-url.ts +44 -0
  174. package/src/utils/rate-limiter.ts +94 -0
  175. package/tsconfig.json +33 -0
@@ -0,0 +1,752 @@
1
+ import crypto from "node:crypto";
2
+ import type { LookupAddress } from "node:dns";
3
+ import * as dns from "node:dns/promises";
4
+ import * as http from "node:http";
5
+ import * as net from "node:net";
6
+ import { URL } from "node:url";
7
+ import type { WorkerTokenData } from "@lobu/core";
8
+ import { createLogger, verifyWorkerToken } from "@lobu/core";
9
+ import {
10
+ isUnrestrictedMode,
11
+ loadAllowedDomains,
12
+ loadDisallowedDomains,
13
+ } from "../config/network-allowlist";
14
+ import type { GrantStore } from "../permissions/grant-store";
15
+
16
+ const logger = createLogger("http-proxy");
17
+
18
+ interface ResolvedNetworkConfig {
19
+ allowedDomains: string[];
20
+ deniedDomains: string[];
21
+ }
22
+
23
+ interface TargetResolutionResult {
24
+ ok: boolean;
25
+ resolvedIp?: string;
26
+ statusCode?: number;
27
+ clientMessage?: string;
28
+ reason?: string;
29
+ }
30
+
31
+ const blockedIpv4Ranges: ReadonlyArray<readonly [string, number]> = [
32
+ ["0.0.0.0", 8],
33
+ ["10.0.0.0", 8],
34
+ ["100.64.0.0", 10],
35
+ ["127.0.0.0", 8],
36
+ ["169.254.0.0", 16],
37
+ ["172.16.0.0", 12],
38
+ ["192.168.0.0", 16],
39
+ ["198.18.0.0", 15],
40
+ ["224.0.0.0", 4],
41
+ ["240.0.0.0", 4],
42
+ ];
43
+
44
+ const blockedIpv6Ranges: ReadonlyArray<readonly [string, number]> = [
45
+ ["fc00::", 7],
46
+ ["fe80::", 10],
47
+ ["ff00::", 8],
48
+ ];
49
+
50
+ const blockedIpv4List = new net.BlockList();
51
+ for (const [address, prefix] of blockedIpv4Ranges) {
52
+ blockedIpv4List.addSubnet(address, prefix, "ipv4");
53
+ }
54
+
55
+ const blockedIpv6List = new net.BlockList();
56
+ blockedIpv6List.addAddress("::", "ipv6");
57
+ blockedIpv6List.addAddress("::1", "ipv6");
58
+ for (const [address, prefix] of blockedIpv6Ranges) {
59
+ blockedIpv6List.addSubnet(address, prefix, "ipv6");
60
+ }
61
+
62
+ // Cache for global defaults (used when no deployment identified)
63
+ let globalConfig: ResolvedNetworkConfig | null = null;
64
+
65
+ // Module-level grant store reference for domain grant checks
66
+ let proxyGrantStore: GrantStore | null = null;
67
+
68
+ /**
69
+ * Set the grant store for the HTTP proxy to check domain grants.
70
+ * Called during gateway initialization.
71
+ */
72
+ export function setProxyGrantStore(store: GrantStore): void {
73
+ proxyGrantStore = store;
74
+ }
75
+
76
+ /**
77
+ * Get global network config (lazy loaded)
78
+ */
79
+ function getGlobalConfig(): ResolvedNetworkConfig {
80
+ if (!globalConfig) {
81
+ globalConfig = {
82
+ allowedDomains: loadAllowedDomains(),
83
+ deniedDomains: loadDisallowedDomains(),
84
+ };
85
+ }
86
+ return globalConfig;
87
+ }
88
+
89
+ /**
90
+ * Unified domain access check: global config → grant store.
91
+ *
92
+ * 1. If denied by global blocklist → block
93
+ * 2. If allowed by global allowlist → check grantStore.isDenied() → allow/block
94
+ * 3. If not in global list → check grantStore.hasGrant() → allow/block
95
+ */
96
+ async function checkDomainAccess(
97
+ hostname: string,
98
+ agentId: string | undefined
99
+ ): Promise<boolean> {
100
+ const global = getGlobalConfig();
101
+
102
+ // Global blocklist always takes precedence
103
+ if (
104
+ global.deniedDomains.length > 0 &&
105
+ matchesDomainPattern(hostname, global.deniedDomains)
106
+ ) {
107
+ return false;
108
+ }
109
+
110
+ // Check if globally allowed (unrestricted or in allowlist)
111
+ const globallyAllowed = isHostnameAllowed(
112
+ hostname,
113
+ global.allowedDomains,
114
+ global.deniedDomains
115
+ );
116
+
117
+ if (globallyAllowed) {
118
+ // Even if globally allowed, a per-agent deny grant can override
119
+ if (proxyGrantStore && agentId) {
120
+ const denied = await proxyGrantStore.isDenied(agentId, hostname);
121
+ if (denied) {
122
+ logger.debug(`Domain ${hostname} denied via grant (agent: ${agentId})`);
123
+ return false;
124
+ }
125
+ }
126
+ return true;
127
+ }
128
+
129
+ // Not globally allowed — check grant store for per-agent access
130
+ if (proxyGrantStore && agentId) {
131
+ const granted = await proxyGrantStore.hasGrant(agentId, hostname);
132
+ if (granted) {
133
+ logger.debug(`Domain ${hostname} allowed via grant (agent: ${agentId})`);
134
+ return true;
135
+ }
136
+ }
137
+
138
+ return false;
139
+ }
140
+
141
+ interface ProxyCredentials {
142
+ deploymentName: string;
143
+ token: string;
144
+ }
145
+
146
+ function parseMappedIpv4Address(ip: string): string | null {
147
+ const normalized = ip.toLowerCase();
148
+ if (!normalized.startsWith("::ffff:")) {
149
+ return null;
150
+ }
151
+
152
+ const mapped = normalized.substring("::ffff:".length);
153
+ return net.isIP(mapped) === 4 ? mapped : null;
154
+ }
155
+
156
+ function parseMappedIpv4HexAddress(ip: string): string | null {
157
+ const normalized = ip.toLowerCase();
158
+ if (!normalized.startsWith("::ffff:")) {
159
+ return null;
160
+ }
161
+
162
+ const mapped = normalized.substring("::ffff:".length);
163
+ if (mapped.includes(".")) {
164
+ return null;
165
+ }
166
+
167
+ const parts = mapped.split(":");
168
+ if (parts.length !== 2) {
169
+ return null;
170
+ }
171
+
172
+ const high = Number.parseInt(parts[0] || "", 16);
173
+ const low = Number.parseInt(parts[1] || "", 16);
174
+ if (
175
+ Number.isNaN(high) ||
176
+ Number.isNaN(low) ||
177
+ high < 0 ||
178
+ high > 0xffff ||
179
+ low < 0 ||
180
+ low > 0xffff
181
+ ) {
182
+ return null;
183
+ }
184
+
185
+ return `${(high >> 8) & 0xff}.${high & 0xff}.${(low >> 8) & 0xff}.${low & 0xff}`;
186
+ }
187
+
188
+ function isBlockedIpAddress(ip: string): boolean {
189
+ const ipv6WithoutZone = ip.split("%", 1)[0] || ip;
190
+ const mappedIpv4 =
191
+ parseMappedIpv4Address(ipv6WithoutZone) ||
192
+ parseMappedIpv4HexAddress(ipv6WithoutZone);
193
+ if (mappedIpv4) {
194
+ return blockedIpv4List.check(mappedIpv4, "ipv4");
195
+ }
196
+
197
+ const family = net.isIP(ipv6WithoutZone);
198
+ if (family === 4) {
199
+ return blockedIpv4List.check(ipv6WithoutZone, "ipv4");
200
+ }
201
+ if (family === 6) {
202
+ return blockedIpv6List.check(ipv6WithoutZone, "ipv6");
203
+ }
204
+ return false;
205
+ }
206
+
207
+ export const __testOnly = {
208
+ isBlockedIpAddress,
209
+ };
210
+
211
+ async function resolveAndValidateTarget(
212
+ hostname: string
213
+ ): Promise<TargetResolutionResult> {
214
+ const ipFamily = net.isIP(hostname);
215
+ if (ipFamily !== 0) {
216
+ if (isBlockedIpAddress(hostname)) {
217
+ return {
218
+ ok: false,
219
+ statusCode: 403,
220
+ clientMessage: `403 Forbidden - Target IP not allowed: ${hostname}`,
221
+ reason: `target is local/private IP (${hostname})`,
222
+ };
223
+ }
224
+ return { ok: true, resolvedIp: hostname };
225
+ }
226
+
227
+ let addresses: LookupAddress[];
228
+ try {
229
+ addresses = await dns.lookup(hostname, { all: true, verbatim: true });
230
+ } catch (error) {
231
+ const message = error instanceof Error ? error.message : "unknown error";
232
+ return {
233
+ ok: false,
234
+ statusCode: 502,
235
+ clientMessage: `Bad Gateway: Could not resolve target host ${hostname}`,
236
+ reason: `DNS lookup failed for ${hostname}: ${message}`,
237
+ };
238
+ }
239
+
240
+ if (addresses.length === 0) {
241
+ return {
242
+ ok: false,
243
+ statusCode: 502,
244
+ clientMessage: `Bad Gateway: No DNS results for ${hostname}`,
245
+ reason: `DNS lookup returned no addresses for ${hostname}`,
246
+ };
247
+ }
248
+
249
+ const blockedAddress = addresses.find((addr) =>
250
+ isBlockedIpAddress(addr.address)
251
+ );
252
+ if (blockedAddress) {
253
+ return {
254
+ ok: false,
255
+ statusCode: 403,
256
+ clientMessage: `403 Forbidden - Target resolves to local/private IP: ${hostname}`,
257
+ reason: `${hostname} resolved to blocked IP ${blockedAddress.address}`,
258
+ };
259
+ }
260
+
261
+ return { ok: true, resolvedIp: addresses[0]?.address };
262
+ }
263
+
264
+ /**
265
+ * Extract deployment name and token from Proxy-Authorization Basic auth header.
266
+ * Workers send: HTTP_PROXY=http://<deploymentName>:<token>@gateway:8118
267
+ * This creates a Basic auth header with username=deploymentName, password=token
268
+ */
269
+ function extractProxyCredentials(
270
+ req: http.IncomingMessage
271
+ ): ProxyCredentials | null {
272
+ const authHeader = req.headers["proxy-authorization"];
273
+ if (!authHeader || typeof authHeader !== "string") {
274
+ return null;
275
+ }
276
+
277
+ // Parse Basic auth: "Basic base64(username:password)"
278
+ const match = authHeader.match(/^Basic\s+(.+)$/i);
279
+ if (!match || !match[1]) {
280
+ return null;
281
+ }
282
+
283
+ try {
284
+ const decoded = Buffer.from(match[1], "base64").toString("utf-8");
285
+ const colonIndex = decoded.indexOf(":");
286
+ if (colonIndex === -1) {
287
+ return null;
288
+ }
289
+ const deploymentName = decoded.substring(0, colonIndex);
290
+ const token = decoded.substring(colonIndex + 1);
291
+ if (!deploymentName || !token) {
292
+ return null;
293
+ }
294
+ return { deploymentName, token };
295
+ } catch {
296
+ return null;
297
+ }
298
+ }
299
+
300
+ interface ValidatedProxy {
301
+ deploymentName: string;
302
+ tokenData: WorkerTokenData;
303
+ }
304
+
305
+ /**
306
+ * Validate proxy authentication by verifying the encrypted worker token
307
+ * and cross-checking the claimed deployment name.
308
+ */
309
+ function validateProxyAuth(req: http.IncomingMessage): ValidatedProxy | null {
310
+ const creds = extractProxyCredentials(req);
311
+ if (!creds) {
312
+ return null;
313
+ }
314
+
315
+ const tokenData = verifyWorkerToken(creds.token);
316
+ if (!tokenData) {
317
+ logger.warn(
318
+ `Proxy auth failed: invalid token (claimed deployment: ${creds.deploymentName})`
319
+ );
320
+ return null;
321
+ }
322
+
323
+ const deploymentMatch =
324
+ tokenData.deploymentName.length === creds.deploymentName.length &&
325
+ crypto.timingSafeEqual(
326
+ Buffer.from(tokenData.deploymentName),
327
+ Buffer.from(creds.deploymentName)
328
+ );
329
+ if (!deploymentMatch) {
330
+ logger.warn(
331
+ `Proxy auth failed: deployment mismatch (claimed: ${creds.deploymentName}, token: ${tokenData.deploymentName})`
332
+ );
333
+ return null;
334
+ }
335
+
336
+ return { deploymentName: creds.deploymentName, tokenData };
337
+ }
338
+
339
+ /**
340
+ * Check if a hostname matches any domain patterns
341
+ * Supports exact matches and wildcard patterns (.example.com matches *.example.com)
342
+ */
343
+ function matchesDomainPattern(hostname: string, patterns: string[]): boolean {
344
+ const lowerHostname = hostname.toLowerCase();
345
+
346
+ for (const pattern of patterns) {
347
+ const lowerPattern = pattern.toLowerCase();
348
+
349
+ if (lowerPattern.startsWith(".")) {
350
+ // Wildcard pattern: .example.com matches *.example.com
351
+ const domain = lowerPattern.substring(1);
352
+ if (lowerHostname === domain || lowerHostname.endsWith(`.${domain}`)) {
353
+ return true;
354
+ }
355
+ } else if (lowerPattern === lowerHostname) {
356
+ // Exact match
357
+ return true;
358
+ }
359
+ }
360
+
361
+ return false;
362
+ }
363
+
364
+ /**
365
+ * Check if a hostname is allowed based on allowlist/blocklist configuration.
366
+ * Rules:
367
+ * - deniedDomains are checked first (take precedence)
368
+ * - allowedDomains are checked second
369
+ * - If allowedDomains contains "*", unrestricted mode is enabled
370
+ * - If allowedDomains is empty, complete isolation (deny all)
371
+ */
372
+ function isHostnameAllowed(
373
+ hostname: string,
374
+ allowedDomains: string[],
375
+ deniedDomains: string[]
376
+ ): boolean {
377
+ // Unrestricted mode - allow all except explicitly disallowed
378
+ if (isUnrestrictedMode(allowedDomains)) {
379
+ if (deniedDomains.length === 0) {
380
+ return true; // No blocklist, allow all
381
+ }
382
+ return !matchesDomainPattern(hostname, deniedDomains);
383
+ }
384
+
385
+ // Complete isolation mode - deny all
386
+ if (allowedDomains.length === 0) {
387
+ return false;
388
+ }
389
+
390
+ // Allowlist mode - check if allowed
391
+ const isAllowed = matchesDomainPattern(hostname, allowedDomains);
392
+
393
+ // Even if allowed, check blocklist
394
+ if (isAllowed && deniedDomains.length > 0) {
395
+ return !matchesDomainPattern(hostname, deniedDomains);
396
+ }
397
+
398
+ return isAllowed;
399
+ }
400
+
401
+ /**
402
+ * Extract hostname from CONNECT request
403
+ */
404
+ function extractConnectHostname(url: string): string | null {
405
+ // CONNECT requests are in format: "host:port"
406
+ const match = url.match(/^([^:]+):\d+$/);
407
+ return match?.[1] ? match[1] : null;
408
+ }
409
+
410
+ /**
411
+ * Handle HTTPS CONNECT tunneling with per-deployment network config
412
+ */
413
+ async function handleConnect(
414
+ req: http.IncomingMessage,
415
+ clientSocket: import("stream").Duplex,
416
+ head: Buffer
417
+ ): Promise<void> {
418
+ const url = req.url || "";
419
+ const hostname = extractConnectHostname(url);
420
+
421
+ if (!hostname) {
422
+ logger.warn(`Invalid CONNECT request: ${url}`);
423
+ clientSocket.write("HTTP/1.1 400 Bad Request\r\n\r\n");
424
+ clientSocket.end();
425
+ return;
426
+ }
427
+
428
+ // Validate worker token
429
+ const auth = validateProxyAuth(req);
430
+ if (!auth) {
431
+ logger.warn(`Proxy auth required for CONNECT to ${hostname}`);
432
+ try {
433
+ clientSocket.write(
434
+ 'HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic realm="lobu-proxy"\r\n\r\n'
435
+ );
436
+ clientSocket.end();
437
+ } catch {
438
+ // Client may have already disconnected
439
+ }
440
+ return;
441
+ }
442
+
443
+ const { deploymentName, tokenData } = auth;
444
+
445
+ // Check domain access: global config → grant store
446
+ const allowed = await checkDomainAccess(hostname, tokenData.agentId);
447
+ if (!allowed) {
448
+ logger.warn(
449
+ `Blocked CONNECT to ${hostname} (deployment: ${deploymentName})`
450
+ );
451
+ try {
452
+ clientSocket.write(
453
+ `HTTP/1.1 403 Domain not allowed: ${hostname}. Network access is configured via lobu.toml or the gateway configuration APIs.\r\nContent-Type: text/plain\r\n\r\n403 Forbidden - Domain not allowed: ${hostname}. Network access is configured via lobu.toml or the gateway configuration APIs.\r\n`
454
+ );
455
+ clientSocket.end();
456
+ } catch {
457
+ // Client may have already disconnected
458
+ }
459
+ return;
460
+ }
461
+
462
+ const targetResolution = await resolveAndValidateTarget(hostname);
463
+ if (!targetResolution.ok) {
464
+ logger.warn(
465
+ `Blocked CONNECT to ${hostname} (deployment: ${deploymentName}) - ${targetResolution.reason}`
466
+ );
467
+ try {
468
+ clientSocket.write(
469
+ `HTTP/1.1 ${targetResolution.statusCode} ${
470
+ targetResolution.statusCode === 403 ? "Forbidden" : "Bad Gateway"
471
+ }\r\nContent-Type: text/plain\r\n\r\n${targetResolution.clientMessage}\r\n`
472
+ );
473
+ clientSocket.end();
474
+ } catch {
475
+ // Client may have already disconnected
476
+ }
477
+ return;
478
+ }
479
+
480
+ const resolvedIp = targetResolution.resolvedIp;
481
+ if (!resolvedIp) {
482
+ clientSocket.write("HTTP/1.1 500 Internal Server Error\r\n\r\n");
483
+ clientSocket.end();
484
+ return;
485
+ }
486
+
487
+ logger.debug(`Allowing CONNECT to ${hostname} via ${resolvedIp}`);
488
+
489
+ // Parse host and port
490
+ const [host, portStr] = url.split(":");
491
+ const port = portStr ? parseInt(portStr, 10) || 443 : 443;
492
+
493
+ if (!host) {
494
+ logger.warn(`Invalid CONNECT host: ${url}`);
495
+ clientSocket.write("HTTP/1.1 400 Bad Request\r\n\r\n");
496
+ clientSocket.end();
497
+ return;
498
+ }
499
+
500
+ // Establish connection to target
501
+ const targetSocket = net.connect(port, resolvedIp, () => {
502
+ // Send success response to client
503
+ clientSocket.write("HTTP/1.1 200 Connection Established\r\n\r\n");
504
+
505
+ // Pipe the connection bidirectionally
506
+ targetSocket.write(head);
507
+ targetSocket.pipe(clientSocket);
508
+ clientSocket.pipe(targetSocket);
509
+ });
510
+
511
+ targetSocket.on("error", (err) => {
512
+ logger.debug(`Target connection error for ${hostname}: ${err.message}`);
513
+ try {
514
+ clientSocket.end();
515
+ } catch {
516
+ // Ignore errors when closing already-closed socket
517
+ }
518
+ });
519
+
520
+ clientSocket.on("error", (err) => {
521
+ // ECONNRESET is common when clients drop connections - don't log as error
522
+ if ((err as NodeJS.ErrnoException).code === "ECONNRESET") {
523
+ logger.debug(`Client disconnected for ${hostname} (ECONNRESET)`);
524
+ } else {
525
+ logger.debug(`Client connection error for ${hostname}: ${err.message}`);
526
+ }
527
+ try {
528
+ targetSocket.end();
529
+ } catch {
530
+ // Ignore errors when closing already-closed socket
531
+ }
532
+ });
533
+
534
+ // Handle close events to clean up
535
+ targetSocket.on("close", () => {
536
+ try {
537
+ clientSocket.end();
538
+ } catch {
539
+ // Ignore
540
+ }
541
+ });
542
+
543
+ clientSocket.on("close", () => {
544
+ try {
545
+ targetSocket.end();
546
+ } catch {
547
+ // Ignore
548
+ }
549
+ });
550
+ }
551
+
552
+ /**
553
+ * Handle regular HTTP proxy requests with per-deployment network config
554
+ */
555
+ async function handleProxyRequest(
556
+ req: http.IncomingMessage,
557
+ res: http.ServerResponse
558
+ ): Promise<void> {
559
+ const targetUrl = req.url;
560
+
561
+ if (!targetUrl) {
562
+ res.writeHead(400, { "Content-Type": "text/plain" });
563
+ res.end("Bad Request: No URL provided\n");
564
+ return;
565
+ }
566
+
567
+ let parsedUrl: URL;
568
+ try {
569
+ parsedUrl = new URL(targetUrl);
570
+ } catch {
571
+ res.writeHead(400, { "Content-Type": "text/plain" });
572
+ res.end("Bad Request: Invalid URL\n");
573
+ return;
574
+ }
575
+
576
+ const hostname = parsedUrl.hostname;
577
+
578
+ // Validate worker token
579
+ const auth = validateProxyAuth(req);
580
+ if (!auth) {
581
+ logger.warn(`Proxy auth required for ${req.method} ${hostname}`);
582
+ res.writeHead(407, {
583
+ "Content-Type": "text/plain",
584
+ "Proxy-Authenticate": 'Basic realm="lobu-proxy"',
585
+ });
586
+ res.end("407 Proxy Authentication Required\n");
587
+ return;
588
+ }
589
+
590
+ const { deploymentName, tokenData } = auth;
591
+
592
+ // Check domain access: global config → grant store
593
+ const allowed = await checkDomainAccess(hostname, tokenData.agentId);
594
+ if (!allowed) {
595
+ logger.warn(
596
+ `Blocked request to ${hostname} (deployment: ${deploymentName})`
597
+ );
598
+ res.writeHead(403, `Domain not allowed: ${hostname}`, {
599
+ "Content-Type": "text/plain",
600
+ });
601
+ res.end(
602
+ `403 Forbidden - Domain not allowed: ${hostname}. Network access is configured via lobu.toml or the gateway configuration APIs.\n`
603
+ );
604
+ return;
605
+ }
606
+
607
+ const targetResolution = await resolveAndValidateTarget(hostname);
608
+ if (!targetResolution.ok) {
609
+ logger.warn(
610
+ `Blocked request to ${hostname} (deployment: ${deploymentName}) - ${targetResolution.reason}`
611
+ );
612
+ res.writeHead(targetResolution.statusCode ?? 502, {
613
+ "Content-Type": "text/plain",
614
+ });
615
+ res.end(`${targetResolution.clientMessage}\n`);
616
+ return;
617
+ }
618
+
619
+ const resolvedIp = targetResolution.resolvedIp;
620
+ if (!resolvedIp) {
621
+ res.writeHead(500, { "Content-Type": "text/plain" });
622
+ res.end("Internal proxy error\n");
623
+ return;
624
+ }
625
+
626
+ logger.debug(
627
+ `Proxying ${req.method} ${hostname}${parsedUrl.pathname} via ${resolvedIp}`
628
+ );
629
+
630
+ // Remove proxy-authorization header before forwarding
631
+ const forwardHeaders = { ...req.headers };
632
+ delete forwardHeaders["proxy-authorization"];
633
+
634
+ // Forward the request
635
+ const options: http.RequestOptions = {
636
+ hostname: resolvedIp,
637
+ port: parsedUrl.port || (parsedUrl.protocol === "https:" ? 443 : 80),
638
+ path: parsedUrl.pathname + parsedUrl.search,
639
+ method: req.method,
640
+ headers: forwardHeaders,
641
+ };
642
+
643
+ const proxyReq = http.request(options, (proxyRes) => {
644
+ // Forward response headers
645
+ res.writeHead(proxyRes.statusCode || 500, proxyRes.headers);
646
+ // Stream response body
647
+ proxyRes.pipe(res);
648
+ });
649
+
650
+ proxyReq.on("error", (err) => {
651
+ logger.error(`Proxy request error for ${hostname}:`, err.message);
652
+ if (!res.headersSent) {
653
+ res.writeHead(502, { "Content-Type": "text/plain" });
654
+ res.end("Bad Gateway: Could not reach target server\n");
655
+ } else {
656
+ res.end();
657
+ }
658
+ });
659
+
660
+ // Stream request body
661
+ req.pipe(proxyReq);
662
+ }
663
+
664
+ /**
665
+ * Start HTTP proxy server with per-deployment network config support.
666
+ *
667
+ * Workers identify themselves via Proxy-Authorization Basic auth:
668
+ * HTTP_PROXY=http://<deploymentName>:<token>@gateway:8118
669
+ *
670
+ * The proxy validates the encrypted worker token, cross-checks the
671
+ * claimed deployment name, and looks up per-deployment network config.
672
+ * Returns 407 if authentication fails.
673
+ *
674
+ * @param port - Port to listen on (default 8118)
675
+ * @param host - Bind address (default "::" for all interfaces)
676
+ * @returns Promise that resolves with the server once listening, or rejects on error
677
+ */
678
+ export function startHttpProxy(
679
+ port: number = 8118,
680
+ host: string = "::"
681
+ ): Promise<http.Server> {
682
+ return new Promise((resolve, reject) => {
683
+ const global = getGlobalConfig();
684
+
685
+ const server = http.createServer((req, res) => {
686
+ handleProxyRequest(req, res).catch((err) => {
687
+ logger.error("Error handling proxy request:", err);
688
+ if (!res.headersSent) {
689
+ res.writeHead(500, { "Content-Type": "text/plain" });
690
+ res.end("Internal proxy error\n");
691
+ }
692
+ });
693
+ });
694
+
695
+ // Handle CONNECT method for HTTPS tunneling
696
+ server.on("connect", (req, clientSocket, head) => {
697
+ handleConnect(req, clientSocket, head).catch((err) => {
698
+ logger.error("Error handling CONNECT:", err);
699
+ try {
700
+ clientSocket.write("HTTP/1.1 500 Internal Server Error\r\n\r\n");
701
+ clientSocket.end();
702
+ } catch {
703
+ // Ignore
704
+ }
705
+ });
706
+ });
707
+
708
+ server.on("error", (err) => {
709
+ logger.error("HTTP proxy server error:", err);
710
+ reject(err);
711
+ });
712
+
713
+ server.listen(port, host, () => {
714
+ // Remove the startup error listener so it doesn't reject later operational errors
715
+ server.removeAllListeners("error");
716
+ server.on("error", (err) => {
717
+ logger.error("HTTP proxy server error:", err);
718
+ });
719
+
720
+ let mode: string;
721
+ if (isUnrestrictedMode(global.allowedDomains)) {
722
+ mode = "unrestricted";
723
+ } else if (global.allowedDomains.length > 0) {
724
+ mode = "allowlist";
725
+ } else {
726
+ mode = "complete-isolation";
727
+ }
728
+
729
+ logger.debug(
730
+ `HTTP proxy started on ${host}:${port} (mode=${mode}, allowed=${global.allowedDomains.length}, denied=${global.deniedDomains.length})`
731
+ );
732
+ resolve(server);
733
+ });
734
+ });
735
+ }
736
+
737
+ /**
738
+ * Stop HTTP proxy server
739
+ */
740
+ export function stopHttpProxy(server: http.Server): Promise<void> {
741
+ return new Promise((resolve, reject) => {
742
+ server.close((err) => {
743
+ if (err) {
744
+ logger.error("Error stopping HTTP proxy:", err);
745
+ reject(err);
746
+ } else {
747
+ logger.info("HTTP proxy stopped");
748
+ resolve();
749
+ }
750
+ });
751
+ });
752
+ }