Skip to content

Commit

Permalink
[mirotalksfu] - add IPLookup on server side (optional)
Browse files Browse the repository at this point in the history
  • Loading branch information
miroslavpejic85 committed Apr 18, 2023
1 parent e0b72dd commit a4bdf0f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
24 changes: 20 additions & 4 deletions app/src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
███████ ███████ ██  ██   ████   ███████ ██  ██                                       
dependencies: {
@sentry/node : https://www.npmjs.com/package/@sentry/node
@sentry/integrations : https://www.npmjs.com/package/@sentry/integrations
axios : https://www.npmjs.com/package/axios
body-parser : https://www.npmjs.com/package/body-parser
compression : https://www.npmjs.com/package/compression
colors : https://www.npmjs.com/package/colors
Expand All @@ -20,8 +23,6 @@ dependencies: {
ngrok : https://www.npmjs.com/package/ngrok
openai : https://www.npmjs.com/package/openai
qs : https://www.npmjs.com/package/qs
@sentry/node : https://www.npmjs.com/package/@sentry/node
@sentry/integrations : https://www.npmjs.com/package/@sentry/integrations
socket.io : https://www.npmjs.com/package/socket.io
swagger-ui-express : https://www.npmjs.com/package/swagger-ui-express
uuid : https://www.npmjs.com/package/uuid
Expand Down Expand Up @@ -50,10 +51,11 @@ const https = require('httpolyglot');
const mediasoup = require('mediasoup');
const mediasoupClient = require('mediasoup-client');
const http = require('http');
const config = require('./config');
const path = require('path');
const axios = require('axios');
const ngrok = require('ngrok');
const fs = require('fs');
const config = require('./config');
const checkXSS = require('./XSS.js');
const Host = require('./Host');
const Room = require('./Room');
Expand Down Expand Up @@ -722,7 +724,7 @@ function startServer() {
roomList.get(socket.room_id).broadCast(socket.id, 'setVideoOff', data);
});

socket.on('join', (dataObject, cb) => {
socket.on('join', async (dataObject, cb) => {
if (!roomList.has(socket.room_id)) {
return cb({
error: 'Room does not exist',
Expand All @@ -732,6 +734,11 @@ function startServer() {
// Get peer IPv4 (::1 Its the loopback address in ipv6, equal to 127.0.0.1 in ipv4)
const peer_ip = socket.handshake.headers['x-forwarded-for'] || socket.conn.remoteAddress;

// Get peer Geo Location
if (config.IPLookup.enabled && (peer_ip != '::1' || peer_ip != '127.0.0.1')) {
dataObject.peer_geo = await getPeerGeoLocation(peer_ip);
}

const data = checkXSS(dataObject);

log.debug('User joined', data);
Expand Down Expand Up @@ -1060,6 +1067,15 @@ function startServer() {
}
});

async function getPeerGeoLocation(ip) {
const endpoint = config.IPLookup.getEndpoint(ip);
log.debug('Get peer geo', { ip: ip, endpoint: endpoint });
return axios
.get(endpoint)
.then((response) => response.data)
.catch((error) => log.error(error));
}

function getIP(req) {
return req.headers['x-forwarded-for'] || req.socket.remoteAddress;
}
Expand Down
12 changes: 11 additions & 1 deletion app/src/config.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = {
signingSecret: '',
},
chatGPT: {
/**
/*
ChatGPT
1. Goto https://platform.openai.com/
2. Create your account
Expand All @@ -92,6 +92,16 @@ module.exports = {
max_tokens: 1000,
temperature: 0,
},
IPLookup: {
/*
GeoJS
https://www.geojs.io/docs/v1/endpoints/geo/
*/
enabled: false,
getEndpoint(ip) {
return `https://get.geojs.io/v1/ip/geo/${ip}.json`;
},
},
mediasoup: {
// Worker settings
numWorkers: Object.keys(os.cpus()).length,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"@sentry/integrations": "7.48.0",
"@sentry/node": "7.48.0",
"axios": "^1.3.5",
"body-parser": "1.20.2",
"colors": "1.4.0",
"compression": "1.7.4",
Expand Down

0 comments on commit a4bdf0f

Please sign in to comment.