forked from Witcher1337/proxy-injection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (46 loc) · 1.42 KB
/
index.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
const express = require('express')
const http = require('http')
const httpProxy = require('http-proxy')
const trumpet = require('trumpet')
const httpProxyInterceptor = require('http-proxy-interceptor')
const interceptorFactory = function () {
var out = `
<script defer src="sw.js"></script>
<script defer>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('sw.js').then(function(registration) {
console.log('Service worker registered with scope: ', registration.scope);
}, function(err) {
console.log('ServiceWorker registration failed: ', err);
});
});
}
</script>
`;
const tr = trumpet()
const elem = tr.select('head')
const rs = elem.createReadStream()
const ws = elem.createWriteStream()
rs.pipe(ws, { end: false })
rs.on('end', function () {
ws.end(out)
})
return tr
}
const port = process.env.PORT
const options = {
target: process.env.BASE_URL,
changeOrigin: true,
hostRewrite: `localhost:${port}`,
protocolRewrite: "http",
cookieDomainRewrite: "localhost",
}
const proxy = httpProxy.createProxyServer(options)
const app = express();
app.use(express.static(__dirname + '/public'));
app.use(httpProxyInterceptor(interceptorFactory, { headers: { 'content-type': /text\/html/ } }))
app.use(function (req, res) {
proxy.web(req, res)
})
http.createServer(app).listen(port)