Files
wekan/server/httpsServer.js
T
Lauri Ojansivu 1bf7a62cab Serve HTTPS from WeKan itself, when a certificate is configured.
#916 (decided 2026-10-14): HTTPS_PORT with HTTPS_CERT_FILE and
HTTPS_KEY_FILE (a path or the PEM itself) makes WeKan serve HTTPS; without
them nothing changes, and a proxy stays the recommendation for
production. Meteor's HTTP server - pages, DDP, websockets - is not
replaced: a TLS listener hands each decrypted connection to it, so
everything works the same over HTTPS. A renewed certificate is loaded
within a minute without a restart (a broken one keeps the old).
HTTPS_REDIRECT=true redirects plain HTTP to ROOT_URL when that is https,
else the same host on the HTTPS port, keeping only the path. The snap
gets https-port, https-cert-file, https-key-file, https-bind-ip and
https-redirect.

tests/httpsServer.test.cjs makes a real certificate with openssl, serves
a page and a websocket upgrade through the bridge, swaps the certificate
without a restart, and refuses incomplete settings, an untrusted
certificate, an injected host and a scheme-relative redirect.

Thanks to 68040 and xet7 !

Fixes #916.
2026-10-14 08:25:25 +03:00

44 lines
1.6 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
// Optional HTTPS in WeKan's own server (#916; settings in
// models/lib/httpsConfig.js). Meteor's HTTP server is what DDP, its websockets
// and every route attach to, so it is not replaced: a TLS listener takes the
// encrypted connections and hands each decrypted socket to that same server.
// Everything WeKan serves over HTTP it then serves over HTTPS too.
//
// The certificate is read again when its file changes, so a renewal (certbot,
// acme.sh) needs no restart. With HTTPS_REDIRECT=true, a plain-HTTP request
// gets a redirect to HTTPS instead of the page.
const { httpsConfig, redirectLocation } = require('/models/lib/httpsConfig');
const { startHttpsBridge } = require('/server/lib/httpsBridge');
let config = null;
try {
config = httpsConfig();
} catch (error) {
console.error(`HTTPS is not started: ${error.message}`);
}
if (config) {
if (config.redirect) {
WebApp.rawConnectHandlers.use((req, res, next) => {
if (req.socket && req.socket.encrypted) return next();
const location = redirectLocation(req.headers.host, req.url, config.port, process.env.ROOT_URL);
if (!location) return next();
res.writeHead(301, { Location: location, 'Cache-Control': 'no-store' });
res.end();
return undefined;
});
}
Meteor.startup(() => {
const { server } = startHttpsBridge(WebApp.httpServer, config, { readConfig: () => httpsConfig() });
server.listen(config.port, config.host, () => {
console.log(`WeKan serves HTTPS on ${config.host || 'all addresses'}:${config.port}.`);
});
});
}
export { config as httpsServerConfig };