1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-14 23:33:31 +00:00

[PM-20220] feat: Add support for fd and unix socket bindings (#14262)

* Add support for unix sockets with (unix://) scheme

* Add support for listening socket FD (fd+listening:// scheme)

* Add support for connected socket FD (fd+connected:// scheme)

---------

Co-authored-by: Addison Beck <github@addisonbeck.com>
This commit is contained in:
Patrick Lenihan
2025-08-11 15:58:47 +01:00
committed by GitHub
parent ecbbd932a0
commit d3c6ba9f4b

View File

@@ -1,3 +1,6 @@
import http from "node:http";
import net from "node:net";
import * as koaRouter from "@koa/router";
import { OptionValues } from "commander";
import * as koa from "koa";
@@ -50,11 +53,33 @@ export class ServeCommand {
this.serveConfigurator.configureRouter(router);
server
.use(router.routes())
.use(router.allowedMethods())
.listen(port, hostname === "all" ? null : hostname, () => {
server.use(router.routes()).use(router.allowedMethods());
if (hostname.startsWith("fd+connected://")) {
const fd = parseInt(hostname.slice("fd+connected://".length));
const httpServer = http.createServer(server.callback());
const socket = new net.Socket({ fd: fd, readable: true, writable: true });
// allow idle sockets, incomplete handshakes and slow requests
httpServer.keepAliveTimeout = 0;
httpServer.headersTimeout = 0;
httpServer.timeout = 0;
socket.pause();
httpServer.emit("connection", socket);
socket.resume(); // Let the HTTP parser start reading
} else if (hostname.startsWith("fd+listening://")) {
const fd = parseInt(hostname.slice("fd+listening://".length));
server.listen({ fd }, () => {
this.serviceContainer.logService.info("Listening on " + hostname);
});
} else if (hostname.startsWith("unix://")) {
const socketPath = hostname.slice("unix://".length);
server.listen(socketPath, () => {
this.serviceContainer.logService.info("Listening on " + hostname);
});
} else {
server.listen(port, hostname === "all" ? null : hostname, () => {
this.serviceContainer.logService.info("Listening on " + hostname + ":" + port);
});
}
}
}