-
Notifications
You must be signed in to change notification settings - Fork 128
/
configurable-http-proxy
executable file
·423 lines (393 loc) · 13.8 KB
/
configurable-http-proxy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env node
//
// cli entrypoint for starting a Configurable Proxy
//
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
//
"use strict";
var fs = require("fs"),
pkg = require("../package.json"),
cli = require("commander"),
tls = require("tls"),
winston = require("winston");
cli
.version(pkg.version)
.option("--ip <ip-address>", "Public-facing IP of the proxy")
.option("--port <n> (defaults to 8000)", "Public-facing port of the proxy", parseInt)
.option("--ssl-key <keyfile>", "SSL key to use, if any")
.option("--ssl-cert <certfile>", "SSL certificate to use, if any")
.option("--ssl-ca <ca-file>", "SSL certificate authority, if any")
.option("--ssl-request-cert", "Request SSL certs to authenticate clients")
.option(
"--ssl-reject-unauthorized",
"Reject unauthorized SSL connections (only meaningful if --ssl-request-cert is given)"
)
.option("--ssl-protocol <ssl-protocol>", "Set specific SSL protocol, e.g. TLSv1_2, SSLv3")
.option("--ssl-ciphers <ciphers>", "`:`-separated ssl cipher list. Default excludes RC4")
.option("--ssl-allow-rc4", "Allow RC4 cipher for SSL (disabled by default)")
.option("--ssl-dhparam <dhparam-file>", "SSL Diffie-Helman Parameters pem file, if any")
.option("--api-ip <ip>", "Inward-facing IP for API requests", "localhost")
.option(
"--api-port <n>",
"Inward-facing port for API requests (defaults to --port=value+1)",
parseInt
)
.option("--api-ssl-key <keyfile>", "SSL key to use, if any, for API requests")
.option("--api-ssl-cert <certfile>", "SSL certificate to use, if any, for API requests")
.option("--api-ssl-ca <ca-file>", "SSL certificate authority, if any, for API requests")
.option("--api-ssl-request-cert", "Request SSL certs to authenticate clients for API requests")
.option(
"--api-ssl-reject-unauthorized",
"Reject unauthorized SSL connections (only meaningful if --api-ssl-request-cert is given)"
)
.option("--client-ssl-key <keyfile>", "SSL key to use, if any, for proxy to client requests")
.option(
"--client-ssl-cert <certfile>",
"SSL certificate to use, if any, for proxy to client requests"
)
.option(
"--client-ssl-ca <ca-file>",
"SSL certificate authority, if any, for proxy to client requests"
)
.option("--client-ssl-request-cert", "Request SSL certs to authenticate clients for API requests")
.option(
"--client-ssl-reject-unauthorized",
"Reject unauthorized SSL connections (only meaningful if --client-ssl-request-cert is given)"
)
.option("--default-target <host>", "Default proxy target (proto://host[:port])")
.option(
"--error-target <host>",
"Alternate server for handling proxy errors (proto://host[:port])"
)
.option("--error-path <path>", "Alternate server for handling proxy errors (proto://host[:port])")
.option(
"--redirect-port <redirect-port>",
"Redirect HTTP requests on this port to the server on HTTPS"
)
.option("--redirect-to <port>", "Redirect HTTP requests from --redirect-port to this port")
.option("--pid-file <pid-file>", "Write our PID to a file")
// passthrough http-proxy options
.option("--no-x-forward", "Don't add 'X-forward-' headers to proxied requests")
.option("--no-prepend-path", "Avoid prepending target paths to proxied requests")
.option("--no-include-prefix", "Don't include the routing prefix in proxied requests")
.option("--auto-rewrite", "Rewrite the Location header host/port in redirect responses")
.option("--change-origin", "Changes the origin of the host header to the target URL")
.option(
"--protocol-rewrite <proto>",
"Rewrite the Location header protocol in redirect responses to the specified protocol"
)
.option(
"--custom-header <header>",
"Custom header to add to proxied requests. Use same option for multiple headers (--custom-header k1:v1 --custom-header k2:v2)",
collectHeadersIntoObject,
{}
)
.option("--insecure", "Disable SSL cert verification")
.option("--host-routing", "Use host routing (host as first level of path)")
.option("--metrics-ip <ip>", "IP for metrics server", "")
.option("--metrics-port <n>", "Port of metrics server. Defaults to no metrics server")
.option("--log-level <loglevel>", "Log level (debug, info, warn, error)", "info")
.option(
"--timeout <n>",
"Timeout (in millis) when proxy drops connection for a request.",
parseInt
)
.option(
"--proxy-timeout <n>",
"Timeout (in millis) when proxy receives no response from target.",
parseInt
)
.option(
"--storage-backend <storage-class>",
"Define an external storage class. Defaults to in-MemoryStore."
)
.option(
"--keep-alive-timeout <timeout>",
"Set timeout (in milliseconds) for Keep-Alive connections",
parseInt
);
// collects multiple flags to an object
// --custom-header "k1:v1" --custom-header " k2 : v2 " --> {"k1":"v1","k2":"v2"}
function collectHeadersIntoObject(value, previous) {
value = value.trim();
var colon = value.indexOf(":");
if (colon <= 0) {
console.error("A colon was expected in custom header: " + value);
process.exit(1);
}
var headerName = value.slice(0, colon).trim();
var headerValue = value.slice(colon + 1).trim();
previous[headerName] = headerValue;
return previous;
}
cli.parse(process.argv);
var args = cli.opts();
var ConfigurableProxy = require("../lib/configproxy.js").ConfigurableProxy;
var log = require("../lib/log.js").defaultLogger({ level: args.logLevel.toLowerCase() });
var options = { log: log };
var sslCiphers;
if (args.sslCiphers) {
sslCiphers = args.sslCiphers;
} else {
var rc4 = "!RC4"; // disable RC4 by default
if (args.sslAllowRc4) {
// autoCamelCase is duMb
rc4 = "RC4";
}
// ref: https://iojs.org/api/tls.html#tls_modifying_the_default_tls_cipher_suite
sslCiphers = [
"ECDHE-RSA-AES128-GCM-SHA256",
"ECDHE-ECDSA-AES128-GCM-SHA256",
"ECDHE-RSA-AES256-GCM-SHA384",
"ECDHE-ECDSA-AES256-GCM-SHA384",
"DHE-RSA-AES128-GCM-SHA256",
"ECDHE-RSA-AES128-SHA256",
"DHE-RSA-AES128-SHA256",
"ECDHE-RSA-AES256-SHA384",
"DHE-RSA-AES256-SHA384",
"ECDHE-RSA-AES256-SHA256",
"DHE-RSA-AES256-SHA256",
"HIGH",
rc4,
"!aNULL",
"!eNULL",
"!EXPORT",
"!DES",
"!RC4",
"!MD5",
"!PSK",
"!SRP",
"!CAMELLIA",
].join(":");
}
function _loadSslCa(caFile) {
// When multiple CAs need to be specified, they must be broken into
// an array of certs unfortunately.
var chain = fs.readFileSync(caFile, "utf8");
var ca = [];
var cert = [];
chain.split("\n").forEach(function (line) {
cert.push(line);
if (line.match(/-END CERTIFICATE-/)) {
ca.push(new Buffer(cert.join("\n")));
cert = [];
}
});
return ca;
}
// ssl options
if (args.sslKey || args.sslCert) {
options.ssl = {};
if (args.sslKey) {
options.ssl.key = fs.readFileSync(args.sslKey);
if (process.env.CONFIGPROXY_SSL_KEY_PASSPHRASE) {
options.ssl.passphrase = process.env.CONFIGPROXY_SSL_KEY_PASSPHRASE;
}
}
if (args.sslCert) {
options.ssl.cert = fs.readFileSync(args.sslCert);
}
if (args.sslCa) {
options.ssl.ca = fs.readFileSync(args.sslCa);
}
if (args.sslDhparam) {
options.ssl.dhparam = fs.readFileSync(args.sslDhparam);
}
if (args.sslProtocol) {
options.ssl.secureProtocol = args.sslProtocol + "_method";
}
options.ssl.ciphers = sslCiphers;
options.ssl.honorCipherOrder = true;
options.ssl.requestCert = args.sslRequestCert;
options.ssl.rejectUnauthorized = args.sslRejectUnauthorized;
}
// ssl options for the API interface
if (args.apiSslKey || args.apiSslCert) {
options.apiSsl = {};
if (args.apiSslKey) {
options.apiSsl.key = fs.readFileSync(args.apiSslKey);
if (process.env.CONFIGPROXY_API_SSL_KEY_PASSPHRASE) {
options.apiSsl.passphrase = process.env.CONFIGPROXY_API_SSL_KEY_PASSPHRASE;
}
}
if (args.apiSslCert) {
options.apiSsl.cert = fs.readFileSync(args.apiSslCert);
}
if (args.apiSslCa) {
options.apiSsl.ca = _loadSslCa(args.apiSslCa);
}
if (args.sslDhparam) {
options.apiSsl.dhparam = fs.readFileSync(args.sslDhparam);
}
if (args.sslProtocol) {
options.apiSsl.secureProtocol = args.sslProtocol + "_method";
}
options.apiSsl.ciphers = sslCiphers;
options.apiSsl.honorCipherOrder = true;
options.apiSsl.requestCert = args.apiSslRequestCert;
options.apiSsl.rejectUnauthorized = args.apiSslRejectUnauthorized;
}
if (args.clientSslKey || args.clientSslCert || args.clientSslCa) {
options.clientSsl = {};
if (args.clientSslKey) {
options.clientSsl.key = fs.readFileSync(args.clientSslKey);
}
if (args.clientSslCert) {
options.clientSsl.cert = fs.readFileSync(args.clientSslCert);
}
if (args.clientSslCa) {
options.clientSsl.ca = _loadSslCa(args.clientSslCa);
}
if (args.sslDhparam) {
options.clientSsl.dhparam = fs.readFileSync(args.sslDhparam);
}
if (args.sslProtocol) {
options.clientSsl.secureProtocol = args.sslProtocol + "_method";
}
options.clientSsl.ciphers = sslCiphers;
options.clientSsl.honorCipherOrder = true;
options.clientSsl.requestCert = args.clientSslRequestCert;
options.clientSsl.rejectUnauthorized = args.clientSslRejectUnauthorized;
}
// because camelCase is the js way!
options.defaultTarget = args.defaultTarget;
options.errorTarget = args.errorTarget;
options.errorPath = args.errorPath;
options.hostRouting = args.hostRouting;
options.authToken = process.env.CONFIGPROXY_AUTH_TOKEN;
options.redirectPort = args.redirectPort;
options.redirectTo = args.redirectTo;
options.headers = args.customHeader;
options.timeout = args.timeout;
options.proxyTimeout = args.proxyTimeout;
options.keepAliveTimeout = args.keepAliveTimeout;
// metrics options
options.enableMetrics = !!args.metricsPort;
// certs need to be provided for https redirection
if (!options.ssl && options.redirectPort) {
log.error("HTTPS redirection specified but certificates not provided.");
process.exit(1);
}
if (options.errorTarget && options.errorPath) {
log.error("Cannot specify both error-target and error-path. Pick one.");
process.exit(1);
}
// passthrough for http-proxy options
if (args.insecure) options.secure = false;
options.xfwd = args.xForward;
options.prependPath = args.prependPath;
options.includePrefix = args.includePrefix;
if (args.autoRewrite) {
options.autoRewrite = true;
log.info("AutoRewrite of Location headers enabled.");
}
if (args.changeOrigin) {
options.changeOrigin = true;
log.info("Change Origin of host headers enabled.");
}
if (args.protocolRewrite) {
options.protocolRewrite = args.protocolRewrite;
log.info("ProtocolRewrite enabled. Rewriting to " + options.protocolRewrite);
}
if (!options.authToken) {
log.warn("REST API is not authenticated.");
}
// external backend class
options.storageBackend = args.storageBackend;
var proxy = new ConfigurableProxy(options);
var listen = {};
listen.port = parseInt(args.port) || 8000;
if (args.ip === "*") {
// handle ip=* alias for all interfaces
log.warn(
"Interpreting ip='*' as all-interfaces. Preferred usage is 0.0.0.0 for all IPv4 or '' for all-interfaces."
);
args.ip = "";
}
listen.ip = args.ip;
listen.apiIp = args.apiIp;
listen.apiPort = args.apiPort || listen.port + 1;
listen.metricsIp = args.metricsIp;
listen.metricsPort = args.metricsPort;
proxy.proxyServer.listen(listen.port, listen.ip);
proxy.apiServer.listen(listen.apiPort, listen.apiIp);
if (listen.metricsPort) {
proxy.metricsServer.listen(listen.metricsPort, listen.metricsIp);
}
log.info(
"Proxying %s://%s:%s to %s",
options.ssl ? "https" : "http",
listen.ip || "*",
listen.port,
options.defaultTarget || "(no default)"
);
log.info(
"Proxy API at %s://%s:%s/api/routes",
options.apiSsl ? "https" : "http",
listen.apiIp || "*",
listen.apiPort
);
if (listen.metricsPort) {
log.info("Serve metrics at %s://%s:%s/metrics", "http", listen.metricsIp, listen.metricsPort);
}
if (args.pidFile) {
log.info("Writing pid %s to %s", process.pid, args.pidFile);
var fd = fs.openSync(args.pidFile, "w");
fs.writeSync(fd, process.pid.toString());
fs.closeSync(fd);
process.on("exit", function () {
log.debug("Removing %s", args.pidFile);
fs.unlinkSync(args.pidFile);
});
}
// Redirect HTTP to HTTPS on the proxy's port
if (options.redirectPort && listen.port !== 80) {
var http = require("http");
var redirectPort = options.redirectTo ? options.redirectTo : listen.port;
var server = http
.createServer(function (req, res) {
if (typeof req.headers.host === "undefined") {
res.statusCode = 400;
res.write(
"This server is HTTPS-only on port " +
redirectPort +
", but an HTTP request was made and the host could not be determined from the request."
);
res.end();
return;
}
var host = req.headers.host.split(":")[0];
// Make sure that when we redirect, it's to the port the proxy is running on
// or the port to which we have been instructed to forward.
if (redirectPort !== 443) {
host = host + ":" + redirectPort;
}
res.writeHead(301, { Location: "https://" + host + req.url });
res.end();
})
.listen(options.redirectPort, () => {
log.info(
"Added HTTP to HTTPS redirection from " + server.address().port + " to " + redirectPort
);
});
}
// trigger normal exit on SIGINT
// without this, PID cleanup won't fire on SIGINT
process.on("SIGINT", function () {
log.warn("Interrupted");
process.exit(2);
});
// trigger normal exit on SIGTERM
// fired on `docker stop` and during Kubernetes pod container evictions
process.on("SIGTERM", function () {
log.warn("Terminated");
process.exit(2);
});
// log uncaught exceptions, don't exit now that setup is complete
process.on("uncaughtException", function (e) {
log.error("Uncaught Exception: " + e.message);
if (e.stack) {
log.error(e.stack);
}
});