-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwizebot-node.js
476 lines (404 loc) · 11.8 KB
/
wizebot-node.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* License
* Copyright 2024 Alexia Gossa / nemelit.com
* Some part are from Fabien Tregan on 2024-05-01
*
* License identifier : BSD-3-Clause
*
* Alexia Gossa
* nemelit.com
* FRANCE
*/
//Load all NodeJS requires...
const fs = require ( 'fs');
const puppeteer = require ( 'puppeteer' );
const http = require ( 'http' );
const jsdom = require ( 'jsdom' );
const https = require ( 'https' );
const path = require ( 'path' );
const axios = require ( 'axios' );
const mime = require ( 'mime-types' );
//All files...
const sWizebotURL = 'wizebot-url.txt'; //Your Wizebot chat link in this file !
const sWizebotIndex = 'wizebot-index.html';
const sWizebotInjectCSS = 'wizebot-inject.css';
const sWizebotInjectJS = 'wizebot-inject.js';
const sWizebotBackup = 'wizebot-backup.html';
//Cert files (if needed)
const sHttpsKey = 'comodo-ssls-STAR-nemelit.com.key'; //Your certificate KEY file here !
const sHttpsCRT = 'comodo-ssls-STAR-nemelit.com.crt'; //Your certificate CRT file here !
const sHttpsCABundle = 'comodo-ssls-STAR-nemelit.com.ca-bundle'; //Your certificate CA bundle file here !
//Your listen port (HTTPS is optionnal if cert files not exist)
const iPortHTTP = 8080;
const iPortHTTPS = 8443;
//Main variables
var oBrowser;
var oPage;
var sPageDomain;
//
// Read certificates
//
var httpsOptions = null;
if (fs.existsSync(sHttpsKey) && fs.existsSync(sHttpsCRT) && fs.existsSync(sHttpsCABundle) )
{
httpsOptions = {
key: fs.readFileSync ( sHttpsKey ),
cert: fs.readFileSync ( sHttpsCRT ),
ca: fs.readFileSync ( sHttpsCABundle ),
};
}
//
// Read wizebot chat URL (external private file)
//
var sURL;
if (fs.existsSync(sWizebotInjectCSS))
sURL = fs.readFileSync ( sWizebotURL, { encoding: 'utf8', flag: 'r' } );
else
{
console.log ( "The file "+sWizebotURL+" not exists.\n" );
process.exit(1);
}
if (sURL.length<40)
{
console.log ( "The file "+sWizebotURL+" is incorrect. You need to paste your Wizebot widget chat link.\n" );
process.exit(1);
}
//
// Read previous backup of messages
//
var sBackupMessages;
if (fs.existsSync(sWizebotBackup))
sBackupMessages = fs.readFileSync ( sWizebotBackup, { encoding: 'utf8', flag: 'r' } );
else
sBackupMessages = "";
//sBackupMessages = "";
//
// Read inject CSS data
//
var sInjectCSS;
if (fs.existsSync(sWizebotInjectCSS))
sInjectCSS = fs.readFileSync ( sWizebotInjectCSS, { encoding: 'utf8', flag: 'r' } );
else
{
console.log ( "The file "+sWizebotInjectCSS+" not exists\n" );
process.exit(1);
}
//
// Read inject JS data
//
var sInjectJS;
if (fs.existsSync(sWizebotInjectJS))
sInjectJS = fs.readFileSync ( sWizebotInjectJS, { encoding: 'utf8', flag: 'r' } );
else
{
console.log ( "The file "+sWizebotInjectJS+" not exists\n" );
process.exit(1);
}
//
// Start web page preparation : concat, clean...
//
(async() => {
//Start Puppeteer to the correct URL
oBrowser = await puppeteer.launch({
executablePath: process.env.CHROME_BIN,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--allow-insecure-localhost',
'--allow-file-access-from-files',
'--enable-local-file-accesses'
]
});
oPage = await oBrowser.newPage();
await oPage.goto(sURL);
//Get a list of all scripts, remove them, insert backup messages and backup the clean page
var oScriptList = await oPage.evaluate( (sBackupMessages,sInjectCSS) => {
var oScripts = document.getElementsByTagName('script');
var oScriptList = [];
for (oScript of oScripts)
{
if (oScript.src!="")
{
oScriptList.push (
{
src: oScript.src,
code: ""
}
);
oScript.src = "";
}
else
{
oScriptList.push (
{
src: "",
code: oScript.innerText
}
);
oScript.innerText = "";
}
}
//Insert backup messages
const oDivMessages = document.getElementById ( 'messages' );
oDivMessages.innerHTML = sBackupMessages;
//Insert inject CSS
const oInsertStyle = document.createElement('style');
oInsertStyle.innerHTML = sInjectCSS;
document.head.appendChild ( oInsertStyle );
//Get the origin
const base = document.createElement('base');
base.href = window.location.origin;
document.head.insertBefore(base, document.head.firstChild);
//Relative to absolute links
var oLinks = document.getElementsByTagName('link');
for (oLink of oLinks)
{
oLink.href = oLink.href;
}
//Relative to absolute styles
var oStyles = document.getElementsByTagName('style');
for (oStyle of oStyles)
{
oStyle.src = oStyle.src;
oStyle.url = oStyle.url;
}
//Store the page
oScriptList.push (
{
src: "page",
code: document.documentElement.innerHTML
}
);
//Store the domain
oScriptList.push (
{
src: "domain",
code: base.href
}
);
return oScriptList;
}, sBackupMessages, sInjectCSS );
//Download and concat all scripts
var sScriptFileConcat = "";
var sPageHTML = "";
for (oScript of oScriptList)
{
switch (oScript.src)
{
case 'page':
sPageHTML = oScript.code;
break;
case 'domain':
sPageDomain = oScript.code;
break;
case '':
sScriptFileConcat += '\n';
sScriptFileConcat += '//Concat javascript embedded code\n\n';
sScriptFileConcat += oScript.code;
sScriptFileConcat += '\n';
break;
default:
sScriptFileConcat += '\n';
sScriptFileConcat += '//Concat javascript external code : ' + oScript.src + '\n\n';
sScriptFileConcat += await fileDownload ( oScript.src );
sScriptFileConcat += '\n';
break;
}
}
//Download all internal fonts
let oMatch = sPageHTML.match(/url\((.+?)\)/g);
for (sUrl of oMatch)
{
var iStringLength;
var sTemp;
var sAbsoluteUrl;
var sDataBase64;
var sMimeType;
iStringLength = sUrl.length;
sTemp = sUrl.substring(4,iStringLength-1);
sAbsoluteUrl = path.join(sPageDomain,sTemp);
sMimeType = await mime.lookup(sAbsoluteUrl);
sDataBase64 = await fileDownloadToBase64 ( sAbsoluteUrl );
sPageHTML = sPageHTML.replace ( sUrl,"url(data:"+sMimeType+";base64,"+sDataBase64+")");
}
//Insert special JS code to intercept all onMessage events
const sPattern = "function onMessage(";
if (sScriptFileConcat.indexOf(sPattern)!=-1)
{
sScriptFileConcat = sInjectJS + sScriptFileConcat;
sInsertCodeJS = 'function onMessage(type, channel, userstate, message, message_color) {\n';
sInsertCodeJS += ' var sMessage = message.replace ( "<", "­ < ­" );\n';
sInsertCodeJS += ' sMessage = sMessage.replace ( ">", "­ > ­" );\n';
sInsertCodeJS += ' var oReturn = onMessageNext(type, channel, userstate, sMessage, message_color);\n';
sInsertCodeJS += ' CheckMessages(true);\n';
sInsertCodeJS += ' return oReturn; }\n';
sInsertCodeJS += 'function onMessageNext(';
sScriptFileConcat = sScriptFileConcat.replace ( sPattern, sInsertCodeJS );
}
else
{
//Fatal error : It seems the code has been changed from Wizebot
sScriptFileConcat = "";
console.log ( "Fatal error ! Wizebot code has been changed !!!" );
process.exit(1);
}
//Write the index file
fs.writeFileSync(sWizebotIndex, sPageHTML + "<script>" + sScriptFileConcat + "</script>", { encoding: '', flag: 'w' } );
//Reload the index file and start new page
var sLoadHTML = fs.readFileSync ( sWizebotIndex, 'utf8' );
await oPage.close();
oPage = await oBrowser.newPage();
await oPage.setContent(sLoadHTML);
})();
//
// Save all backup messages
//
setTimeout ( ProcessBackupMessages, 2000 );
function ProcessBackupMessages ( )
{
(async() => {
var sBackupAllMessages = await oPage.evaluate(() => {
var oMessages = document.getElementById("messages");
var sHTML = null;
if (oMessages!=null)
sHTML = oMessages.innerHTML;
return sHTML;
});
if (sBackupAllMessages!=null)
fs.writeFileSync(sWizebotBackup, sBackupAllMessages, { encoding: '', flag: 'w' } );
})();
setTimeout ( ProcessBackupMessages, 1000 );
}
//
// HTTP/HTTPS Server processing
//
function ResponseServerToQuery ( req, res )
{
//Get highlights and reset parameters
const { searchParams } = new URL ( "https://fakedomain.com"+req.url );
var bHighlight = searchParams.has("highlight");
var bReset = searchParams.has("reset");
var sHeight = "";
var sHeightColor = "";
var sFontSize = "";
if (searchParams.has("height"))
sHeight = searchParams.get("height");
if (searchParams.has("heightcolor"))
sHeightColor = searchParams.get("heightcolor");
if (searchParams.has("messagesize"))
{
sFontSize = "<style>.message { font-size: "+searchParams.get("messagesize")+";}</style>";
}
if (searchParams.has("nicksize"))
{
sFontSize = "<style>.nicksimple { font-size: "+searchParams.get("nicksize")+";}</style>";
}
//Query data
(async() => {
var sData = await oPage.evaluate( (bReset,sHeight,sHeightColor,sFontSize) => {
var oMessages;
var oMessage;
var sGradient;
if (bReset==true)
{
//Clear all messages
document.getElementById("messages").innerHTML = "";
}
else
{
//Remove all animation remaining
oMessages = document.getElementById ("messages").children;
for (oMessage of oMessages)
{
oMessage.style.animationDuration = "";
oMessage.style.animationName = "";
}
}
if (sHeight=="")
sGradient = "";
else
{
if (sHeightColor=="")
sGradient = ""
else
sGradient = 'linear-gradient(180deg, rgba(120, 120, 120, 0.5) 0%, rgba(120, 120, 120, 0.5) '+sHeight+', '+sHeightColor+' 1px, '+sHeightColor+' 100%)';
}
document.getElementById("messages").style.height = sHeight;
document.getElementById("messagescontainer").style.background = sGradient;
return document.documentElement.innerHTML + sFontSize;
}, bReset,sHeight,sHeightColor,sFontSize );
//Insert highlight parameter
sData += "<script>var bNewMessageHighlight = "+bHighlight+";</script>";
//Send output
res.setHeader('Access-Control-Allow-Origin','*');
res.setHeader('Access-Control-Allow-Methods','OPTIONS, GET, POST, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Referrer-Policy', 'unsafe-url');
res.statusCode = 200;
res.write ( sData );
res.end();
})();
}
//
//Create HTTPS and HTTP servers
//
if (httpsOptions!=null)
{
const serverHTTPS = https.createServer ( httpsOptions, (req, res) => {
ResponseServerToQuery ( req, res );
}).listen(iPortHTTPS);
console.log ( "HTTPS server started" );
}
const serverHTTP = http.createServer ( (req, res) => {
ResponseServerToQuery ( req, res );
}).listen(iPortHTTP);
console.log ( "HTTP server started" );
//
// Waiting for Puppeteer stops...
//
console.log ( "Running..." );
//
// Download a file and convert it to Base64
//
async function fileDownloadToBase64 ( sUrl )
{
try {
//Get resource
const image = await axios.get(
sUrl,
{
responseType: 'arraybuffer'
}
);
//Convert to Base64
const raw = Buffer.from(image.data).toString('base64');
//Return data
return raw;
} catch(error)
{
return "";
}
}
//
// Download a file
//
async function fileDownload ( sUrl )
{
try {
//Get resource
const image = await axios.get(
sUrl,
{
responseType: 'arraybuffer'
}
);
//Convert to Base64
const raw = Buffer.from(image.data);
//Return data
return raw;
} catch(error)
{
return "";
}
}