Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

3.5 KB
Raw Blame
/** Pure guidance shared by desktop/web presentation paths; keep copy out of error constructors consumed by CLI/logs/mobile. */

const TAILSCALE_DOWNLOAD_URL = 'https://tailscale.com/download'

// Only unreachable-runtime errors have a Tailscale remedy; preserve auth/protocol errors.
const REMOTE_RUNTIME_UNREACHABLE_RE =
  /could not connect to the remote orca runtime|remote orca runtime closed the connection|timed out (?:waiting for|while connecting to) the remote orca runtime/i

const TAILSCALE_MAGIC_DNS_SUFFIX_RE = /(?:^|\.)ts\.net$/i
// Require an IPv4 literal so CGNAT-shaped DNS names do not qualify.
const IPV4_LITERAL_RE =
  /^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$/
// Tailscale assigns node IPs from the 100.64.0.0/10 CGNAT range (second octet 64–127).
const TAILSCALE_CGNAT_RE = /^100\.(?:6[4-9]|[7-9]\d|1[01]\d|12[0-7])\./
// Pairing also accepts Tailscale’s fd7a:115c:a1e0::/48 IPv6 literals.
const TAILSCALE_IPV6_RE = /^fd7a:115c:a1e0:/i
const SAFE_HOST_RE =
  /^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*|[0-9a-f:]+)$/i

function extractHost(endpoint: string): string | null {
  let host: string | null
  try {
    host = new URL(endpoint).hostname || null
  } catch {
    // Accept bare hosts by extracting authority before the port/path/query delimiter.
    host = endpoint.replace(/^[a-z]+:\/\//i, '').split(/[/:?#]/, 1)[0] || null
  }
  if (!host) {
    return null
  }
  // Normalize URL-preserved IPv6 brackets and FQDN trailing dots before matching.
  return host.replace(/^\[|\]$/g, '').replace(/\.$/, '') || null
}

/** The endpoint's host when it is a Tailscale address (what `tailscale ping` takes); else null. */
export function tailscaleHostOfEndpoint(endpoint: string | null | undefined): string | null {
  const host = endpoint ? extractHost(endpoint) : null
  // Why strict: the host is pasted into a shell, so only DNS-label or IP-literal characters pass.
  if (!host || !SAFE_HOST_RE.test(host)) {
    return null
  }
  return TAILSCALE_MAGIC_DNS_SUFFIX_RE.test(host) ||
    (IPV4_LITERAL_RE.test(host) && TAILSCALE_CGNAT_RE.test(host)) ||
    TAILSCALE_IPV6_RE.test(host)
    ? host
    : null
}

/** A command, never translated: the user pastes it into a shell. */
export function tailscalePingCommand(host: string): string {
  return `tailscale ping ${host}`
}

export function isTailscaleEndpoint(endpoint: string | null | undefined): boolean {
  return tailscaleHostOfEndpoint(endpoint) !== null
}

/** Tailnet endpoints need reachability guidance; saved tokens survive restarts, so re-pair only for new devices. */
const TAILNET_ENDPOINT_HINT =
  "The server may be offline on your tailnet, or its Tailscale Funnel reverted to tailnet-only. Confirm it's reachable; re-pair only when adding a new device, since already-paired devices reconnect with their saved token."

const OTHER_NETWORK_HINT = `If the server is on another network, connect both devices to Tailscale and pair using its Tailscale address (100.x or a *.ts.net name). See ${TAILSCALE_DOWNLOAD_URL}.`

export function withRemoteRuntimeTailscaleHint(
  message: string,
  endpoint: string | null | undefined
): string {
  if (!REMOTE_RUNTIME_UNREACHABLE_RE.test(message)) {
    return message
  }
  // Deduplicate exact hint suffixes; the endpoint itself may contain “tailscale”.
  if (message.endsWith(TAILNET_ENDPOINT_HINT) || message.endsWith(OTHER_NETWORK_HINT)) {
    return message
  }
  return `${message} ${isTailscaleEndpoint(endpoint) ? TAILNET_ENDPOINT_HINT : OTHER_NETWORK_HINT}`
}