@@ -9,6 +9,7 @@ import { Config } from "@/config/config" |
9 | 9 | import { GlobalBus } from "@/bus/global" |
10 | 10 | import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2" |
11 | 11 | import type { BunWebSocketData } from "hono/bun" |
| 12 | +import { Flag } from "@/flag/flag" |
12 | 13 |
|
13 | 14 | await Log.init({ |
14 | 15 | print: process.argv.includes("--print-logs"), |
@@ -50,6 +51,8 @@ const startEventStream = (directory: string) => { |
50 | 51 |
|
51 | 52 | const fetchFn = (async (input: RequestInfo | URL, init?: RequestInit) => { |
52 | 53 | const request = new Request(input, init) |
| 54 | + const auth = getAuthorizationHeader() |
| 55 | + if (auth) request.headers.set("Authorization", auth) |
53 | 56 | return Server.App().fetch(request) |
54 | 57 | }) as typeof globalThis.fetch |
55 | 58 |
|
@@ -95,9 +98,14 @@ startEventStream(process.cwd()) |
95 | 98 |
|
96 | 99 | export const rpc = { |
97 | 100 | async fetch(input: { url: string; method: string; headers: Record<string, string>; body?: string }) { |
| 101 | + const headers = { ...input.headers } |
| 102 | + const auth = getAuthorizationHeader() |
| 103 | + if (auth && !headers["authorization"] && !headers["Authorization"]) { |
| 104 | + headers["Authorization"] = auth |
| 105 | + } |
98 | 106 | const request = new Request(input.url, { |
99 | 107 | method: input.method, |
100 | | - headers: input.headers, |
| 108 | + headers, |
101 | 109 | body: input.body, |
102 | 110 | }) |
103 | 111 | const response = await Server.App().fetch(request) |
@@ -135,3 +143,10 @@ export const rpc = { |
135 | 143 | } |
136 | 144 |
|
137 | 145 | Rpc.listen(rpc) |
| 146 | + |
| 147 | +function getAuthorizationHeader(): string | undefined { |
| 148 | + const password = Flag.OPENCODE_SERVER_PASSWORD |
| 149 | + if (!password) return undefined |
| 150 | + const username = Flag.OPENCODE_SERVER_USERNAME ?? "opencode" |
| 151 | + return `Basic ${btoa(`${username}:${password}`)}` |
| 152 | +} |