Skip to content

Commit 7ee4d09

Browse files
committed
Optionally listen on unix sockets
1 parent 1a6d3c6 commit 7ee4d09

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

bin/configurable-http-proxy

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ cli
2121
.version(pkg.version)
2222
.option("--ip <ip-address>", "Public-facing IP of the proxy")
2323
.option("--port <n> (defaults to 8000)", "Public-facing port of the proxy", parseInt)
24+
.option("--socket <path>", "Path to a UNIX domain socket for the proxy to listen on. Alternative to specifying IP and port.")
2425
.option("--ssl-key <keyfile>", "SSL key to use, if any")
2526
.option("--ssl-cert <certfile>", "SSL certificate to use, if any")
2627
.option("--ssl-ca <ca-file>", "SSL certificate authority, if any")
@@ -39,6 +40,7 @@ cli
3940
"Inward-facing port for API requests (defaults to --port=value+1)",
4041
parseInt
4142
)
43+
.option("--api-socket <path>", "Path to a UNIX domain socket for the API server to listen on. Alternative to specifying API IP and port.")
4244
.option("--api-ssl-key <keyfile>", "SSL key to use, if any, for API requests")
4345
.option("--api-ssl-cert <certfile>", "SSL certificate to use, if any, for API requests")
4446
.option("--api-ssl-ca <ca-file>", "SSL certificate authority, if any, for API requests")
@@ -93,6 +95,7 @@ cli
9395
.option("--host-routing", "Use host routing (host as first level of path)")
9496
.option("--metrics-ip <ip>", "IP for metrics server", "")
9597
.option("--metrics-port <n>", "Port of metrics server. Defaults to no metrics server")
98+
.option("--metrics-socket <path>", "Path to a UNIX domain socket for the metrics server to listen on. Alternative to specifying metrics IP and port.")
9699
.option("--log-level <loglevel>", "Log level (debug, info, warn, error)", "info")
97100
.option(
98101
"--timeout <n>",
@@ -321,41 +324,62 @@ options.storageBackend = args.storageBackend;
321324
var proxy = new ConfigurableProxy(options);
322325

323326
var listen = {};
324-
listen.port = parseInt(args.port) || 8000;
325-
if (args.ip === "*") {
326-
// handle ip=* alias for all interfaces
327+
328+
if (args.socket) {
329+
listen.proxyTarget = [args.socket];
327330
log.warn(
328-
"Interpreting ip='*' as all-interfaces. Preferred usage is 0.0.0.0 for all IPv4 or '' for all-interfaces."
331+
"Proxy will listen on UNIX domain socket, --ip and --port options will be ignored."
329332
);
330-
args.ip = "";
333+
} else {
334+
listen.port = parseInt(args.port) || 8000;
335+
if (args.ip === "*") {
336+
// handle ip=* alias for all interfaces
337+
log.warn(
338+
"Interpreting ip='*' as all-interfaces. Preferred usage is 0.0.0.0 for all IPv4 or '' for all-interfaces."
339+
);
340+
args.ip = "";
341+
}
342+
listen.ip = args.ip;
343+
listen.proxyTarget = [listen.port, listen.ip];
331344
}
332-
listen.ip = args.ip;
333-
listen.apiIp = args.apiIp;
334-
listen.apiPort = args.apiPort || listen.port + 1;
335-
listen.metricsIp = args.metricsIp;
336-
listen.metricsPort = args.metricsPort;
337-
338-
proxy.proxyServer.listen(listen.port, listen.ip);
339-
proxy.apiServer.listen(listen.apiPort, listen.apiIp);
340-
if (listen.metricsPort) {
341-
proxy.metricsServer.listen(listen.metricsPort, listen.metricsIp);
345+
346+
if (args.apiSocket) {
347+
listen.apiSocket = [args.apiSocket];
348+
log.warn(
349+
"API server will listen on UNIX domain socket, --api-ip and --api-port options will be ignored."
350+
);
351+
} else {
352+
listen.apiTarget = [args.apiPort || (listen.port ? listen.port + 1 : 8001), args.apiIp];
353+
}
354+
355+
if (args.metricsSocket) {
356+
listen.metricsSocket = [args.metricsSocket];
357+
log.warn(
358+
"Metrics server will listen on UNIX domain socket, --metrics-ip and --metrics-port options will be ignored."
359+
);
360+
} else {
361+
listen.metricsTarget = [args.metricsPort, args.metricsIp];
362+
}
363+
364+
proxy.proxyServer.listen(...listen.proxyTarget);
365+
proxy.apiServer.listen(...listen.apiTarget);
366+
if (listen.metricsTarget) {
367+
proxy.metricsServer.listen(...listen.metricsTarget);
342368
}
343369

344370
log.info(
345-
"Proxying %s://%s:%s to %s",
371+
"Proxying %s://%s to %s",
346372
options.ssl ? "https" : "http",
347-
listen.ip || "*",
348-
listen.port,
373+
listen.proxyTarget.join(":"),
349374
options.defaultTarget || "(no default)"
350375
);
351376
log.info(
352-
"Proxy API at %s://%s:%s/api/routes",
377+
"Proxy API at %s://%s/api/routes",
353378
options.apiSsl ? "https" : "http",
354-
listen.apiIp || "*",
355-
listen.apiPort
379+
listen.apiTarget.join(":")
356380
);
357381
if (listen.metricsPort) {
358-
log.info("Serve metrics at %s://%s:%s/metrics", "http", listen.metricsIp, listen.metricsPort);
382+
log.info("Serve metrics at %s://%s/metrics", "http", listen.metricsTarget.join(":"));
359383
}
360384

361385
if (args.pidFile) {
@@ -370,7 +394,7 @@ if (args.pidFile) {
370394
}
371395

372396
// Redirect HTTP to HTTPS on the proxy's port
373-
if (options.redirectPort && listen.port !== 80) {
397+
if (options.redirectPort && listen.port && listen.port !== 80) {
374398
var http = require("http");
375399
var redirectPort = options.redirectTo ? options.redirectTo : listen.port;
376400
var server = http

0 commit comments

Comments
 (0)