diff --git a/README.md b/README.md index fb051f8..c72dfef 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ Opens a dashboard at `http://localhost:50234` showing live agent sessions, tool | Flag | Description | |------|-------------| | `--port ` | Server port (default: 50234) | +| `--host
` | Bind address (default: localhost, use 0.0.0.0 for all interfaces) | | `--no-browser` | Don't auto-open browser | | `--project ` | Set project directory for plan tracking | | `--help` | Show help | diff --git a/src/server/cli.ts b/src/server/cli.ts index 47df7ef..7d7f6dd 100644 --- a/src/server/cli.ts +++ b/src/server/cli.ts @@ -2,6 +2,7 @@ import { DEFAULT_PORT } from "../shared/constants"; export interface CLIFlags { port: number; + host: string; noBrowser: boolean; projectPath: string | null; showHelp: boolean; @@ -11,6 +12,7 @@ export function parseArgs(): CLIFlags { const args = process.argv.slice(2); const flags: CLIFlags = { port: DEFAULT_PORT, + host: "localhost", noBrowser: false, projectPath: null, showHelp: false, @@ -29,6 +31,17 @@ export function parseArgs(): CLIFlags { flags.port = parseInt(portValue); i++; } + } else if (arg === "--host") { + const hostValue = args[i + 1]; + if (hostValue && /^[a-zA-Z0-9.\-:]+$/.test(hostValue)) { + flags.host = hostValue; + i++; + } else if (hostValue) { + console.warn(`[ocwatch] Invalid --host value ignored: ${hostValue}`); + i++; + } else { + console.warn(`[ocwatch] --host requires a value`); + } } else if (arg === "--project") { const projectPath = args[i + 1]; if (projectPath) { @@ -49,6 +62,7 @@ Usage: ocwatch [options] Options: --port Server port (default: 50234) + --host
Bind address (default: localhost, use 0.0.0.0 for all interfaces) --no-browser Skip auto-opening browser --project Set default project filter --help, -h Show this help message @@ -56,6 +70,7 @@ Options: Examples: ocwatch ocwatch --port 50999 + ocwatch --host 0.0.0.0 ocwatch --no-browser ocwatch --project /path/to/project `); diff --git a/src/server/index.ts b/src/server/index.ts index 9e786c6..a9da550 100755 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -69,6 +69,8 @@ async function getDefaultProjectIdFromFlag(projectPath: string | null): Promise< const defaultProjectIdPromise = getDefaultProjectIdFromFlag(flags.projectPath); +const isWildcard = flags.host === "0.0.0.0" || flags.host === "::"; + const app = new Hono(); app.use("*", compress()); @@ -76,10 +78,18 @@ app.use("*", errorHandler); app.use( "/api/*", - cors({ - origin: ["http://localhost:3000", "http://localhost:50234"], - credentials: true, - }) + cors( + isWildcard + ? { origin: "*", credentials: false } + : { + origin: [ + `http://localhost:3000`, + `http://localhost:${flags.port}`, + `http://${flags.host}:${flags.port}`, + ], + credentials: true, + } + ) ); registerRoutes(app, { defaultProjectIdPromise }); @@ -103,10 +113,13 @@ app.notFound(async (c) => { export { app }; const port = flags.port; -const url = `http://localhost:${port}`; +const hostname = flags.host; +const displayHost = isWildcard ? "localhost" : hostname; +const url = `http://${displayHost}:${port}`; export default { port, + hostname, fetch: app.fetch, };