Created
June 30, 2015 18:35
-
-
Save kizzx2/76dda4dd941eee7dd6b3 to your computer and use it in GitHub Desktop.
Simple node-http-proxy with HTTPS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "node-https-proxy", | |
"version": "1.0.0", | |
"description": "", | |
"main": "auth.js", | |
"scripts": { | |
"start": "node_modules/LiveScript/bin/lsc server.ls" | |
}, | |
"dependencies": { | |
"LiveScript": "^1.3.1", | |
"basic-auth": "^1.0.2", | |
"http-proxy": "^1.11.1", | |
"https": "^1.0.0" | |
}, | |
"devDependencies": {}, | |
"author": "", | |
"license": "ISC" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require \fs | |
https = require \https | |
auth = require \basic-auth | |
http-proxy = require \http-proxy | |
user = process.env.USER | |
pass = process.env.PASS | |
realm = process.env.REALM ? "Realm" | |
dst-host = process.env.DST_HOST ? 'localhost' | |
dst-port = process.env.DST_PORT ? 3000 | |
port = process.env.PORT ? 8080 | |
proxy = http-proxy.create-proxy-server do | |
target: | |
host: dst-host | |
port: dst-port | |
https.create-server do | |
key: fs.read-file-sync 'https.key' | |
cert: fs.read-file-sync 'https.cer' | |
(req, res) -> | |
creds = auth req | |
if creds?.name != user && creds?.pass != pass | |
res.status-code = 401 | |
res.set-header 'WWW-Authenticate', "Basic realm='#{realm}'" | |
res.end! | |
return | |
proxy.web req, res | |
.listen port | |
console.log "Port #{port} -> #{dst-host}:#{dst-port}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment