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.

1.7 KB
Raw Blame
import { isTailscaleEndpoint } from '../../../src/shared/remote-runtime/remote-runtime-tailscale-hint'

const HOST_REACHABILITY_TIMEOUT_MS = 4000

// Probe reachability without completing the encrypted runtime handshake.
export async function testHostReachability(endpoint: string): Promise<boolean> {
  return new Promise((resolve) => {
    let ws: WebSocket
    try {
      ws = new WebSocket(endpoint)
    } catch {
      resolve(false)
      return
    }

    let settled = false
    let timeout: ReturnType<typeof setTimeout> | null = null

    const finish = (reachable: boolean, closeSocket: boolean): void => {
      if (settled) {
        return
      }
      settled = true
      if (timeout) {
        clearTimeout(timeout)
        timeout = null
      }
      if (closeSocket) {
        try {
          ws.close()
        } catch {
          // The probe is already complete; close failures should not change the diagnostic result.
        }
      }
      resolve(reachable)
    }

    timeout = setTimeout(() => {
      finish(false, true)
    }, HOST_REACHABILITY_TIMEOUT_MS)

    ws.onopen = () => {
      finish(true, true)
    }

    ws.onerror = () => {
      finish(false, true)
    }
  })
}

export function formatEndpoint(endpoint: string): string {
  try {
    const url = new URL(endpoint)
    return url.host
  } catch {
    return 'invalid endpoint'
  }
}

// Unreachable 100.x/*.ts.net usually means a wedged Tailscale tunnel on iOS; suggest toggling the VPN.
export function unreachableHostDetail(endpoint: string): string {
  if (isTailscaleEndpoint(endpoint)) {
    return `Cannot reach ${formatEndpoint(endpoint)} — check Tailscale`
  }
  return `Cannot reach ${formatEndpoint(endpoint)}`
}