-
Notifications
You must be signed in to change notification settings - Fork 3
/
inject-finger-print.js
32 lines (30 loc) · 1.46 KB
/
inject-finger-print.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
'use strict';
/*eslint require-yield: off*/
const { loadFingerPrint } = require('./finger-print-builder');
const { join } = require('path');
module.exports = directory => {
const fingerPrint = loadFingerPrint('default', join(directory, require('./package.json').version));
const fingerPrintScript = `<script>${fingerPrint}</script>`;
return {
summary: 'FingerPrint Scripts Injection ',
*beforeSendResponse(requestDetail, responseDetail) {
const newResponse = responseDetail.response;
if (newResponse
&& newResponse.statusCode === 200
&& newResponse.header['Content-Type']
&& newResponse.header['Content-Type'].includes('text/html')
&& newResponse.body) {
const { body } = newResponse;
if (body && body.includes('<head>')) {
const encode = body.includes('charset="iso-8859-1"') || body.includes('charset="ISO-8859-1"') ? 'latin1' : 'utf8';
newResponse.body = Buffer.from(body.toString(encode).replace('<head>', `<head>${fingerPrintScript}`), encode);
}
else
__LOGGER_FINGERPRINT.silly(`[inject-finger-print] Could not find <head> tag to inject scripts in URL response: '${requestDetail && requestDetail.url}'`);
}
return new Promise((resolve) => {
resolve({ response: newResponse });
});
}
};
};