Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added proxy config for complex proxy rules #907

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ if (argv.h || argv.help) {
'',
' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com',
' --proxy-options Pass options to proxy using nested dotted objects. e.g.: --proxy-options.secure false',
' --proxy-config Pass in .json configuration file. e.g.: ./path/to/config.json',
'',
' --username Username for basic authentication [none]',
' Can also be specified with the env variable NODE_HTTP_SERVER_USERNAME',
Expand All @@ -76,6 +77,7 @@ var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
sslPassphrase = process.env.NODE_HTTP_SERVER_SSL_PASSPHRASE,
proxy = argv.P || argv.proxy,
proxyOptions = argv['proxy-options'],
proxyConfig = argv['proxy-config'],
utc = argv.U || argv.utc,
version = argv.v || argv.version,
baseDir = argv['base-dir'],
Expand Down Expand Up @@ -157,6 +159,7 @@ function listen(port) {
logFn: logger.request,
proxy: proxy,
proxyOptions: proxyOptions,
proxyConfig: proxyConfig,
showDotfiles: argv.dotfiles,
mimetypes: argv.mimetypes,
username: argv.username || process.env.NODE_HTTP_SERVER_USERNAME,
Expand Down Expand Up @@ -199,6 +202,19 @@ function listen(port) {
}
}

if (proxyConfig) {
try {
proxyConfig = JSON.parse(fs.readFileSync(proxyConfig));
}
catch (err) {
logger.info(chalk.red('Error: Invalid proxy config file'));
process.exit(1);
}
// Proxy file overrides cli config
proxy = undefined;
proxyOptions = undefined;
}

if (tls) {
options.https = {
cert: argv.C || argv.cert || 'cert.pem',
Expand Down
31 changes: 31 additions & 0 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,37 @@ function HttpServer(options) {
});
}

if (typeof options.proxyConfig === 'object') {
var proxy = httpProxy.createProxyServer();
before.push(function (req, res) {
var matchOptions = {};

for (var key in Object.keys(options.proxyConfig)) {
var regex = new RegExp(key);
if (regex.test(req.url)) {
var matchConfig = options.proxyConfig[key];

Object.entries(matchConfig.pathRewrite).forEach(rewrite => {
req.url = req.url.replace(new RegExp(rewrite[0]), rewrite[1]);
});

var configEntries = Object.entries(matchConfig).filter(entry => entry[0] !== "pathRewrite");
configEntries.forEach(entry => matchOptions.options[entry[0]] = entry[1]);
break;
}
}

proxy.web(req, res, matchOptions, function (err, req, res) {
if (options.logFn) {
options.logFn(req, res, {
message: err.message,
status: res.statusCode });
}
res.emit('next');
});
});
}

var serverOptions = {
before: before,
headers: this.headers,
Expand Down