Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628import http from 'http';import net from 'net';import { randomUUID } from 'crypto';import { WebSocket } from 'ws';import { IService, ServiceHealth } from './service-manager';import { CONFIG } from '../config';import { analyzeExecCommand, commandToText, ExecCommandKind, ExecMode, ExecRisk, maybePersistLargeOutput,} from './execution-core';
const DOCKER_SOCKET = '/var/run/docker.sock';const MAX_LOG_RESPONSE_BYTES = 5 * 1024 * 1024;const MIN_EXEC_RESPONSE_BYTES = 4 * 1024 * 1024;const CONTAINER_TERMINAL_MAX_MESSAGE_BYTES = 16 * 1024;
type DockerRequestOptions = { timeoutMs?: number; maxBytes?: number;};
export interface ContainerInfo { id: string; name: string; image: string; status: 'created' | 'running' | 'paused' | 'restarting' | 'removing' | 'exited' | 'dead'; created: string; ports: Array<{ container: number; host?: number; protocol: string }>; networks: string[]; allowInternet: boolean;}
export interface CreateContainerRequest { name: string; image: string; env?: string[]; allowInternet?: boolean; cpu?: number; memoryMb?: number;}
export interface ExecResult { success: boolean; stdout: string; stderr: string; exitCode: number; mode: ExecMode; command: string; workdir?: string; summary: string; kind: ExecCommandKind; risk: ExecRisk; noOutputExpected: boolean; normalizedPrefix?: string; semanticMessage?: string; truncated?: boolean; persistedOutputRef?: string;}
export interface BackgroundExecJob { id: string; scope: 'workspace' | 'container'; containerId?: string; command: string; workdir?: string; mode: ExecMode; summary: string; kind: ExecCommandKind; risk: ExecRisk; noOutputExpected: boolean; normalizedPrefix?: string; semanticMessage?: string; status: 'queued' | 'running' | 'completed' | 'failed' | 'cancelled'; stdout?: string; stderr?: string; exitCode?: number; truncated?: boolean; persistedOutputRef?: string; createdAt: string; startedAt?: string; completedAt?: string; updatedAt?: string;}
type BackgroundExecJobRecord = BackgroundExecJob & { containerId: string; jobDir: string;};
// Orchestrates Docker containers via the Engine API over the Unix socket.// Enforces image allowlists, resource caps, capability dropping, network// isolation, and namespace checks.export class ContainerManager implements IService { readonly name = 'containers'; readonly type = 'container' as const;
private containerTerminalSessions = new Set<{ destroy: () => void }>(); private backgroundJobs = new Map<string, BackgroundExecJobRecord>();
// --- Docker Engine API helpers ---
private dockerRequest( method: string, path: string, body?: any, requestOptions: DockerRequestOptions = {}, ): Promise<{ statusCode: number; data: any; rawData: Buffer }> { return new Promise((resolve, reject) => { let settled = false; const timeoutMs = requestOptions.timeoutMs && requestOptions.timeoutMs > 0 ? requestOptions.timeoutMs : undefined; const maxBytes = requestOptions.maxBytes && requestOptions.maxBytes > 0 ? requestOptions.maxBytes : undefined; const options: http.RequestOptions = { socketPath: DOCKER_SOCKET, path, method, headers: { 'Content-Type': 'application/json' }, };
let timeoutHandle: ReturnType<typeof setTimeout> | undefined; const settleResolve = (value: { statusCode: number; data: any; rawData: Buffer }) => { if (settled) return; settled = true; if (timeoutHandle) clearTimeout(timeoutHandle); resolve(value); }; const settleReject = (error: unknown) => { if (settled) return; settled = true; if (timeoutHandle) clearTimeout(timeoutHandle); reject(error); };
const req = http.request(options, (res) => { const chunks: Buffer[] = []; let totalBytes = 0; res.on('data', (chunk) => { if (settled) return; const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); totalBytes += buf.length; if (maxBytes && totalBytes > maxBytes) { res.destroy(); req.destroy(); settleReject(Object.assign( new Error(`Docker API response exceeded ${maxBytes} bytes`), { statusCode: 413 }, )); return; } chunks.push(buf); }); res.on('end', () => { if (settled) return; const rawData = Buffer.concat(chunks); let data: any; try { data = JSON.parse(rawData.toString()); } catch { data = rawData.toString(); } settleResolve({ statusCode: res.statusCode || 0, data, rawData }); }); res.on('error', (error) => { settleReject(error); }); });
req.on('error', settleReject);
if (timeoutMs) { timeoutHandle = setTimeout(() => { req.destroy(); settleReject(Object.assign( new Error(`Docker API request timed out after ${timeoutMs}ms`), { statusCode: 408 }, )); }, timeoutMs); }
if (body) { req.write(JSON.stringify(body)); } req.end(); }); }
private rawDockerRequest(method: string, path: string, body?: any): Promise<net.Socket> { return new Promise((resolve, reject) => { const options: http.RequestOptions = { socketPath: DOCKER_SOCKET, path, method, headers: { 'Content-Type': 'application/json', 'Upgrade': 'tcp', 'Connection': 'Upgrade', }, };
const req = http.request(options); req.on('upgrade', (_res, socket) => { resolve(socket as net.Socket); }); req.on('response', (res) => { // If we get a normal response instead of upgrade, read it const chunks: Buffer[] = []; res.on('data', (chunk) => chunks.push(chunk)); res.on('end', () => { reject(new Error(`Expected upgrade but got ${res.statusCode}: ${Buffer.concat(chunks).toString()}`)); }); }); req.on('error', reject);
if (body) { req.write(JSON.stringify(body)); } req.end(); }); }
// --- Validation ---
private validateImage(image: string): boolean { return CONFIG.CONTAINERS.IMAGE_ALLOWLIST.includes(image); }
private async enforceContainerLimit(): Promise<void> { const containers = await this.listManagedContainers(); if (containers.length >= CONFIG.CONTAINERS.MAX_CONTAINERS) { throw Object.assign( new Error(`Container limit reached (${CONFIG.CONTAINERS.MAX_CONTAINERS}). Remove a container first.`), { statusCode: 429 } ); } }
private sanitizeName(name: string): string { const stripped = name.replace(new RegExp(`^${CONFIG.CONTAINERS.NAMESPACE_PREFIX}`), ''); // Reject names that would not round-trip to the same Docker name. if (!/^[a-z0-9][a-z0-9-]*[a-z0-9]$/.test(stripped) && !/^[a-z0-9]$/.test(stripped)) { throw Object.assign( new Error('Invalid container name: use lowercase letters, digits, and hyphens (no leading/trailing hyphen)'), { statusCode: 400 }, ); } return CONFIG.CONTAINERS.NAMESPACE_PREFIX + stripped; }
// Strips env vars that could escalate privileges, leak host credentials, or hijack // resolution (LD_PRELOAD, NODE_OPTIONS, *_PROXY, socket paths, substrate/CI secrets). private sanitizeEnvVars(env: string[]): string[] { // Expanded blocklist: dangerous prefixes and specific variable names const BLOCKED_KEYS = [ 'DOCKER_', 'SUBSTRATE_', 'AWS_', 'GOOGLE_', 'AZURE_', 'KUBERNETES_', 'GCP_', 'GITHUB_', 'GITLAB_', 'CI_', 'BUILDKITE_', ]; const BLOCKED_EXACT = [ 'LD_PRELOAD', 'LD_LIBRARY_PATH', 'NODE_OPTIONS', 'NODE_EXTRA_CA_CERTS', 'HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'NO_PROXY', 'KUBECONFIG', 'SSH_AUTH_SOCK', ]; const socketPaths = ['/var/run/docker.sock', 'docker.sock'];
return env.filter(entry => { const eqIndex = entry.indexOf('='); if (eqIndex === -1 || eqIndex === 0) return false;
const key = entry.substring(0, eqIndex); const value = entry.substring(eqIndex + 1);
// Key must be valid env var name if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) return false;
const upperKey = key.toUpperCase();
// Block dangerous prefixes if (BLOCKED_KEYS.some(prefix => upperKey.startsWith(prefix))) return false; // Block dangerous exact names if (BLOCKED_EXACT.includes(upperKey)) return false; // Block socket paths in value if (socketPaths.some(p => value.includes(p))) return false; // Limit value length if (value.length > 4096) return false;
return true; }); }
private isOwnContainer(name: string): boolean { return name.startsWith(CONFIG.CONTAINERS.NAMESPACE_PREFIX) || name.startsWith('/' + CONFIG.CONTAINERS.NAMESPACE_PREFIX); }
private shellQuote(value: string): string { return `'${value.replace(/'/g, `'\\''`)}'`; }
private async buildExecResult(params: { mode: ExecMode; command: string | string[]; workdir?: string; stdout: string; stderr: string; exitCode: number; }): Promise<ExecResult> { const { mode, command, workdir, stdout, stderr, exitCode } = params; const analysis = analyzeExecCommand(mode, command, exitCode); const output = await maybePersistLargeOutput({ mode, command, workdir, stdout, stderr, exitCode, maxBytes: CONFIG.CONTAINERS.EXEC_MAX_OUTPUT_BYTES, });
return { success: analysis.semanticSuccess, stdout: output.stdout, stderr: output.stderr, exitCode, mode, command: commandToText(mode, command), workdir, summary: analysis.summary, kind: analysis.kind, risk: analysis.risk, noOutputExpected: analysis.noOutputExpected, normalizedPrefix: analysis.normalizedPrefix, semanticMessage: analysis.semanticMessage, truncated: output.truncated, persistedOutputRef: output.persistedOutputRef, }; }
private toBackgroundExecJob(record: BackgroundExecJobRecord): BackgroundExecJob { return { id: record.id, scope: record.scope, containerId: record.scope === 'container' ? record.containerId : undefined, command: record.command, workdir: record.workdir, mode: record.mode, summary: record.summary, kind: record.kind, risk: record.risk, noOutputExpected: record.noOutputExpected, normalizedPrefix: record.normalizedPrefix, semanticMessage: record.semanticMessage, status: record.status, stdout: record.stdout, stderr: record.stderr, exitCode: record.exitCode, truncated: record.truncated, persistedOutputRef: record.persistedOutputRef, createdAt: record.createdAt, startedAt: record.startedAt, completedAt: record.completedAt, updatedAt: record.updatedAt, }; }
private async readOptionalFile(containerId: string, filePath: string): Promise<string | undefined> { const result = await this.exec( containerId, ['sh', '-c', `cat ${this.shellQuote(filePath)} 2>/dev/null || true`], { timeoutMs: 5000, workdir: '/', mode: 'argv' }, ); const value = result.stdout?.trimEnd(); return value ? value : undefined; }
// Polls the sentinel files written by execBackground's wrapper script to decide whether // the job is still running, completed, cancelled, or failed, then hydrates stdout/stderr. private async refreshBackgroundJob(record: BackgroundExecJobRecord): Promise<BackgroundExecJobRecord> { if (record.status !== 'running') { return record; }
try { // Use newlines so POSIX sh accepts the `then`/`else` branches. const statusScript = [ `JOB_DIR=${this.shellQuote(record.jobDir)}`, 'if [ -f "$JOB_DIR/cancelledAt" ]; then', ' echo cancelled', 'elif [ -f "$JOB_DIR/done" ]; then', ' echo done', 'else', ' echo running', 'fi', ].join('\n'); const statusResult = await this.exec( record.containerId, ['sh', '-c', statusScript], { timeoutMs: 5000, workdir: '/', mode: 'argv' }, );
const state = statusResult.stdout.trim(); if (state === 'running') { const nextRecord = { ...record, updatedAt: new Date().toISOString(), }; this.backgroundJobs.set(record.id, nextRecord); return nextRecord; }
const [stdout, stderr, exitCodeRaw, completedAtRaw, cancelledAtRaw] = await Promise.all([ this.readOptionalFile(record.containerId, `${record.jobDir}/stdout`), this.readOptionalFile(record.containerId, `${record.jobDir}/stderr`), this.readOptionalFile(record.containerId, `${record.jobDir}/exitCode`), this.readOptionalFile(record.containerId, `${record.jobDir}/completedAt`), this.readOptionalFile(record.containerId, `${record.jobDir}/cancelledAt`), ]);
const exitCode = exitCodeRaw !== undefined ? parseInt(exitCodeRaw, 10) : undefined; const resolvedExitCode: number = typeof exitCode === 'number' && Number.isFinite(exitCode) ? exitCode : -1; const status: BackgroundExecJob['status'] = state === 'cancelled' || cancelledAtRaw || exitCode === 143 || exitCode === 137 ? 'cancelled' : exitCode === 0 ? 'completed' : 'failed';
const nextRecord = { ...record, status, ...(await maybePersistLargeOutput({ mode: record.mode, command: record.command, workdir: record.workdir, stdout: stdout || '', stderr: stderr || '', exitCode: resolvedExitCode, maxBytes: CONFIG.CONTAINERS.EXEC_MAX_OUTPUT_BYTES, })), exitCode: Number.isFinite(exitCode) ? exitCode : undefined, completedAt: completedAtRaw || record.completedAt || new Date().toISOString(), updatedAt: new Date().toISOString(), };
this.backgroundJobs.set(record.id, nextRecord); return nextRecord; } catch (error) { const nextRecord = { ...record, status: 'failed' as const, stderr: error instanceof Error ? error.message : 'Failed to refresh background job', completedAt: new Date().toISOString(), updatedAt: new Date().toISOString(), }; this.backgroundJobs.set(record.id, nextRecord); return nextRecord; } }
private pruneBackgroundJobs(): void { const jobs = Array.from(this.backgroundJobs.values()) .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()); for (const [index, job] of jobs.entries()) { if (index < 100) continue; this.backgroundJobs.delete(job.id); } }
private extractContainerInfo(c: any): ContainerInfo { const name = (c.Name || (c.Names && c.Names[0]) || '').replace(/^\//, ''); const networks = c.NetworkSettings?.Networks ? Object.keys(c.NetworkSettings.Networks) : (c.HostConfig?.NetworkMode ? [c.HostConfig.NetworkMode] : []); const hasInternet = networks.includes(CONFIG.CONTAINERS.NETWORK_NAME + '-external');
const status: ContainerInfo['status'] = c.State?.Status || c.State || (typeof c.Status === 'string' ? (c.Status.toLowerCase().includes('up') ? 'running' : 'exited') : 'created');
return { id: (c.Id || '').substring(0, 12), name, image: c.Image || c.Config?.Image || '', status, created: c.Created ? new Date(typeof c.Created === 'number' ? c.Created * 1000 : c.Created).toISOString() : '', ports: (c.Ports || []).map((p: any) => ({ container: p.PrivatePort, host: p.PublicPort, protocol: p.Type || 'tcp', })), networks, allowInternet: hasInternet, }; }
// --- Public API ---
async create(req: CreateContainerRequest): Promise<ContainerInfo> { if (!this.validateImage(req.image)) { throw Object.assign(new Error(`Image '${req.image}' is not in the allowlist`), { statusCode: 400 }); }
await this.enforceContainerLimit();
const containerName = this.sanitizeName(req.name); // Default env vars that ensure dev servers bind to all interfaces so the // proxy can reach them via the container's Docker IP. User-supplied vars // override these defaults. const defaultEnv = [ 'HOST=0.0.0.0', // Generic (Vite, Nuxt, many others) 'HOSTNAME=0.0.0.0', // Some frameworks respect HOSTNAME ]; const userEnv = req.env ? this.sanitizeEnvVars(req.env) : []; const userKeys = new Set(userEnv.map((e: string) => e.split('=')[0])); const env = [ ...defaultEnv.filter((e) => !userKeys.has(e.split('=')[0])), ...userEnv, ]; const cpu = Math.min(req.cpu || CONFIG.CONTAINERS.MAX_CPU, CONFIG.CONTAINERS.MAX_CPU); const memoryMb = Math.min(req.memoryMb || CONFIG.CONTAINERS.MAX_MEMORY_MB, CONFIG.CONTAINERS.MAX_MEMORY_MB); const networkName = CONFIG.CONTAINERS.NETWORK_NAME;
// Pull image first console.log(`[Containers] Pulling image: ${req.image}`); const pullRes = await this.dockerRequest('POST', `/images/create?fromImage=${encodeURIComponent(req.image)}`); if (pullRes.statusCode !== 200) { // 200 means streaming response, which is normal for pull // Check for actual errors if (pullRes.statusCode && pullRes.statusCode >= 400) { throw new Error(`Failed to pull image '${req.image}': ${JSON.stringify(pullRes.data)}`); } }
// Create container const createBody = { Hostname: containerName.replace(CONFIG.CONTAINERS.NAMESPACE_PREFIX, ''), Image: req.image, Env: env, Labels: { 'com.docker.compose.project': 'aetheros-containers', 'managed-by': 'aetheros-substrate', }, HostConfig: { NanoCPUs: Math.floor(cpu * 1e9), Memory: memoryMb * 1024 * 1024, MemorySwap: memoryMb * 1024 * 1024, // No swap PidsLimit: CONFIG.CONTAINERS.PIDS_LIMIT, Privileged: false, CapDrop: ['ALL'], CapAdd: ['CHOWN', 'DAC_OVERRIDE', 'FOWNER', 'SETGID', 'SETUID', 'NET_BIND_SERVICE'], SecurityOpt: ['no-new-privileges'], Binds: [] as string[], Devices: [] as any[], ExtraHosts: [] as string[], NetworkMode: networkName, }, NetworkingConfig: { EndpointsConfig: { [networkName]: {}, }, }, // Create writable /workspace directory and keep container alive Cmd: ['sh', '-c', 'mkdir -p /workspace && chmod 777 /workspace && trap "exit 0" TERM; while true; do sleep 1; done'], OpenStdin: true, Tty: true, };
console.log(`[Containers] Creating container: ${containerName}`); const createRes = await this.dockerRequest('POST', `/containers/create?name=${encodeURIComponent(containerName)}`, createBody); if (createRes.statusCode !== 201) { throw new Error(`Failed to create container: ${JSON.stringify(createRes.data)}`); }
const containerId = createRes.data.Id;
// Connect to external network if internet is allowed if (req.allowInternet) { const externalNetwork = CONFIG.CONTAINERS.NETWORK_NAME + '-external'; await this.dockerRequest('POST', `/networks/${encodeURIComponent(externalNetwork)}/connect`, { Container: containerId, }).catch(err => { console.warn(`[Containers] Failed to connect to external network: ${err.message}`); }); }
// Start container const startRes = await this.dockerRequest('POST', `/containers/${containerId}/start`); if (startRes.statusCode !== 204 && startRes.statusCode !== 304) { throw new Error(`Failed to start container: ${JSON.stringify(startRes.data)}`); }
return this.inspect(containerId.substring(0, 12)); }
async startContainer(id: string): Promise<ContainerInfo> { const info = await this.inspect(id); if (!this.isOwnContainer(info.name)) { throw Object.assign(new Error('Access denied'), { statusCode: 403 }); }
const res = await this.dockerRequest('POST', `/containers/${id}/start`); if (res.statusCode !== 204 && res.statusCode !== 304) { throw new Error(`Failed to start container: ${JSON.stringify(res.data)}`); }
return this.inspect(id); }
async stopContainer(id: string): Promise<ContainerInfo> { const info = await this.inspect(id); if (!this.isOwnContainer(info.name)) { throw Object.assign(new Error('Access denied'), { statusCode: 403 }); }
const res = await this.dockerRequest('POST', `/containers/${id}/stop?t=${CONFIG.CONTAINERS.STOP_TIMEOUT_S}`); if (res.statusCode !== 204 && res.statusCode !== 304) { throw new Error(`Failed to stop container: ${JSON.stringify(res.data)}`); }
return this.inspect(id); }
async remove(id: string): Promise<void> { const info = await this.inspect(id); if (!this.isOwnContainer(info.name)) { throw Object.assign(new Error('Access denied'), { statusCode: 403 }); }
// Stop first if running if (info.status === 'running') { await this.dockerRequest('POST', `/containers/${id}/stop?t=${CONFIG.CONTAINERS.STOP_TIMEOUT_S}`).catch(() => {}); }
const res = await this.dockerRequest('DELETE', `/containers/${id}?force=true`); if (res.statusCode !== 204) { throw new Error(`Failed to remove container: ${JSON.stringify(res.data)}`); } }
async list(): Promise<ContainerInfo[]> { return this.listManagedContainers(); }
private async listManagedContainers(): Promise<ContainerInfo[]> { const res = await this.dockerRequest('GET', '/containers/json?all=true'); if (res.statusCode !== 200) { throw new Error('Failed to list containers'); }
const allContainers: any[] = res.data; return allContainers .filter((c: any) => { const name = (c.Names && c.Names[0]) || ''; return this.isOwnContainer(name); }) .map(c => this.extractContainerInfo(c)); }
async inspect(id: string): Promise<ContainerInfo> { const res = await this.dockerRequest('GET', `/containers/${id}/json`); if (res.statusCode === 404) { throw Object.assign(new Error('Container not found'), { statusCode: 404 }); } if (res.statusCode !== 200) { throw new Error(`Failed to inspect container: ${JSON.stringify(res.data)}`); }
const info = this.extractContainerInfo(res.data);
// Verify it's our container if (!this.isOwnContainer(info.name)) { throw Object.assign(new Error('Access denied'), { statusCode: 403 }); }
return info; }
async logs(id: string, opts: { tail?: number; since?: string } = {}): Promise<string> { const info = await this.inspect(id); // validates ownership if (!this.isOwnContainer(info.name)) { throw Object.assign(new Error('Access denied'), { statusCode: 403 }); }
const params = new URLSearchParams({ stdout: 'true', stderr: 'true', tail: String(Math.min(Math.max(opts.tail || 100, 1), 1000)), }); if (opts.since) { params.set('since', opts.since); }
const res = await this.dockerRequest( 'GET', `/containers/${id}/logs?${params}`, undefined, { maxBytes: MAX_LOG_RESPONSE_BYTES }, ); if (res.statusCode !== 200) { throw new Error('Failed to get logs'); }
// Docker log stream has 8-byte header per frame — strip them return this.stripDockerLogHeaders(res.rawData); }
private stripDockerLogHeaders(buf: Buffer): string { const lines: string[] = []; let offset = 0; while (offset + 8 <= buf.length) { const size = buf.readUInt32BE(offset + 4); if (offset + 8 + size > buf.length) break; lines.push(buf.subarray(offset + 8, offset + 8 + size).toString('utf8')); offset += 8 + size; } // If no headers detected, return raw string if (lines.length === 0) return buf.toString('utf8'); return lines.join(''); }
async exec( id: string, command: string[], options: { timeoutMs?: number; workdir?: string; mode?: ExecMode; commandText?: string; } = {}, ): Promise<ExecResult> { const info = await this.inspect(id); // validates ownership if (!this.isOwnContainer(info.name)) { throw Object.assign(new Error('Access denied'), { statusCode: 403 }); }
if (info.status !== 'running') { throw Object.assign(new Error('Container is not running'), { statusCode: 400 }); }
// Create exec instance — default to /workspace, fall back to / if it doesn't exist const useWorkdir = options.workdir || '/workspace'; const execBody: any = { AttachStdout: true, AttachStderr: true, Cmd: command, WorkingDir: useWorkdir, }; let createRes = await this.dockerRequest('POST', `/containers/${id}/exec`, execBody);
// If workdir doesn't exist, retry with root if (createRes.statusCode !== 201 && useWorkdir !== '/' && !options.workdir) { execBody.WorkingDir = '/'; createRes = await this.dockerRequest('POST', `/containers/${id}/exec`, execBody); }
if (createRes.statusCode !== 201) { throw new Error('Failed to execute command'); }
const execId = createRes.data.Id;
// Bound the raw Docker stream in memory. maybePersistLargeOutput() runs after // this, so without an earlier hard ceiling a single verbose command could // exhaust the API before truncation/persistence logic ever runs. const timeoutMs = options.timeoutMs ?? CONFIG.CONTAINERS.EXEC_TIMEOUT_MS; const maxResponseBytes = Math.max( CONFIG.CONTAINERS.EXEC_MAX_OUTPUT_BYTES * 4, MIN_EXEC_RESPONSE_BYTES, );
// Start exec and capture output const startRes = await this.dockerRequest( 'POST', `/exec/${execId}/start`, { Detach: false, Tty: false, }, { timeoutMs, maxBytes: maxResponseBytes, }, );
// Parse multiplexed stream (stdout=1, stderr=2) const rawBuf = startRes.rawData; let stdout = ''; let stderr = ''; let pos = 0;
while (pos + 8 <= rawBuf.length) { const streamType = rawBuf[pos]; const size = rawBuf.readUInt32BE(pos + 4); if (pos + 8 + size > rawBuf.length) break; const text = rawBuf.subarray(pos + 8, pos + 8 + size).toString('utf8'); if (streamType === 1) stdout += text; else if (streamType === 2) stderr += text; pos += 8 + size; }
// If no headers found, treat as stdout if (pos === 0 && rawBuf.length > 0) { stdout = rawBuf.toString('utf8'); }
// Get exit code const inspectRes = await this.dockerRequest('GET', `/exec/${execId}/json`); const exitCode = inspectRes.data?.ExitCode ?? -1;
return this.buildExecResult({ mode: options.mode || 'argv', command: options.commandText || command, workdir: execBody.WorkingDir, stdout, stderr, exitCode, }); }
// Runs a command detached inside the container. Detached Docker execs drop exit code and // output, so the wrapper script persists pid/stdout/stderr/exitCode to $JOB_DIR for later // polling by refreshBackgroundJob. async execBackground( id: string, command: string[], options: { scope: 'workspace' | 'container'; workdir?: string; mode?: ExecMode; commandText?: string; }, ): Promise<BackgroundExecJob> { const info = await this.inspect(id); if (!this.isOwnContainer(info.name)) { throw Object.assign(new Error('Access denied'), { statusCode: 403 }); }
if (info.status !== 'running') { throw Object.assign(new Error('Container is not running'), { statusCode: 400 }); }
const jobId = randomUUID(); const jobDir = `/tmp/aether-jobs/${jobId}`; const workdir = options.workdir || '/workspace'; const mode = options.mode || 'argv'; const commandPreview = options.commandText || commandToText(mode, command); const analysis = analyzeExecCommand(mode, commandPreview); const commandScript = command.map((segment) => this.shellQuote(segment)).join(' '); // EXIT writes the completion sentinel; avoids PID-reuse races. const shellScript = [ `JOB_DIR=${this.shellQuote(jobDir)}`, 'mkdir -p "$JOB_DIR"', 'touch "$JOB_DIR/stdout" "$JOB_DIR/stderr"', `printf '%s' "$$" > "$JOB_DIR/pid"`, // Finalise on EXIT so a killed wrapper still marks the job terminated. // SIGKILL bypasses traps — that's the only remaining orphan case. 'trap \'rc=$?; printf "%s" "$rc" > "$JOB_DIR/exitCode"; date -u +"%Y-%m-%dT%H:%M:%SZ" > "$JOB_DIR/completedAt"; : > "$JOB_DIR/done"\' EXIT', `TARGET_DIR=${this.shellQuote(workdir)}`, 'if [ -d "$TARGET_DIR" ]; then cd "$TARGET_DIR"; else cd /; fi', `${commandScript} >"$JOB_DIR/stdout" 2>"$JOB_DIR/stderr"`, 'STATUS=$?', 'exit "$STATUS"', ].join('; ');
const createRes = await this.dockerRequest('POST', `/containers/${id}/exec`, { AttachStdout: false, AttachStderr: false, Cmd: ['sh', '-lc', shellScript], WorkingDir: '/', });
if (createRes.statusCode !== 201) { throw new Error('Failed to create background exec'); }
const execId = createRes.data.Id; const startRes = await this.dockerRequest('POST', `/exec/${execId}/start`, { Detach: true, Tty: false, });
if (startRes.statusCode !== 200) { throw new Error('Failed to start background exec'); }
const now = new Date().toISOString(); const record: BackgroundExecJobRecord = { id: jobId, scope: options.scope, containerId: id, command: commandPreview, workdir, mode, summary: analysis.summary, kind: analysis.kind, risk: analysis.risk, noOutputExpected: analysis.noOutputExpected, normalizedPrefix: analysis.normalizedPrefix, semanticMessage: analysis.semanticMessage, status: 'running', createdAt: now, startedAt: now, updatedAt: now, jobDir, };
this.backgroundJobs.set(jobId, record); this.pruneBackgroundJobs(); return this.toBackgroundExecJob(record); }
async listBackgroundJobs(scope: 'workspace' | 'container', containerId?: string): Promise<BackgroundExecJob[]> { const candidates = Array.from(this.backgroundJobs.values()) .filter((job) => job.scope === scope && (scope === 'workspace' || job.containerId === containerId), );
const refreshed = await Promise.all(candidates.map((job) => this.refreshBackgroundJob(job))); return refreshed .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()) .map((job) => this.toBackgroundExecJob(job)); }
async getBackgroundJob(jobId: string, scope: 'workspace' | 'container', containerId?: string): Promise<BackgroundExecJob> { const record = this.backgroundJobs.get(jobId); if (!record || record.scope !== scope || (scope === 'container' && record.containerId !== containerId)) { throw Object.assign(new Error('Background job not found'), { statusCode: 404 }); }
const refreshed = await this.refreshBackgroundJob(record); return this.toBackgroundExecJob(refreshed); }
async cancelBackgroundJob(jobId: string, scope: 'workspace' | 'container', containerId?: string): Promise<BackgroundExecJob> { const record = this.backgroundJobs.get(jobId); if (!record || record.scope !== scope || (scope === 'container' && record.containerId !== containerId)) { throw Object.assign(new Error('Background job not found'), { statusCode: 404 }); }
if (record.status === 'running') { await this.exec( record.containerId, [ 'sh', '-c', [ `JOB_DIR=${this.shellQuote(record.jobDir)}`, 'if [ -f "$JOB_DIR/pid" ]; then', ' kill "$(cat "$JOB_DIR/pid")" 2>/dev/null || true', 'fi', 'date -u +"%Y-%m-%dT%H:%M:%SZ" > "$JOB_DIR/cancelledAt"', ].join('; '), ], { timeoutMs: 5000, workdir: '/', mode: 'argv' }, ).catch(() => undefined); }
const updated: BackgroundExecJobRecord = { ...record, status: 'cancelled', completedAt: new Date().toISOString(), updatedAt: new Date().toISOString(), }; this.backgroundJobs.set(jobId, updated); const refreshed = await this.refreshBackgroundJob(updated); return this.toBackgroundExecJob(refreshed); }
async createTerminalSession(containerId: string, ws: WebSocket): Promise<void> { const info = await this.inspect(containerId); if (!this.isOwnContainer(info.name)) { ws.send(JSON.stringify({ type: 'error', message: 'Access denied' })); ws.close(1008, 'Access denied'); return; }
if (info.status !== 'running') { ws.send(JSON.stringify({ type: 'error', message: 'Container is not running' })); ws.close(1008, 'Container not running'); return; }
// Create exec instance for interactive shell // Start in /workspace, try bash first, fall back to sh. const termExecBody = { AttachStdin: true, AttachStdout: true, AttachStderr: true, Tty: true, WorkingDir: '/workspace', ConsoleSize: [24, 120], Env: ['TERM=xterm-256color', 'COLORTERM=truecolor', 'COLUMNS=120', 'LINES=24'], Cmd: ['/bin/sh', '-c', 'if command -v bash > /dev/null 2>&1; then exec bash -l; else exec sh; fi'], }; let createRes = await this.dockerRequest('POST', `/containers/${containerId}/exec`, termExecBody);
// If /workspace doesn't exist, fall back to / if (createRes.statusCode !== 201) { termExecBody.WorkingDir = '/'; createRes = await this.dockerRequest('POST', `/containers/${containerId}/exec`, termExecBody); }
if (createRes.statusCode !== 201) { console.warn( `[Terminal] exec create failed for ${containerId}: ${JSON.stringify(createRes.data)}`, ); ws.send(JSON.stringify({ type: 'error', message: 'Failed to create terminal session' })); ws.close(1011, 'Exec create failed'); return; }
const execId = createRes.data.Id;
// Start exec with hijack (upgrade to raw TCP) let socket: net.Socket; try { socket = await this.rawDockerRequest('POST', `/exec/${execId}/start`, { Detach: false, Tty: true, }); } catch (err) { console.warn( `[Terminal] exec start failed for ${containerId}/${execId}: ${err instanceof Error ? err.message : String(err)}`, ); ws.send(JSON.stringify({ type: 'error', message: 'Failed to start terminal session' })); ws.close(1011, 'Exec start failed'); return; }
// Idle timeout — kill session after 30 minutes of no input const IDLE_TIMEOUT_MS = 30 * 60 * 1000; let idleTimer = setTimeout(() => { if (ws.readyState === WebSocket.OPEN) { ws.send(JSON.stringify({ type: 'error', message: 'Session timed out due to inactivity' })); ws.close(1000, 'Idle timeout'); } socket.destroy(); }, IDLE_TIMEOUT_MS);
const resetIdleTimer = () => { clearTimeout(idleTimer); idleTimer = setTimeout(() => { if (ws.readyState === WebSocket.OPEN) { ws.send(JSON.stringify({ type: 'error', message: 'Session timed out due to inactivity' })); ws.close(1000, 'Idle timeout'); } socket.destroy(); }, IDLE_TIMEOUT_MS); };
const session = { destroy() { clearTimeout(idleTimer); socket.destroy(); } }; this.containerTerminalSessions.add(session);
// Bridge Docker TCP stream <-> WebSocket socket.on('data', (data: Buffer) => { if (ws.readyState === WebSocket.OPEN) { ws.send(JSON.stringify({ type: 'output', data: data.toString('utf8') })); } });
socket.on('end', () => { clearTimeout(idleTimer); this.containerTerminalSessions.delete(session); if (ws.readyState === WebSocket.OPEN) { ws.close(1000, 'Container session ended'); } });
socket.on('error', (err) => { clearTimeout(idleTimer); this.containerTerminalSessions.delete(session); console.warn( `[Terminal] exec stream failed for ${containerId}/${execId}: ${err instanceof Error ? err.message : String(err)}`, ); if (ws.readyState === WebSocket.OPEN) { ws.send(JSON.stringify({ type: 'error', message: 'Container terminal stream failed' })); ws.close(1011, 'Container session error'); } });
ws.on('message', (msg: string) => { if (Buffer.byteLength(msg.toString()) > CONTAINER_TERMINAL_MAX_MESSAGE_BYTES) { ws.send(JSON.stringify({ type: 'error', message: 'Terminal message too large' })); ws.close(1009, 'Message too large'); socket.destroy(); return; } try { const parsed = JSON.parse(msg.toString()); if (parsed.type === 'ping') { // Heartbeat — reset idle timer and ack so client can detect half-open // sockets (NAT/proxy idle drops). resetIdleTimer(); if (ws.readyState === WebSocket.OPEN) { ws.send(JSON.stringify({ type: 'pong', ts: Date.now() })); } return; } resetIdleTimer(); if (parsed.type === 'input' && typeof parsed.data === 'string') { socket.write(parsed.data); } else if (parsed.type === 'resize' && parsed.data) { // Resize exec TTY — log failures rather than swallowing them so // operators can see when Docker resize signalling is broken. this.dockerRequest('POST', `/exec/${execId}/resize?h=${parsed.data.rows}&w=${parsed.data.cols}`) .catch((err) => { console.warn( `[Terminal] resize failed for exec ${execId} (${parsed.data.rows}x${parsed.data.cols}): ${err instanceof Error ? err.message : String(err)}`, ); }); } } catch { // Plain text — write directly socket.write(msg.toString()); } });
ws.on('close', () => { clearTimeout(idleTimer); this.containerTerminalSessions.delete(session); socket.destroy(); });
ws.send(JSON.stringify({ type: 'connected', service: 'container-terminal', containerId })); }
// --- Port Proxy API ---
// Resolve a running managed container's internal-network IP. async getContainerIp(id: string): Promise<string> { const res = await this.dockerRequest('GET', `/containers/${id}/json`); if (res.statusCode === 404) { throw Object.assign(new Error('Container not found'), { statusCode: 404 }); } if (res.statusCode !== 200) { throw new Error(`Failed to inspect container: ${JSON.stringify(res.data)}`); }
const info = this.extractContainerInfo(res.data); if (!this.isOwnContainer(info.name)) { throw Object.assign(new Error('Access denied'), { statusCode: 403 }); } if (info.status !== 'running') { throw Object.assign(new Error('Container is not running'), { statusCode: 400 }); }
const networkName = CONFIG.CONTAINERS.NETWORK_NAME; const networks = res.data.NetworkSettings?.Networks; const ip = networks?.[networkName]?.IPAddress; if (!ip) { throw Object.assign(new Error('Container has no IP on the internal network'), { statusCode: 400 }); }
return ip; }
// Proxy a request to a container port. proxyRequest( containerIp: string, port: number, method: string, path: string, headers: Record<string, string | string[] | undefined>, body?: Buffer, ): Promise<http.IncomingMessage> { return new Promise((resolve, reject) => { // Strip hop-by-hop headers const hopByHop = new Set([ 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization', 'te', 'trailer', 'transfer-encoding', 'upgrade', 'host', 'x-substrate-key', // Don't leak the API key to the container 'authorization', 'cookie', ]); const forwardHeaders: Record<string, string | string[]> = {}; const maybeHeaders = headers as unknown as { entries?: () => IterableIterator<[string, string]>; forEach?: (callback: (value: string, key: string) => void) => void; }; const headerEntries = typeof maybeHeaders.forEach === 'function' && typeof maybeHeaders.entries === 'function' ? Array.from(maybeHeaders.entries()) : Object.entries(headers); for (const [key, val] of headerEntries) { if (val !== undefined && !hopByHop.has(key.toLowerCase())) { forwardHeaders[key] = val; } } forwardHeaders['host'] = `${containerIp}:${port}`;
const req = http.request({ hostname: containerIp, port, path, method, headers: forwardHeaders, timeout: 30_000, }, (res) => { resolve(res); });
req.on('error', (err) => { reject(Object.assign(new Error(`Proxy connection failed: ${err.message}`), { statusCode: 502 })); });
req.on('timeout', () => { req.destroy(); reject(Object.assign(new Error('Proxy request timed out'), { statusCode: 504 })); });
if (body) { req.write(body); } req.end(); }); }
// --- Update API ---
async setInternetAccess(id: string, allow: boolean): Promise<ContainerInfo> { const info = await this.inspect(id); if (!this.isOwnContainer(info.name)) { throw Object.assign(new Error('Access denied'), { statusCode: 403 }); }
const externalNetwork = CONFIG.CONTAINERS.NETWORK_NAME + '-external';
if (allow && !info.allowInternet) { // Connect to external network const res = await this.dockerRequest('POST', `/networks/${encodeURIComponent(externalNetwork)}/connect`, { Container: id, }); if (res.statusCode !== 200 && res.statusCode !== 201) { throw new Error(`Failed to enable internet: ${JSON.stringify(res.data)}`); } } else if (!allow && info.allowInternet) { // Disconnect from external network const res = await this.dockerRequest('POST', `/networks/${encodeURIComponent(externalNetwork)}/disconnect`, { Container: id, Force: false, }); if (res.statusCode !== 200) { throw new Error(`Failed to disable internet: ${JSON.stringify(res.data)}`); } }
return this.inspect(id); }
// --- Path validation ---
// Guards paths passed into shell-quoted exec commands. Rejects traversal, NULs, newlines, // and shell metacharacters — our single-quote wrapping isn't enough on its own. private validatePath(p: string): string { const normalized = p.replace(/\\/g, '/'); if (normalized.includes('..') || normalized.includes('\0')) { throw Object.assign(new Error('Invalid path: traversal not allowed'), { statusCode: 400 }); } // Reject shell metacharacters that could break out of single-quoted shell strings if (/[;|&$`!"'<>{}()\n\r]/.test(normalized)) { throw Object.assign(new Error('Invalid path: contains forbidden characters'), { statusCode: 400 }); } if (normalized.length > 1024) { throw Object.assign(new Error('Path too long'), { statusCode: 400 }); } return normalized; }
// --- Workspace API ---
async getOrCreateWorkspace(): Promise<ContainerInfo> { if (!CONFIG.CONTAINERS.ENABLED) { throw Object.assign( new Error('Container orchestration must be enabled for workspace. Set SUBSTRATE_ENABLE_CONTAINERS=true.'), { statusCode: 403 }, ); } if (!CONFIG.WORKSPACE.ENABLED) { throw Object.assign( new Error('Agent workspace is disabled. Set SUBSTRATE_ENABLE_WORKSPACE=true to enable.'), { statusCode: 403 }, ); }
const wsName = CONFIG.WORKSPACE.CONTAINER_NAME; const fullName = CONFIG.CONTAINERS.NAMESPACE_PREFIX + wsName;
// Check if workspace already exists const containers = await this.listManagedContainers(); const existing = containers.find((c) => c.name === fullName); if (existing) { if (existing.status !== 'running') { await this.startContainer(existing.id); return this.inspect(existing.id); } return existing; }
// Pull image const image = CONFIG.WORKSPACE.IMAGE; console.log(`[Workspace] Pulling image: ${image}`); await this.dockerRequest('POST', `/images/create?fromImage=${encodeURIComponent(image)}`);
const networkName = CONFIG.CONTAINERS.NETWORK_NAME; const cpu = Math.min(CONFIG.WORKSPACE.CPU, CONFIG.CONTAINERS.MAX_CPU * 2); const memoryMb = Math.min(CONFIG.WORKSPACE.MEMORY_MB, CONFIG.CONTAINERS.MAX_MEMORY_MB * 2);
const createBody = { Hostname: wsName, Image: image, Env: [] as string[], Labels: { 'com.docker.compose.project': 'aetheros-containers', 'managed-by': 'aetheros-substrate', }, HostConfig: { NanoCPUs: Math.floor(cpu * 1e9), Memory: memoryMb * 1024 * 1024, MemorySwap: memoryMb * 1024 * 1024, PidsLimit: CONFIG.CONTAINERS.PIDS_LIMIT, Privileged: false, CapDrop: ['ALL'], CapAdd: ['CHOWN', 'DAC_OVERRIDE', 'FOWNER', 'SETGID', 'SETUID', 'NET_BIND_SERVICE'], SecurityOpt: ['no-new-privileges'], Binds: [`${CONFIG.WORKSPACE.VOLUME_NAME}:${CONFIG.WORKSPACE.MOUNT_PATH}`], Devices: [] as any[], ExtraHosts: [] as string[], NetworkMode: networkName, }, NetworkingConfig: { EndpointsConfig: { [networkName]: {} }, }, Cmd: ['sh', '-c', `mkdir -p ${CONFIG.WORKSPACE.MOUNT_PATH} && trap "exit 0" TERM; while true; do sleep 1; done`], OpenStdin: true, Tty: true, };
console.log(`[Workspace] Creating workspace container: ${fullName}`); const createRes = await this.dockerRequest('POST', `/containers/create?name=${encodeURIComponent(fullName)}`, createBody); if (createRes.statusCode !== 201) { throw new Error(`Failed to create workspace: ${JSON.stringify(createRes.data)}`); }
const containerId = createRes.data.Id; const startRes = await this.dockerRequest('POST', `/containers/${containerId}/start`); if (startRes.statusCode !== 204 && startRes.statusCode !== 304) { throw new Error(`Failed to start workspace: ${JSON.stringify(startRes.data)}`); }
return this.inspect(containerId.substring(0, 12)); }
async writeWorkspaceFile(filePath: string, content: string): Promise<void> { const safePath = this.validatePath(filePath); const ws = await this.getOrCreateWorkspace(); const fullPath = `${CONFIG.WORKSPACE.MOUNT_PATH}/${safePath.replace(/^\//, '')}`; const dir = fullPath.substring(0, fullPath.lastIndexOf('/')); if (dir) { await this.exec(ws.id, ['mkdir', '-p', dir]); } const b64 = Buffer.from(content, 'utf-8').toString('base64'); // Pass b64 and target path as positional sh args (`$1`, `$2`) so neither // can be interpreted as shell syntax. b64 is already in the safe alphabet, // but this pattern keeps us robust if validatePath is ever relaxed. await this.exec(ws.id, [ 'sh', '-c', 'printf %s "$1" | base64 -d > "$2"', 'sh', b64, fullPath, ]); }
async readWorkspaceFile(filePath: string): Promise<string> { const safePath = this.validatePath(filePath); const ws = await this.getOrCreateWorkspace(); const fullPath = `${CONFIG.WORKSPACE.MOUNT_PATH}/${safePath.replace(/^\//, '')}`; const result = await this.exec(ws.id, ['cat', fullPath]); if (!result.success) { throw Object.assign(new Error('Failed to read file'), { statusCode: 404 }); } return result.stdout; }
async listWorkspaceFiles(filePath: string = '/'): Promise<string> { const safePath = this.validatePath(filePath); const ws = await this.getOrCreateWorkspace(); const fullPath = `${CONFIG.WORKSPACE.MOUNT_PATH}/${safePath.replace(/^\//, '')}`; const result = await this.exec(ws.id, ['ls', '-la', fullPath]); if (!result.success) { throw Object.assign(new Error('Failed to list directory'), { statusCode: 404 }); } return result.stdout; }
// --- Container file operations (for user-created containers) ---
async writeContainerFile(containerId: string, filePath: string, content: string): Promise<void> { const safePath = this.validatePath(filePath); const fullPath = safePath.startsWith('/') ? safePath : `/workspace/${safePath}`; const dir = fullPath.substring(0, fullPath.lastIndexOf('/')); if (dir) { await this.exec(containerId, ['mkdir', '-p', dir]); } const b64 = Buffer.from(content, 'utf-8').toString('base64'); const result = await this.exec(containerId, [ 'sh', '-c', 'printf %s "$1" | base64 -d > "$2"', 'sh', b64, fullPath, ]); if (!result.success) { throw Object.assign(new Error('Failed to write file'), { statusCode: 500 }); } }
async readContainerFile(containerId: string, filePath: string): Promise<string> { const safePath = this.validatePath(filePath); const fullPath = safePath.startsWith('/') ? safePath : `/workspace/${safePath}`; const result = await this.exec(containerId, ['cat', fullPath]); if (!result.success) { throw Object.assign(new Error('Failed to read file'), { statusCode: 404 }); } return result.stdout; }
async listContainerFiles(containerId: string, filePath: string = '/'): Promise<string> { const safePath = this.validatePath(filePath); const fullPath = safePath.startsWith('/') ? safePath : `/workspace/${safePath}`; const result = await this.exec(containerId, ['ls', '-la', fullPath]); if (!result.success) { throw Object.assign(new Error('Failed to list directory'), { statusCode: 404 }); } return result.stdout; }
// --- Git clone operations ---
async cloneRepo(containerId: string, url: string, branch?: string): Promise<ExecResult> { // Validate git URL — only allow http(s):// protocols const GIT_URL_PATTERN = /^https?:\/\/[a-zA-Z0-9._-]+(:[0-9]+)?(\/[a-zA-Z0-9._~:/?#\[\]@!$&'()*+,;=%-]*)?$/; if (!GIT_URL_PATTERN.test(url)) { throw Object.assign(new Error('Invalid git URL. Only HTTP(S) URLs are allowed.'), { statusCode: 400 }); }
// Validate branch name — strict alphanumeric + dots/dashes/underscores/slashes if (branch !== undefined) { if (typeof branch !== 'string' || branch.length > 256) { throw Object.assign(new Error('Invalid branch name'), { statusCode: 400 }); } const BRANCH_PATTERN = /^[a-zA-Z0-9._\/-]+$/; if (!BRANCH_PATTERN.test(branch)) { throw Object.assign(new Error('Invalid branch name. Only alphanumeric, dots, dashes, underscores, and slashes allowed.'), { statusCode: 400 }); } }
// Ensure container has internet access — needed for both git install and clone const info = await this.inspect(containerId); const externalNetwork = CONFIG.CONTAINERS.NETWORK_NAME + '-external'; const hadInternet = info.networks.includes(externalNetwork); if (!hadInternet) { await this.setInternetAccess(containerId, true); }
try { // Ensure git is available (alpine images need apk add, debian/ubuntu need apt-get) const whichGit = await this.exec(containerId, ['sh', '-c', 'which git 2>/dev/null || echo "NOT_FOUND"']); if (whichGit.stdout.trim() === 'NOT_FOUND' || !whichGit.success) { const installResult = await this.exec(containerId, ['sh', '-c', 'apk add --no-cache git 2>/dev/null || (apt-get update -qq && apt-get install -y -qq git) 2>/dev/null'], { timeoutMs: 120000, mode: 'shell', commandText: 'apk add --no-cache git || apt-get update -qq && apt-get install -y -qq git' } ); if (!installResult.success) { throw new Error('Failed to install git'); } }
const cloneArgs = branch ? ['--branch', branch, '--', url] : ['--', url]; const cloneScript = [ 'rm -rf /workspace/repo', 'git clone --depth 1 "$@" /workspace/repo', 'for entry in /workspace/repo/* /workspace/repo/.[!.]* /workspace/repo/..?*; do', ' [ -e "$entry" ] || continue', ' mv "$entry" /workspace/', 'done', 'rmdir /workspace/repo', ].join('\n'); const commandText = branch ? `git clone --depth 1 --branch ${branch} -- ${url} /workspace/repo` : `git clone --depth 1 -- ${url} /workspace/repo`;
const result = await this.exec(containerId, ['sh', '-c', cloneScript, 'sh', ...cloneArgs], { timeoutMs: 120000, mode: 'argv', commandText } );
return result; } finally { // Restore original internet state if (!hadInternet) { await this.setInternetAccess(containerId, false); } } }
// --- IService lifecycle ---
async start(): Promise<void> { if (!CONFIG.CONTAINERS.ENABLED) { console.log('[Containers] Container orchestration is disabled'); return; }
console.log('[Containers] Starting container manager...');
// Create internal network (no internet) await this.ensureNetwork(CONFIG.CONTAINERS.NETWORK_NAME, true); // Create external network (internet-capable) await this.ensureNetwork(CONFIG.CONTAINERS.NETWORK_NAME + '-external', false);
if (CONFIG.WORKSPACE.ENABLED) { console.log('[Workspace] Ensuring workspace container exists...'); await this.getOrCreateWorkspace(); console.log('[Workspace] Workspace ready'); }
console.log('[Containers] Container manager ready'); }
private async ensureNetwork(name: string, internal: boolean): Promise<void> { // Check if network already exists const res = await this.dockerRequest('GET', `/networks/${encodeURIComponent(name)}`); if (res.statusCode === 200) { console.log(`[Containers] Network '${name}' already exists`); return; }
const createRes = await this.dockerRequest('POST', '/networks/create', { Name: name, Driver: 'bridge', Internal: internal, Labels: { 'managed-by': 'aetheros-substrate' }, });
if (createRes.statusCode === 201) { console.log(`[Containers] Created network '${name}' (internal=${internal})`); } else { console.warn(`[Containers] Failed to create network '${name}': ${JSON.stringify(createRes.data)}`); } }
async stop(): Promise<void> { if (!CONFIG.CONTAINERS.ENABLED) return;
console.log('[Containers] Stopping all managed containers...');
// Destroy terminal sessions for (const session of this.containerTerminalSessions) { session.destroy(); } this.containerTerminalSessions.clear(); this.backgroundJobs.clear();
// Stop and remove all managed containers try { const containers = await this.listManagedContainers(); for (const container of containers) { try { if (container.status === 'running') { await this.dockerRequest('POST', `/containers/${container.id}/stop?t=${CONFIG.CONTAINERS.STOP_TIMEOUT_S}`); } await this.dockerRequest('DELETE', `/containers/${container.id}?force=true`); console.log(`[Containers] Removed: ${container.name}`); } catch (err: any) { console.warn(`[Containers] Failed to remove ${container.name}: ${err.message}`); } } } catch { // Docker may be unavailable during shutdown }
// Remove networks for (const suffix of ['', '-external']) { const netName = CONFIG.CONTAINERS.NETWORK_NAME + suffix; await this.dockerRequest('DELETE', `/networks/${encodeURIComponent(netName)}`).catch(() => {}); }
console.log('[Containers] Container manager stopped'); }
async health(): Promise<ServiceHealth> { if (!CONFIG.CONTAINERS.ENABLED) { return { status: 'stopped', details: { enabled: false } }; }
try { const containers = await this.listManagedContainers(); const running = containers.filter(c => c.status === 'running').length; return { status: 'running', details: { enabled: true, total: containers.length, running, max: CONFIG.CONTAINERS.MAX_CONTAINERS, terminalSessions: this.containerTerminalSessions.size, }, }; } catch { return { status: 'error', details: { enabled: true, error: 'Docker unreachable' } }; } }}