-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_1page_perimeter81.com_02.js
642 lines (533 loc) · 19.9 KB
/
get_1page_perimeter81.com_02.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// export ARCROOT='../arcroot'
// node --inspect get_logrocket_blog.js
'use strict';
const fs = require('fs');
const util = require('util');
const path = require('path');
const axios = require("axios");
const colors = require("colors");
const crypto = require('crypto');
const hashmap = require('hashmap');
const cheerio = require("cheerio");
const execcmd = require('child_process').execFileSync;
const argv = process.execArgv.join();
const isDebug = argv.includes('inspect');
const isVerbs = argv.includes('verbose');
const urlroot = 'https://www.perimeter81.com/blog';
const REGular = ".blog-post-container";
const FEAture = ".NONO";
const blgBODY = ".entry-content";
const ua_chrm = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36';
var ONEPAGE = false;
var CHCKNEW = false;
if (! process.env.ARCROOT) {
console.log('warning: export ARCROOT first, use ../arcroot as default');
process.env.ARCROOT = '../arcroot';
}
// create blog data folder
let www = urlroot.match(/http[s]:\/\/(.+?)\//);
let bserver = www[1].replace("www\.", '');
const prjroot = process.env.ARCROOT + '/' + bserver;
const pagroot = process.env.ARCROOT + '/' + bserver + '/page';
const datroot = process.env.ARCROOT + '/' + bserver + '/data';
const blgroot = process.env.ARCROOT + '/' + bserver + '/blog';
const onepage = process.env.ARCROOT + '/' + bserver + '/' + bserver + '.html';
const blogarry = prjroot + '/blogs.array';
const bloghash = prjroot + '/blogs.hash' ;
const blgindex = prjroot + '/index.html' ;
const config = {
headers: { 'User-Agent': ua_chrm },
}
function parse_blog_url($, section){
const posts = $(section);
let blogarr = [];
let ttag = 'h3';
if(section.match(/cfUVPA/)) ttag = 'h2';
for (let i = 0; i < posts.length; i++) {
let postTitleWrapper = $(posts[i]).find(".title-post")[0],
postTitle = $(postTitleWrapper).text();
postTitle = postTitle.replace(/\n/g, '');
let title = postTitle.replace(/ /g, '_');
title = title.replace(/[\W]+/g, '');
if(title.length < 10) continue;
let postLinkWrapper = $(posts[i]).find("a")[0],
postLink = $(postLinkWrapper).attr("href");
if(! postLink.match('www') ) postLink = www[0] + postLink;
let author = 'MK', postDate = 'January 5, 2000', postDesc=title;
let authorWrapper = $(posts[i]).find(".author-post")[0];
author = $(authorWrapper).text();
postDate = $(posts[i]).find("time").text();
if(! postDate.match(/\w+/) ) postDate = 'January 20, 2000',
postDate = postDate.replace("th", '');
postDate = postDate.replace("st", '');
postDate = postDate.replace("nd", '');
postDate = postDate.replace("rd", '');
let postID = "post-" + crypto.createHash('md5').update(author+title, 'utf8').digest('hex').slice(0,6);
let blogFile = dateFormat (new Date(postDate), "%Y-%m-%d", true);
blogFile += '~' + postID + '~' + title;
const item_json = blog_attr(postID, postTitle, author, postLink, postDate, blogFile, postDesc);
//console.log(item_json); process.exit(1);
blogarr.push(item_json);
}
return blogarr;
}
async function find_blog_links(purl){
let num = purl.match(/http[s]:\/\/.+\/(\d+)\//);
let page = pagroot + '/p' + int3(num[1]) + '.html';
let firstp = false;
if (purl.search(/\/1\//) > 0 ){
purl = urlroot; // blog root page with featureed post
firstp = true;
}
var html;
if( ONEPAGE ) {
if(CHCKNEW) {
let resp = await axios.get(purl);
await save_to_file(page, resp.data);
html = resp.data;
}
else{
console.log('reading from: ' + onepage);
html = fs.readFileSync(onepage).toString();
}
}else{
if (fs.existsSync(page) && ! firstp ){ // always read the 1st page
html = fs.readFileSync(page).toString();
}else{
let resp = await axios.get(purl);
await save_to_file(page, resp.data);
html = resp.data;
}
}
var $ = cheerio.load(html)
// parse regular posts
var regular_posts = parse_blog_url($, REGular);
if (firstp){ // parse featured posts in first page
var feature_posts=[];
feature_posts = parse_blog_url($, FEAture);
regular_posts = [...feature_posts, ...regular_posts];
}
console.log("blog found: %d", regular_posts.length);
return regular_posts;
}
async function save_to_file(file, content){
try {
if (fs.existsSync(file)) {
return;
}else{
await fs.writeFile(file, content, function (err) {if (err) throw err;});
}
} catch(err) {
console.error(err)
}
}
async function save_blog_content(url, file){
let stime = process.hrtime();
let resp = await axios.get(url, config);
let orgh = resp.data.toString();
var $ = cheerio.load(orgh);
//<meta property="article:published_time" content="2022-04-18T19:30:43+00:00" />
let dd = orgh.match(/article:published_time.*?="(.+)T/);
let bdate = dd[1];
//remove product/share/links to keep layout clean
$(".share").remove();
$(".blog-plug").remove();
$(".sharedaddy").remove();
$(".hidden-lg-up").remove();
$(".prevnextlinks").remove();
$(".sidebar").remove();
$(".blogNavbar").remove();
$(".article__footer").remove();
$(".related-wrapper").remove(); // p81
let title = $("title").text().replace('- Axis Security', '-JK CTI');
let btags = $('.aretags').find('li').text().split('#').slice(1,);
if(btags == null) btags = '';
let bodym = $(blgBODY).html(); // locate blog main body
let topic = $(".title-page").text();
if(urlroot.match('perimeter81')) bodym = `<br><h1> ${topic} </h1><br>\n` + bodym;
if(bodym == null) bodym = $(".blog-template-grid-expand").html(); // twingate
// to load img directly
bodym = bodym.replaceAll(/srcset=".*?"/g, ' ');
bodym = bodym.replaceAll(/background-image:url\(.*?\);/g, ' ');
if(file.match('2000-01-20') && bdate != null){
file = file.replace('2000-01-20', bdate);
}else{
bdate = '2000-01-20';
}
$ = cheerio.load(bodym); // only blog main part
const bhead_s =`<!DOCTYPE html>
<html lang="en-US">
<head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="JK" content="SZ Threat Intel" />
<title>${title}</title>
<link rel='stylesheet' id='bootstrap4-css' href='../data/bootstrap.min.css' type='text/css' media='all' />
<link rel='stylesheet' id='divi-plus-styles-css' href='../data/style.css' type='text/css' media='all' />
<style>.hljs {background: #ededed !important;}</style>
</head>
<body class="post-template-default single single-post single-format-standard">
<div class="container">
`;
const bhead_e ='</div></body></html>';
var gg = $('img') // parse all images link and save to local
for(let i=0; i < gg.length; i++ ){
let imgsrc = $(gg[i]).attr('src');
let datsrc = $(gg[i]).attr('data-src');
if( imgsrc == null || ! imgsrc.match(/\w+/)) continue;
let imgurl = imgsrc;
if( imgurl.match(/data:image/) ) imgurl = datsrc;
if( imgurl == null) continue;
// skip if already downloaded
let imagename = imgurl.split('/').slice(-1)[0].split('?')[0];
let filelocal = datroot + '/' + imagename; //save
// handle special * in file name
if(imagename.match(/[\*]/)) {
filelocal = filelocal.replace('*', 'A');
imagename = imagename.replace('*', 'A');
process.stdout.write(`*`);
}
if(imagename.match(/%20/)) {
filelocal = filelocal.replaceAll(/%20/g, 'B');
imagename = imagename.replaceAll(/%20/g, 'B');
process.stdout.write(`%`);
}
// use local downloaded images
bodym = bodym.replaceAll(imgsrc, '../data/' + imagename);
if (fs.existsSync(filelocal)) continue; // skip if already downloaded
if(! imgurl.match(/http/)) imgurl = www[0] + imgurl;
let encodedurl = imgurl;
if(imgurl.includes('*')) encodedurl= encodeURI(imgurl); // handle special char
try {
const response = await axios({
method: 'GET',
url: encodedurl,
responseType: 'stream',
});
const w = response.data.pipe(fs.createWriteStream(filelocal));
w.on('finish', () => {
process.stdout.write(`.`);
});
} catch (err) {
try {
process.stdout.write(`!`);
execcmd('wget', [imgurl, '-q', '-O', filelocal], {stdio:'inherit'});
} catch (err) {
console.log(err.message);
}
}
}
console.log("");
// only keep the blog main body, saved space a lot
bodym = bhead_s + bodym + bhead_e;
await save_to_file(file, bodym);
let stopt = process.hrtime(stime);
console.log("-- %s %s %s %s", String(parseInt(bodym.length/1024)+'k').padEnd(7), String(gg.length+'p').padEnd(4), `${(stopt[0] * 1e9 + stopt[1])/1e9}s`, url);
// original response data
// await save_to_file(blgroot + '/' + file + '.resp', resp.data);
return [btags, bdate, file];
}
async function get_all_blog_url2array(urlp, firstpage_only=false, finalpage=0) {
let stime = process.hrtime();
!fs.existsSync(prjroot) && fs.mkdirSync(prjroot);
!fs.existsSync(pagroot) && fs.mkdirSync(pagroot);
!fs.existsSync(datroot) && fs.mkdirSync(datroot);
!fs.existsSync(blgroot) && fs.mkdirSync(blgroot);
var loadhtml;
if ( fs.existsSync(onepage) ){ // onepage is saved manually
ONEPAGE = true;
loadhtml = fs.readFileSync(onepage).toString();
}else{
let resp = await axios.get(urlp, config);
loadhtml = resp.data;
}
var $ = cheerio.load(loadhtml);
var lastpage = 1;
if( ! ONEPAGE ) { //get max page count
if(finalpage > 0)
lastpage = finalpage;
else
lastpage = $(".bottompagination .next > a").attr("href").match(/\d+/)[0];
if(isDebug) console.log("\nlast page num:" + lastpage + "\n");
}
// load existing blog urls when checking new
var blogs = [];
if (fs.existsSync(blogarry) && firstpage_only){
console.log('read existing blog array');
let buffe = fs.readFileSync(blogarry).toString();
blogs = JSON.parse(buffe);
}
let bfilelist = fs.readdirSync(blgroot).join(' ');
// find all posts in each blog page
for(let i = 1; i <= lastpage; i++) {
let page = urlroot +'page/' + i + '/';
if( ONEPAGE ) page = urlroot + '/1/';
let pburls = await find_blog_links(page);
if(firstpage_only){ // only check 1st page on new blog
console.log("get %d blogs 1st page, %d", pburls.length, blogs.length);
pburls.forEach( e => {
let o = JSON.parse(e);
let f = o.idblg + '~' + o.bdesc + '.html';
//if(! fs.existsSync(f)){
if(! bfilelist.match(f)){
console.log(" add %s %s".green, f, o.blink);
blogs.unshift(e); //add new
}else{
console.log(" skip %s".grey, o.blink);
}
});
}
else{ // full from page 1 to last
blogs = [...blogs, ...pburls];
}
// save blog array for debug
fs.writeFile(blogarry, JSON.stringify(blogs), function (err) { if (err) throw err; });
if(isDebug) console.log('blogs in page %d %d %s', i, blogs.length, page);
if(firstpage_only) break;
}
let stopt = process.hrtime(stime);
console.log(`\nblog array: ${(stopt[0] * 1e9 + stopt[1])/1e9} seconds`.yellow);
}
async function process_blog_cont2file( stopnum = 10000 ){
let stime = process.hrtime();
let buffe = fs.readFileSync(blogarry).toString();
let blogs = JSON.parse(buffe);
var bhmap;
if (fs.existsSync(bloghash)){
buffe = fs.readFileSync(bloghash).toString();
bhmap = new hashmap(JSON.parse(buffe));
}else{
bhmap = new hashmap();
}
let bfilelist = fs.readdirSync(blgroot).join(' ');
// save each individual blog
let tablerow = '';
let indexblg = new Set()
for(let i = 0, j=1; i < blogs.length; i++) {
if( i+1 > stopnum) break;
try {
let obj = JSON.parse(blogs[i]);
let ofile = blgroot + '/' + obj.bfile + '.html';
let f = obj.idblg + '~' + obj.bdesc + '.html';
let k = obj.authr + ' ' + obj.bdesc; // hash key
if( indexblg.has(k) ) continue;
indexblg.add(k); // add to index at least once
let bdate, bfile;
if(bfilelist.match(f) ){
if( bhmap.get(k) != null ){
obj.btags = bhmap.get(k).btags;
obj.datep = bhmap.get(k).datep;
obj.bfile = bhmap.get(k).bfile;
obj.bdate = bhmap.get(k).bdate;
bdate = obj.bdate;
bfile = obj.bfile;
}
}else{
[obj.btags, bdate, bfile] = await save_blog_content(obj.blink, ofile);
}
bfile = bfile.split('/').slice(-1)[0].split('.')[0];
obj.bdate = bdate;
obj.bfile = bfile;
f = 'blog/' + obj.bfile + '.html';
// add a uniq id
obj.md5sm = crypto.createHash('md5').update(obj.authr+obj.title, 'utf8').digest('hex').slice(0,16);
bhmap.set(k, obj);
obj.btags = []; // extract tag from url path
obj.btags.push(obj.blink.split('/').slice(-2)[0]);
var bltgs = '';
if(obj.btags.length > 0)
String(obj.btags).split(',').forEach( e => {
bltgs += `<a class="btn btn-success">${e}</a> `;
});
let rr;
if(rr = bltgs.match(/(.*)( )/)) bltgs = rr[1];
obj.title = obj.title.slice(0, 100);
// create index table rows
tablerow += `
<tr >
<td >${j}</td>
<td >${bdate}</td>
<td ><a href="${f}">${obj.title}</a></td>
<td >${bltgs}</a></td>
<td ><a href="${obj.blink}" target="_blank" class="extlk"></a></td>
</tr>`;
j++;
//process.stdout.write(`b`.grey);
if( (i+1)%100 == 0 ){ console.log(); }
} catch(err) {
console.log(i.yellow + blogs[i]);
console.log(err)
}
}
// create blogs.hash
fs.writeFile(bloghash, JSON.stringify(bhmap), function (err) { if (err) throw err; });
// create index.html
let tt = blog_index();
fs.writeFile(blgindex, tt[0] + tablerow + tt[1], function (err) { if (err) throw err; });
console.log("\n");
console.log("blog count: " + blogs.length);
let stopt = process.hrtime(stime);
console.log(`total time: ${(stopt[0] * 1e9 + stopt[1])/1e9} seconds`.cyan);
}
function blog_index(){
let tt = [];
tt[0] = `<!DOCTYPE html>
<html lang="en-US"><head><meta charset="utf-8">
<meta name="JK" content="SZ Threat Intel" />
<title>blog index</title>
<style>
.styled-table {
border-collapse: collapse;
margin: auto;
font-size: 0.9em;
font-family: Segoe UI,Arial,sans-serif;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
border-radius: 5px 5px 0 0;
overflow: hidden;
}
.styled-table caption {
margin-top: 1em;
margin-bottom: 1em;
font-size: 2em;
font-weight: bold;
text-align: center;
color: #327A81;
}
.styled-table thead tr {
background-color: #91CED4;
color: #ffffff;
text-align: left;
font-size: 1.1em;
text-decoration:none;
}
.styled-table th,
.styled-table td {
padding: 12px 15px;
}
.styled-table tbody tr {
border-bottom: 1px solid #dddddd;
}
.styled-table tbody tr:nth-of-type(even) {
background-color: #f3f3f3;
}
.styled-table tbody tr:last-of-type {
border-bottom: 2px solid #91CED4;
}
.styled-table tbody tr.active-row {
font-weight: bold;
color: #91CED4;
}
a { text-decoration: none; color: #000}
a:hover { color: Orange ; }
.btn {
text-align: center;
vertical-align: middle;
border: 1px solid transparent;
padding: 0.15rem 0.5rem;
border-radius: 0.25rem;
text-decoration:none;
color: #fff;
background-color: #67D5AC;
border-color: #67D5AC; }
.btn-success:hover {
color: #fff;
background-color: #91CED4;
border-color: #25BCAA; }
}
</style></head><body>
<table class="styled-table">
<caption>${bserver}</caption>
<thead>
<tr >
<th style="border-radius: 5px 0 0 0;">seq</th>
<th >Date</th>
<th >Title</th>
<th >Tags</th>
<th style="border-radius: 0 5px 0 0;">org</th>
</tr></thead>
<tbody>
`;
// create svg and add to <a> https://seanyeh.com/pages/creating_svgs_in_javascript/
tt[1] = `</tbody></table>
<script type="text/javascript">
var extlk = document.getElementsByClassName('extlk');
for (var i=0; i < extlk.length; i++) {
var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("class", "octicon octicon-star");
svg.setAttribute("viewBox", "0 0 16 16");
svg.setAttribute("width", "16");
svg.setAttribute("height", "16");
svg.setAttribute("fill", "#67D5AC");
var path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("d", "M15 2a1 1 0 0 0-1-1H2a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V2zM0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V2zm5.854 8.803a.5.5 0 1 1-.708-.707L9.243 6H6.475a.5.5 0 1 1 0-1h3.975a.5.5 0 0 1 .5.5v3.975a.5.5 0 1 1-1 0V6.707l-4.096 4.096z");
svg.appendChild(path);
extlk[i].textContent ='';
extlk[i].appendChild(svg);
}
</script></body></html>`;
return tt;
}
function blog_attr(...attr) {
if(attr[6] == undefined ) attr[6]=''
return `{
"idblg" : "${attr[0]}",
"title" : "${attr[1]}",
"authr" : "${attr[2]}",
"blink" : "${attr[3]}",
"datep" : "${attr[4]}",
"bfile" : "${attr[5]}",
"bdesc" : "${attr[6]}"
}`;
}
function int3(num, size=3) {
let s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
function dateFormat (date, fstr, utc) {
utc = utc ? 'getUTC' : 'get';
return fstr.replace (/%[YmdHMS]/g, function (m) {
switch (m) {
case '%Y': return date[utc + 'FullYear'] (); // no leading zeros required
case '%m': m = 1 + date[utc + 'Month'] (); break;
case '%d': m = date[utc + 'Date'] (); break;
case '%H': m = date[utc + 'Hours'] (); break;
case '%M': m = date[utc + 'Minutes'] (); break;
case '%S': m = date[utc + 'Seconds'] (); break;
default: return m.slice (1); // unknown code, remove %
}
// add leading zero if required
return ('0' + m).slice (-2);
});
}
// --- start ---
const arg = process.argv.slice(2);
switch (arg[0]) {
case '--save-urls':
console.log('== save-urls ==');
get_all_blog_url2array(urlroot);
break;
case '--check-new':
console.log('== check-new ==');
CHCKNEW = true;
get_all_blog_url2array(urlroot, true);
break;
case '--save-blog':
console.log('== save-blog ==');
if(arg[1] > 0){
console.log("process first " + arg[1] + " blogs");
process_blog_cont2file(arg[1]);
}else{
if(arg.length ==2 && arg[1].match(/[a-zA-Z]/)) break;
console.log("process all blogs");
process_blog_cont2file();
}
break;
case '--help':
default:
let ndjs = path.basename(__filename);
console.log(`\nUsage:
node --inspect ${ndjs} --save-urls
node --inspect ${ndjs} --save-blog NUM
node --inspect ${ndjs} --check-new`);
}