Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.3 KB
Raw Blame
import { z } from "zod";

function isLoopbackHostname(hostname: string): boolean {
  const normalized = hostname.toLowerCase();
  if (normalized === "localhost" || normalized === "[::1]") return true;

  const octets = normalized.split(".");
  return (
    octets.length === 4 &&
    octets[0] === "127" &&
    octets.every((octet) => /^\d{1,3}$/.test(octet) && Number(octet) <= 255)
  );
}

/**
 * Owner-configured browser destination for the private Radar operations panel.
 * Public/tailnet tunnels must use HTTPS; HTTP is accepted only for an SSH
 * local-forward loopback destination. Credentials are never accepted.
 */
export const radarAdminUrlSchema = z
  .string()
  .trim()
  .min(1)
  .max(2048)
  .url()
  .transform((raw, context) => {
    const url = new URL(raw);
    const safeProtocol =
      url.protocol === "https:" || (url.protocol === "http:" && isLoopbackHostname(url.hostname));

    if (!safeProtocol || url.username || url.password) {
      context.addIssue({
        code: z.ZodIssueCode.custom,
        message: "Radar Admin URL must be HTTPS or HTTP loopback without credentials",
      });
      return z.NEVER;
    }

    return url.toString();
  });

export function parseRadarAdminUrl(value: unknown): string | null {
  const parsed = radarAdminUrlSchema.safeParse(value);
  return parsed.success ? parsed.data : null;
}