Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/cli/src/services/service-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ export function filename(channel = OPENCODE_CHANNEL) {

export function defaultPort(channel = OPENCODE_CHANNEL, platform = process.platform) {
const port = channelPort(channel)
// Windows hands out 0xC000-0xFFFF for outgoing connections, and Hyper-V and WinNAT reserve blocks of that range at
// boot, so a port there can fail as in use while nothing listens on it. Moving into the 0xB000 block keeps the low
// 12 bits: 0xC0DE becomes 0xB0DE, which is 0xC0DE - 4096, a nod to the v1 port.
if (platform === "win32" && port >= 0xc000) return 0xb000 | (port & 0xfff)
// Windows hands out 0xC000-0xFFFF for outgoing connections, Hyper-V and WinNAT reserve blocks of that range at boot,
// and WSL mirrored networking reserves a 4096-port block that usually covers 44620-48715. A reserved port fails as in
// use while nothing listens on it. Moving into the 0x7000 block, below 32768, avoids all of them and keeps the low
// 12 bits: 0xC0DE becomes 0x70DE.
if (platform === "win32" && port >= 0x8000) return 0x7000 | (port & 0xfff)
return port
}

Expand Down
14 changes: 7 additions & 7 deletions packages/cli/test/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ test("managed service ports are stable per installation channel", () => {
expect(ServiceConfig.defaultPort("preview-a", "linux")).not.toBe(ServiceConfig.defaultPort("preview-b", "linux"))
})

test("managed service ports on Windows stay below the range Windows reserves", () => {
expect(ServiceConfig.defaultPort("latest", "win32")).toBe(0xc0de - 4096)
expect(ServiceConfig.defaultPort("dev", "win32")).toBe(0xc0de - 4096)
expect(ServiceConfig.defaultPort("local", "win32")).toBe(0xc0df - 4096)
test("managed service ports on Windows stay below the ranges Windows and WSL reserve", () => {
expect(ServiceConfig.defaultPort("latest", "win32")).toBe(0x70de)
expect(ServiceConfig.defaultPort("dev", "win32")).toBe(0x70de)
expect(ServiceConfig.defaultPort("local", "win32")).toBe(0x70df)
const ports = Array.from({ length: 200 }, (_, index) => ({
windows: ServiceConfig.defaultPort(`preview-${index}`, "win32"),
linux: ServiceConfig.defaultPort(`preview-${index}`, "linux"),
}))
expect(ports.some((port) => port.linux >= 0xc000)).toBe(true)
expect(ports.every((port) => port.windows >= 10_000 && port.windows < 0xc000)).toBe(true)
expect(ports.filter((port) => port.linux < 0xc000 && port.windows !== port.linux)).toEqual([])
expect(ports.some((port) => port.linux >= 0x8000)).toBe(true)
expect(ports.every((port) => port.windows >= 10_000 && port.windows < 0x8000)).toBe(true)
expect(ports.filter((port) => port.linux < 0x8000 && port.windows !== port.linux)).toEqual([])
})

test("service disabled accepts only booleans without changing configuration on invalid input", async () => {
Expand Down
5 changes: 3 additions & 2 deletions services/www/src/docs/content/cli/web.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ platform:
| Platform | Default port |
| --------------------- | ------------ |
| macOS, Linux, and WSL | 49374 |
| Windows | 45278 |
| Windows | 28894 |

Windows reserves ports from 49152 up. Print the current address with
Windows reserves ports from 49152 up, and WSL mirrored networking reserves
another block below that. Print the current address with
`opencode service status`:

```bash
Expand Down
Loading