Last active
August 30, 2018 03:29
-
-
Save DogeVenci/5d3e861fc0a40b60405ee79b1bea8992 to your computer and use it in GitHub Desktop.
[nodejs proxy] #nodejs #proxy
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
'use strict'; | |
var http = require('http'); | |
var https = require('https'); | |
var httpProxy = require('http-proxy'); | |
var url = require('url'); | |
var PROXY_PORT = 80; | |
var proxy, server; | |
// Create a proxy server with custom application logic | |
proxy = httpProxy.createProxy({}); | |
proxy.on('error', function (err) { | |
console.log('ERROR'); | |
console.log(err); | |
}); | |
server = http.createServer(function (req, res) { | |
//var finalUrl = req.url, | |
var host = req.headers.host | |
console.log("host:" + host); | |
var finalUrl = 'https://www.baidu.com'; | |
var finalAgent = null; | |
switch(host){ | |
case 'g.gxsna.info': | |
finalUrl = 'https://www.google.com'; | |
break; | |
} | |
var parsedUrl = url.parse(finalUrl); | |
if (parsedUrl.protocol === 'https:') { | |
finalAgent = https.globalAgent; | |
} else { | |
finalAgent = http.globalAgent; | |
} | |
proxy.web(req, res, { | |
target: finalUrl, | |
agent: finalAgent, | |
headers: { host: parsedUrl.hostname }, | |
prependPath: false, | |
xfwd : true, | |
hostRewrite: finalUrl.host, | |
protocolRewrite: parsedUrl.protocol | |
}); | |
}); | |
console.log('listening on port ' + PROXY_PORT); | |
server.listen(PROXY_PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment