detect loopback and coturn option

This commit is contained in:
vorotamoroz
2026-06-05 09:44:17 +01:00
parent 6b7816d334
commit 60f21eb9d2
7 changed files with 55 additions and 13 deletions
+6 -3
View File
@@ -625,7 +625,7 @@ export async function startP2pRelay(): Promise<void> {
}
export function isLocalP2pRelay(relayUrl: string): boolean {
return relayUrl === "ws://localhost:4000" || relayUrl === "ws://localhost:4000/";
return relayUrl.includes("localhost") || relayUrl.includes("127.0.0.1") || relayUrl.includes("[::1]");
}
// ---------------------------------------------------------------------------
@@ -648,7 +648,10 @@ export async function startCoturn(
console.log("[INFO] stopping leftover Coturn container if present");
await stopCoturn().catch(() => {});
console.log("[INFO] starting local Coturn container");
const { getOptimalLoopbackIp } = await import("./net.ts");
const externalIp = await getOptimalLoopbackIp();
console.log(`[INFO] starting local Coturn container with external-ip ${externalIp}`);
await dockerOrFail(
"run",
"-d",
@@ -660,7 +663,7 @@ export async function startCoturn(
`${port}:${port}/udp`,
COTURN_IMAGE,
"--log-file=stdout",
"--external-ip=127.0.0.1",
`--external-ip=${externalIp}`,
`--user=${user}:${pass}`,
`--realm=${realm}`
);
+19
View File
@@ -47,3 +47,22 @@ export async function waitForPort(hostname: string, port: number, options: WaitF
(lastError ? ` (last error: ${String(lastError)})` : "")
);
}
export async function getOptimalLoopbackIp(): Promise<string> {
const ipv4 = "127.0.0.1";
const ipv6 = "::1";
try {
const l = Deno.listen({ hostname: ipv4, port: 0 });
l.close();
return ipv4;
} catch {
try {
const l = Deno.listen({ hostname: ipv6, port: 0 });
l.close();
return ipv6;
} catch {
return ipv4; // fallback to default
}
}
}
+1 -1
View File
@@ -97,7 +97,7 @@ export async function stopLocalRelayIfStarted(started: boolean): Promise<void> {
}
export async function maybeStartCoturn(turnServers: string): Promise<boolean> {
if (turnServers.includes("localhost") || turnServers.includes("127.0.0.1")) {
if (turnServers.includes("localhost") || turnServers.includes("127.0.0.1") || turnServers.includes("[::1]")) {
await startCoturn();
return true;
}