Skip to content

Commit a9fca05

Browse files
authored
feat(server): add --mdns-domain flag to customize mDNS hostname (anomalyco#11796)
1 parent 824165e commit a9fca05

5 files changed

Lines changed: 21 additions & 6 deletions

File tree

packages/opencode/src/cli/cmd/web.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const WebCommand = cmd({
6363
UI.println(
6464
UI.Style.TEXT_INFO_BOLD + " mDNS: ",
6565
UI.Style.TEXT_NORMAL,
66-
`opencode.local:${server.port}`,
66+
`${opts.mdnsDomain}:${server.port}`,
6767
)
6868
}
6969

packages/opencode/src/cli/network.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ const options = {
1717
describe: "enable mDNS service discovery (defaults hostname to 0.0.0.0)",
1818
default: false,
1919
},
20+
"mdns-domain": {
21+
type: "string" as const,
22+
describe: "custom domain name for mDNS service (default: opencode.local)",
23+
default: "opencode.local",
24+
},
2025
cors: {
2126
type: "string" as const,
2227
array: true,
@@ -36,9 +41,11 @@ export async function resolveNetworkOptions(args: NetworkOptions) {
3641
const portExplicitlySet = process.argv.includes("--port")
3742
const hostnameExplicitlySet = process.argv.includes("--hostname")
3843
const mdnsExplicitlySet = process.argv.includes("--mdns")
44+
const mdnsDomainExplicitlySet = process.argv.includes("--mdns-domain")
3945
const corsExplicitlySet = process.argv.includes("--cors")
4046

4147
const mdns = mdnsExplicitlySet ? args.mdns : (config?.server?.mdns ?? args.mdns)
48+
const mdnsDomain = mdnsDomainExplicitlySet ? args["mdns-domain"] : (config?.server?.mdnsDomain ?? args["mdns-domain"])
4249
const port = portExplicitlySet ? args.port : (config?.server?.port ?? args.port)
4350
const hostname = hostnameExplicitlySet
4451
? args.hostname
@@ -49,5 +56,5 @@ export async function resolveNetworkOptions(args: NetworkOptions) {
4956
const argsCors = Array.isArray(args.cors) ? args.cors : args.cors ? [args.cors] : []
5057
const cors = [...configCors, ...argsCors]
5158

52-
return { hostname, port, mdns, cors }
59+
return { hostname, port, mdns, mdnsDomain, cors }
5360
}

packages/opencode/src/config/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ export namespace Config {
860860
port: z.number().int().positive().optional().describe("Port to listen on"),
861861
hostname: z.string().optional().describe("Hostname to listen on"),
862862
mdns: z.boolean().optional().describe("Enable mDNS service discovery"),
863+
mdnsDomain: z.string().optional().describe("Custom domain name for mDNS service (default: opencode.local)"),
863864
cors: z.array(z.string()).optional().describe("Additional domains to allow for CORS"),
864865
})
865866
.strict()

packages/opencode/src/server/mdns.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ export namespace MDNS {
77
let bonjour: Bonjour | undefined
88
let currentPort: number | undefined
99

10-
export function publish(port: number) {
10+
export function publish(port: number, domain?: string) {
1111
if (currentPort === port) return
1212
if (bonjour) unpublish()
1313

1414
try {
15+
const host = domain ?? "opencode.local"
1516
const name = `opencode-${port}`
1617
bonjour = new Bonjour()
1718
const service = bonjour.publish({
1819
name,
1920
type: "http",
20-
host: "opencode.local",
21+
host,
2122
port,
2223
txt: { path: "/" },
2324
})

packages/opencode/src/server/server.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,13 @@ export namespace Server {
563563
return result
564564
}
565565

566-
export function listen(opts: { port: number; hostname: string; mdns?: boolean; cors?: string[] }) {
566+
export function listen(opts: {
567+
port: number
568+
hostname: string
569+
mdns?: boolean
570+
mdnsDomain?: string
571+
cors?: string[]
572+
}) {
567573
_corsWhitelist = opts.cors ?? []
568574

569575
const args = {
@@ -591,7 +597,7 @@ export namespace Server {
591597
opts.hostname !== "localhost" &&
592598
opts.hostname !== "::1"
593599
if (shouldPublishMDNS) {
594-
MDNS.publish(server.port!)
600+
MDNS.publish(server.port!, opts.mdnsDomain)
595601
} else if (opts.mdns) {
596602
log.warn("mDNS enabled but hostname is loopback; skipping mDNS publish")
597603
}

0 commit comments

Comments
 (0)