tailscale-web - v0.1.13
    Preparing search index...

    Variable networkConst

    network: {
        dialTCP(addr: string): Promise<Connection>;
        fetch(url: string, init?: RequestInit): Promise<Response>;
        getDNS(): DNSInfo;
        getPrefs(): Prefs;
        getRoutes(): Route[];
        init(options?: InitOptions): Promise<void>;
        listenTCP(
            port?: number,
            onConnection: (conn: Connection) => void,
        ): Promise<Listener>;
        listExitNodes(): ExitNode[];
        localIPv4(): string;
        localIPv6(): string;
        ping(addr: string): Promise<PingResult>;
        setAcceptRoutes(accept: boolean): Promise<void>;
        setExitNode(id?: string): Promise<void>;
    } = ...

    Type Declaration

    • dialTCP: function
      • Open a raw TCP connection through the Tailscale network. Returns a Connection object for sending and receiving data.

        Parameters

        • addr: string

        Returns Promise<Connection>

        const conn = await network.dialTCP("my-server:8080")

        conn.onData(data => {
        console.log(new TextDecoder().decode(data))
        })

        conn.write("hello\n")
        conn.close()
    • fetch: function
      • Make an HTTP request through the Tailscale network. Supports method, headers, and body. Does not yet support AbortSignal, streaming bodies or responses, or other advanced Fetch API options.

        Parameters

        Returns Promise<Response>

        const resp = await network.fetch("https://internal-service/api", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ key: "value" }),
        })
        console.log(resp.status, resp.ok)
        const data = await resp.json()
    • getDNS: function
      • Return the current Tailscale-managed DNS configuration. Synchronous — no await needed. Returns an empty DNSInfo object if called before init() resolves.

        Returns DNSInfo

        const dns = network.getDNS()
        console.log("resolvers:", dns.resolvers)
        console.log("MagicDNS:", dns.magicDNS)
        for (const [suffix, resolvers] of Object.entries(dns.routes)) {
        console.log(`split-DNS: ${suffix}${resolvers.join(", ")}`)
        }
    • getPrefs: function
      • Return the current preferences (acceptRoutes, exitNodeId). Synchronous — no await needed. Must be called after init() resolves.

        Returns Prefs

        const { acceptRoutes, exitNodeId } = network.getPrefs()
        console.log("exit node:", exitNodeId || "(none)")
    • getRoutes: function
      • Return the full routing table (self + all peers). Synchronous — no await needed. Returns an empty array if called before init() resolves.

        Returns Route[]

        const routes = network.getRoutes()
        for (const r of routes) {
        console.log(r.prefix, "via", r.via, r.isExitRoute ? "(exit)" : "")
        }
    • init: function
      • Initialize and connect the Tailscale node. Must be called before any other method. Resolves once the node is authenticated and ready.

        If the node has persisted state from a previous session it reconnects automatically. Otherwise the OAuth flow is triggered via onAuthRequired. Rejects if the auth URL does not arrive within 60 seconds, or if the user does not complete authentication within 5 minutes.

        Parameters

        Returns Promise<void>

        await network.init({
        hostname: "my-app",
        onAuthRequired(url) {
        window.open(url, "_blank", "width=600,height=700")
        },
        onAuthComplete() {
        console.log("connected!")
        },
        })
        // Custom storage backend (e.g. sessionStorage or any key/value store)
        await network.init({
        hostname: "my-app",
        storage: {
        get: key => sessionStorage.getItem(key),
        set: (key, val) => sessionStorage.setItem(key, val),
        },
        onAuthRequired(url) { console.log("Authenticate at:", url) },
        })
    • listenTCP: function
      • Listen for inbound TCP connections on the given Tailscale port. Pass port 0 (default) to get an ephemeral port assigned automatically. onConnection is called for each accepted connection. Returns a Listener with the assigned port number and a close() method.

        Parameters

        • port: number = 0
        • onConnection: (conn: Connection) => void

        Returns Promise<Listener>

        const listener = await network.listenTCP(8080, conn => {
        conn.onData(data => console.log(new TextDecoder().decode(data)))
        conn.write("hello\n")
        })
        console.log("listening on port", listener.port)
        // Ephemeral port
        const listener = await network.listenTCP(0, conn => { conn.close() })
        console.log("assigned port:", listener.port)
        listener.close()
    • listExitNodes: function
      • Return all peers that advertise exit-node capability. Synchronous — no await needed. Returns an empty array if called before init() resolves.

        Returns ExitNode[]

        const nodes = network.listExitNodes()
        for (const n of nodes) {
        console.log(n.hostName, n.tailscaleIP, n.online ? "online" : "offline")
        }
    • localIPv4: function
      • Return this node's Tailscale IPv4 address, or an empty string if not yet assigned. Synchronous — no await needed. Must be called after init() resolves.

        Returns string

    • localIPv6: function
      • Return this node's Tailscale IPv6 address, or an empty string if not yet assigned. Synchronous — no await needed. Must be called after init() resolves.

        Returns string

    • ping: function
      • Send an ICMP ping to addr and measure round-trip time. addr may be a hostname or Tailscale IP.

        Parameters

        • addr: string

        Returns Promise<PingResult>

        const result = await network.ping("my-server")
        if (result.alive) {
        console.log(`rtt: ${result.rttMs.toFixed(3)} ms ip: ${result.nodeIP}`)
        } else {
        console.warn("unreachable:", result.err)
        }
    • setAcceptRoutes: function
      • Enable or disable acceptance of subnet routes advertised by peers. Equivalent to tailscale set --accept-routes.

        Parameters

        • accept: boolean

        Returns Promise<void>

        await network.setAcceptRoutes(true)
        
    • setExitNode: function
      • Activate an exit node by its stable node ID. Pass an empty string (or omit) to clear the exit node.

        Parameters

        • id: string = ""

        Returns Promise<void>

        // Activate the first available online exit node
        const node = network.listExitNodes().find(n => n.online)
        if (node) await network.setExitNode(node.id)
        // Clear the active exit node
        await network.setExitNode()