mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Merge remote-tracking branch 'origin/feat/linked-opencode-servers' into feat/remote-server-automation
# Conflicts: # backend/src/services/remote-server-events.ts # frontend/src/components/navigation/MoreDrawer.test.tsx # frontend/src/components/navigation/moreDrawerItems.test.ts # frontend/src/lib/opencode-event-stream/openCodeEventStream.ts # frontend/src/pages/SessionDetail.tsx # frontend/src/pages/__tests__/SessionDetail.remote.test.tsx
This commit is contained in:
commit
780d31087b
92 files changed
+3828
-817
No files matched your search
@@ -1,4 +1,7 @@
|
||||
import { OpenCode } from '@opencode/client'
|
||||
import { OPENCODE_SERVER_USERNAME } from '../schemas/opencode-servers'
|
||||
|
||||
export { OPENCODE_SERVER_USERNAME }
|
||||
|
||||
export type {
|
||||
AgentInfo,
|
||||
@@ -92,6 +95,23 @@ export type {
|
||||
McpTimeoutConfig,
|
||||
} from './mcp'
|
||||
|
||||
export {
|
||||
createOpenCodePairingNonce,
|
||||
createOpenCodePairingProof,
|
||||
decryptOpenCodePairingCredentials,
|
||||
encryptOpenCodePairingCredentials,
|
||||
isOpenCodePairingCredentials,
|
||||
isOpenCodePairingNonce,
|
||||
verifyOpenCodePairingProof,
|
||||
} from './pairing'
|
||||
|
||||
export type {
|
||||
OpenCodePairingCredentials,
|
||||
OpenCodePairingDecryptOptions,
|
||||
OpenCodePairingEncryptedCredentials,
|
||||
OpenCodePairingKeyOptions,
|
||||
} from './pairing'
|
||||
|
||||
export {
|
||||
OPENCODE_PINNED_VERSION,
|
||||
OPENCODE_SUPPORTED_VERSION_RANGE,
|
||||
@@ -109,8 +129,6 @@ export { OPENCODE_LIFECYCLE_STATES, OPENCODE_RECOVERY_ACTIONS } from './lifecycl
|
||||
|
||||
export type { OpenCodeLifecycleState, OpenCodeLifecycleStatus, OpenCodeRecoveryAction } from './lifecycle'
|
||||
|
||||
export const OPENCODE_SERVER_USERNAME = 'opencode'
|
||||
|
||||
export type OpenCodeApi = ReturnType<typeof OpenCode.make>
|
||||
|
||||
export interface OpenCodeApiOptions {
|
||||
@@ -120,8 +138,8 @@ export interface OpenCodeApiOptions {
|
||||
headers?: Record<string, string>
|
||||
}
|
||||
|
||||
export function buildOpenCodeBasicAuth(password: string): string {
|
||||
const bytes = new TextEncoder().encode(`${OPENCODE_SERVER_USERNAME}:${password}`)
|
||||
export function buildOpenCodeBasicAuth(password: string, username: string = OPENCODE_SERVER_USERNAME): string {
|
||||
const bytes = new TextEncoder().encode(`${username}:${password}`)
|
||||
let binary = ''
|
||||
for (const byte of bytes) binary += String.fromCharCode(byte)
|
||||
return `Basic ${btoa(binary)}`
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
const PAIRING_PROTOCOL = 'ocm-pair-v2'
|
||||
const PAIRING_PROOF_PREFIX = `${PAIRING_PROTOCOL}:`
|
||||
const PAIRING_KEY_INFO = `${PAIRING_PROTOCOL} credentials`
|
||||
const PAIRING_NONCE_BYTES = 32
|
||||
const PAIRING_IV_BYTES = 12
|
||||
const PAIRING_KEY_BITS = 256
|
||||
|
||||
const textEncoder = new TextEncoder()
|
||||
const textDecoder = new TextDecoder()
|
||||
|
||||
type WebCryptoKey = Awaited<ReturnType<typeof globalThis.crypto.subtle.importKey>>
|
||||
|
||||
export interface OpenCodePairingCredentials {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export interface OpenCodePairingEncryptedCredentials {
|
||||
iv: string
|
||||
ciphertext: string
|
||||
}
|
||||
|
||||
export interface OpenCodePairingKeyOptions {
|
||||
code: string
|
||||
nonce: string
|
||||
}
|
||||
|
||||
export interface OpenCodePairingDecryptOptions extends OpenCodePairingKeyOptions {
|
||||
iv: string
|
||||
ciphertext: string
|
||||
}
|
||||
|
||||
/** Generates a fresh 32-byte pairing nonce encoded as base64url. */
|
||||
export function createOpenCodePairingNonce(): string {
|
||||
return base64UrlEncode(globalThis.crypto.getRandomValues(new Uint8Array(PAIRING_NONCE_BYTES)))
|
||||
}
|
||||
|
||||
/** Reports whether a value is a base64url-encoded 32-byte pairing nonce. */
|
||||
export function isOpenCodePairingNonce(value: unknown): value is string {
|
||||
if (typeof value !== 'string') return false
|
||||
const bytes = base64UrlDecode(value)
|
||||
return bytes !== null && bytes.length === PAIRING_NONCE_BYTES
|
||||
}
|
||||
|
||||
/** Reports whether a value carries non-empty string credentials. */
|
||||
export function isOpenCodePairingCredentials(value: unknown): value is OpenCodePairingCredentials {
|
||||
if (typeof value !== 'object' || value === null) return false
|
||||
const { username, password } = value as { username?: unknown; password?: unknown }
|
||||
return typeof username === 'string' && username.length > 0 && typeof password === 'string'
|
||||
}
|
||||
|
||||
/** Computes the base64url HMAC-SHA256 proof that binds a pairing code to a nonce. */
|
||||
export async function createOpenCodePairingProof(code: string, nonce: string): Promise<string> {
|
||||
const key = await importHmacKey(code)
|
||||
const signature = await globalThis.crypto.subtle.sign('HMAC', key, pairingProofMessage(nonce))
|
||||
return base64UrlEncode(new Uint8Array(signature))
|
||||
}
|
||||
|
||||
/** Verifies a pairing proof in constant time through the Web Crypto HMAC verifier. */
|
||||
export async function verifyOpenCodePairingProof(code: string, nonce: string, proof: string): Promise<boolean> {
|
||||
const signature = base64UrlDecode(proof)
|
||||
if (signature === null) return false
|
||||
const key = await importHmacKey(code)
|
||||
try {
|
||||
return await globalThis.crypto.subtle.verify('HMAC', key, signature, pairingProofMessage(nonce))
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/** Encrypts pairing credentials with AES-256-GCM under a key derived from the code and nonce. */
|
||||
export async function encryptOpenCodePairingCredentials(
|
||||
options: OpenCodePairingKeyOptions & { credentials: OpenCodePairingCredentials },
|
||||
): Promise<OpenCodePairingEncryptedCredentials> {
|
||||
const key = await deriveCredentialsKey(options.code, options.nonce)
|
||||
const iv = globalThis.crypto.getRandomValues(new Uint8Array(PAIRING_IV_BYTES))
|
||||
const plaintext = encodeText(JSON.stringify({
|
||||
username: options.credentials.username,
|
||||
password: options.credentials.password,
|
||||
}))
|
||||
const ciphertext = await globalThis.crypto.subtle.encrypt(
|
||||
{ name: 'AES-GCM', iv, additionalData: pairingAdditionalData() },
|
||||
key,
|
||||
plaintext,
|
||||
)
|
||||
return {
|
||||
iv: base64UrlEncode(iv),
|
||||
ciphertext: base64UrlEncode(new Uint8Array(ciphertext)),
|
||||
}
|
||||
}
|
||||
|
||||
/** Decrypts pairing credentials, throwing when the payload is tampered with or malformed. */
|
||||
export async function decryptOpenCodePairingCredentials(
|
||||
options: OpenCodePairingDecryptOptions,
|
||||
): Promise<OpenCodePairingCredentials> {
|
||||
const iv = base64UrlDecode(options.iv)
|
||||
const ciphertext = base64UrlDecode(options.ciphertext)
|
||||
if (iv === null || iv.length !== PAIRING_IV_BYTES || ciphertext === null) {
|
||||
throw new Error('Invalid pairing payload')
|
||||
}
|
||||
const key = await deriveCredentialsKey(options.code, options.nonce)
|
||||
const plaintext = await globalThis.crypto.subtle.decrypt(
|
||||
{ name: 'AES-GCM', iv, additionalData: pairingAdditionalData() },
|
||||
key,
|
||||
ciphertext,
|
||||
)
|
||||
const parsed: unknown = JSON.parse(textDecoder.decode(plaintext))
|
||||
if (!isOpenCodePairingCredentials(parsed)) throw new Error('Invalid pairing payload')
|
||||
return parsed
|
||||
}
|
||||
|
||||
function encodeText(value: string): Uint8Array<ArrayBuffer> {
|
||||
return new Uint8Array(textEncoder.encode(value))
|
||||
}
|
||||
|
||||
function pairingProofMessage(nonce: string): Uint8Array<ArrayBuffer> {
|
||||
return encodeText(`${PAIRING_PROOF_PREFIX}${nonce}`)
|
||||
}
|
||||
|
||||
function pairingAdditionalData(): Uint8Array<ArrayBuffer> {
|
||||
return encodeText(PAIRING_PROTOCOL)
|
||||
}
|
||||
|
||||
async function importHmacKey(code: string): Promise<WebCryptoKey> {
|
||||
return globalThis.crypto.subtle.importKey(
|
||||
'raw',
|
||||
encodeText(code),
|
||||
{ name: 'HMAC', hash: 'SHA-256' },
|
||||
false,
|
||||
['sign', 'verify'],
|
||||
)
|
||||
}
|
||||
|
||||
async function deriveCredentialsKey(code: string, nonce: string): Promise<WebCryptoKey> {
|
||||
const keyMaterial = await globalThis.crypto.subtle.importKey('raw', encodeText(code), 'HKDF', false, ['deriveKey'])
|
||||
return globalThis.crypto.subtle.deriveKey(
|
||||
{
|
||||
name: 'HKDF',
|
||||
hash: 'SHA-256',
|
||||
salt: encodeText(nonce),
|
||||
info: encodeText(PAIRING_KEY_INFO),
|
||||
},
|
||||
keyMaterial,
|
||||
{ name: 'AES-GCM', length: PAIRING_KEY_BITS },
|
||||
false,
|
||||
['encrypt', 'decrypt'],
|
||||
)
|
||||
}
|
||||
|
||||
function base64UrlEncode(bytes: Uint8Array): string {
|
||||
let binary = ''
|
||||
for (const byte of bytes) binary += String.fromCharCode(byte)
|
||||
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
|
||||
}
|
||||
|
||||
function base64UrlDecode(value: string): Uint8Array<ArrayBuffer> | null {
|
||||
if (!/^[A-Za-z0-9_-]+={0,2}$/.test(value)) return null
|
||||
const base64 = value.replace(/-/g, '+').replace(/_/g, '/').replace(/=+$/, '')
|
||||
const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4)
|
||||
try {
|
||||
const binary = atob(padded)
|
||||
const bytes = new Uint8Array(binary.length)
|
||||
for (let index = 0; index < binary.length; index++) bytes[index] = binary.charCodeAt(index)
|
||||
return bytes
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const LOCAL_OPENCODE_SERVER_ID = "local";
|
||||
export const OPENCODE_SERVER_DEFAULT_USERNAME = "opencode";
|
||||
export const OPENCODE_SERVER_USERNAME = "opencode";
|
||||
export const OPENCODE_SERVER_LINK_PROTOCOL = "ocm:";
|
||||
export const OPENCODE_SERVER_LINK_VERSION = "1";
|
||||
export const OPENCODE_SERVER_LINK_VERSION = "2";
|
||||
|
||||
const HttpUrlSchema = z
|
||||
.string()
|
||||
@@ -31,7 +31,7 @@ export const CreateOpenCodeServerRequestSchema = z
|
||||
.object({
|
||||
name: z.string().trim().min(1).max(100),
|
||||
baseUrl: HttpUrlSchema,
|
||||
username: z.string().trim().min(1).max(100).default(OPENCODE_SERVER_DEFAULT_USERNAME),
|
||||
username: z.string().trim().min(1).max(100).default(OPENCODE_SERVER_USERNAME),
|
||||
password: z.string().min(1).max(1024).optional(),
|
||||
pairing: OpenCodeServerPairingSchema.optional(),
|
||||
enabled: z.boolean().default(true),
|
||||
@@ -51,7 +51,7 @@ export const UpdateOpenCodeServerRequestSchema = z
|
||||
|
||||
export const TestOpenCodeServerRequestSchema = z.object({
|
||||
baseUrl: HttpUrlSchema,
|
||||
username: z.string().trim().min(1).max(100).default(OPENCODE_SERVER_DEFAULT_USERNAME),
|
||||
username: z.string().trim().min(1).max(100).default(OPENCODE_SERVER_USERNAME),
|
||||
password: z.string().max(1024).optional(),
|
||||
});
|
||||
|
||||
@@ -100,13 +100,8 @@ export function buildOpenCodeServerLink(link: OpenCodeServerLink): string {
|
||||
|
||||
/** Parses an `ocm://link` URL from a QR code or pasted text; returns null when it is not a valid link. */
|
||||
export function parseOpenCodeServerLink(text: string): OpenCodeServerLink | null {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(text.trim());
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (url.protocol !== OPENCODE_SERVER_LINK_PROTOCOL || url.hostname !== "link") return null;
|
||||
const url = parseOpenCodeServerLinkUrl(text);
|
||||
if (!url) return null;
|
||||
if (url.searchParams.get("v") !== OPENCODE_SERVER_LINK_VERSION) return null;
|
||||
|
||||
const parsed = OpenCodeServerLinkSchema.safeParse({
|
||||
@@ -118,6 +113,22 @@ export function parseOpenCodeServerLink(text: string): OpenCodeServerLink | null
|
||||
return parsed.success ? parsed.data : null;
|
||||
}
|
||||
|
||||
/** Reads the `v` parameter of an `ocm://link` URL, or null when the text is not an `ocm://link` URL. */
|
||||
export function readOpenCodeServerLinkVersion(text: string): string | null {
|
||||
return parseOpenCodeServerLinkUrl(text)?.searchParams.get("v") ?? null;
|
||||
}
|
||||
|
||||
function parseOpenCodeServerLinkUrl(text: string): URL | null {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(text.trim());
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (url.protocol !== OPENCODE_SERVER_LINK_PROTOCOL || url.hostname !== "link") return null;
|
||||
return url;
|
||||
}
|
||||
|
||||
const REMOTE_DIRECTORY_KEY_PATTERN = /^(srv_[A-Za-z0-9_-]+)\|(.+)$/s;
|
||||
|
||||
/** Builds the event-stream subscription key for a directory; local directories stay unchanged. */
|
||||
|
||||
@@ -18,5 +18,6 @@ export type SSEVisibilityRequest = z.infer<typeof SSEVisibilitySchema>;
|
||||
|
||||
export interface SSEEventEnvelope {
|
||||
directory?: string | null;
|
||||
serverId?: string;
|
||||
payload: V2Event;
|
||||
}
|
||||
@@ -5,3 +5,4 @@ export * from './sandbox-command'
|
||||
export * from './semver'
|
||||
export * from './terminal-title'
|
||||
export * from './unified-diff'
|
||||
export * from './url'
|
||||
@@ -0,0 +1,4 @@
|
||||
/** Trims a URL and removes its trailing slashes. */
|
||||
export function trimUrlTrailingSlashes(url: string): string {
|
||||
return url.trim().replace(/\/+$/, '')
|
||||
}
|
||||
Reference in new issue
Block a user