-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathresponseInterceptorConfig.js
106 lines (92 loc) · 3.54 KB
/
responseInterceptorConfig.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
// demo说明:
// 拦截所有服务器响应在html中注入 样式 '<style>body {background:red !important}</style>'
var through = require('through2');
var zlib = require('zlib');
var url = require('url');
var charset = require('charset');
var iconv = require('iconv-lite');
var jschardet = require('jschardet');
var httpUtil = {}
httpUtil.isGzip = function (res) {
var contentEncoding = res.headers['content-encoding'];
return !!(contentEncoding && contentEncoding.toLowerCase() == 'gzip');
}
httpUtil.isHtml = function (res) {
var contentType = res.headers['content-type'];
return (typeof contentType != 'undefined') && /text\/html|application\/xhtml\+xml/.test(contentType);
}
function injectScriptIntoHtml(html, script) {
html = html.replace(/(<\/head>)/i, function (match) {
return script + match;
});
return html;
}
function chunkReplace (_this, chunk, enc, callback, injectScriptTag, proxyRes) {
var _charset = charset(proxyRes, chunk) || jschardet.detect(chunk).encoding.toLowerCase();
var chunkString;
if (_charset != null && _charset != 'utf-8') {
chunkString = iconv.decode(chunk, _charset);
} else {
chunkString = chunk.toString();
}
var newChunkString = injectScriptIntoHtml(chunkString, injectScriptTag);
var buffer;
if (_charset != null && _charset != 'utf-8') {
buffer = iconv.encode(newChunkString, _charset);
} else {
buffer = new Buffer(newChunkString);
}
_this.push(buffer);
callback();
}
module.exports = {
sslConnectInterceptor: (req, cltSocket, head) => {
var srvUrl = url.parse(`https://${req.url}`);
// 忽略微信的推广页
if (srvUrl.host === 'mp.weixin.qq.com:443') {
return false;
}
// 只拦截浏览器的https请求
if (req.headers && req.headers['user-agent'] && /^Mozilla/.test(req.headers['user-agent'])) {
return true
} else {
return false
}
},
responseInterceptor: (req, res, proxyReq, proxyRes, ssl, next) => {
var isHtml = httpUtil.isHtml(proxyRes);
var contentLengthIsZero = (() => {
return proxyRes.headers['content-length'] == 0;
})();
if (!isHtml || contentLengthIsZero) {
next();
} else {
Object.keys(proxyRes.headers).forEach(function(key) {
if(proxyRes.headers[key] != undefined){
var newkey = key.replace(/^[a-z]|-[a-z]/g, (match) => {
return match.toUpperCase()
});
var newkey = key;
if (isHtml && key === 'content-length') {
// do nothing
} else {
res.setHeader(newkey, proxyRes.headers[key]);
}
}
});
res.writeHead(proxyRes.statusCode);
var isGzip = httpUtil.isGzip(proxyRes);
if (isGzip) {
proxyRes.pipe(new zlib.Gunzip())
.pipe(through(function (chunk, enc, callback) {
chunkReplace(this, chunk, enc, callback, '<style>body {background:red !important}</style>', proxyRes);
})).pipe(new zlib.Gzip()).pipe(res);
} else {
proxyRes.pipe(through(function (chunk, enc, callback) {
chunkReplace(this, chunk, enc, callback, '<style>body {background:red !important}</style>', proxyRes);
})).pipe(res);
}
}
next();
}
};