|
| 1 | +import type { Target } from "@/control-plane/types" |
| 2 | +import { lazy } from "@/util/lazy" |
| 3 | +import { Hono } from "hono" |
| 4 | +import { upgradeWebSocket } from "hono/bun" |
| 5 | + |
| 6 | +const hop = new Set([ |
| 7 | + "connection", |
| 8 | + "keep-alive", |
| 9 | + "proxy-authenticate", |
| 10 | + "proxy-authorization", |
| 11 | + "proxy-connection", |
| 12 | + "te", |
| 13 | + "trailer", |
| 14 | + "transfer-encoding", |
| 15 | + "upgrade", |
| 16 | + "host", |
| 17 | +]) |
| 18 | + |
| 19 | +type Msg = string | ArrayBuffer | Uint8Array |
| 20 | + |
| 21 | +function headers(req: Request, extra?: HeadersInit) { |
| 22 | + const out = new Headers(req.headers) |
| 23 | + for (const key of hop) out.delete(key) |
| 24 | + out.delete("x-opencode-directory") |
| 25 | + out.delete("x-opencode-workspace") |
| 26 | + if (!extra) return out |
| 27 | + for (const [key, value] of new Headers(extra).entries()) { |
| 28 | + out.set(key, value) |
| 29 | + } |
| 30 | + return out |
| 31 | +} |
| 32 | + |
| 33 | +function protocols(req: Request) { |
| 34 | + const value = req.headers.get("sec-websocket-protocol") |
| 35 | + if (!value) return [] |
| 36 | + return value |
| 37 | + .split(",") |
| 38 | + .map((item) => item.trim()) |
| 39 | + .filter(Boolean) |
| 40 | +} |
| 41 | + |
| 42 | +function socket(url: string | URL) { |
| 43 | + const next = new URL(url) |
| 44 | + if (next.protocol === "http:") next.protocol = "ws:" |
| 45 | + if (next.protocol === "https:") next.protocol = "wss:" |
| 46 | + return next.toString() |
| 47 | +} |
| 48 | + |
| 49 | +function send(ws: { send(data: string | ArrayBuffer | Uint8Array): void }, data: any) { |
| 50 | + if (data instanceof Blob) { |
| 51 | + return data.arrayBuffer().then((x) => ws.send(x)) |
| 52 | + } |
| 53 | + return ws.send(data) |
| 54 | +} |
| 55 | + |
| 56 | +const app = lazy(() => |
| 57 | + new Hono().get( |
| 58 | + "/__workspace_ws", |
| 59 | + upgradeWebSocket((c) => { |
| 60 | + const url = c.req.header("x-opencode-proxy-url") |
| 61 | + const queue: Msg[] = [] |
| 62 | + let remote: WebSocket | undefined |
| 63 | + return { |
| 64 | + onOpen(_, ws) { |
| 65 | + if (!url) { |
| 66 | + ws.close(1011, "missing proxy target") |
| 67 | + return |
| 68 | + } |
| 69 | + remote = new WebSocket(url, protocols(c.req.raw)) |
| 70 | + remote.binaryType = "arraybuffer" |
| 71 | + remote.onopen = () => { |
| 72 | + for (const item of queue) remote?.send(item) |
| 73 | + queue.length = 0 |
| 74 | + } |
| 75 | + remote.onmessage = (event) => { |
| 76 | + send(ws, event.data) |
| 77 | + } |
| 78 | + remote.onerror = () => { |
| 79 | + ws.close(1011, "proxy error") |
| 80 | + } |
| 81 | + remote.onclose = (event) => { |
| 82 | + ws.close(event.code, event.reason) |
| 83 | + } |
| 84 | + }, |
| 85 | + onMessage(event) { |
| 86 | + const data = event.data |
| 87 | + if (typeof data !== "string" && !(data instanceof Uint8Array) && !(data instanceof ArrayBuffer)) return |
| 88 | + if (remote?.readyState === WebSocket.OPEN) { |
| 89 | + remote.send(data) |
| 90 | + return |
| 91 | + } |
| 92 | + queue.push(data) |
| 93 | + }, |
| 94 | + onClose(event) { |
| 95 | + remote?.close(event.code, event.reason) |
| 96 | + }, |
| 97 | + } |
| 98 | + }), |
| 99 | + ), |
| 100 | +) |
| 101 | + |
| 102 | +export namespace ServerProxy { |
| 103 | + export function http(target: Extract<Target, { type: "remote" }>, req: Request) { |
| 104 | + return fetch( |
| 105 | + new Request(target.url, { |
| 106 | + method: req.method, |
| 107 | + headers: headers(req, target.headers), |
| 108 | + body: req.method === "GET" || req.method === "HEAD" ? undefined : req.body, |
| 109 | + redirect: "manual", |
| 110 | + signal: req.signal, |
| 111 | + }), |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + export function websocket(target: Extract<Target, { type: "remote" }>, req: Request, env: unknown) { |
| 116 | + const url = new URL(req.url) |
| 117 | + url.pathname = "/__workspace_ws" |
| 118 | + url.search = "" |
| 119 | + const next = new Headers(req.headers) |
| 120 | + next.set("x-opencode-proxy-url", socket(target.url)) |
| 121 | + return app().fetch( |
| 122 | + new Request(url, { |
| 123 | + method: req.method, |
| 124 | + headers: next, |
| 125 | + signal: req.signal, |
| 126 | + }), |
| 127 | + env as never, |
| 128 | + ) |
| 129 | + } |
| 130 | +} |
0 commit comments