Skip to content

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

4.1 KB
Raw Blame
import { tailnetName, tailscaleAddress } from "./listener.ts";
import { directRoutes, routedAddresses } from "./direct-routes.ts";
import { defaultHostName } from "./mdns.ts";

export const COMPANION_ENDPOINT_KINDS = ["hosted", "tailnet", "lan", "bonjour"] as const;

export type CompanionEndpointKind = (typeof COMPANION_ENDPOINT_KINDS)[number];

/** A complete base URL the mobile app can dial. */
export interface CompanionEndpoint {
  url: string;
  kind: CompanionEndpointKind;
  priority: number;
}

export const MAX_COMPANION_ENDPOINTS = 8;

/** A hosted endpoint is public internet infrastructure, so a typo refuses to
 * start rather than pass as a local route. It must be a bare HTTPS origin. */
export function hostedCompanionUrl(value: string | undefined): string | null {
  const configured = value?.trim();
  if (!configured) return null;
  return companionHttpsOrigin(configured, "OMB_COMPANION_HOSTED_URL");
}

/** One origin, or one reason it cannot be a companion route. The variable name
 * is in the message because a typo stops the sidecar before it serves. */
function companionHttpsOrigin(configured: string, name: string): string {
  let parsed: URL;
  try {
    parsed = new URL(configured);
  } catch {
    throw new Error(`${name} must be an absolute HTTPS origin`);
  }

  if (
    parsed.protocol !== "https:" ||
    parsed.username ||
    parsed.password ||
    (parsed.pathname !== "" && parsed.pathname !== "/") ||
    parsed.search ||
    parsed.hash
  ) {
    throw new Error(`${name} must be an HTTPS origin without a path, credentials, query, or fragment`);
  }

  return parsed.origin;
}

/** Custom HTTPS origins in `OMB_COMPANION_EXTRA_URL` (commas or newlines), for a
 * self-hosted reverse proxy in front of this companion. Empty entries are skipped. */
export function extraCompanionUrls(value: string | undefined): string[] {
  const origins = (value ?? "")
    .split(/[\n,]/)
    .map((entry) => entry.trim())
    .filter(Boolean)
    .map((entry) => companionHttpsOrigin(entry, "OMB_COMPANION_EXTRA_URL"));
  return [...new Set(origins)].slice(0, MAX_COMPANION_ENDPOINTS);
}

const httpOrigin = (host: string, port: number): string => {
  const authority = host.includes(":") && !host.startsWith("[") ? `[${host}]` : host;
  return `http://${authority}:${port}`;
};

/** Best first: the hosted HTTPS route when configured, then custom HTTPS origins,
 * then the direct routes in the host list's order, Bonjour last. The cap bounds QR
 * size and the connection walk. */
export function companionEndpointCandidates(
  port: number,
  addresses: string[] = routedAddresses(),
  magicDnsName: string | null = tailnetName(),
  hostedUrl: string | null = null,
  bonjourHost: string | null = directRoutes().wifi ? defaultHostName() : null,
  extraUrls: readonly string[] = [],
): CompanionEndpoint[] {
  const tailscale = tailscaleAddress(addresses);
  const candidates: CompanionEndpoint[] = [];

  if (hostedUrl) candidates.push({ url: hostedUrl, kind: "hosted", priority: 0 });
  extraUrls.forEach((url, index) => {
    candidates.push({ url, kind: "hosted", priority: 50 + index });
  });
  if (tailscale && magicDnsName) {
    candidates.push({ url: httpOrigin(magicDnsName, port), kind: "tailnet", priority: 100 });
  }
  addresses.forEach((address, index) => {
    if (address !== tailscale) {
      candidates.push({ url: httpOrigin(address, port), kind: "lan", priority: 200 + index });
    }
  });
  if (bonjourHost) candidates.push({ url: httpOrigin(bonjourHost, port), kind: "bonjour", priority: 300 });

  const seen = new Set<string>();
  const ordered = candidates
    .sort((left, right) => left.priority - right.priority)
    .filter((endpoint) => {
      if (seen.has(endpoint.url)) return false;
      seen.add(endpoint.url);
      return true;
    });

  if (ordered.length <= MAX_COMPANION_ENDPOINTS) return ordered;
  const bonjour = ordered.find((endpoint) => endpoint.kind === "bonjour");
  const head = ordered.filter((endpoint) => endpoint.kind !== "bonjour").slice(0, MAX_COMPANION_ENDPOINTS - 1);
  return bonjour ? [...head, bonjour] : ordered.slice(0, MAX_COMPANION_ENDPOINTS);
}