mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
* test(remote-browser): reproduce a client-hosted browser tab left unavailable after a network outage A paired desktop that loses its host for longer than the browser-host lease grace (25s reset cut, or a 100s silent cut the host's heartbeat reaps) tears down its client-hosted pages and never hosts them again, leaving 'Client-hosted browser unavailable'. Red on main. Extends the freezable TCP proxy with a full silent or reset link cut. * fix(remote-browser): host client browser pages again when a paired host comes back after an outage When the network between a paired desktop and its Orca host dropped for longer than the browser-host lease grace, the desktop tore down its client-hosted pages and never offered to host them again, so the tab stayed on 'Client-hosted browser unavailable' until the app was relaunched. Regaining contact with a runtime now re-claims browser hosting the same way a runtime restart already did: if this desktop still has client-placed pages for that host, it starts its browser client host again and the host's existing recovery re-places them, reloading at their last URL. The notice now says the host is offline (and that the page reloads when it reconnects), names another desktop only when the placement belongs to one, and otherwise says the page is not available here right now. * fix(remote-browser): claim offline only for a host that can reconnect, and re-host from the notice The unavailable notice said the host was offline for any null status, which also covers a retired or refused pairing and a host whose runtime did not answer. It now uses the host connection state and excludes revoked verdicts. When the control link returns while the old browser host is still inside its lease reconnect, the contact-edge ensure is a no-op; if that reconnect then fails, nothing re-hosts. The notice now re-claims hosting on mount while the host is reachable, which is exactly that stranded state. Plain regained contact no longer queues a second preparation behind one already in flight; only a runtime restart does. * fix(remote-browser): re-prepare browser hosting on every regained contact A preparation still in flight when contact returns started while the host was unreachable, so it is the one most likely to fail. Coalescing the contact edge into it dropped the only trigger that could succeed and left the page unavailable. Always queue one follow-up preparation instead.
120 lines
3.8 KiB
TypeScript
120 lines
3.8 KiB
TypeScript
import net from 'node:net'
|
|
|
|
export type FreezableTcpProxy = {
|
|
port: number
|
|
/** Connection open/close timestamps, for diagnosing client reconnect behaviour. */
|
|
events: string[]
|
|
/** Silently stops forwarding on every open connection while keeping it open, like a NAT drop. */
|
|
freezeExisting: (direction?: 'both' | 'to-client') => number
|
|
/**
|
|
* Takes the whole link down until `restore`. `silent` freezes open connections and holds new ones
|
|
* unanswered, like a sleeping laptop; `reset` closes everything and refuses new connections.
|
|
*/
|
|
cut: (mode: 'silent' | 'reset') => void
|
|
/** Ends a `cut`: drops connections that lived through it, as a returning network does, and relays new ones. */
|
|
restore: () => void
|
|
close: () => Promise<void>
|
|
}
|
|
|
|
type ProxyPair = { client: net.Socket; upstream: net.Socket | null; frozen: boolean }
|
|
|
|
/** A loopback TCP proxy whose established connections can go half-open while new ones still pass. */
|
|
export async function startFreezableTcpProxy(
|
|
targetHost: string,
|
|
targetPort: number
|
|
): Promise<FreezableTcpProxy> {
|
|
const pairs = new Set<ProxyPair>()
|
|
const events: string[] = []
|
|
let nextId = 0
|
|
let cutMode: 'silent' | 'reset' | null = null
|
|
const destroyPair = (pair: ProxyPair): void => {
|
|
pair.client.destroy()
|
|
pair.upstream?.destroy()
|
|
pairs.delete(pair)
|
|
}
|
|
const server = net.createServer((client) => {
|
|
const id = nextId++
|
|
events.push(`${Date.now()} open#${id}${cutMode ? ` (during ${cutMode} cut)` : ''}`)
|
|
client.on('close', () => events.push(`${Date.now()} close#${id}`))
|
|
client.on('error', () => undefined)
|
|
if (cutMode === 'reset') {
|
|
client.destroy()
|
|
return
|
|
}
|
|
if (cutMode === 'silent') {
|
|
// Why held, not refused: an unreachable host answers nothing; the dial must time out on its own.
|
|
client.pause()
|
|
pairs.add({ client, upstream: null, frozen: true })
|
|
return
|
|
}
|
|
const upstream = net.connect(targetPort, targetHost)
|
|
const pair: ProxyPair = { client, upstream, frozen: false }
|
|
pairs.add(pair)
|
|
client.pipe(upstream)
|
|
upstream.pipe(client)
|
|
const end = (): void => {
|
|
// Why: a frozen pair must not relay a close either; the far side would learn of the drop.
|
|
if (!pair.frozen) {
|
|
destroyPair(pair)
|
|
}
|
|
}
|
|
client.on('close', end)
|
|
upstream.on('close', end)
|
|
upstream.on('error', () => undefined)
|
|
})
|
|
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
|
|
const address = server.address()
|
|
if (!address || typeof address === 'string') {
|
|
throw new Error('Proxy did not bind a TCP port')
|
|
}
|
|
const freezeExisting = (direction: 'both' | 'to-client' = 'both'): number => {
|
|
let frozen = 0
|
|
for (const pair of pairs) {
|
|
if (pair.frozen || !pair.upstream) {
|
|
continue
|
|
}
|
|
pair.frozen = true
|
|
// Why one-way: requests still reach the host while its replies and publications vanish.
|
|
pair.upstream.unpipe(pair.client)
|
|
pair.upstream.pause()
|
|
if (direction === 'both') {
|
|
pair.client.unpipe(pair.upstream)
|
|
pair.client.pause()
|
|
}
|
|
frozen += 1
|
|
}
|
|
return frozen
|
|
}
|
|
return {
|
|
port: address.port,
|
|
events,
|
|
freezeExisting,
|
|
cut: (mode) => {
|
|
cutMode = mode
|
|
events.push(`${Date.now()} cut:${mode}`)
|
|
if (mode === 'silent') {
|
|
freezeExisting('both')
|
|
return
|
|
}
|
|
for (const pair of pairs) {
|
|
destroyPair(pair)
|
|
}
|
|
},
|
|
restore: () => {
|
|
cutMode = null
|
|
events.push(`${Date.now()} restore`)
|
|
for (const pair of pairs) {
|
|
if (pair.frozen) {
|
|
destroyPair(pair)
|
|
}
|
|
}
|
|
},
|
|
close: async () => {
|
|
for (const pair of pairs) {
|
|
destroyPair(pair)
|
|
}
|
|
await new Promise<void>((resolve) => server.close(() => resolve()))
|
|
}
|
|
}
|
|
}
|