forked from udger/udger-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-alt.js
1120 lines (930 loc) · 45.9 KB
/
index-alt.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const Database = require('better-sqlite3');
const debug = require('debug')('udger-nodejs');
const Address6 = require('ip-address').Address6;
const Address4 = require('ip-address').Address4;
const utils = require('./utils');
const fs = require('fs-extra');
const dotProp = require('dot-prop');
const path = require('path');
const RandExp = require('randexp');
const jsons = {
udger_crawler_list: require('./out/udger_crawler_list'),
udger_crawler_class: require('./out/udger_crawler_class'),
udger_os_regex: require('./out/udger_os_regex'),
udger_os_list: require('./out/udger_os_list'),
udger_deviceclass_regex: require('./out/udger_deviceclass_regex'),
udger_deviceclass_list: require('./out/udger_deviceclass_list'),
udger_client_class: require('./out/udger_client_class'),
udger_devicename_regex: require('./out/udger_devicename_regex'),
udger_devicename_list: require('./out/udger_devicename_list'),
udger_devicename_brand: require('./out/udger_devicename_brand'),
}
function map(table, data) {
return table.headers.reduce((acc, v, i) => {
acc[v] = data[i];
return acc;
}, {})
}
/** Class exposing udger parser methods */
class UdgerParser {
/**
* Load udger SQLite3 database.
* @param {string} file - full path to udgerdb_v3.dat
*/
constructor(file) {
this.db = new Database(file, { readonly: true, fileMustExist: true });
this.file = file;
this.ip = null;
this.ua = null;
this.cacheEnable = false;
this.cacheMaxRecords = 4000;
this.cache = {};
this.keyCache = '';
this.defaultRet = fs.readJsonSync(path.resolve(__dirname+'/defaultResult.json'));
this.retUa = {};
this.retIp = {};
}
/**
* Connect (reconnect) sqlite database
* @return {Boolean} true if db has been opened, false if already connected
*/
connect() {
if (!this.db) {
this.db = new Database(this.file, { readonly: true, fileMustExist: true });
return true;
}
return false;
}
/**
* Disconnect sqlite database, avoid read/write conflict
* see https://github.com/udger/udger-updater-nodejs/issues/5
* @return {Boolean} true if db has been closed, false if no db opened
*/
disconnect() {
if (this.db) {
this.db.close();
this.db = null;
return true;
}
return false;
}
/**
* Initialize User-Agent or IP(v4/v6), or both
* @param {Object} data - An object
* @param {String} data.ua - User-Agent
* @param {String} data.ip - IP Address
*/
set(data) {
const help = 'set() is waiting for an object having only ip and/or ua attribute';
if (!data) {
throw new Error(help);
}
if (typeof data === 'string') {
throw new Error(help);
}
for (const key in data) {
if (key === 'ua') {
this.ua = data.ua;
} else if (key === 'ip') {
this.ip = data.ip.toLowerCase();
} else {
throw new Error(help);
}
}
this.keyCache = '';
if (this.cacheEnable) {
if (this.ip) this.keyCache = this.ip;
if (this.ua) this.keyCache += this.ua;
}
this.retUa = JSON.parse(JSON.stringify(this.defaultRet['user_agent']));
this.retIp = JSON.parse(JSON.stringify(this.defaultRet['ip_address']));
}
/**
* Activate cache
* @param {Boolean} cache - true or false
*/
setCacheEnable(cache) {
this.cacheEnable = cache;
}
/**
* Return if the cache is enable or not
* @return {Boolean} true if the cache is enable, false if not
*/
isCacheEnable() {
return this.cacheEnable;
}
/**
* Set Cache Size
* @param {Number} records - the maximum number of items we want to keep in the cache
*/
setCacheSize(records) {
this.cacheMaxRecords = records;
}
/**
* Check if a key exist in the cache
* @param {Number} key - key can be UA or UA+IP
* @return {Boolean} return true if the key exist in the cache, false if not
*/
cacheKeyExist(key) {
if (this.cache[key]) {
return true;
}
return false;
}
/**
* Return an item from the cache
* @param {String} key - key can be UA or UA+IP
* @return {Object} stored parser result
*/
cacheRead(key, opts) {
const ret = this.cache[key];
if (opts && opts.json) {
if (opts.full) {
ret['fromCache'] = true;
}
} else {
ret['from_cache'] = true;
}
return ret;
}
/**
* Write an item into the cache
* @param {String} key - key can be UA or UA+IP
*/
cacheWrite(key, data) {
if (this.cache[key]) {
// already in the cache
return;
}
this.cache[key] = data;
debug('cache: store result of %s (length=%s)', key);
debug('cache: entries count: %s/%s', (Object.keys(this.cache).length || 0), this.cacheMaxRecords);
// warning, js object is used for performance reason
// as opposite of php object, we can not use splice/pop stuff here
// so, when an entry must be remove because the cache is full, we
// can not determine which one will be removed
while (Object.keys(this.cache).length > this.cacheMaxRecords) {
debug('cache: removing entry', Object.keys(this.cache)[0]);
delete this.cache[Object.keys(this.cache)[0]];
}
}
/**
* Clean the cache
*/
cacheClean() {
this.cache = {};
}
/**
* Parse the User-Agent string
* @param {String} ua - An User-Agent string
*/
parseUa(ua, opts) {
const rua = JSON.parse(JSON.stringify(this.retUa));
const ruaJson = {};
if (!ua) return {
udger: rua,
json: ruaJson
};
let q;
let r;
let e;
let client_id = 0;
let client_class_id = -1;
let os_id = 0;
let deviceclass_id = 0;
debug('parse useragent string: START (useragent: ' + ua + ')');
rua['ua_string'] = ua;
rua['ua_class'] = 'Unrecognized';
rua['ua_class_code'] = 'unrecognized';
dotProp.set(ruaJson, 'ua.string', ua);
if (opts.full) {
dotProp.set(ruaJson, 'ua.class.name', 'Unrecognized');
dotProp.set(ruaJson, 'ua.class.code', 'unrecognized');
} else {
dotProp.set(ruaJson, 'ua.class', 'unrecognized');
}
////////////////////////////////////////////////
// search for crawlers
////////////////////////////////////////////////
// q = this.db.prepare(
// 'SELECT ' +
// 'udger_crawler_list.id as botid,' +
// 'name, ver, ver_major, last_seen, respect_robotstxt,' +
// 'family, family_code, family_homepage, family_icon,' +
// 'vendor, vendor_code, vendor_homepage,' +
// 'crawler_classification, crawler_classification_code ' +
// 'FROM udger_crawler_list ' +
// 'LEFT JOIN udger_crawler_class ON udger_crawler_class.id=udger_crawler_list.class_id ' +
// 'WHERE ua_string=?'
// );
// r = q.get(ua);
var crawler = jsons.udger_crawler_list.data.find(v => v[1] === ua);
if(crawler) {
var crawler_class = jsons.udger_crawler_class.data.find(v => v[0] === crawler[4]);
r = {
...map(jsons.udger_crawler_class, crawler_class),
...map(jsons.udger_crawler_list, crawler),
};
r.botid = r.id;
delete r.id;
debug('parse useragent string: crawler found');
client_class_id = 99;
// UDGER FORMAT
rua['ua_class'] = 'Crawler';
rua['ua_class_code'] = 'crawler';
rua['ua'] = r['name'] || '';
rua['ua_version'] = r['ver'] || '';
rua['ua_version_major'] = r['ver_major'] || '';
rua['ua_family'] = r['family'] || '';
rua['ua_family_code'] = r['family_code'] || '';
rua['ua_family_homepage'] = r['family_homepage'] || '';
rua['ua_family_vendor'] = r['vendor'] || '';
rua['ua_family_vendor_code'] = r['vendor_code'] || '';
rua['ua_family_vendor_homepage'] = r['vendor_homepage'] || '';
rua['ua_family_icon'] = r['family_icon'] || '';
rua['ua_family_info_url'] = 'https://udger.com/resources/ua-list/bot-detail?bot=' + (r['family'] || '') + '#id' + (r['botid'] || '');
rua['crawler_last_seen'] = r['last_seen'] || '';
rua['crawler_category'] = r['crawler_classification'] || '';
rua['crawler_category_code'] = r['crawler_classification_code'] || '';
rua['crawler_respect_robotstxt'] = r['respect_robotstxt'] || '';
// JSON FORMAT
rua['ua'] && dotProp.set(ruaJson, 'ua.name', rua['ua']);
if (opts.full) {
dotProp.set(ruaJson, 'ua.class.name', 'Crawler');
dotProp.set(ruaJson, 'ua.class.code', 'crawler');
rua['ua_version'] && dotProp.set(ruaJson, 'ua.version.current', rua['ua_version']);
rua['ua_version_major'] && dotProp.set(ruaJson, 'ua.version.major', rua['ua_version_major']);
rua['ua_family'] && dotProp.set(ruaJson, 'ua.family.name', rua['ua_family']);
rua['ua_family_code'] && dotProp.set(ruaJson, 'ua.family.code', rua['ua_family_code']);
rua['ua_family_homepage'] && dotProp.set(ruaJson, 'ua.family.homepage', rua['ua_family_homepage']);
rua['ua_family_vendor'] && dotProp.set(ruaJson, 'ua.family.vendor.name', rua['ua_family_vendor']);
rua['ua_family_vendor_code'] && dotProp.set(ruaJson, 'ua.family.vendor.code', rua['ua_family_vendor_code']);
rua['ua_family_homepage'] && dotProp.set(ruaJson, 'ua.family.vendor.homepage', rua['ua_family_homepage']);
rua['ua_family_icon'] && dotProp.set(ruaJson, 'ua.family.icon', rua['ua_family_icon']);
rua['ua_family'] && r['botid'] && dotProp.set(ruaJson, 'ua.family.infoUrl', rua['ua_family_info_url']);
} else {
dotProp.set(ruaJson, 'ua.class', 'crawler');
rua['ua_family_code'] && dotProp.set(ruaJson, 'ua.family.code', rua['ua_family_code']);
rua['ua_family_homepage'] && dotProp.set(ruaJson, 'ua.family.homepage', rua['ua_family_homepage']);
rua['ua_family_vendor_code'] && dotProp.set(ruaJson, 'ua.family.vendor', rua['ua_family_vendor_code']);
}
rua['crawler_last_seen'] && dotProp.set(ruaJson, 'crawler.lastSeen', rua['crawler_last_seen']);
if (opts.full) {
rua['crawler_category'] && dotProp.set(ruaJson, 'crawler.category.name', rua['crawler_category']);
rua['crawler_category_code'] && dotProp.set(ruaJson, 'crawler.category.code', rua['crawler_category_code']);
rua['crawler_respect_robotstxt'] && dotProp.set(ruaJson, 'crawler.respectRobotsTxt', rua['crawler_respect_robotstxt']);
} else {
rua['crawler_category_code'] && dotProp.set(ruaJson, 'crawler.category', rua['crawler_category_code']);
}
} else {
q = this.db.prepare(
'SELECT class_id,client_id,regstring,name,name_code,homepage,icon,icon_big,engine,vendor,vendor_code,vendor_homepage,uptodate_current_version,client_classification,client_classification_code ' +
'FROM udger_client_regex ' +
'JOIN udger_client_list ON udger_client_list.id=udger_client_regex.client_id ' +
'JOIN udger_client_class ON udger_client_class.id=udger_client_list.class_id ' +
'ORDER BY sequence ASC'
);
for (r of q.iterate()) {
e = ua.match(utils.phpRegexpToJs(r['regstring']));
if (e) {
debug('parse useragent string: client found');
client_id = r['client_id'];
client_class_id = r['class_id'];
rua['ua_class'] = r['client_classification'];
rua['ua_class_code'] = r['client_classification_code'];
if (opts.full) {
dotProp.set(ruaJson, 'ua.class.name', rua['ua_class']);
dotProp.set(ruaJson, 'ua.class.code', rua['ua_class_code']);
} else {
dotProp.set(ruaJson, 'ua.class', rua['ua_class_code']);
}
if (e[1]) {
rua['ua'] = r['name'] + ' ' + e[1];
rua['ua_version'] = e[1];
rua['ua_version_major'] = e[1].split('.')[0];
} else {
rua['ua'] = r['name'];
rua['ua_version'] = '';
rua['ua_version_major'] = '';
}
if (rua['ua']) {
dotProp.set(ruaJson, 'ua.name', rua['ua']);
} else {
dotProp.delete(ruaJson, 'ua.name');
}
if (opts.full) {
if (rua['ua_version']) {
dotProp.set(ruaJson, 'ua.version.current', rua['ua_version']);
} else {
dotProp.delete(ruaJson, 'ua.version.current');
}
if (rua['ua_version_major']) {
dotProp.set(ruaJson, 'ua.version.current', rua['ua_version_major']);
} else {
dotProp.delete(ruaJson, 'ua.version.current');
}
}
rua['ua_uptodate_current_version'] = r['uptodate_current_version'] || '';
rua['ua_family'] = r['name'] || '';
rua['ua_family_code'] = r['name_code'] || '';
rua['ua_family_homepage'] = r['homepage'] || '';
rua['ua_family_vendor'] = r['vendor'] || '';
rua['ua_family_vendor_code'] = r['vendor_code'] || '';
rua['ua_family_vendor_homepage'] = r['vendor_homepage'] || '';
rua['ua_family_icon'] = r['icon'] || '';
rua['ua_family_icon_big'] = r['icon_big'] || '';
rua['ua_family_info_url'] = 'https://udger.com/resources/ua-list/browser-detail?browser=' + (r['name'] || '');
rua['ua_engine'] = r['engine'] || '';
if (opts.full) {
rua['ua_uptodate_current_version'] && dotProp.set(ruaJson, 'ua.uptodateCurrentVersion', rua['ua_uptodate_current_version']);
rua['ua_family'] && dotProp.set(ruaJson, 'ua.family.name', rua['ua_family']);
rua['ua_family_code'] && dotProp.set(ruaJson, 'ua.family.code', rua['ua_family_code']);
rua['ua_family_homepage'] && dotProp.set(ruaJson, 'ua.family.homepage', rua['ua_family_homepage']);
rua['ua_family_vendor'] && dotProp.set(ruaJson, 'ua.family.vendor.name', rua['ua_family_vendor']);
rua['ua_family_vendor_code'] && dotProp.set(ruaJson, 'ua.family.vendor.code', rua['ua_family_vendor_code']);
rua['ua_family_vendor_homepage'] && dotProp.set(ruaJson, 'ua.family.vendor.homepage', rua['ua_family_vendor_homepage']);
rua['ua_family_icon'] && dotProp.set(ruaJson, 'ua.family.icon', rua['ua_family_icon']);
rua['ua_family_icon_big'] && dotProp.set(ruaJson, 'ua.family.iconBig', rua['ua_family_icon_big']);
if (r['name']) {
dotProp.set(ruaJson, 'ua.family.infoUrl', rua['ua_family_info_url']);
}
} else {
rua['ua_family_code'] && dotProp.set(ruaJson, 'ua.family', rua['ua_family_code']);
}
rua['ua_engine'] && dotProp.set(ruaJson, 'ua.engine', rua['ua_engine']);
break;
}
}
}
////////////////////////////////////////////////
// os
////////////////////////////////////////////////
// q = this.db.prepare(
// 'SELECT os_id,regstring,family,family_code,name,name_code,homepage,icon,icon_big,vendor,vendor_code,vendor_homepage ' +
// 'FROM udger_os_regex ' +
// 'JOIN udger_os_list ON udger_os_list.id=udger_os_regex.os_id ' +
// 'ORDER BY sequence ASC'
// );
var udger_os_regex = jsons.udger_os_regex.data.sort((a,b) => a[2] - b[2]);
for (r of udger_os_regex) {
let e = ua.match(utils.phpRegexpToJs(r[1]));
if (e) {
let os = jsons.udger_os_list.data.find(v => r[0] === v[0]);
r = {
...map(jsons.udger_os_regex, r),
...map(jsons.udger_os_list, os)
}
debug('parse useragent string: os found');
os_id = r['os_id'];
rua['os'] = r['name'] || '';
rua['os_code'] = r['name_code'] || '';
rua['os_homepage'] = r['homepage'] || '';
rua['os_icon'] = r['icon'] || '';
rua['os_icon_big'] = r['icon_big'] || '';
rua['os_info_url'] = 'https://udger.com/resources/ua-list/os-detail?os=' + (r['name'] || '');
rua['os_family'] = r['family'] || '';
rua['os_family_code'] = r['family_code'] || '';
rua['os_family_vendor'] = r['vendor'] || '';
rua['os_family_vendor_code'] = r['vendor_code'] || '';
rua['os_family_vendor_homepage'] = r['vendor_homepage'] || '';
if (opts.full) {
rua['os'] && dotProp.set(ruaJson, 'os.name', rua['os']);
rua['os_code'] && dotProp.set(ruaJson, 'os.code', rua['os_code']);
rua['os_homepage'] && dotProp.set(ruaJson, 'os.homepage', rua['os_homepage']);
rua['os_icon'] && dotProp.set(ruaJson, 'os.icon', rua['os_icon']);
rua['os_icon_big'] && dotProp.set(ruaJson, 'os.iconBig', rua['os_icon_big']);
rua['os_info_url'] && dotProp.set(ruaJson, 'os.infoUrl', rua['os_info_url']);
rua['os_family'] && dotProp.set(ruaJson, 'os.family.name', rua['os_family']);
rua['os_family_code'] && dotProp.set(ruaJson, 'os.family.code', rua['os_family_code']);
rua['os_family_vendor'] && dotProp.set(ruaJson, 'os.family.vendor.name', rua['os_family_vendor']);
rua['os_family_vendor_code'] && dotProp.set(ruaJson, 'os.family.vendor.code', rua['os_family_vendor_code']);
rua['os_family_vendor_homepage'] && dotProp.set(ruaJson, 'os.family.vendor.homepage', rua['os_family_vendor_homepage']);
} else {
rua['os_code'] && dotProp.set(ruaJson, 'os.code', rua['os_code']);
rua['os_family_code'] && dotProp.set(ruaJson, 'os.family', rua['os_family_code']);
}
break;
}
}
////////////////////////////////////////////////
// client/os relation
////////////////////////////////////////////////
if (os_id == 0 && client_id != 0) {
q = this.db.prepare(
'SELECT os_id,family,family_code,name,name_code,homepage,icon,icon_big,vendor,vendor_code,vendor_homepage ' +
'FROM udger_client_os_relation ' +
'JOIN udger_os_list ON udger_os_list.id=udger_client_os_relation.os_id ' +
'WHERE client_id=?'
);
r = q.get(client_id);
if (r) {
debug('parse useragent string: client os relation found');
os_id = r['os_id'];
rua['os'] = r['name'] || '';
rua['os_code'] = r['name_code'] || '';
rua['os_homepage'] = r['homepage'] || '';
rua['os_icon'] = r['icon'] || '';
rua['os_icon_big'] = r['icon_big'] || '';
rua['os_info_url'] = 'https://udger.com/resources/ua-list/os-detail?os=' + (r['name'] || '');
rua['os_family'] = r['family'] || '';
rua['os_family_code'] = r['family_code'] || '';
rua['os_family_vendor'] = r['vendor'] || '';
rua['os_family_vendor_code'] = r['vendor_code'] || '';
rua['os_family_vendor_homepage'] = r['vendor_homepage'] || '';
rua['os'] && dotProp.set(ruaJson, 'os.name', rua['os']);
rua['os_code'] && dotProp.set(ruaJson, 'os.code', rua['os_code']);
rua['os_homepage'] && dotProp.set(ruaJson, 'os.homepage', rua['os_homepage']);
rua['os_icon'] && dotProp.set(ruaJson, 'os.icon', rua['os_icon']);
rua['os_icon_big'] && dotProp.set(ruaJson, 'os.iconBig', rua['os_icon_big']);
rua['os_info_url'] && dotProp.set(ruaJson, 'os.infoUrl', rua['os_info_url']);
rua['os_family'] && dotProp.set(ruaJson, 'os.family.name', rua['os_family']);
rua['os_family_code'] && dotProp.set(ruaJson, 'os.family.code', rua['os_family_code']);
rua['os_family_vendor'] && dotProp.set(ruaJson, 'os.family.vendor.name', rua['os_family_vendor']);
rua['os_family_vendor_code'] && dotProp.set(ruaJson, 'os.family.vendor.code', rua['os_family_vendor_code']);
rua['os_family_vendor_homepage'] && dotProp.set(ruaJson, 'os.family.vendor.homepage', rua['os_family_vendor_homepage']);
}
}
////////////////////////////////////////////////
// device
////////////////////////////////////////////////
// q = this.db.prepare(
// 'SELECT deviceclass_id,regstring,name,name_code,icon,icon_big ' +
// 'FROM udger_deviceclass_regex ' +
// 'JOIN udger_deviceclass_list ON udger_deviceclass_list.id=udger_deviceclass_regex.deviceclass_id ' +
// 'ORDER BY sequence ASC'
// );
var udger_deviceclass_regex = jsons.udger_deviceclass_regex.data.sort((a,b) => a[2] - b[2]);
for (r of udger_deviceclass_regex) {
let e = ua.match(utils.phpRegexpToJs(r[1]));
if (e) {
let os = jsons.udger_deviceclass_list.data.find(v => r[0] === v[0]);
r = {
...map(jsons.udger_deviceclass_regex, r),
...map(jsons.udger_deviceclass_list, os)
}
debug('parse useragent string: device found by regex');
deviceclass_id = r['deviceclass_id'];
rua['device_class'] = r['name'] || '';
rua['device_class_code'] = r['name_code'] || '';
rua['device_class_icon'] = r['icon'] || '';
rua['device_class_icon_big'] = r['icon_big'] || '';
rua['device_class_info_url'] = 'https://udger.com/resources/ua-list/device-detail?device=' + r['name'];
if (opts.full) {
rua['device_class'] && dotProp.set(ruaJson, 'device.class.name', rua['device_class']);
rua['device_class_code'] && dotProp.set(ruaJson, 'device.class.code', rua['device_class_code']);
rua['device_class_icon'] && dotProp.set(ruaJson, 'device.class.icon', rua['device_class_icon']);
rua['device_class_icon_big'] && dotProp.set(ruaJson, 'device.class.iconBig', rua['device_class_icon_big']);
rua['device_class_info_url'] && dotProp.set(ruaJson, 'device.class.infoUrl', rua['device_class_info_url']);
} else {
rua['device_class_code'] && dotProp.set(ruaJson, 'device.class', rua['device_class_code']);
}
break;
}
}
if (deviceclass_id == 0 && client_class_id != -1) {
// q = this.db.prepare(
// 'SELECT deviceclass_id,name,name_code,icon,icon_big ' +
// 'FROM udger_deviceclass_list ' +
// 'JOIN udger_client_class ON udger_client_class.deviceclass_id=udger_deviceclass_list.id ' +
// 'WHERE udger_client_class.id=?'
// );
// r = q.get(client_class_id);
var client_class = jsons.udger_client_class.data.find(v => v[0] === client_class_id);
if(client_class) {
var deviceclass = jsons.udger_deviceclass_list.data.find(v => v[0] === client_class[3]);
r = {
...map(jsons.udger_client_class, client_class),
...map(jsons.udger_deviceclass_list, deviceclass)
};
debug('parse useragent string: device found by deviceclass');
deviceclass_id = r['deviceclass_id'];
rua['device_class'] = r['name'] || '';
rua['device_class_code'] = r['name_code'] || '';
rua['device_class_icon'] = r['icon'] || '';
rua['device_class_icon_big'] = r['icon_big'] || '';
rua['device_class_info_url'] = 'https://udger.com/resources/ua-list/device-detail?device=' + (r['name'] || '');
if (opts.full) {
rua['device_class'] && dotProp.set(ruaJson, 'device.class.name', rua['device_class']);
rua['device_class_code'] && dotProp.set(ruaJson, 'device.class.code', rua['device_class_code']);
rua['device_class_icon'] && dotProp.set(ruaJson, 'device.class.icon', rua['device_class_icon']);
rua['device_class_icon_big'] && dotProp.set(ruaJson, 'device.class.iconBig', rua['device_class_icon_big']);
rua['device_class_info_url'] && dotProp.set(ruaJson, 'device.class.infoUrl', rua['device_class_info_url']);
} else {
rua['device_class_code'] && dotProp.set(ruaJson, 'device.class', rua['device_class_code']);
}
}
}
////////////////////////////////////////////////
// device marketname
////////////////////////////////////////////////
if (rua['os_family_code']) {
// q = this.db.prepare(
// 'SELECT id,regstring FROM udger_devicename_regex ' +
// 'WHERE (' +
// '(os_family_code=? AND os_code=\'-all-\') OR ' +
// '(os_family_code=? AND os_code=?)' +
// ') ' +
// 'ORDER BY sequence'
// );
var devicename_regex_list = jsons.udger_devicename_regex.data.filter(v => {
let c1 = v[1] === rua['os_family_code'] && v[2] === '-all-';
let c2 = v[1] === rua['os_family_code'] && v[2] === rua['os_code'];
return c1 || c2;
}).sort((a,b) => a[4] - b[4]);
const bindParams = [
rua['os_family_code'],
rua['os_family_code'],
rua['os_code']
];
let match;
let rId;
for (const r of devicename_regex_list) {
e = ua.match(utils.phpRegexpToJs(r[3]));
if (e && e[1]) {
match = e[1].trim();
rId = r[0];
break;
}
}
// const qC = this.db.prepare(
// 'SELECT marketname,brand_code,brand,brand_url,icon,icon_big ' +
// 'FROM udger_devicename_list ' +
// 'JOIN udger_devicename_brand ON udger_devicename_brand.id=udger_devicename_list.brand_id ' +
// 'WHERE regex_id=? AND code=?'
// );
// const rC = qC.get(rId, match);
const qC = jsons.udger_devicename_list.data.find(v => {
return v[0] === rId && v[2] === match;
})
if (qC) {
let devicename_brand = jsons.udger_devicename_brand.data.find(v => v[0] === qC[1]);
const rC = {
...map(jsons.udger_devicename_list, qC),
...map(jsons.udger_devicename_brand, devicename_brand)
}
debug('parse useragent string: device marketname found');
rua['device_marketname'] = rC['marketname'] || '';
rua['device_brand'] = rC['brand'] || '';
rua['device_brand_code'] = rC['brand_code'] || '';
rua['device_brand_homepage'] = rC['brand_url'] || '';
rua['device_brand_icon'] = rC['icon'] || '';
rua['device_brand_icon_big'] = rC['icon_big'] || '';
rua['device_brand_info_url'] = 'https://udger.com/resources/ua-list/devices-brand-detail?brand=' + (rC['brand_code'] || '');
rua['device_marketname'] && dotProp.set(ruaJson, 'device.marketName', rua['device_marketname']);
rua['device_brand'] && dotProp.set(ruaJson, 'device.brand.name', rua['device_brand']);
rua['device_brand_code'] && dotProp.set(ruaJson, 'device.brand.code', rua['device_brand_code']);
rua['device_brand_homepage'] && dotProp.set(ruaJson, 'device.brand.homepage', rua['device_brand_homepage']);
rua['device_brand_icon'] && dotProp.set(ruaJson, 'device.brand.icon', rua['device_brand_icon']);
rua['device_brand_icon_big'] && dotProp.set(ruaJson, 'device.brand.iconBig', rua['device_brand_icon_big']);
rua['device_brand_info_url'] && dotProp.set(ruaJson, 'device.brand.infoUrl', rua['device_brand_info_url']);
}
}
debug('parse useragent string: END, unset useragent string');
return {
udger:rua,
json:ruaJson
};
}
/**
* Parse the IP Address
* @param {String} ip - An IPv4 or IPv6 Address
*/
parseIp(ip, opts) {
const rip = JSON.parse(JSON.stringify(this.retIp));
const ripJson = {};
if (!ip) return {
udger:rip,
json:ripJson
};
let q;
let r;
let ipInt;
let ipa;
debug('parse IP address: START (IP: ' + ip + ')');
rip['ip'] = ip;
dotProp.set(ripJson, 'ip', ip);
const ipver = utils.getIpVersion(ip);
if (ipver === 4 || ipver === 6) {
if (ipver === 6) {
ip = utils.inetNtop(utils.inetPton(ip));
debug('compress IP address is:' + ip);
}
}
rip['ip_ver'] = ipver;
if (opts.full) {
dotProp.set(ripJson, 'version', ipver);
}
q = this.db.prepare(
'SELECT udger_crawler_list.id as botid, ip_last_seen, ip_hostname, ip_country, ip_city, ' +
'ip_country_code, ip_classification, ip_classification_code, name, ver, ver_major, last_seen, '+
'respect_robotstxt, family, family_code, family_homepage, family_icon, vendor, vendor_code, '+
'vendor_homepage, crawler_classification, crawler_classification_code '+
'FROM udger_ip_list '+
'JOIN udger_ip_class ON udger_ip_class.id=udger_ip_list.class_id '+
'LEFT JOIN udger_crawler_list ON udger_crawler_list.id=udger_ip_list.crawler_id '+
'LEFT JOIN udger_crawler_class ON udger_crawler_class.id=udger_crawler_list.class_id '+
'WHERE ip=? ORDER BY sequence'
);
r = q.get(ip);
if (r) {
// UDGER FORMAT
rip['ip_classification'] = r['ip_classification'] || '';
rip['ip_classification_code'] = r['ip_classification_code'] || '';
rip['ip_last_seen'] = r['ip_last_seen'] || '';
rip['ip_hostname'] = r['ip_hostname'] || '';
rip['ip_country'] = r['ip_country'] || '';
rip['ip_country_code'] = r['ip_country_code'] || '';
rip['ip_city'] = r['ip_city'] || '';
rip['crawler_name'] = r['name'] || '';
rip['crawler_ver'] = r['ver'] || '';
rip['crawler_ver_major'] = r['ver_major'] || '';
rip['crawler_family'] = r['family'] || '';
rip['crawler_family_code'] = r['family_code'] || '';
rip['crawler_family_homepage'] = r['family_homepage'] || '';
rip['crawler_family_vendor'] = r['vendor'] || '';
rip['crawler_family_vendor_code'] = r['vendor_code'] || '';
rip['crawler_family_vendor_homepage'] = r['vendor_homepage'] || '';
rip['crawler_family_icon'] = r['family_icon'] || '';
if (r['ip_classification_code'] === 'crawler') {
rip['crawler_family_info_url'] = 'https://udger.com/resources/ua-list/bot-detail?bot=' + (r['family'] || '') + '#id' + (r['botid']|| '');
}
rip['crawler_last_seen'] = r['last_seen'] || '';
rip['crawler_category'] = r['crawler_classification'] || '';
rip['crawler_category_code'] = r['crawler_classification_code'] || '';
rip['crawler_respect_robotstxt'] = r['respect_robotstxt'] || '';
// JSON FORMAT
if (opts.full) {
rip['ip_classification'] && dotProp.set(ripJson, 'classification.name', rip['ip_classification']);
rip['ip_classification_code'] && dotProp.set(ripJson, 'classification.code', rip['ip_classification_code']);
} else {
rip['ip_classification_code'] && dotProp.set(ripJson, 'classification', rip['ip_classification_code']);
}
rip['ip_last_seen'] && dotProp.set(ripJson, 'lastSeen', rip['ip_last_seen']);
rip['ip_hostname'] && dotProp.set(ripJson, 'hostname', rip['ip_hostname']);
rip['ip_country'] && dotProp.set(ripJson, 'geo.country.name', rip['ip_country']);
rip['ip_country_code'] && dotProp.set(ripJson, 'geo.country.code', rip['ip_country_code']);
rip['ip_city'] && dotProp.set(ripJson, 'geo.city', rip['ip_city']);
rip['crawler_name'] && dotProp.set(ripJson, 'crawler.name', rip['crawler_name']);
if (opts.full) {
rip['crawler_ver'] && dotProp.set(ripJson, 'crawler.version.current', rip['crawler_ver']);
rip['crawler_ver_major'] && dotProp.set(ripJson, 'crawler.version.major', rip['crawler_ver_major']);
rip['crawler_family'] && dotProp.set(ripJson, 'crawler.family.name', rip['crawler_family']);
rip['crawler_family_code'] && dotProp.set(ripJson, 'crawler.family.code', rip['crawler_family_code']);
rip['crawler_family_homepage'] && dotProp.set(ripJson, 'crawler.family.homepage', rip['crawler_family_homepage']);
rip['crawler_family_vendor'] && dotProp.set(ripJson, 'crawler.family.vendor.name', rip['crawler_family_vendor']);
rip['crawler_family_vendor_code'] && dotProp.set(ripJson, 'crawler.family.vendor.code', rip['crawler_family_vendor_code']);
rip['crawler_family_vendor_homepage'] && dotProp.set(ripJson, 'crawler.family.vendor.homepage', rip['crawler_family_vendor_homepage']);
rip['crawler_family_icon'] && dotProp.set(ripJson, 'crawler.family.icon', rip['crawler_family_icon']);
if (r['ip_classification_code'] === 'crawler') {
rip['crawler_family_info_url'] && dotProp.set(ripJson, 'crawler.family.infoUrl', rip['crawler_family_info_url']);
}
rip['crawler_last_seen'] && dotProp.set(ripJson, 'crawler.lastSeen', rip['crawler_last_seen']);
rip['crawler_category'] && dotProp.set(ripJson, 'crawler.category.name', rip['crawler_category']);
rip['crawler_category_code'] && dotProp.set(ripJson, 'crawler.category.code', rip['crawler_category_code']);
rip['crawler_respect_robotstxt'] && dotProp.set(ripJson, 'crawler.respectRobotsTxt', rip['crawler_category_code']);
} else {
rip['crawler_family_code'] && dotProp.set(ripJson, 'crawler.family', rip['crawler_family_code']);
rip['crawler_category_code'] && dotProp.set(ripJson, 'crawler.category', rip['crawler_category_code']);
rip['crawler_last_seen'] && dotProp.set(ripJson, 'crawler.lastSeen', rip['crawler_last_seen']);
}
} else {
rip['ip_classification'] = 'Unrecognized';
rip['ip_classification_code'] = 'unrecognized';
if (opts.full) {
dotProp.set(ripJson, 'classification.name', rip['ip_classification']);
dotProp.set(ripJson, 'classification.code', rip['ip_classification_code']);
} else {
dotProp.set(ripJson, 'classification', rip['ip_classification_code']);
}
}
if (ipver === 4) {
ipInt = utils.ip2long(ip);
q = this.db.prepare(
'SELECT name, name_code, homepage '+
'FROM udger_datacenter_range '+
'JOIN udger_datacenter_list ON udger_datacenter_range.datacenter_id=udger_datacenter_list.id '+
'WHERE iplong_from <=? AND iplong_to >=?'
);
r = q.get(ipInt, ipInt);
if (r) {
rip['datacenter_name'] = r['name'] || '';
rip['datacenter_name_code'] = r['name_code'] || '';
rip['datacenter_homepage'] = r['homepage'] || '';
if (opts.full) {
rip['datacenter_name'] && dotProp.set(ripJson, 'datacenter.name', rip['datacenter_name']);
rip['datacenter_name_code'] && dotProp.set(ripJson, 'datacenter.code', rip['datacenter_name_code']);
rip['datacenter_homepage'] && dotProp.set(ripJson, 'datacenter.homepage', rip['datacenter_homepage']);
} else {
rip['datacenter_name_code'] && dotProp.set(ripJson, 'datacenter', rip['datacenter_name_code']);
}
}
} else if (ipver === 6) {
ipa = new Address6(ip);
const t = ipa.canonicalForm().split(':');
const ipInts = {};
t.forEach((h, i) => {
ipInts['ipInt'+i] = parseInt(h, 16);
});
q = this.db.prepare(
'SELECT name, name_code, homepage '+
'FROM udger_datacenter_range6 '+
'JOIN udger_datacenter_list ON udger_datacenter_range6.datacenter_id=udger_datacenter_list.id '+
'WHERE '+
'iplong_from0 <= @ipInt0 AND iplong_to0 >= @ipInt0 AND '+
'iplong_from1 <= @ipInt1 AND iplong_to1 >= @ipInt1 AND '+
'iplong_from2 <= @ipInt2 AND iplong_to2 >= @ipInt2 AND '+
'iplong_from3 <= @ipInt3 AND iplong_to3 >= @ipInt3 AND '+
'iplong_from4 <= @ipInt4 AND iplong_to4 >= @ipInt4 AND '+
'iplong_from5 <= @ipInt5 AND iplong_to5 >= @ipInt5 AND '+
'iplong_from6 <= @ipInt6 AND iplong_to6 >= @ipInt6 AND '+
'iplong_from7 <= @ipInt7 AND iplong_to7 >= @ipInt7'
);
r = q.get(ipInts);
if (r) {
rip['datacenter_name'] = r['name'] || '';
rip['datacenter_name_code'] = r['name_code'] || '';
rip['datacenter_homepage'] = r['homepage'] || '';
if (opts.full) {
rip['datacenter_name'] && dotProp.set(ripJson, 'datacenter.name', rip['datacenter_name']);
rip['datacenter_name_code'] && dotProp.set(ripJson, 'datacenter.code', rip['datacenter_name_code']);
rip['datacenter_homepage'] && dotProp.set(ripJson, 'datacenter.homepage', rip['datacenter_homepage']);
} else {
rip['datacenter_name_code'] && dotProp.set(ripJson, 'datacenter', rip['datacenter_name_code']);
}
}
}
debug('parse IP address: END');
return {
udger:rip,
json:ripJson
};
}
/**
* Main parser
* @return {Object} Parsing result
*/
parse(opts) {
if (!this.db) return {};
if (
this.isCacheEnable() &&
this.cacheKeyExist(this.keyCache)
) {
return this.cacheRead(this.keyCache, opts);
}
const ret = {};
if (!opts) opts = {};
if (opts.json) {
if (this.ua) ret.userAgent =this.parseUa(this.ua, opts).json;
if (this.ip) ret.ipAddress = this.parseIp(this.ip, opts).json;
if (opts.full) ret.fromCache = false;
} else {
ret['user_agent'] = this.parseUa(this.ua, opts).udger;
ret['ip_address'] = this.parseIp(this.ip, opts).udger;
ret['from_cache'] = false;
}
if (this.isCacheEnable()) {
this.cacheWrite(this.keyCache, ret);
}
return ret;
}
randomSanityChecks(max, callback) {
if (!this.db) {
callback(new Error('Database not ready'));
return false;
}
if (!max) {
callback(new Error('Please specify maximum number of records'));
return false;
}
if (typeof max!= 'number') {
callback(new Error('Maximum number of records is not a number'));
return false;
}
return true;
}
randomUACrawlers(max, callback) {
if (!this.randomSanityChecks(max, callback)) return;
const q = this.db.prepare(
'SELECT ua_string FROM udger_crawler_list ORDER BY RANDOM() LIMIT ?'
);
callback(null, q.all(max));
return;
}
randomUAClientsRegex(max, callback) {
if (!this.randomSanityChecks(max, callback)) return;
const q = this.db.prepare(
'SELECT regstring FROM udger_client_regex ORDER BY RANDOM() LIMIT ?'
);
callback(null, q.all(max));
return;
}
randomUAClients(max, callback) {
if (!this.randomSanityChecks(max, callback)) return;
this.randomUAClientsRegex(max, (err, results) => {
let regex;
let regexClean;
let randomUA;
let re;
let reClean;
for (let i = 0, len=results.length; i<len; i++) {
regex = new RegExp(results[i].regstring);
regexClean = results[i].regstring.replace(/^\//, '');
regexClean = regexClean.replace(/\/si$/, '');
reClean = new RegExp(regexClean);
re = new RandExp(reClean);