diff --git a/.env.example b/.env.example index 7839a1c..203d514 100644 --- a/.env.example +++ b/.env.example @@ -8,8 +8,8 @@ MAIL_SERVER_URL=https://mail.example.com # Generate with: openssl rand -base64 48 APP_SECRET=change-me -# Listen address -HOST=0.0.0.0 +# Listen address. :: answers on IPv6 and IPv4 alike; 0.0.0.0 is IPv4 only. +HOST=:: PORT=8080 # Serve the app from a subpath instead of the domain root, for a reverse proxy diff --git a/Dockerfile b/Dockerfile index b4bee82..d395d57 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,7 +33,7 @@ FROM mirror.gcr.io/library/node:26-alpine AS runtime ARG IHASMAIL_VERSION="" ARG BASE_PATH="" ENV NODE_ENV=production \ - HOST=0.0.0.0 \ + HOST=:: \ PORT=8080 \ STATIC_DIR=/app/web/dist \ SESSION_FILE=/data/sessions.json \ diff --git a/server/src/config.ts b/server/src/config.ts index 9a4be28..82ba221 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -302,7 +302,12 @@ export const config = { * container's hostname. See nodes.ts. */ nodeName: env("NODE_NAME", ""), - host: env("HOST", "0.0.0.0"), + /** + * The address to listen on. `::` takes IPv6 and IPv4 on one socket; a + * kernel without IPv6 at all refuses it, and index.ts then falls back to + * `0.0.0.0` rather than failing to start. + */ + host: env("HOST", "::"), port: int("PORT", 8080), /** * The subpath this instance answers on: `/mail` for a proxy that maps diff --git a/server/src/index.ts b/server/src/index.ts index 6c4583f..51cf79f 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -6,10 +6,23 @@ async function main() { await sessions.init(); const app = createApp(); const server = serve({ fetch: app.fetch, hostname: config.host, port: config.port }, (info) => { - console.log(`[ihasmail] ${config.appName} listening on http://${info.address}:${info.port}`); + const address = info.address.includes(":") ? `[${info.address}]` : info.address; + console.log(`[ihasmail] ${config.appName} listening on http://${address}:${info.port}`); console.log(`[ihasmail] mail server: ${config.stalwartUrl}`); console.log(`[ihasmail] static dir: ${config.staticDir}`); }); + // `::` is the default, and a kernel booted without IPv6 cannot open it at + // all. Listening on IPv4 alone is still a working server; refusing to start + // over an address family nobody asked for is not. + server.once("error", (err: NodeJS.ErrnoException) => { + if (config.host === "::" && err.code === "EAFNOSUPPORT") { + console.log("[ihasmail] no IPv6 on this host, listening on IPv4 only"); + server.listen(config.port, "0.0.0.0"); + return; + } + console.error("[ihasmail] fatal:", err); + process.exit(1); + }); const shutdown = async (signal: string) => { console.log(`[ihasmail] ${signal} received, shutting down`);