mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-06-11 00:40:14 +00:00
detect loopback and coturn option
This commit is contained in:
@@ -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}`
|
||||
);
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user