feat(assist): refactored extractSessionInfo
feat(assist): hardcoded session's attributes
This commit is contained in:
parent
71a3581ebb
commit
735c1cfaba
4 changed files with 54 additions and 86 deletions
|
|
@ -1,7 +1,7 @@
|
|||
const _io = require('socket.io');
|
||||
const express = require('express');
|
||||
const uaParser = require('ua-parser-js');
|
||||
const {
|
||||
extractSessionInfo,
|
||||
extractPeerId,
|
||||
hasFilters,
|
||||
isValidSession,
|
||||
|
|
@ -14,7 +14,6 @@ const {
|
|||
extractSessionIdFromRequest,
|
||||
extractPayloadFromRequest
|
||||
} = require('../utils/helper-ee');
|
||||
const {geoip} = require('../utils/geoIP');
|
||||
const {createAdapter} = require("@socket.io/redis-adapter");
|
||||
const {createClient} = require("redis");
|
||||
const wsRouter = express.Router();
|
||||
|
|
@ -267,33 +266,6 @@ async function get_all_agents_ids(io, socket) {
|
|||
return agents;
|
||||
}
|
||||
|
||||
|
||||
function extractSessionInfo(socket) {
|
||||
if (socket.handshake.query.sessionInfo !== undefined) {
|
||||
debug && console.log("received headers");
|
||||
debug && console.log(socket.handshake.headers);
|
||||
socket.handshake.query.sessionInfo = JSON.parse(socket.handshake.query.sessionInfo);
|
||||
|
||||
let ua = uaParser(socket.handshake.headers['user-agent']);
|
||||
socket.handshake.query.sessionInfo.userOs = ua.os.name || null;
|
||||
socket.handshake.query.sessionInfo.userBrowser = ua.browser.name || null;
|
||||
socket.handshake.query.sessionInfo.userBrowserVersion = ua.browser.version || null;
|
||||
socket.handshake.query.sessionInfo.userDevice = ua.device.model || null;
|
||||
socket.handshake.query.sessionInfo.userDeviceType = ua.device.type || 'desktop';
|
||||
socket.handshake.query.sessionInfo.userCountry = null;
|
||||
if (geoip() !== null) {
|
||||
debug && console.log(`looking for location of ${socket.handshake.headers['x-forwarded-for'] || socket.handshake.address}`);
|
||||
try {
|
||||
let country = geoip().country(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
|
||||
socket.handshake.query.sessionInfo.userCountry = country.country.isoCode;
|
||||
} catch (e) {
|
||||
debug && console.log("geoip-country failed");
|
||||
debug && console.log(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wsRouter.get(`/sockets-list`, socketsList);
|
||||
wsRouter.post(`/sockets-list`, socketsList);
|
||||
wsRouter.get(`/sockets-list/:projectKey/autocomplete`, autocomplete);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
const _io = require('socket.io');
|
||||
const express = require('express');
|
||||
const uaParser = require('ua-parser-js');
|
||||
const {
|
||||
extractSessionInfo,
|
||||
extractPeerId,
|
||||
hasFilters,
|
||||
isValidSession,
|
||||
|
|
@ -14,7 +14,6 @@ const {
|
|||
extractSessionIdFromRequest,
|
||||
extractPayloadFromRequest,
|
||||
} = require('../utils/helper-ee');
|
||||
const {geoip} = require('../utils/geoIP');
|
||||
const wsRouter = express.Router();
|
||||
const UPDATE_EVENT = "UPDATE_SESSION";
|
||||
const IDENTITIES = {agent: 'agent', session: 'session'};
|
||||
|
|
@ -245,33 +244,6 @@ async function get_all_agents_ids(io, socket) {
|
|||
return agents;
|
||||
}
|
||||
|
||||
|
||||
function extractSessionInfo(socket) {
|
||||
if (socket.handshake.query.sessionInfo !== undefined) {
|
||||
debug && console.log("received headers");
|
||||
debug && console.log(socket.handshake.headers);
|
||||
socket.handshake.query.sessionInfo = JSON.parse(socket.handshake.query.sessionInfo);
|
||||
|
||||
let ua = uaParser(socket.handshake.headers['user-agent']);
|
||||
socket.handshake.query.sessionInfo.userOs = ua.os.name || null;
|
||||
socket.handshake.query.sessionInfo.userBrowser = ua.browser.name || null;
|
||||
socket.handshake.query.sessionInfo.userBrowserVersion = ua.browser.version || null;
|
||||
socket.handshake.query.sessionInfo.userDevice = ua.device.model || null;
|
||||
socket.handshake.query.sessionInfo.userDeviceType = ua.device.type || 'desktop';
|
||||
socket.handshake.query.sessionInfo.userCountry = null;
|
||||
if (geoip() !== null) {
|
||||
debug && console.log(`looking for location of ${socket.handshake.headers['x-forwarded-for'] || socket.handshake.address}`);
|
||||
try {
|
||||
let country = geoip().country(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
|
||||
socket.handshake.query.sessionInfo.userCountry = country.country.isoCode;
|
||||
} catch (e) {
|
||||
debug && console.log("geoip-country failed");
|
||||
debug && console.log(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wsRouter.get(`/sockets-list`, socketsList);
|
||||
wsRouter.post(`/sockets-list`, socketsList);
|
||||
wsRouter.get(`/sockets-list/:projectKey/autocomplete`, autocomplete);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
const _io = require('socket.io');
|
||||
const express = require('express');
|
||||
const uaParser = require('ua-parser-js');
|
||||
const {
|
||||
extractSessionInfo,
|
||||
extractPeerId,
|
||||
extractProjectKeyFromRequest,
|
||||
extractSessionIdFromRequest,
|
||||
|
|
@ -12,7 +12,6 @@ const {
|
|||
getValidAttributes,
|
||||
uniqueAutocomplete
|
||||
} = require('../utils/helper');
|
||||
const {geoip} = require('../utils/geoIP');
|
||||
const wsRouter = express.Router();
|
||||
const UPDATE_EVENT = "UPDATE_SESSION";
|
||||
const IDENTITIES = {agent: 'agent', session: 'session'};
|
||||
|
|
@ -227,32 +226,6 @@ async function get_all_agents_ids(io, socket) {
|
|||
}
|
||||
|
||||
|
||||
function extractSessionInfo(socket) {
|
||||
if (socket.handshake.query.sessionInfo !== undefined) {
|
||||
debug && console.log("received headers");
|
||||
debug && console.log(socket.handshake.headers);
|
||||
socket.handshake.query.sessionInfo = JSON.parse(socket.handshake.query.sessionInfo);
|
||||
|
||||
let ua = uaParser(socket.handshake.headers['user-agent']);
|
||||
socket.handshake.query.sessionInfo.userOs = ua.os.name || null;
|
||||
socket.handshake.query.sessionInfo.userBrowser = ua.browser.name || null;
|
||||
socket.handshake.query.sessionInfo.userBrowserVersion = ua.browser.version || null;
|
||||
socket.handshake.query.sessionInfo.userDevice = ua.device.model || null;
|
||||
socket.handshake.query.sessionInfo.userDeviceType = ua.device.type || 'desktop';
|
||||
socket.handshake.query.sessionInfo.userCountry = null;
|
||||
if (geoip() !== null) {
|
||||
debug && console.log(`looking for location of ${socket.handshake.headers['x-forwarded-for'] || socket.handshake.address}`);
|
||||
try {
|
||||
let country = geoip().country(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
|
||||
socket.handshake.query.sessionInfo.userCountry = country.country.isoCode;
|
||||
} catch (e) {
|
||||
debug && console.log("geoip-country failed");
|
||||
debug && console.log(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wsRouter.get(`/sockets-list`, socketsList);
|
||||
wsRouter.post(`/sockets-list`, socketsList);
|
||||
wsRouter.get(`/sockets-list/:projectKey/autocomplete`, autocomplete);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
const uaParser = require('ua-parser-js');
|
||||
const {geoip} = require('./geoIP');
|
||||
let PROJECT_KEY_LENGTH = parseInt(process.env.PROJECT_KEY_LENGTH) || 20;
|
||||
let debug = process.env.debug === "1" || false;
|
||||
const extractPeerId = (peerId) => {
|
||||
|
|
@ -184,7 +186,56 @@ const uniqueAutocomplete = function (list) {
|
|||
}
|
||||
return _list;
|
||||
}
|
||||
const BASE_sessionInfo = {
|
||||
"pageTitle": "Page",
|
||||
"active": true,
|
||||
"live": true,
|
||||
"sessionID": "0",
|
||||
"metadata": {},
|
||||
"userID": "",
|
||||
"userUUID": "",
|
||||
"projectKey": "",
|
||||
"revID": "",
|
||||
"timestamp": 0,
|
||||
"trackerVersion": "",
|
||||
"isSnippet": true,
|
||||
"userOs": "",
|
||||
"userBrowser": "",
|
||||
"userBrowserVersion": "",
|
||||
"userDevice": "",
|
||||
"userDeviceType": "",
|
||||
"userCountry": "",
|
||||
"projectId": 0
|
||||
};
|
||||
const extractSessionInfo = function (socket) {
|
||||
if (socket.handshake.query.sessionInfo !== undefined) {
|
||||
debug && console.log("received headers");
|
||||
debug && console.log(socket.handshake.headers);
|
||||
socket.handshake.query.sessionInfo = JSON.parse(socket.handshake.query.sessionInfo);
|
||||
socket.handshake.query.sessionInfo = {...BASE_sessionInfo, ...socket.handshake.query.sessionInfo};
|
||||
|
||||
let ua = uaParser(socket.handshake.headers['user-agent']);
|
||||
socket.handshake.query.sessionInfo.userOs = ua.os.name || null;
|
||||
socket.handshake.query.sessionInfo.userBrowser = ua.browser.name || null;
|
||||
socket.handshake.query.sessionInfo.userBrowserVersion = ua.browser.version || null;
|
||||
socket.handshake.query.sessionInfo.userDevice = ua.device.model || null;
|
||||
socket.handshake.query.sessionInfo.userDeviceType = ua.device.type || 'desktop';
|
||||
socket.handshake.query.sessionInfo.userCountry = null;
|
||||
if (geoip() !== null) {
|
||||
debug && console.log(`looking for location of ${socket.handshake.headers['x-forwarded-for'] || socket.handshake.address}`);
|
||||
try {
|
||||
let country = geoip().country(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
|
||||
socket.handshake.query.sessionInfo.userCountry = country.country.isoCode;
|
||||
} catch (e) {
|
||||
debug && console.log("geoip-country failed");
|
||||
debug && console.log(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
extractSessionInfo,
|
||||
extractPeerId,
|
||||
request_logger,
|
||||
getValidAttributes,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue