Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/socket/utils/publicHost.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { publicHost } from "./server";

describe("publicHost", () => {
it("turns EXTERNAL_HOST into the bare host a client saves", () => {
assert.equal(publicHost("https://beta.sivert.io"), "beta.sivert.io");
assert.equal(publicHost("http://203.0.113.5:5000/"), "203.0.113.5:5000");
assert.equal(publicHost("chat.example.com"), "chat.example.com");
assert.equal(publicHost(""), undefined);
assert.equal(publicHost(undefined), undefined);
});
});
13 changes: 13 additions & 0 deletions src/socket/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ export function setSocketRefs(io: Server, serverId: string, clientsInfo: Clients
_clientsInfo = clientsInfo;
}

/** EXTERNAL_HOST is documented as a URL; clients save a bare host[:port]. */
export function publicHost(external: string | undefined): string | undefined {
const raw = external?.trim();
if (!raw) return undefined;
try {
return new URL(/^[a-z]+:\/\//i.test(raw) ? raw : `http://${raw}`).host || undefined;
} catch {
return undefined;
}
}

export function broadcastServerUiUpdate(reason: "settings" | "icon" | "other" = "other"): void {
if (!_io || !_serverId || !_clientsInfo) return;
consola.info(`Broadcasting server UI update (${reason})`);
Expand Down Expand Up @@ -442,6 +453,8 @@ export async function sendServerDetails(socket: Socket, clientsInfo: Clients, in
})),
/** Takes polls on chat:send and poll:vote (GRYT-1732), so a client offers them. */
polls: true,
/** Where this server answers from anywhere, for a member who saved its LAN address (GRYT-2007). */
public_host: publicHost(process.env.EXTERNAL_HOST),
max_members: parseInt(process.env.MAX_MEMBERS || "100"),
voice_enabled: !!sfuHost,
avatar_max_bytes: cfgAvatarMaxBytes,
Expand Down
Loading