The best way to control Claude Code, Codex and more from any computer or phone
fix(server): log the connections the private-network gate closes (#31647)
Julius Marminge committed
Nov 16, 2026 at 13:11 UTC
ca432d7ed645a8ebd032456b7bac95c0a0590ced
3 files changed
+99
-6
apps/server/src/server.ts
+3
-6
@@ -219,7 +219,7 @@ import * as RunFinalizationService from "./orchestration-v2/RunFinalizationServi
219
import * as ProjectionMaintenance from "./orchestration-v2/ProjectionMaintenance.ts";
220
import * as ProjectionStoreV2 from "./orchestration-v2/ProjectionStore.ts";
221
import { makePersistedServerRuntimeState } from "./serverRuntimeState.ts";
222
-import { acceptsPrivateNetworkConnection, loopbackUrlHostForAddress } from "./startupAccess.ts";
222
+import { closeNonPrivateNetworkConnections, loopbackUrlHostForAddress } from "./startupAccess.ts";
223
import * as OrchestrationHttp from "./orchestration-v2/http.ts";
224
import * as ProjectHttp from "./project/http.ts";
225
import * as NetService from "@t3tools/shared/Net";
@@ -364,17 +364,14 @@ const layerHttpServer = Layer.unwrap(
364
privateNetworkOnly: config.privateNetworkOnly,
365
});
366
yield* ManagedLimits.closeTailscaleServeTo(policy, { host: bindHost, port: config.port });
367
+ const runSync = Effect.runSyncWith(yield* Effect.context<never>());
368
return NodeHttpServer.layer(
369
() => {
370
const server = rejectRequestsUntilServing(
371
keepIdleConnections(guardHttpResponseWriteErrors(NodeHttp.createServer())),
372
);
373
if (config.privateNetworkOnly) {
373
- server.on("connection", (socket) => {
374
- if (!acceptsPrivateNetworkConnection(socket)) {
375
- socket.destroy();
376
- }
377
- });
374
+ runSync(closeNonPrivateNetworkConnections(server));
375
}
376
return server;
377
},
apps/server/src/startupAccess.test.ts
+55
@@ -1,10 +1,17 @@
1
+// @effect-diagnostics nodeBuiltinImport:off
2
+import * as NodeHttp from "node:http";
3
+import * as NodeNet from "node:net";
4
import type * as NodeOS from "node:os";
5
6
import { describe, expect, it } from "@effect/vitest";
7
+import * as Effect from "effect/Effect";
8
+import * as Logger from "effect/Logger";
9
import * as NetAddress from "effect/net/NetAddress";
10
+import * as TestClock from "effect/testing/TestClock";
11
12
import {
13
acceptsPrivateNetworkConnection,
14
+ closeNonPrivateNetworkConnections,
15
loopbackUrlHostForAddress,
16
resolveHeadlessConnectionString,
17
} from "./startupAccess.ts";
@@ -104,3 +111,51 @@ describe("acceptsPrivateNetworkConnection", () => {
111
expect(accepts(undefined, "192.168.1.31")).toBe(false);
112
});
113
});
114
+
115
+describe("closeNonPrivateNetworkConnections", () => {
116
+ it.effect("logs the local and remote address of a connection it closes", () =>
117
+ Effect.gen(function* () {
118
+ const warnings: Array<Logger.Options<unknown>> = [];
119
+ const server = NodeHttp.createServer();
120
+ yield* closeNonPrivateNetworkConnections(server).pipe(
121
+ Effect.provide(
122
+ Logger.layer([Logger.make((entry) => warnings.push(entry))], {
123
+ mergeWithExisting: false,
124
+ }),
125
+ ),
126
+ );
127
+
128
+ // An unconnected socket: Node reports neither address.
129
+ const socket = new NodeNet.Socket();
130
+ server.emit("connection", socket);
131
+
132
+ expect(socket.destroyed).toBe(true);
133
+ expect(warnings.map((entry) => entry.logLevel)).toEqual(["Warn"]);
134
+ const line = Logger.formatLogFmt.log(warnings[0]!);
135
+ expect(line).toMatch(/localAddress\W+undefined/);
136
+ expect(line).toMatch(/remoteAddress\W+undefined/);
137
+ }),
138
+ );
139
+
140
+ it.effect("logs a flood of closed connections at most once a minute, with a count", () =>
141
+ Effect.gen(function* () {
142
+ const warnings: Array<Logger.Options<unknown>> = [];
143
+ const server = NodeHttp.createServer();
144
+ yield* closeNonPrivateNetworkConnections(server).pipe(
145
+ Effect.provide(
146
+ Logger.layer([Logger.make((entry) => warnings.push(entry))], {
147
+ mergeWithExisting: false,
148
+ }),
149
+ ),
150
+ );
151
+ const sockets = Array.from({ length: 1000 }, () => new NodeNet.Socket());
152
+ for (const socket of sockets) server.emit("connection", socket);
153
+ yield* TestClock.adjust("1 minute");
154
+ server.emit("connection", new NodeNet.Socket());
155
+
156
+ expect(sockets.every((socket) => socket.destroyed)).toBe(true);
157
+ expect(warnings).toHaveLength(2);
158
+ expect(Logger.formatLogFmt.log(warnings[1]!)).toMatch(/closedSinceLastLog\W+999/);
159
+ }),
160
+ );
161
+});
apps/server/src/startupAccess.ts
+41
@@ -1,3 +1,5 @@
1
+// @effect-diagnostics nodeBuiltinImport:off - The private-network gate guards the node:http server.
2
+import type * as NodeHttp from "node:http";
3
import * as NodeNet from "node:net";
4
import * as NodeOS from "node:os";
5
@@ -10,6 +12,7 @@ import {
12
import * as HostProcess from "@t3tools/shared/HostProcess";
13
import { QrCode } from "@t3tools/shared/qrCode";
14
import { buildPairingAppLink } from "@t3tools/shared/remote";
15
+import * as Clock from "effect/Clock";
16
import * as Effect from "effect/Effect";
17
import { HttpServer } from "effect/http";
18
import * as NetAddress from "effect/net/NetAddress";
@@ -81,6 +84,44 @@ export const acceptsPrivateNetworkConnection = (socket: {
84
(address) => address !== undefined && isPrivateNetworkAddress(address),
85
);
86
87
+const CLOSED_CONNECTION_LOG_INTERVAL_NANOS = 60_000_000_000n;
88
+
89
+/**
90
+ * Closing is silent to the client, so closed connections are logged with both
91
+ * addresses: the first at once, then at most one line a minute with a count, so
92
+ * a connect flood from outside cannot grow the service log without bound.
93
+ */
94
+// Untraced: the captured context would otherwise tie every later warning to a span that ended at startup.
95
+export const closeNonPrivateNetworkConnections = Effect.fnUntraced(function* (
96
+ server: NodeHttp.Server,
97
+) {
98
+ const runSync = Effect.runSyncWith(yield* Effect.context<never>());
99
+ let nextLogAt: bigint | undefined;
100
+ let closedSinceLastLog = 0;
101
+ server.on("connection", (socket) => {
102
+ if (acceptsPrivateNetworkConnection(socket)) return;
103
+ // Read before destroy: Node forgets localAddress once the handle closes.
104
+ const addresses = { localAddress: socket.localAddress, remoteAddress: socket.remoteAddress };
105
+ socket.destroy();
106
+ runSync(
107
+ Effect.gen(function* () {
108
+ // Monotonic: a wall clock set backwards must not silence the warnings.
109
+ const now = yield* Clock.monotonicTimeNanos;
110
+ if (nextLogAt !== undefined && now < nextLogAt) {
111
+ closedSinceLastLog += 1;
112
+ return;
113
+ }
114
+ nextLogAt = now + CLOSED_CONNECTION_LOG_INTERVAL_NANOS;
115
+ yield* Effect.logWarning("closed a connection outside the private network", {
116
+ ...addresses,
117
+ ...(closedSinceLastLog > 0 ? { closedSinceLastLog } : {}),
118
+ });
119
+ closedSinceLastLog = 0;
120
+ }),
121
+ );
122
+ });
123
+});
124
+
125
export const loopbackUrlHostForAddress = (address: NetAddress.InetAddress): string =>
126
NetAddress.isUnspecified(address.address)
127
? "127.0.0.1"