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

Adds ability to proxy requests insecurely #236

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ This will install `http-server` globally so that it may be run from the command

`-P` or `--proxy` Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com

`--proxy-secure` Verify SSL certificates for proxied requests (defaults to true)

`-S` or `--ssl` Enable https.

`-C` or `--cert` Path to ssl cert file (default: cert.pem).
Expand Down
10 changes: 9 additions & 1 deletion bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ if (argv.h || argv.help) {
' -U --utc Use UTC time format in log messages.',
'',
' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com',
' --proxy-secure Verify proxied SSL certificates [true]',
'',
' -S --ssl Enable https.',
' -C --cert Path to ssl cert file (default: cert.pem).',
Expand Down Expand Up @@ -102,10 +103,17 @@ function listen(port) {
robots: argv.r || argv.robots,
ext: argv.e || argv.ext,
logFn: logger.request,
proxy: proxy,
showDotfiles: argv.dotfiles
};

if (proxy) {
options.proxy = {
target: proxy,
secure: argv['proxy-secure'] !== 'false',
changeOrigin: true
};
}

if (argv.cors) {
options.cors = true;
if (typeof argv.cors === 'string') {
Expand Down
9 changes: 3 additions & 6 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,13 @@ function HttpServer(options) {
defaultExt: this.ext,
gzip: this.gzip,
contentType: this.contentType,
handleError: typeof options.proxy !== 'string'
handleError: typeof options.proxy !== 'object'
}));

if (typeof options.proxy === 'string') {
if (options.proxy) {
var proxy = httpProxy.createProxyServer({});
before.push(function (req, res) {
proxy.web(req, res, {
target: options.proxy,
changeOrigin: true
});
proxy.web(req, res, options.proxy);
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@
],
"dependencies": {
"colors": "1.0.3",
"http-proxy": "^v1.11.1",
"corser": "~2.0.0",
"ecstatic": "^2.0.0",
"http-proxy": "^1.8.1",
"opener": "~1.4.0",
"optimist": "0.6.x",
"portfinder": "^1.0.13",
Expand Down