feat(utilities): optimized websocket geo-ip logic

feat(utilities): refactored websocket
This commit is contained in:
Taha Yassine Kraiem 2022-04-11 18:17:49 +02:00
parent f2b429a3d8
commit 58af31c35e
7 changed files with 48 additions and 63 deletions

View file

@ -1,4 +1,4 @@
const dumps = require('./utils/dump');
const dumps = require('./utils/HeapSnapshot');
const sourcemapsReaderServer = require('./servers/sourcemaps-server');
const {peerRouter, peerConnection, peerDisconnect, peerError} = require('./servers/peerjs-server');
const express = require('express');

View file

@ -255,6 +255,16 @@ async function get_all_agents_ids(io, socket) {
return agents;
}
let geoip = null;
geoip2Reader.open(process.env.MAXMINDDB_FILE, {})
.then(reader => {
geoip = reader;
})
.catch(error => {
console.log("Error while opening the MAXMINDDB_FILE.")
console.error(error);
});
function extractSessionInfo(socket) {
if (socket.handshake.query.sessionInfo !== undefined) {
debug && console.log("received headers");
@ -268,21 +278,11 @@ function extractSessionInfo(socket) {
socket.handshake.query.sessionInfo.userDevice = ua.device.model || null;
socket.handshake.query.sessionInfo.userDeviceType = ua.device.type || 'desktop';
socket.handshake.query.sessionInfo.userCountry = null;
const options = {
// you can use options like `cache` or `watchForUpdates`
};
// console.log("Looking for MMDB file in " + process.env.MAXMINDDB_FILE);
geoip2Reader.open(process.env.MAXMINDDB_FILE, options)
.then(reader => {
debug && console.log("looking for location of ");
debug && console.log(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
let country = reader.country(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
socket.handshake.query.sessionInfo.userCountry = country.country.isoCode;
})
.catch(error => {
console.error(error);
});
if (geoip !== null) {
debug && console.log(`looking for location of ${socket.handshake.headers['x-forwarded-for'] || socket.handshake.address}`);
let country = geoip.country(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
socket.handshake.query.sessionInfo.userCountry = country.country.isoCode;
}
}
}
@ -294,10 +294,6 @@ module.exports = {
debug && console.log(`WS started:${socket.id}, Query:${JSON.stringify(socket.handshake.query)}`);
socket.peerId = socket.handshake.query.peerId;
socket.identity = socket.handshake.query.identity;
const {projectKey, sessionId} = extractPeerId(socket.peerId);
socket.sessionId = sessionId;
socket.projectKey = projectKey;
socket.lastMessageReceivedAt = Date.now();
let {c_sessions, c_agents} = await sessions_agents_count(io, socket);
if (socket.identity === IDENTITIES.session) {
if (c_sessions > 0) {
@ -361,7 +357,6 @@ module.exports = {
});
socket.onAny(async (eventName, ...args) => {
socket.lastMessageReceivedAt = Date.now();
if (socket.identity === IDENTITIES.session) {
debug && console.log(`received event:${eventName}, from:${socket.identity}, sending message to room:${socket.peerId}`);
socket.to(socket.peerId).emit(eventName, args[0]);

View file

@ -233,6 +233,16 @@ async function get_all_agents_ids(io, socket) {
return agents;
}
let geoip = null;
geoip2Reader.open(process.env.MAXMINDDB_FILE, {})
.then(reader => {
geoip = reader;
})
.catch(error => {
console.log("Error while opening the MAXMINDDB_FILE.")
console.error(error);
});
function extractSessionInfo(socket) {
if (socket.handshake.query.sessionInfo !== undefined) {
debug && console.log("received headers");
@ -246,21 +256,11 @@ function extractSessionInfo(socket) {
socket.handshake.query.sessionInfo.userDevice = ua.device.model || null;
socket.handshake.query.sessionInfo.userDeviceType = ua.device.type || 'desktop';
socket.handshake.query.sessionInfo.userCountry = null;
const options = {
// you can use options like `cache` or `watchForUpdates`
};
// console.log("Looking for MMDB file in " + process.env.MAXMINDDB_FILE);
geoip2Reader.open(process.env.MAXMINDDB_FILE, options)
.then(reader => {
debug && console.log("looking for location of ");
debug && console.log(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
let country = reader.country(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
socket.handshake.query.sessionInfo.userCountry = country.country.isoCode;
})
.catch(error => {
console.error(error);
});
if (geoip !== null) {
debug && console.log(`looking for location of ${socket.handshake.headers['x-forwarded-for'] || socket.handshake.address}`);
let country = geoip.country(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
socket.handshake.query.sessionInfo.userCountry = country.country.isoCode;
}
}
}
@ -272,10 +272,6 @@ module.exports = {
debug && console.log(`WS started:${socket.id}, Query:${JSON.stringify(socket.handshake.query)}`);
socket.peerId = socket.handshake.query.peerId;
socket.identity = socket.handshake.query.identity;
const {projectKey, sessionId} = extractPeerId(socket.peerId);
socket.sessionId = sessionId;
socket.projectKey = projectKey;
socket.lastMessageReceivedAt = Date.now();
let {c_sessions, c_agents} = await sessions_agents_count(io, socket);
if (socket.identity === IDENTITIES.session) {
if (c_sessions > 0) {
@ -337,7 +333,6 @@ module.exports = {
});
socket.onAny(async (eventName, ...args) => {
socket.lastMessageReceivedAt = Date.now();
if (socket.identity === IDENTITIES.session) {
debug && console.log(`received event:${eventName}, from:${socket.identity}, sending message to room:${socket.peerId}`);
socket.to(socket.peerId).emit(eventName, args[0]);

View file

@ -1,4 +1,4 @@
FROM node:17-alpine
FROM node:17-stretch
WORKDIR /work
COPY . .
RUN npm install

View file

@ -1,4 +1,4 @@
const dumps = require('./utils/dump');
const dumps = require('./utils/HeapSnapshot');
const sourcemapsReaderServer = require('./servers/sourcemaps-server');
const {peerRouter, peerConnection, peerDisconnect, peerError} = require('./servers/peerjs-server');
const express = require('express');

View file

@ -204,6 +204,16 @@ async function get_all_agents_ids(io, socket) {
return agents;
}
let geoip = null;
geoip2Reader.open(process.env.MAXMINDDB_FILE, {})
.then(reader => {
geoip = reader;
})
.catch(error => {
console.log("Error while opening the MAXMINDDB_FILE.")
console.error(error);
});
function extractSessionInfo(socket) {
if (socket.handshake.query.sessionInfo !== undefined) {
debug && console.log("received headers");
@ -217,21 +227,11 @@ function extractSessionInfo(socket) {
socket.handshake.query.sessionInfo.userDevice = ua.device.model || null;
socket.handshake.query.sessionInfo.userDeviceType = ua.device.type || 'desktop';
socket.handshake.query.sessionInfo.userCountry = null;
const options = {
// you can use options like `cache` or `watchForUpdates`
};
// console.log("Looking for MMDB file in " + process.env.MAXMINDDB_FILE);
geoip2Reader.open(process.env.MAXMINDDB_FILE, options)
.then(reader => {
debug && console.log("looking for location of ");
debug && console.log(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
let country = reader.country(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
socket.handshake.query.sessionInfo.userCountry = country.country.isoCode;
})
.catch(error => {
console.error(error);
});
if (geoip !== null) {
debug && console.log(`looking for location of ${socket.handshake.headers['x-forwarded-for'] || socket.handshake.address}`);
let country = geoip.country(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address);
socket.handshake.query.sessionInfo.userCountry = country.country.isoCode;
}
}
}
@ -243,10 +243,6 @@ module.exports = {
debug && console.log(`WS started:${socket.id}, Query:${JSON.stringify(socket.handshake.query)}`);
socket.peerId = socket.handshake.query.peerId;
socket.identity = socket.handshake.query.identity;
const {projectKey, sessionId} = extractPeerId(socket.peerId);
socket.sessionId = sessionId;
socket.projectKey = projectKey;
socket.lastMessageReceivedAt = Date.now();
let {c_sessions, c_agents} = await sessions_agents_count(io, socket);
if (socket.identity === IDENTITIES.session) {
if (c_sessions > 0) {
@ -308,7 +304,6 @@ module.exports = {
});
socket.onAny(async (eventName, ...args) => {
socket.lastMessageReceivedAt = Date.now();
if (socket.identity === IDENTITIES.session) {
debug && console.log(`received event:${eventName}, from:${socket.identity}, sending message to room:${socket.peerId}`);
socket.to(socket.peerId).emit(eventName, args[0]);