Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Train nov8-b: #4608 tracks every dial attempt, drops them when the browser leaves, and tests offline

milind-soni28a3fc48183f
diff --git a/server/browser/egress/gate.test.ts b/server/browser/egress/gate.test.ts
index 607aa5242b..7501675c1f 100644
--- a/server/browser/egress/gate.test.ts
+++ b/server/browser/egress/gate.test.ts
@@ -213,12 +213,12 @@ describe("egress gate", () => {
const reachable = NodeNet.createServer((socket) => socket.end("reached"));
await new Promise<void>((resolve) => reachable.listen(0, "127.0.0.1", resolve));
const reachablePort = (reachable.address() as NodeNet.AddressInfo).port;
- // A mixed list is what a public name usually resolves to. The first address is a public IPv6 this host cannot
- // route (a machine with no IPv6 route leaves the old code stalled until the socket times out); the second is
- // the local listener the gate must reach anyway. The name is this machine's own, as in the test above, so the
- // loopback answer is a place to connect to, not a rebinding.
+ // A mixed list is what a public name usually resolves to. The first address fails (nothing listens on ::1 at this
+ // port, or the host has no IPv6 at all), with no network needed; the second is the local listener the gate must
+ // reach anyway. The name is this machine's own, as in the test above, so the loopback answer is a place to
+ // connect to, not a rebinding.
const resolve = async () => [
- { address: "2a00:1450:4009:c0f::54", family: 6 },
+ { address: "::1", family: 6 },
{ address: "127.0.0.1", family: 4 },
];
const { port } = await gate({ ...serverRules, host: "desktop" }, [], resolve);
@@ -231,9 +231,7 @@ describe("egress gate", () => {
socket.destroy();
await new Promise<void>((resolve) => reachable.close(() => resolve()));
expect(code).toBe(0);
- // Without a route to the first address the second answers; with one, the first does. Either way the gate
- // connected instead of stalling.
- expect(answer.length === 0 || answer.toString() === "reached").toBe(true);
+ expect(answer.toString()).toBe("reached");
});
it("fails instead of crashing when it cannot listen", async () => {
diff --git a/server/browser/egress/gate.ts b/server/browser/egress/gate.ts
index 7b2d898edd..4281b2dc83 100644
--- a/server/browser/egress/gate.ts
+++ b/server/browser/egress/gate.ts
@@ -122,27 +122,38 @@ function readRequest(data: Buffer) {
* By hand, because autoSelectFamily with a custom lookup never falls back: on Node 24, a list that mixes families
* where the first cannot route (a host with no IPv6 route is the usual case) leaves the socket stalled until it
* times out, with the IPv4 answer never tried. */
-function connectChecked(port: number, addresses: readonly ResolvedAddress[], staggerMs = 250): Promise<Socket> {
+function connectChecked(port: number, addresses: readonly ResolvedAddress[], track: (socket: Socket) => Socket, client: Socket, staggerMs = 250): Promise<Socket> {
return new Promise((resolve, reject) => {
const inFlight = new Set<Socket>();
let next = 0;
let settled = false;
+ // The gate closing or the browser leaving drops every attempt still dialing, so none waits out the OS timeout.
+ const giveUp = () => {
+ if (settled) return;
+ settled = true;
+ for (const attempt of inFlight) attempt.destroy();
+ reject(new Error("client left"));
+ };
+ client.once("close", giveUp);
+ const settle = () => { settled = true; client.off("close", giveUp); };
const startNext = (): void => {
if (settled || next >= addresses.length) return;
const { address, family } = addresses[next++]!;
- const attempt = connect({ host: address, port, family });
+ const attempt = track(connect({ host: address, port, family }));
inFlight.add(attempt);
attempt.once("connect", () => {
if (settled) return void attempt.destroy();
- settled = true;
+ settle();
for (const other of inFlight) if (other !== attempt) other.destroy();
resolve(attempt);
});
attempt.once("error", (error: NodeJS.ErrnoException) => {
inFlight.delete(attempt);
if (settled) return;
- if (inFlight.size === 0 && next >= addresses.length) {
- settled = true;
+ // A quick failure moves straight on to the next address instead of waiting out the stagger.
+ if (next < addresses.length) return startNext();
+ if (inFlight.size === 0) {
+ settle();
reject(error);
}
});
@@ -223,8 +234,9 @@ export function openGate(options: GateOptions): Promise<EgressGate> {
// attempts go to exactly the checked addresses, so a name that now resolves elsewhere changes nothing.
let upstream: Socket;
try {
- upstream = track(await connectChecked(target.port, addresses));
+ upstream = await connectChecked(target.port, addresses, track, client);
} catch {
+ if (client.destroyed) return;
return refuse(client, CONNECTION_REFUSED);
}
if (client.destroyed) return void upstream.destroy();