-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy path_session.js
More file actions
executable file
·1242 lines (987 loc) · 42.4 KB
/
Copy path_session.js
File metadata and controls
executable file
·1242 lines (987 loc) · 42.4 KB
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
/**
* Copyright: The PastVu contributors.
* GNU Affero General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/agpl.txt)
*/
import ms from 'ms';
import _ from 'lodash';
import log4js from 'log4js';
import locale from 'locale';
import config from '../config';
import Utils from '../commons/Utils';
import * as regionController from './region';
import { parse as parseCookie } from 'cookie';
import { userSettingsDef, clientParams } from './settings';
import { Session, SessionArchive } from '../models/Sessions';
import { User } from '../models/User';
import { Photo } from '../models/Photo';
import { Comment, CommentN } from '../models/Comment';
import constantsError from '../app/errors/constants';
import { AuthorizationError, ApplicationError, BadParamsError, NotFoundError/*, TimeoutError*/ } from '../app/errors';
const logger = log4js.getLogger('session');
const SESSION_COOKIE_KEY = 'past.sid'; // Session key in client cookies
const SESSION_USER_LIFE = ms('21d'); // Lifetime of a registered user session
const SESSION_ANON_LIFE = ms('7d'); // Lifetime of an anonymous user session
const loopbackIPs = new Set(['127.0.0.1', '::ffff:127.0.0.1', '::1']);
// Locales Map for checking their presence after header parsing
const localesMap = new Map(config.locales.map(locale => [locale, locale]));
// Default locale is the first one from config
const localeDefault = config.locales[0];
// Method for parsing and checking user-gent
export const checkUserAgent = Utils.checkUserAgent(config.browsers);
// Create cookie session object
export const createSidCookieObj = (function () {
const key = SESSION_COOKIE_KEY;
const domain = config.client.hostname;
const cookieUserMaxAge = SESSION_USER_LIFE / 1000;
const cookieAnonMaxAge = SESSION_ANON_LIFE / 1000;
return (session, registered) => ({
key, domain, path: '/', value: session.key, 'max-age': registered ? cookieUserMaxAge : cookieAnonMaxAge,
});
}());
// Create cookie lang object (temporary)
const createLangCookieObj = (function () {
const key = 'past_lang';
const domain = config.client.hostname;
const cookieUserMaxAge = SESSION_USER_LIFE / 1000;
const cookieAnonMaxAge = SESSION_ANON_LIFE / 1000;
return (lang, registered) => ({ key, domain, path: '/', value: lang, 'max-age': registered ? cookieUserMaxAge : cookieAnonMaxAge });
}());
const getBrowserAgent = function (browser) {
const agent = {
n: browser.agent.family, // Agent name e.g. 'Chrome'
v: browser.agent.toVersion(), // Agent version string e.g. '15.0.874'
};
const device = browser.agent.device.toString(); // Device e.g 'Asus A100'
const os = browser.agent.os.toString(); // Operation system e.g. 'Mac OSX 10.10.1'
if (os) {
agent.os = os;
}
if (device && device !== 'Other') {
agent.d = device;
}
return agent;
};
// Determine user locale that we support based on 'accept-language' header
export const identifyUserLocale = (function () {
// Locales for comparison
const localesSuported = new locale.Locales(config.locales, localeDefault);
return function (acceptLanguage) {
if (_.isEmpty(acceptLanguage)) {
return localeDefault; // If parameter is not specified, return default locale
}
// Find the most suitable locale for agent
const suggestedLocale = new locale.Locales(acceptLanguage).best(localesSuported);
return localesMap.get(suggestedLocale.normalized) || localesMap.get(suggestedLocale.language) || localeDefault;
};
}());
export const getPlainUser = (function () {
const userToPublicObject = function (doc, ret/* , options */) {
// Transforms are applied to the document and each of its sub-documents.
// Check that it's exactly user
if (doc.login !== undefined) {
delete ret.pass;
delete ret.activatedate;
delete ret.loginAttempts;
delete ret.active;
delete ret.__v;
}
delete ret._id;
};
return user => user && user.toObject ? user.toObject({ transform: userToPublicObject }) : null;
}());
// usObjs by session key. Hash of users object by session keys
// Several sessions can have one user object, if client loggedin through several devices
export const usSid = new Map();
// usObjs loggedin by user login. Hash of users object by registered users logins
export const usLogin = new Map();
// usObjs loggedin by user _id. Hash of users object by registered users _ids
export const usId = new Map();
export const sessConnected = new Map(); // Map of all the active sessions (by key) with established websocket connection
export const sessWaitingConnect = new Map(); // Map of the sessions (by key), which are waiting for websocket connection
export const sessWaitingSelect = new Map(); // Map of the sessions, which are waiting to be selected from the db
class UsObj {
constructor(user, registered = false) {
this.user = user;
this.registered = registered;
this.sessions = Object.create(null);
this.rquery = Object.create(null);
this.rshortsel = Object.create(null);
this.rshortlvls = [];
this.photoFilterTypes = [];
this.photoFilterQuery = Object.create(null);
}
get isOwner() {
return this.registered && this.user.role > 10;
}
get isAdmin() {
return this.registered && this.user.role > 9;
}
get isModerator() {
return this.registered && this.user.role === 5;
}
}
// Emit data to specified socket
function emitSocket({ socket, data, waitResponse = false, timeout = 10000 }) {
if (!Array.isArray(data)) {
data = [data];
}
if (waitResponse) {
return new Promise((resolve, reject) => {
let overdue = false;
const overdueTimeout = setTimeout(() => {
overdue = true;
resolve({ data: [] });
// Replace with next when all clients are updated
// reject(new TimeoutError({ timeout, data }));
}, timeout);
socket.emit(...data, result => {
if (overdue) {
return;
}
clearTimeout(overdueTimeout);
if (_.get(result, 'error')) {
reject(result.error);
} else {
resolve(result);
}
});
});
}
socket.emit(...data);
}
// Send command to all session's sockets
const emitSessionSockets = (session, data, waitResponse, excludeSocket) => _.chain(session.sockets)
.filter(socket => socket && socket !== excludeSocket && _.isFunction(socket.emit))
.map(socket => emitSocket({ socket, data, waitResponse })).value();
const emitSidCookie = (socket, waitResponse, regitered) => emitSocket({
socket,
data: ['command', [{ name: 'updateCookie', data: createSidCookieObj(socket.handshake.session, regitered) }]],
waitResponse,
});
const emitLangCookie = (socket, lang, waitResponse, registered) => emitSocket({
socket,
data: ['command', [{ name: 'updateCookie', data: createLangCookieObj(lang, registered) }]],
waitResponse,
});
const sendReload = (session, waitResponse, excludeSocket) =>
emitSessionSockets(session, ['command', [{ name: 'location' }]], waitResponse, excludeSocket);
// Send user to all his sockets
export async function emitUser({ usObj, login, userId, sessId, wait, excludeSocket }) {
if (!usObj) {
usObj = getOnline({ login, userId, sessionKey: sessId });
}
if (!usObj) {
return 0;
}
const params = ['youAre', { user: getPlainUser(usObj.user), registered: usObj.registered }];
const emits = _.reduce(usObj.sessions,
(result, session) => result.concat(emitSessionSockets(session, params, wait, excludeSocket)), []
);
if (wait && emits.length) {
await Promise.all(emits);
}
return emits.length;
}
// Save and send user to his sockets
export async function saveEmitUser({ usObj, login, userId, sessId, wait, excludeSocket }) {
if (!usObj) {
usObj = getOnline({ login, userId, sessionKey: sessId });
}
if (!usObj || !usObj.user) {
return 0;
}
await usObj.user.save();
return emitUser({ usObj, wait, excludeSocket });
}
// Create usObj in hashes (if doesn't exists) and add session to it
async function addSessionToUserObject(session, updateRid) {
const registered = Boolean(session.user);
let user = registered ? session.user : session.anonym;
// For registered users we must get user through logins hash, so existing usObj will be selected,
// if user has already loggedin through another device
let usObj = registered ? usLogin.get(user.login) : usSid.get(session.key);
let firstAdding = false;
if (usObj === undefined) {
firstAdding = true;
usObj = new UsObj(user, registered);
usSid.set(session.key, usObj);
if (registered) {
usId.set(user._id.toString(), usObj);
usLogin.set(user.login, usObj);
logger.info(`${this.ridMark} Create us hash: ${user.login}`);
}
} else if (registered) {
// If user is already in hashes, he is logged in through another device
// Insert into usSid by key of current session existing usObj and assign existing user to current session
usSid.set(session.key, usObj);
user = session.user = usObj.user;
logger.info(`${this.ridMark} Add new session to us hash: ${user.login}`);
} else {
logger.warn(`${this.ridMark} Anonym trying to add new session?! Key: ${session.key}`);
}
if (updateRid) {
this.addUserIdToRidMark(usObj, session);
}
usObj.sessions[session.key] = session; // Add session to sessions hash of usObj
if (firstAdding) {
await userObjectTreatUser(usObj);
}
return usObj;
}
function userObjectTreatUser(usObj) {
const user = usObj.user;
// Assign to user default settings
user.settings = _.defaults(user.settings || {}, userSettingsDef);
if (usObj.registered && user.settings.photo_filter_type.length &&
!_.isEqual(user.settings.photo_filter_type, userSettingsDef.photo_filter_type)) {
const types = user.settings.photo_filter_type;
usObj.photoFilterTypes = types;
usObj.photoFilterQuery = { type: types.length === 1 ? types[0] : { $in: types } };
} else if (usObj.photoFilterTypes.length) {
usObj.photoFilterTypes = [];
usObj.photoFilterQuery = {};
}
return popUserRegions(usObj);
}
// Create and save session to db
async function createSession(ip, headers, browser) {
const session = new Session({
key: Utils.randomString(12),
stamp: new Date(),
data: {
ip,
headers,
lang: config.lang,
agent: getBrowserAgent(browser),
},
anonym: {
regionHome: regionController.DEFAULT_HOME._id,
regions: [],
},
});
return session.save();
}
// Update session in db, if it was select from db at entrance
async function updateSession(session, ip, headers, browser) {
const stamp = new Date();
const data = session.data;
// Update session stamp
session.stamp = stamp;
// If user is registered, zeroize 'anonym' field, because mongoose when select session from db, set 'anonym' to {}
if (session.user) {
session.anonym = undefined;
}
// If user ip is changed, write old one to history with change time
// Limit history to 100 items
if (ip !== data.ip && !loopbackIPs.has(ip)) {
if (!data.ip_hist) {
data.ip_hist = [];
} else if (data.ip_hist.length >= 100) {
data.ip_hist = data.ip_hist.slice(-99);
}
data.ip_hist.push({ ip: data.ip, off: stamp });
data.ip = ip;
}
data.lang = config.lang;
// If user-agent is changed, parse it and write previous one to history with change time
// Limit history to 100 items
if (headers['user-agent'] !== data.headers['user-agent']) {
const agent = getBrowserAgent(browser);
if (!_.isEqual(agent, data.agent)) {
if (data.agent) {
if (!data.agent_hist) {
data.agent_hist = [];
} else if (data.agent_hist.length >= 100) {
data.agent_hist = data.agent_hist.slice(-99);
}
data.agent_hist.push({ agent: data.agent, off: stamp });
}
data.agent = agent;
}
}
data.headers = headers;
return Session.findOneAndUpdate({ _id: session._id }, session);
}
// Create session as copy from transfered session (ip, header, agent)
function copySession(sessionSource) {
const session = new Session({
key: Utils.randomString(12),
stamp: new Date(),
data: _.pick(sessionSource.data, 'lang', 'ip', 'headers', 'agent'),
});
return session;
}
// Add newly created or newly selected session in hashes
async function addSessionToHashes(session) {
sessWaitingConnect.set(session.key, session);
const usObj = await addSessionToUserObject.call(this, session);
return usObj;
}
// Remove session from hashes, and remove usObj if it doesn't contains sessiona anymore
export function removeSessionFromHashes({ session: { key: sessionKey }, usObj, logPrefix = '' }) {
const userKey = usObj.registered ? usObj.user.login : sessionKey;
let someCountPrev;
let someCountNew;
logPrefix = `${_.get(this, 'ridMark', '')} ${logPrefix}`;
sessWaitingConnect.delete(sessionKey);
sessConnected.delete(sessionKey);
someCountPrev = usSid.size;
usSid.delete(sessionKey);
someCountNew = usSid.size;
// logger.info('Delete session from usSid', someCountNew);
if (someCountNew !== someCountPrev - 1) {
logger.warn(`${logPrefix} Session from usSid not removed (${sessionKey}) ${userKey}`);
}
someCountPrev = Object.keys(usObj.sessions).length;
delete usObj.sessions[sessionKey];
someCountNew = Object.keys(usObj.sessions).length;
// logger.info('Delete session from usObj.sessions', someCountNew);
if (someCountNew !== someCountPrev - 1) {
logger.warn(`${logPrefix} WARN-Session from usObj not removed (${sessionKey}) ${userKey}`);
}
if (!someCountNew && usObj.registered) {
// logger.info('Delete user from hashes', usObj.user.login);
// If there is no more sessions in usObj of registered object, remove usObj of users hashes
// (from usSid is already removed)
usLogin.delete(usObj.user.login);
usId.delete(usObj.user._id.toString());
}
}
// Send session to archive
async function archiveSession(session, reason) {
// Take plain object of session, with _id instead of populated objects
const sessionPlain = session.toObject({ minimize: true, depopulate: true, versionKey: false });
// Set the reason for archiving
sessionPlain.archive_reason = reason;
// Don't save headers to archive
if (sessionPlain.data.headers) {
delete sessionPlain.data.headers;
}
const archivingSession = new SessionArchive(sessionPlain);
if (sessionPlain.user) {
archivingSession.anonym = undefined;
}
await Promise.all([
session.deleteOne(), // Remove session from collections of active sessions
archivingSession.save(), // Save archive session to collection of archive sessions
]);
return archivingSession;
}
// Pupulate user regions and build queries for them
async function popUserRegions(usObj) {
const user = usObj.user;
const registered = usObj.registered;
const paths = [
{
path: 'regionHome',
select: { _id: 1, cid: 1, parents: 1, title_en: 1, title_local: 1, center: 1, bbox: 1, bboxhome: 1 },
},
{
path: 'regions',
select: { _id: 1, cid: 1, parents: 1, title_en: 1, title_local: 1 },
},
];
let modregionsEquals; // Profile regions and moderation regions are equals
if (registered && user.role === 5) {
modregionsEquals = _.isEqual(user.regions, user.mod_regions) || undefined;
paths.push({ path: 'mod_regions', select: { _id: 1, cid: 1, parents: 1, title_en: 1, title_local: 1 } });
}
await user.populate(paths);
let regionsData = regionController.buildQuery(user.regions);
let shortRegions = regionController.getShortRegionsParams(regionsData.rhash);
usObj.rhash = regionsData.rhash;
usObj.rquery = regionsData.rquery;
usObj.rshortlvls = shortRegions.lvls;
usObj.rshortsel = shortRegions.sel;
if (user.role === 5) {
regionsData = regionController.buildQuery(user.mod_regions);
shortRegions = regionController.getShortRegionsParams(regionsData.rhash);
usObj.mod_rhash = regionsData.rhash;
usObj.mod_rquery = regionsData.rquery;
usObj.mod_rshortlvls = shortRegions.lvls;
usObj.mod_rshortsel = shortRegions.sel;
}
if (!modregionsEquals) {
delete usObj.mod_regions_equals;
} else {
usObj.mod_regions_equals = modregionsEquals;
}
return usObj;
}
// Reget user from db and populate all his dependencies
export async function regetUser(usObj, emitHim, excludeSocket) {
if (!usObj.registered) {
throw new ApplicationError(constantsError.SESSION_CAN_REGET_REGISTERED_ONLY);
}
const user = await User.findOne({ login: usObj.user.login }).exec();
if (!user) {
throw new NotFoundError(constantsError.NO_SUCH_USER);
}
// Assign new user object to usObj and to all its sessions
usObj.user = user;
_.forOwn(usObj.sessions, session => {
session.user = user;
});
await userObjectTreatUser(usObj);
if (emitHim) {
emitUser({ usObj, excludeSocket });
}
return user;
}
// Reget online users from db and populate all dependencies. Replace links in hashes with this new objects
// Receive 'all' or filter function
// Doesn't wait for execution, returns number of affected users immediately
// TODO: precess anonymouse users too, populate theirs regions
export function regetUsers(filterFn, emitThem) {
let usersCount = 0;
for (const usObj of usLogin.values()) {
if (filterFn === 'all' || filterFn(usObj)) {
regetUser(usObj, emitThem);
usersCount++;
}
}
return usersCount;
}
// Session treatment when user logging in, invokes from auth-controller
export async function loginUser({ user }) {
const { socket, handshake: { session: sessionOld, usObj: usObjOld } } = this;
const sessionNew = copySession(sessionOld);
const sessHash = sessWaitingConnect.has(sessionOld.key) ? sessWaitingConnect : sessConnected;
// Assign user to session
sessionNew.user = user;
// Remove propery of anonym user (it will remain in sessionOld)
sessionNew.anonym = undefined;
// Set link to old session
sessionNew.previous = sessionOld.key;
await sessionNew.save();
// Add session to existing usObj or will create new usObj
// usObj already exists if user already logged in on some other device (other session), in this case
// user must be taken from usObj instaed of incoming
const usObj = await addSessionToUserObject.call(this, sessionNew, true);
user = usObj.user;
// For all socket of currect (old) session assign new session and usObj
if (_.isEmpty(sessionOld.sockets)) {
logger.warn(`${this.ridMark} SessionOld have no sockets while login ${user.login}`);
} else {
_.forOwn(sessionOld.sockets, ({ handshake }) => {
handshake.usObj = usObj;
handshake.session = sessionNew;
});
// Transfer all sockets from old session to new
sessionNew.sockets = sessionOld.sockets;
}
delete sessionOld.sockets;
// Remove old session from sessions map
this.call('session.removeSessionFromHashes', { usObj: usObjOld, session: sessionOld, logPrefix: 'loginUser' });
// Put new session into sessions map
sessHash.set(sessionNew.key, sessionNew);
// Send old session to archive
await archiveSession(sessionOld, 'login');
// Update cookie in current socket, all browser tabs will see it
emitSidCookie(socket, false, true);
const userPlain = getPlainUser(user);
// Send user to all sockets of session, except current socket (auth-controller will send user there)
_.forOwn(sessionNew.sockets, sock => {
if (sock !== socket && _.isFunction(sock.emit)) {
sock.emit('youAre', { user: userPlain, registered: true });
}
});
return { session: sessionNew, userPlain };
}
// Session treatment when user exit, invokes from auth-controller
export async function logoutUser({ socket, usObj: usObjOld, session: sessionOld, currentSession = true }) {
const sessionNew = copySession(sessionOld);
const sessHash = sessWaitingConnect.has(sessionOld.key) ? sessWaitingConnect : sessConnected;
const user = usObjOld.user;
sessionNew.anonym.settings = user.toObject().settings;
// Array of regions _ids
sessionNew.anonym.regions = user.populated('regions') || [];
// _id of user's home regions
sessionNew.anonym.regionHome = user.populated('regionHome') || regionController.DEFAULT_HOME._id;
// Set link to old session
sessionNew.previous = sessionOld.key;
await sessionNew.save();
// Create new usObj and new session into it
const usObj = await addSessionToUserObject.call(this, sessionNew, currentSession);
// For all socket of currect (old) session assign new session and usObj
if (_.isEmpty(sessionOld.sockets)) {
logger.warn(`${this.ridMark} SessionOld have no sockets while logout ${user.login}`);
} else {
_.forOwn(sessionOld.sockets, ({ handshake }) => {
handshake.usObj = usObj;
handshake.session = sessionNew;
});
// Transfer all sockets from old session to new
sessionNew.sockets = sessionOld.sockets;
}
delete sessionOld.sockets;
// Remove old session from sessions map
this.call('session.removeSessionFromHashes', {
usObj: usObjOld, session: sessionOld, logPrefix: currentSession ? 'logoutCurrentUser' : 'logoutUser',
});
// Put new session in sessions map
sessHash.set(sessionNew.key, sessionNew);
// Send old session to archive
await archiveSession(sessionOld, currentSession ? 'logout' : 'destroy');
if (socket) {
// Send client new cookie of anonym session
await emitSidCookie(socket, true, false);
}
sendReload(sessionNew);
}
// Check user is online
export function isOnline({ login, userId, sessionKey } = {}) {
if (login) {
return usLogin.has(login);
}
if (userId && typeof userId.toString === 'function') {
return usId.has(userId.toString());
}
if (sessionKey) {
return usSid.has(sessionKey);
}
return false;
}
// Get online user
export function getOnline({ login, userId, sessionKey } = {}) {
if (login) {
return usLogin.get(login);
}
if (userId && typeof userId.toString === 'function') {
return usId.get(userId.toString());
}
if (sessionKey) {
return usSid.get(sessionKey);
}
}
// Periodic process for dropping waiting connection sessions, if they don't establish connection in given time
export const checkSessWaitingConnect = (function () {
const SESSION_WAIT_CHECK_INTERVAL = ms('10s');
const SESSION_WAIT_TIMEOUT = ms('1m');
function procedure() {
const expiredFrontier = Date.now() - SESSION_WAIT_TIMEOUT;
let removedCount = 0;
for (const [key, session] of sessWaitingConnect.entries()) {
const stamp = new Date(session.stamp || 0).getTime();
if (!stamp || stamp <= expiredFrontier) {
removedCount++;
removeSessionFromHashes({ usObj: usSid.get(key), session, logPrefix: 'checkSessWaitingConnect' });
}
}
if (removedCount) {
logger.info(`${removedCount} waiting sessions were removed from hashes`);
}
checkSessWaitingConnect();
}
return function () {
setTimeout(procedure, SESSION_WAIT_CHECK_INTERVAL).unref();
};
}());
/**
* Periodically sends expired session to archive.
* Used by archiveExpiredSessions job in session queue.
*
* @returns {object} object containing message and data.
*/
export const archiveExpiredSessions = async function () {
const archiveDate = new Date();
const start = archiveDate.getTime();
const resultKeys = [];
const insertBulk = [];
let counter = 0;
const userQuery = { user: { $exists: true }, stamp: { $lte: new Date(start - SESSION_USER_LIFE) } };
const anonQuery = { anonym: { $exists: true }, stamp: { $lte: new Date(start - SESSION_ANON_LIFE) } };
// Simply remove anonymous sessions older then SESSION_ANON_LIFE, there is no point in storing them
const { deletedCount: countRemovedAnon } = await Session.deleteMany(anonQuery).exec();
// Move each expired registered user session to sessions_archive
for await (const session of Session.find(userQuery).limit(5000)) {
counter++;
if (session.__v) {
delete session.__v;
}
if (session.data && session.data.headers) {
delete session.data.headers;
}
session.archived = archiveDate;
session.archive_reason = 'expire';
insertBulk.push(session);
resultKeys.push(session.key);
}
if (insertBulk.length) {
// TODO: Wrap both queries in transaction when we have replica set.
await Session.deleteMany({ key: { $in: resultKeys } }).exec();
await SessionArchive.insertMany(insertBulk, { ordered: false }); // no exec needed, returns proper promise already!
}
return {
message: `${counter} expired registered sessions moved to archive, ${countRemovedAnon} expired anonymous sessions dropped`,
data: JSON.stringify({ keys: resultKeys }),
};
};
/**
* Clean archived sessions on frontends following archiveExpiredSessions call
* by worker process.
*
* @param {string} data JSON serialised data returned by archiveExpiredSessions.
*/
export const cleanArchivedSessions = function (data) {
data = JSON.parse(data);
let removedCount = 0;
// Check if some of archived sessions is still in memory (in hashes), remove it from memory
_.forEach(data.keys, key => {
if (sessConnected.has(key)) {
removedCount++;
const session = sessConnected.get(key);
const usObj = usSid.get(key);
if (usObj !== undefined) {
removeSessionFromHashes({ usObj, session, logPrefix: 'checkExpiredSessions' });
}
// If session contains sockets, break connection
_.forEach(session.sockets, socket => {
if (socket.disconnet) {
socket.disconnet();
}
});
delete session.sockets;
}
});
logger.info(`cleanArchivedSessions: ${removedCount} archived sessions were removed from hashes`);
};
/**
* Periodically recalculate user statistics, like pcount, which might get out of sync over time.
* Used by calcUserStats job in session queue.
*
* @param {string[]} [logins] Array of logins to limit query to.
* @returns {object} object containing message.
*/
export const calcUserStats = async function (logins) {
const query = {};
let usersUpdated = 0;
if (logins && logins.length) {
query.login = { $in: logins };
}
// Using cursor.
for await (const user of User.find(query, { _id: 1 }).sort({ cid: -1 })) {
const $set = {};
const $unset = {};
const $update = {};
await Promise.all([
Photo.countDocuments({ user: user._id, s: 5 }),
Photo.countDocuments({ user: user._id, s: { $in: [0, 1, 2] } }),
Photo.countDocuments({ user: user._id, s: { $in: [3, 4, 7, 9] } }),
Comment.countDocuments({ user: user._id, del: null }),
CommentN.countDocuments({ user: user._id, del: null }),
]).then(([pcount, pfcount, pdcount, ccount, cncount]) => {
if (pcount > 0) {
$set.pcount = pcount;
} else {
$unset.pcount = 1;
}
if (pfcount > 0) {
$set.pfcount = pfcount;
} else {
$unset.pfcount = 1;
}
if (pdcount > 0) {
$set.pdcount = pdcount;
} else {
$unset.pdcount = 1;
}
if (ccount + cncount > 0) {
$set.ccount = ccount + cncount;
} else {
$unset.ccount = 1;
}
//Нельзя присваивать пустой объект $set или $unset - обновления не будет, поэтому проверяем на кол-во ключей
if (Object.keys($set).length) {
$update.$set = $set;
}
if (Object.keys($unset).length) {
$update.$unset = $unset;
}
usersUpdated++;
return User.updateOne({ _id: user._id }, $update, { upsert: false }).exec();
});
}
return { message: `User statistics for ${usersUpdated} users was calculated` };
};
/**
* Reget users on frontends following calcUserStats call
* by worker process.
*/
export const regetUsersAfterStatsUpdate = function () {
const onlineUserCount = regetUsers(usObj => usObj.registered, true);
logger.info(`regetUsersAfterStatsUpdate: ${onlineUserCount} online users have been synced with db and re-populated.`);
};
// Handler of http-request or websocket-connection for session and usObj create/select
export async function handleConnection(ip, headers, overHTTP, req) {
if (!headers || !headers['user-agent']) {
// If session doesn't contain header or user-agent - deny
throw new BadParamsError(constantsError.SESSION_NO_HEADERS, this.rid);
}
if (overHTTP) {
// Ability to set most priority locale by header 'X-Facebook-Locale' or by 'fb_locale'/'locale' uri parameters
// http://developers.facebook.com/docs/opengraph/guides/internationalization
const localeOverride = headers['x-facebook-locale'] || req.query.fb_locale || req.query.locale;
if (localeOverride) {
if (req.headers['accept-language']) {
req.headers['accept-language'] = localeOverride + ',' + req.headers['accept-language'];
} else {
req.headers['accept-language'] = localeOverride;
}
}
}
// Parse user-agent information
const browser = checkUserAgent(headers['user-agent']);
if (browser.badbrowser) {
throw new BadParamsError({ code: constantsError.BAD_BROWSER, agent: browser.agent, trace: false }, this.rid);
}
const cookieObj = parseCookie(headers.cookie || ''); // Parse cookie
const sid = cookieObj[SESSION_COOKIE_KEY]; // Get session key from cookie
let session;
let track;
let usObj;
if (!sid) {
track = 'No incoming sid, creating new session';
session = await createSession.call(this, ip, headers, browser);
usObj = await addSessionToHashes.call(this, session);
} else if ((sessConnected.has(sid) || sessWaitingConnect.has(sid)) && usSid.has(sid)) {
// If key exists and such session already in hash, then just get this session
// logger.info(this.ridMark, 'handleConnection', 'Get session from hash');
track = `Session found among ${sessConnected.has(sid) ? 'connected' : 'waiting connect'} sessions`;
session = sessConnected.get(sid) || sessWaitingConnect.get(sid);
usObj = usSid.get(sid);
this.addUserIdToRidMark(usObj, session);
if (overHTTP) {
// If client made http request again (open new browser tab), update session data, to set current stamp,
// to postpone for this session checkSessWaitingConnect
// And check if locale changed
await updateSession.call(this, session, ip, headers, browser);
}
} else {
// If session key is exists, but session not in hashes,
// then select session from db, but if it's already selecting just wait promise
if (!sessWaitingSelect.has(sid)) {
sessWaitingSelect.set(sid, (async () => {
try {
let session = await Session.findOne({ key: sid }).populate('user').exec();
// If session is found, update data in db and populate user
if (session) {
await updateSession.call(this, session, ip, headers, browser);
} else {
// If session with such key doesn't exist, create new one
track = 'Session has\'t been found in the db by the incoming sid, creating a new one';
session = await createSession.call(this, ip, headers, browser);
}
const usObj = await addSessionToHashes.call(this, session);
return { session, usObj };
} finally {
// Remove promise from hash of waiting connect by session key, anyway - success or error
sessWaitingSelect.delete(sid);
}
})());
} else {
track = 'Session searching has already started, waiting for that promise';
}
({ session, usObj } = await sessWaitingSelect.get(sid));
}
if (!usObj || !session) {
logger.error(
`${this.ridMark} Handling incoming ${overHTTP ? 'http' : 'websocket'} connection`,
`finished with empty ${!usObj ? 'usObj' : 'session'}, incoming sid: ${sid}, track: ${track}`
);
throw new ApplicationError({ code: constantsError.SESSION_NOT_FOUND, agent: browser.agent, trace: false }, this.rid);
}