feat(server): support configurable hostname for managed OpenCode server spawn (#599)
* feat(server): support configurable hostname for managed OpenCode server spawn Allow the managed OpenCode server bind hostname to be configured via OPENCHAMBER_OPENCODE_HOSTNAME environment variable (default: 127.0.0.1). This enables LAN/Tailscale access without a reverse proxy by setting the hostname to 0.0.0.0. Closes #597 * fix: address review feedback — input validation, port probe hostname, security docs - Add defensive parsing for OPENCHAMBER_OPENCODE_HOSTNAME with trim/empty check and warning log, matching OPENCODE_HOST validation pattern - Pass configured hostname to resolveManagedOpenCodePort() so port availability is probed on the actual bind address, avoiding EADDRINUSE - Add security note in README docs warning about 0.0.0.0 exposure on untrusted networks
This commit is contained in:
@@ -3788,6 +3788,21 @@ const ENV_CONFIGURED_OPENCODE_HOST = (() => {
|
||||
// OPENCODE_HOST takes precedence over OPENCODE_PORT when both are set
|
||||
const ENV_EFFECTIVE_PORT = ENV_CONFIGURED_OPENCODE_HOST?.port ?? ENV_CONFIGURED_OPENCODE_PORT;
|
||||
|
||||
const ENV_CONFIGURED_OPENCODE_HOSTNAME = (() => {
|
||||
const raw = process.env.OPENCHAMBER_OPENCODE_HOSTNAME;
|
||||
if (typeof raw !== 'string') {
|
||||
return '127.0.0.1';
|
||||
}
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) {
|
||||
console.warn(
|
||||
`[config] Ignoring OPENCHAMBER_OPENCODE_HOSTNAME=${JSON.stringify(raw)}: empty after trimming`,
|
||||
);
|
||||
return '127.0.0.1';
|
||||
}
|
||||
return trimmed;
|
||||
})();
|
||||
|
||||
const ENV_SKIP_OPENCODE_START = process.env.OPENCODE_SKIP_START === 'true' ||
|
||||
process.env.OPENCHAMBER_SKIP_OPENCODE_START === 'true';
|
||||
const ENV_DESKTOP_NOTIFY = process.env.OPENCHAMBER_DESKTOP_NOTIFY === 'true';
|
||||
@@ -6049,7 +6064,7 @@ async function createManagedOpenCodeServerProcess({
|
||||
};
|
||||
}
|
||||
|
||||
async function resolveManagedOpenCodePort(requestedPort) {
|
||||
async function resolveManagedOpenCodePort(requestedPort, hostname = '127.0.0.1') {
|
||||
if (typeof requestedPort === 'number' && Number.isFinite(requestedPort) && requestedPort > 0) {
|
||||
return requestedPort;
|
||||
}
|
||||
@@ -6079,13 +6094,13 @@ async function resolveManagedOpenCodePort(requestedPort) {
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(0, '127.0.0.1');
|
||||
server.listen(0, hostname);
|
||||
});
|
||||
}
|
||||
|
||||
async function startOpenCode() {
|
||||
const desiredPort = ENV_CONFIGURED_OPENCODE_PORT ?? 0;
|
||||
const spawnPort = await resolveManagedOpenCodePort(desiredPort);
|
||||
const spawnPort = await resolveManagedOpenCodePort(desiredPort, ENV_CONFIGURED_OPENCODE_HOSTNAME);
|
||||
console.log(
|
||||
desiredPort > 0
|
||||
? `Starting OpenCode on requested port ${desiredPort}...`
|
||||
@@ -6100,7 +6115,7 @@ async function startOpenCode() {
|
||||
|
||||
try {
|
||||
const serverInstance = await createManagedOpenCodeServerProcess({
|
||||
hostname: '127.0.0.1',
|
||||
hostname: ENV_CONFIGURED_OPENCODE_HOSTNAME,
|
||||
port: spawnPort,
|
||||
timeout: 30000,
|
||||
cwd: openCodeWorkingDirectory,
|
||||
|
||||
Reference in New Issue
Block a user