-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathproxy.js
200 lines (142 loc) · 5.82 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const httpProxy = require('http-proxy')
const fs = require('fs')
const path = require('path')
const httpsAgent = require('https').globalAgent
const pUrl = require('url')
const defaultAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/610.0.3239.132 Safari/537.36'
const proxies = {}
let endpoint
function getDirPath(path) {
let dirPath
if (path.includes('/'))
dirPath = path.substr(0, path.lastIndexOf('/') + 1)
return dirPath
}
var scriptElm = fs.readFileSync(path.join(__dirname, 'inject.html'))
var scriptElmHead = fs.readFileSync(path.join(__dirname, 'injectHead.html'))
function modifyHtml( str ) {
// Add or script to the page
if (str.indexOf('</body>') > -1 ) {
str = str.replace( '</body>', scriptElm + '</body>' );
} else if ( str.indexOf( '</html>' ) > -1 ){
str = str.replace( '</html>', scriptElm + '</html>' );
} else {
str = str + scriptElm;
}
if (str.indexOf('<head>') > -1 ) {
str = str.replace( '<head>', '<head>' + scriptElmHead );
}
return str;
}
const web_o = Object.values(require('http-proxy/lib/http-proxy/passes/web-outgoing'));
const proxify = {
setEndpoint: url => {
endpoint = url
},
getEndpoint: () => {
return endpoint
},
addProxy: (url, opts) => {
const urlParser = pUrl.parse(url)
const host = urlParser.host
const result = endpoint + '/web/' + urlParser.host + (urlParser.path || '')
if (process.send) {
// is child
process.send({ proxy: true, url, opts })
return result
}
const path = urlParser.path
const dirPath = getDirPath(path)
if (proxies && proxies[host]) {
proxies[host].paths[path] = opts
if (dirPath && !proxies[host].paths[dirPath])
proxies[host].paths[dirPath] = opts
return result
}
proxies[host] = {
host,
protocol: urlParser.protocol,
opts,
paths: {}
}
proxies[host].paths[path] = opts
if (dirPath && !proxies[host].paths[dirPath])
proxies[host].paths[dirPath] = opts
return result
},
createProxyServer: router => {
const proxy = httpProxy.createProxyServer({ selfHandleResponse: true })
proxy.on('error', e => {
if (e) {
console.error('http proxy error')
console.error(e)
}
})
proxy.on('proxyRes', (proxyRes, request, response) => {
proxyRes.headers['Access-Control-Allow-Origin'] = '*'
for(var i=0; i < web_o.length; i++) {
if(web_o[i](request, response, proxyRes, {})) { break; }
}
let body = []
proxyRes.on('data', chunk => { body.push(chunk) })
// if ((proxyRes.headers['content-type'] || '').match('text/html') || request.url.includes('/blob.css')) {
proxyRes.on('end', () => {
body = Buffer.concat(body).toString()
// This disables chunked encoding
proxyRes.headers['transfer-encoding'] = ''
// Disable cache for all http as well
proxyRes.headers['cache-control'] = 'no-cache'
if ((proxyRes.headers['content-type'] || '').match('text/html')) {
if (proxyRes.headers['content-length'])
proxyRes.headers['content-length'] = body.length + scriptElm.length + scriptElmHead.length
response.end(modifyHtml(body))
} else if (request.url.includes('/blob.css')) {
body = body.split("url('fonts/").join("url('" + endpoint + "/assets/fonts/")
body = body.split('url("fonts/').join('url("' + endpoint + '/assets/fonts/')
if (proxyRes.headers['content-length'])
proxyRes.headers['content-length'] = body.length
response.end(body)
} else {
response.end(body)
}
})
// } else
// proxyRes.pipe(response)
})
router.all('/web/*', (req, res) => {
var parts = req.url.split('/')
var host = parts[2]
parts.splice(0, 3)
req.url = '/'+parts.join('/')
let configProxy = {}
let opts = {}
let config = {}
if (proxies[host]) {
config = proxies[host]
configProxy = { target: config.protocol+'//'+config.host }
configProxy.headers = {
host: config.host,
agent: defaultAgent,
}
req.headers['host'] = configProxy.headers.host
req.headers['user-agent'] = configProxy.headers.agent
if ((req.headers['accept-encoding'] || '').includes('gzip'))
req.headers['accept-encoding'] = 'deflate, br'
opts = config.paths[req.url] || config.paths[getDirPath(req.url)] || config.opts || {}
if (opts.headers)
for (let key in opts.headers)
configProxy.headers[key] = req.headers[key] = opts.headers[key]
if (config.protocol == 'https:')
configProxy.agent = httpsAgent
res.setHeader('Access-Control-Allow-Origin', '*')
}
if (!configProxy.target) {
res.writeHead(500)
res.end(JSON.stringify({ err: 'handler error' }))
} else {
proxy.web(req, res, configProxy)
}
})
}
}
module.exports = proxify