Skip to content

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

fix(companion): give a far relay longer than 250 ms per address

AdityaUmalebb67cecd3cce
diff --git a/companion/src/e2e/relay-client.ts b/companion/src/e2e/relay-client.ts
index e779d772ba..583f8b1dcd 100644
--- a/companion/src/e2e/relay-client.ts
+++ b/companion/src/e2e/relay-client.ts
@@ -1,5 +1,7 @@
// The computer's side of MausBot Relay: one outbound control socket, and one data socket per phone session. The relay
// splices sealed bytes and can't open them; everything here is outside the encryption. Wire spec: README section 9.
+import { getDefaultAutoSelectFamilyAttemptTimeout, setDefaultAutoSelectFamilyAttemptTimeout } from "node:net";
+
import { webSocketTransport } from "./phone.ts";
import type { RecordTransport } from "./session.ts";
@@ -10,6 +12,18 @@ export type RelayStatus = "connected" | "connecting" | "off";
* ("unreachable"), or another socket for this computer took over (4409). */
export type RelayProblem = "no_answer" | "token_refused" | "busy" | "unreachable" | "replaced";
+/** Node gives each of a host's addresses but the last 250 ms to connect, then drops it for the next, so a relay further
+ * away than that is unreachable whenever the address after a working one (often IPv6 that doesn't route) fails. */
+const RELAY_ADDRESS_ATTEMPT_MS = 2_500;
+
+/** Gives every address this process dials long enough to reach a far relay. Process-wide, so only a process whose
+ * outbound connections are the relay's should call it. */
+export function allowFarRelay(): void {
+ if (getDefaultAutoSelectFamilyAttemptTimeout() < RELAY_ADDRESS_ATTEMPT_MS) {
+ setDefaultAutoSelectFamilyAttemptTimeout(RELAY_ADDRESS_ATTEMPT_MS);
+ }
+}
+
/** README section 10: 4429 means back off at least this long. */
const LIMITED_MIN_MS = 30_000;
const DEFAULT_TIMING = {
diff --git a/companion/src/index.ts b/companion/src/index.ts
index d863c3fbf3..c1ea2538d6 100755
--- a/companion/src/index.ts
+++ b/companion/src/index.ts
@@ -30,7 +30,7 @@ import { handleDirectUpgrade, isE2EUpgrade } from "./e2e/direct.ts";
import { CLOSE, TRANSPORT_RELAY } from "./e2e/noise.ts";
import { createE2EOrigin } from "./e2e/origin.ts";
import { E2ERegistry } from "./e2e/registry.ts";
-import { RelayClient } from "./e2e/relay-client.ts";
+import { allowFarRelay, RelayClient } from "./e2e/relay-client.ts";
import { companionOriginSocket, listenCompanionOrigin } from "./origin.ts";
import { gzipTunnelReplies } from "./tunnel-gzip.ts";
import { normalizedPhoneSecretPublicKey } from "./phone-secret-key.ts";
@@ -201,6 +201,7 @@ function applyRelay(config: RelayConfig | null): void {
e2e.setRelay(config && e2e.enabled ? { hostId: config.hostId, nodes: config.nodes } : null);
if (!config || !e2e.enabled) return;
if (relay) return relay.setToken(config.token, config.nodes);
+ allowFarRelay();
relay = new RelayClient({
nodes: config.nodes,
token: config.token,
diff --git a/companion/test/e2e/relay-client.test.ts b/companion/test/e2e/relay-client.test.ts
index 42657705f1..f290d115e0 100644
--- a/companion/test/e2e/relay-client.test.ts
+++ b/companion/test/e2e/relay-client.test.ts
@@ -1,9 +1,10 @@
import { createHash, generateKeyPairSync, randomBytes } from "node:crypto";
+import { getDefaultAutoSelectFamilyAttemptTimeout, setDefaultAutoSelectFamilyAttemptTimeout } from "node:net";
import { afterEach, describe, expect, it, vi } from "vitest";
import { WebSocket, WebSocketServer } from "ws";
import { b64u, ed25519RawPublicKey, mintRelayToken } from "../../src/e2e/credentials.ts";
-import { RelayClient, type RelayClientOptions } from "../../src/e2e/relay-client.ts";
+import { allowFarRelay, RelayClient, type RelayClientOptions } from "../../src/e2e/relay-client.ts";
import { startFakeRelay, type FakeRelay } from "./fake-relay.ts";
const signingKey = generateKeyPairSync("ed25519").privateKey;
@@ -74,6 +75,26 @@ function client(relays: Record<string, Pick<FakeRelay, "url">>, overrides: Parti
return { instance, statuses, rejected };
}
+describe("connecting to a far relay", () => {
+ const atAttemptTimeout = (ms: number) => {
+ const before = getDefaultAutoSelectFamilyAttemptTimeout();
+ cleanup.push(() => setDefaultAutoSelectFamilyAttemptTimeout(before));
+ setDefaultAutoSelectFamilyAttemptTimeout(ms);
+ };
+
+ it("gives each relay address longer than Node's 250 ms to connect", () => {
+ atAttemptTimeout(250);
+ allowFarRelay();
+ expect(getDefaultAutoSelectFamilyAttemptTimeout()).toBeGreaterThanOrEqual(2_000);
+ });
+
+ it("keeps a longer per-address connect time already set", () => {
+ atAttemptTimeout(5_000);
+ allowFarRelay();
+ expect(getDefaultAutoSelectFamilyAttemptTimeout()).toBe(5_000);
+ });
+});
+
describe("the relay client", () => {
it("authenticates, then sends the admission set, and again only when it changes", async () => {
const relay = await relayAt();