-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-http-proxy.js
40 lines (33 loc) · 957 Bytes
/
module-http-proxy.js
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
const http = require('http')
const https = require('https')
const httpProxy = require('http-proxy')
const url = require('url')
const PROXY_PORT = 8080
// Create a proxy server with custom application logic
const proxy = httpProxy.createProxy({})
proxy.on('error', function (err) {
console.log('ERROR')
console.log(err)
})
const server = http.createServer(function (req, res) {
//const finalUrl = req.url,
const finalUrl = 'https://www.sogou.com/'
let finalAgent = null
const 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)