forked from homerquan/node-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-analytics.js
1012 lines (796 loc) · 25.1 KB
/
node-analytics.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
/*
* node-analytics
*/
'use strict';
// defaults
const fs = require('fs'),
path = require('path'),
http = require('http'),
crypto = require('crypto');
// installed
const get_ip = require('ipware')().get_ip,
mongoose = require('mongoose'),
useragent = require('useragent'),
maxmind = require('maxmind'),
cookie = require('cookie'),
async = require('async'),
s_io = require('socket.io'),
chalk = require('chalk'),
CryptoJS = require('crypto-js'),
onHeaders = require('on-headers'),
onFinished = require('on-finished');
// globals
let db,
geo_lookup,
io = false;
let log = require('andrao-logger')('n-a');
// -------------------------------------------------------------
const Request_Schema = mongoose.Schema({
_id: { type: String, unique: true, index: true },
host: String,
date: { type: Date, default: Date.now },
url: { type: String, index: true },
query: [{ field: String, value: String }],
ref: { type: String, index: true },
referrer: String,
method: String,
time: Number,
reaches: [String],
pauses: [{
_id: false,
id: String,
time: Number
}],
clicks: [String]
});
const Session_Schema = mongoose.Schema({
user: { type: String, index: true },
name: { type: String, index: true },
date: { type: Date, default: Date.now },
last: { type: Date, default: Date.now },
ip: String,
is_bot: { type: Boolean, default: true },
geo: {
_id: false,
city: { type: String, index: true },
state: { type: String, index: true },
country: { type: String, index: true },
continent: { type: String, index: true },
time_zone: { type: String, index: true }
},
system: {
os: {
_id: false,
name: String,
version: String
},
browser: {
_id: false,
name: String,
version: String
}
},
time: Number,
resolution: {
_id: false,
width: Number,
height: Number
},
reqs: [Request_Schema],
state: String,
flash_data: mongoose.Schema.Types.Mixed
});
const Session = mongoose.model('Session', Session_Schema);
module.exports = analytics;
module.exports.sessions = sessions;
const opts = {
db_host: '127.0.0.1',
db_port: 27017,
db_name: 'node_analytics_db',
ws_port: 8080,
ws_server: false,
s_io: false,
geo_ip: true,
mmdb: 'GeoLite2-City.mmdb',
log: true,
log_all: false,
error_log: true,
secure: true,
secret: 'changeMe',
log_opts: {
pre: 'n-a'
},
mongoose_params: {
server: {
auto_reconnect: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 3000,
keepAlive: 120,
connectTimeoutMS: 30000,
socketOptions: {
keepAlive: 120,
connectTimeoutMS: 30000,
reconnectTries: 500,
reconnectInterval: 3000,
},
poolSize: 50
},
replset: {
keepAlive: 1,
connectTimeoutMS: 30000,
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000
}
}
}
};
log("active: wait for MongoDB, GeoIP, & WebSocket");
log("don't forget to copy", chalk.red('node-analytics-client.js'), "to public directory");
function mongoDB(cb){
// Connect to MongoDB
const db_url = 'mongodb://' + opts.db_host + ':' + opts.db_port + '/' + opts.db_name;
db = mongoose.connection;
const db_connect = setTimeout(() => {
log(chalk.cyan('mongoose.connect'));
mongoose.connect(db_url, opts.mongoose_params)
}, 500);
db.on('connecting', () => {
log(chalk.yellow('MongoDB connecting'));
});
db.on('error', (err) => {
log.error(chalk.red('MongoDB error'), err);
});
db.on('connected', () => {
log(chalk.yellow('MongoDB connected:'), 'Wait for open.');
if(db_connect)
clearTimeout(db_connect);
});
db.once('open', () => {
log(chalk.green('MongoDB connection open'));
cb(null);
});
db.on('reconnected', () => {
log(chalk.green('MongoDB reconnected.'));
});
db.on('disconnected', () => {
log.error(chalk.red('MongoDB disconnected!'), 'Attempting reconnect.');
});
}
function geoDB(cb){
// Check for mmdb
if(opts.geo_ip){
maxmind.open(opts.mmdb, (err, mmdb) => {
if(err){
log.error('GeoIP DB open error', opts.mmdb, err);
return cb(true)
}
geo_lookup = mmdb;
log('GeoIP DB loaded successfully');
cb(null);
});
}
else {
log('GeoIP disabled');
cb(null);
}
}
function socketInit(cb){
io = opts.s_io ? opts.s_io : opts.ws_server ? s_io.listen(opts.ws_server) : s_io(opts.ws_port);
io.of('/node-analytics')
.use(function(socket, next){
if(socket.handshake.headers.cookie){
const cookies = getCookies(socket.handshake.headers.cookie);
if(cookies && cookies.na_session)
next();
}
else
log.error('Socket authentication error; no session cookie');
})
.on('connection', socketConnection);
log('Websocket server established');
cb(null);
}
// =====================
function socketConnection(socket){
const cookies = getCookies(socket.handshake.headers.cookie);
socket.session_start = Date.now();
socket.blurred = 0;
socket.blurring = Date.now();
socket.req_id = cookies.na_req;
socket.session_id = cookies.na_session;
// Get session
if(socket.session_id){
Session.findById(socket.session_id, function(err, session){
if(err)
return log.error('Session find error :: id[socket]', this.session_id, err);
if(!session)
return log.error('Session not found :: id[socket]', this.session_id);
const socket = this;
// set regional session and request
socket.session = session;
if(socket.req_id){
for(let i = session.reqs.length - 1; i >= 0; i--){
if(session.reqs[i]._id.toString() == socket.req_id){
socket.req = session.reqs[i];
break;
}
}
}
// log and initiate socket sensitivity
if(!socket.req)
log.error('socket connected; request not found');
else if(opts.log_all)
log.session(session, 'socket connected; request:', socket.req._id);
socketResponse(socket);
}.bind(socket));
}
// =============
function socketResponse(socket){
// session updates from the client
// Trivial not-bot check: socket connects;
// Could / should be improved to having done action on page
if(socket.session.is_bot)
Update.session(socket.session, { $set: { is_bot: false }});
if(!socket.session.resolution)
socket.on('resolution', _socket.resolution.bind(socket));
// request updates
socket.on('click', _socket.click.bind(socket));
socket.on('reach', _socket.reach.bind(socket));
socket.on('pause', _socket.pause.bind(socket));
// session timer
socket.on('blur', _socket.blur.bind(socket));
socket.on('focus', _socket.focus.bind(socket));
// Disconnection
socket.on('disconnect', _socket.disconnect.bind(socket));
}
}
const _socket = {
click: function(id){
if(this.req)
Update.request(this, { $push: { clicks : id }});
if(opts.log)
log.session(this.session, chalk.green('click'), '@', chalk.cyan(id))
},
reach: function(id){
if(this.req)
Update.request(this, { $push: { reaches: id }});
if(opts.log)
log.session(this.session, chalk.yellow('reach'), '@', chalk.cyan(id))
},
pause: function(params){
if(this.req)
Update.request(this, { $push: { pauses: params }});
if(opts.log)
log.session(this.session, chalk.magenta('pause'), `for ${params.time}s @`, chalk.cyan(params.id));
},
blur: function(){
this.blurring = Date.now();
},
focus: function(){
this.blurred += Date.now() - this.blurring;
},
resolution: function(params){
Update.session(this.session, { $set: { resolution: params }});
},
disconnect: function(){
if(!this || !this.req)
return;
// request time, sans blurred time
const t = (Date.now() - this.session_start - this.blurred) / 1000;
// total session time; begin with this request
let session_t = t;
for(let i = 0; i < this.session.reqs.length; i++)
session_t += this.session.reqs[i].time;
// update request & session
this.req.time = t;
if(this.req)
Update.request(this, { $set: { time: t }});
Update.session(this.session, { $set: { session_time: session_t }});
if(opts.log)
log.session(this.session, chalk.red(t));
}
};
// ===============
const session_fields = '_id user name date last flash_data';
function analytics(opts_in){
for(let k in opts_in)
opts[k] = opts_in[k];
if(!opts.log_opts.pre)
opts.log_opts.pre = 'n-a';
log = require('andrao-logger')(opts.log_opts);
async.parallelLimit([
mongoDB,
socketInit,
geoDB,
], 2, (err) => {
if(err)
return log.error('start-up interrupted');
log(chalk.green('NODE ANALYTICS READY'));
});
// HTTP request:
return function(req, res, next){
// Skip cases
if(req.url.indexOf('/socket.io/?EIO=') == 0)
return next();
async.waterfall([
(cb) => {
getSession(req, res, cb);
},
setCookies,
sessionData,
newRequest,
logRequest,
sessionSave,
sessionFlash
], (err, session) => {
if(err){
log.error(err);
next();
return false;
}
req.node_analytics = session;
next();
});
}
}
// =====================
// populate var session; returns boolean on whether newly formed
function getSession(req, res, cb){
const now = new Date();
const cookies = getCookies(req.headers.cookie);
// cookies.na_session :: session._id
// cookies.na_user :: session.user
// Establish session: new/old session? new/old user?
if(cookies.na_session){
if(opts.log_all)
log('Session cookie found:', cookies.na_session);
Session.findById(cookies.na_session, session_fields).lean().exec(function(err, session){
if(err){
log.error('getSession error', err);
return cb(err);
}
if(!session){
log.error('Session not found :: id[cookie]:', this.cookies.na_session);
// send to check if user instead
if(cookies.na_user)
userSession();
else
newSession();
}
else {
Update.session(session, {
$set: {
last: Date.now()
}
}, (err, session) => {
if(err){
log.error('establish session / update error');
return cb(true);
}
session.continued = true;
cb(err, this.req, this.res, session)
});
}
}.bind({
cookies: cookies,
req: req,
res: res
}))
}
else if(cookies.na_user)
userSession();
else
newSession();
// ====================
function userSession(){
// OLD USER, NEW SESSION
cb(null, req, res, new Session({
user: cookies.na_user,
new_session: true
}).toObject({ virtuals: true }));
if(opts.log_all)
log.timer('getSession 1', now);
}
function newSession(){
// NEW USER, NEW SESSION
// Initiate session to get _id
let session = new Session();
session.user = session._id.toString();
session.new_user = true;
session = session.toObject({ virtuals: true });
cb(null, req, res, session);
if(opts.log_all)
log.timer('getSession 2', now);
}
}
// set cookies
function setCookies(req, res, session, cb){
const now = new Date();
// Set cookies
res.cookie('na_session', AES.encrypt(session._id.toString()), {
maxAge: 1000 * 60 * 15, // 15 mins
httpOnly: true,
secure: opts.secure
});
res.cookie('na_user', AES.encrypt(session.user), {
maxAge: 1000 * 60 * 60 * 24 * 365, // 1 year
httpOnly: true,
secure: opts.secure
});
cb(null, req, res, session);
if(opts.log_all)
log.timer('setCookies', now);
}
// append session data
function sessionData(req, res, session, cb){
const now = new Date();
if(session.continued)
return cb(null, req, res, session);
async.parallelLimit([
getIp,
getLocation,
getSystem
], 2,
function(err){
cb(err, this.req, this.res, this.session);
if(opts.log_all)
log.timer('sessionData', now);
}.bind({
req: req,
res: res,
session: session
})
);
// ======================
// .ip
function getIp(cb){
session.ip = get_ip(req).clientIp;
cb(null)
}
// .geo :: .city, .state, .country
function getLocation(cb){
if(!geo_lookup)
return cb(null);
const loc = geo_lookup.get(session.ip);
if(!session.geo)
session.geo = {};
if(loc){
try {
if(loc.city)
session.geo.city = loc.city.names.en;
if(loc.subdivisions)
session.geo.state = loc.subdivisions[0].iso_code;
if(loc.country)
session.geo.country = loc.country.iso_code;
if(loc.continent)
session.geo.continent = loc.continent.code;
if(loc.location)
session.geo.time_zone = loc.location.time_zone;
}
catch(e){
log.error('geoIP error:', e);
}
}
cb(null)
}
// .system :: .os{, .broswer{ .name, .version
function getSystem(cb){
var agent = useragent.parse(req.headers['user-agent']);
var os = agent.os;
if(!session.system)
session.system = {};
if(!session.system.browser)
session.system.browser = {};
if(!session.system.os)
session.system.os = {};
session.system.browser.name = agent.family;
session.system.browser.version = agent.major + '.' + agent.minor + '.' + agent.patch;
session.system.os.name = os.family;
session.system.os.version = os.major + '.' + os.minor + '.' + os.patch;
cb(null)
}
}
// return new request document, create req cookie
function newRequest(req, res, session, cb){
const now = new Date();
const request = {
_id: `r${crypto.randomBytes(16).toString('hex')}${Date.now()}`,
host: req.hostname,
url: req.url,
method: req.method,
referrer: req.get('Referrer') || req.get('Referer')
};
// populate request query
for(let field in req.query){
if(field === 'ref')
request.ref = req.query[field];
else {
if(!request.query)
request.query = [];
request.query.push({
field: field,
value: req.query[field]
})
}
}
// add request cookie for communication/association with socket
res.cookie('na_req', AES.encrypt(request._id), {
maxAge: 1000 * 60 * 15, // 15 mins
httpOnly: true,
secure: opts.secure
});
// return request object: will be added at sessionSave();
cb(null, req, res, session, request);
if(opts.log_all)
log.timer('newRequest', now);
}
// log request
function logRequest(req, res, session, request, cb){
const now = new Date();
if(opts.log){
onHeaders(res, log_start.bind(res));
onFinished(res, req_log.bind({
req: request,
ses: session
}));
}
cb(null, session, request);
if(opts.log_all)
log.timer('logRequest', now);
/// ==============
function log_start(){
this._log_start = process.hrtime();
}
function req_log(){
const request = this.req;
const session = this.ses;
// Status colour
const sc = res.statusCode < 400 ? 'green' : 'red';
// Res time
const ms = nano_time(res._log_start);
// Referrer
let ref = request.referrer;
if(ref){
ref = ref.replace('http://', '');
ref = ref.replace('https://', '');
}
// Args
const args = [session, '|', chalk.magenta(request.url), '|', request.method, chalk[sc](res.statusCode), `: ${ms} ms`];
if(session && session.system){
args.push('|');
if(session.system.browser){
args.push(chalk.grey(session.system.browser.name));
args.push(chalk.grey(session.system.browser.version));
}
if(session.system.os){
args.push(chalk.grey(session.system.os.name));
args.push(chalk.grey(session.system.os.version));
}
}
if(ref)
args.push('|', chalk.grey(`from ${ref}`));
// Apply
log.session.apply(log, args);
// ===
function nano_time(start){
let t = conv(process.hrtime()) - conv(start); // ns
t = Math.round(t / 1000); // µs
return t / 1000; // ms [3 dec]
// ====
function conv(t){
if(!t || typeof t[0] === 'undefined')
return 0;
return t[0] * 1e9 + t[1];
}
}
}
}
// save / update session to DB & proceed to socket
function sessionSave(session, request, cb){
const now = new Date();
if(!session.continued){
session.reqs = [request];
Update.session(
session,
{ $set: session },
(err, session) => {
if(err)
return cb('db session save error');
if(opts.log_all)
log.session(session, 'session active [ new ]');
cb(null, session);
if(opts.log_all)
log.timer('sessionSave 1', now);
})
}
else {
// an old session: all that needs be updated is request
Update.session(
session,
{ $push: { reqs: request } },
(err, session) => {
if(err){
log.error('db session update error');
return cb(true);
}
if(opts.log_all)
log.session(session, 'session active [ updated ]');
cb(null, session);
if(opts.log_all)
log.timer('sessionSave 2', now);
});
}
}
function sessionFlash(session, cb){
const now = new Date();
// SESSION OBJECT FUNCTIONS
session.identify = Identify.bind(session);
session.flash = Flash.bind(session);
session.save = Update.session_save.bind(session);
// Expire and clear flash data
for(let k in session.flash_data){
if(session.flash_data[k].endurance !== 'indefinite' && (!session.flash_data[k].endurance || !(session.flash_data[k].endurance - 1)))
delete session.flash_data[k];
else if(session.flash_data[k].endurance !== 'indefinite')
session.flash_data[k].endurance--;
}
Update.session(session, {
$set: {
flash_data: session.flash_data
}
}, function(err){
if(err)
log.error('sessionFlash update error', err);
cb(null, this);
if(opts.log_all)
log.timer('sessionFlash', now);
}.bind(session));
}
// =====================
function Identify(name){
Update.session(this, { $set: { name: name }}, (err) => {
if(err)
log.error('session.associate: name save error', err);
});
}
function Flash(field, value, endurance, cb){
// Return saved field value
if(typeof value === 'undefined'){
if(!this.flash_data || !this.flash_data[field])
return null;
return this.flash_data[field].val;
}
// Save new field value for next session
else {
// Endurance represents number of page loads that this variable will last for
// (Helpful in redirect situations)
if(typeof endurance === 'function'){
cb = endurance;
endurance = 1;
}
else if(!endurance)
endurance = 1;
if(!this.flash_data)
this.flash_data = {};
this.flash_data[field] = {
val: value,
endurance: endurance
};
Update.session(this, { $set: { flash_data: this.flash_data }}, (err, doc) => {
if(err)
log.error('flash data save error', err);
if(cb)
cb(err);
});
}
}
// =====================
const Update = {
session_save: function(cb){
const session = this;
Update.session(session, {
$set: session
}, cb);
},
session: function(session, upd, cb){
const keys = Update._keys(upd);
Session.findByIdAndUpdate(
session._id,
upd,
{
new: true,
fields: session_fields,
upsert: true
}
).lean().exec(function(err, doc){
if(err)
log.error('Update.session error [', this, ']', err);
else if(!doc)
log.error('Update.session no session found!', session);
else if(doc && opts.log_all)
log.session(doc, 'Update.session success [', this, ']');
if(cb)
cb(err, doc);
}.bind(keys))
},
request: function(socket, upd_in, cb){
let upd = {};
for(let k in upd_in){
// $push: { clicks: id }
// -->
// $push: { reqs.$.clicks: id }
if(!upd[k])
upd[k] = {};
for(let k2 in upd_in[k]){
let req_key = 'reqs.$.' + k2;
upd[k][req_key] = upd_in[k][k2];
}
}
let keys = Update._keys(upd);
Session.update({
_id: socket.session._id,
"reqs._id": socket.req._id
}, upd, function(err, raw){
const socket = this.socket;
if(err)
log.error('Update.request error [', this.keys, ']', socket.session._id, socket.req._id, err);
else if(raw.n < 1)
log.error('Update.request not found!', socket.session._id, socket.req._id);
else if(opts.log_all)
log.session(socket.session, 'Update.request success [', this.keys, ']');
if(cb)
return cb(err);
}.bind({
socket: socket,
keys: keys
}))
},
_keys: function(params){
let keys = [];
for(let k in params)
for(let k2 in params[k])
keys.push(k2);
return keys;
}
};
// =====================
function getCookies(src){
let cookies = cookie.parse(src || '');
for(let k in cookies){
if(k.indexOf('na_') === 0){
try {
cookies[k] = AES.decrypt(cookies[k]);
}
catch(err){
log.error('getCookies error', err);
delete cookies[k];
}
}
}
return cookies;
}
const AES = {
encrypt: function(value){
return CryptoJS.AES.encrypt(value, opts.secret).toString();
},
decrypt: function(encrypted){
return CryptoJS.AES.decrypt(encrypted, opts.secret).toString(CryptoJS.enc.Utf8);
}
};
// =====================
function sessions(options, cb){
if(!cb){
cb = options;
options = { is_bot: false };
}