initial commit

needs work - currently just an api endpoint that will return geolocation data for a given IP.
ToDo:
- Display IP in session-card
- Clicking IP opens modal with geolocation info
This commit is contained in:
fuzz
2023-10-25 16:23:25 +01:00
parent 186ef87635
commit 5e238fdad2
2 changed files with 52 additions and 0 deletions

50
backend/routes/utils.js Normal file
View File

@@ -0,0 +1,50 @@
// api.js
const https = require("https");
const axios = require("axios");
const express = require("express");
const router = express.Router();
const geoliteUrlBase = 'https://geolite.info/geoip/v2.1/city';
const geoliteAccountId = process.env.GEOLITE_ACCOUNT_ID;
const geoliteLicenseKey = process.env.GEOLITE_LICENSE_KEY;
//https://stackoverflow.com/questions/5284147/validating-ipv4-addresses-with-regexp
const ipRegex = new RegExp(`^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}$`);
const agent = new https.Agent({
rejectUnauthorized: (process.env.REJECT_SELF_SIGNED_CERTIFICATES || 'true').toLowerCase() ==='true'
});
const axios_instance = axios.create({
httpsAgent: agent
});
router.get("/geolocateIp", async (req, res) => {
try {
if(!(geoliteAccountId && geoliteLicenseKey)) {
return res.status(501).send('GeoLite information missing!');
}
const { ipAddress } = req.body;
ipRegex.lastIndex = 0;
if(!ipAddress || !ipRegex.test(ipAddress)) {
return res.status(400).send('Invalid IP address sent!');
}
const response = await axios_instance.get(`${geoliteUrlBase}/${ipAddress}`, {
auth: {
username: geoliteAccountId,
password: geoliteLicenseKey
}
});
return res.send(response.data);
} catch (error) {
res.status(503);
res.send(error);
}
});
module.exports = router;

View File

@@ -15,6 +15,7 @@ const ActivityMonitor = require('./tasks/ActivityMonitor');
const SyncTask = require('./tasks/SyncTask');
const BackupTask = require('./tasks/BackupTask');
const logRouter = require('./routes/logging');
const utilsRouter = require('./routes/utils');
const dbInstance = require("./db");
@@ -109,6 +110,7 @@ app.use('/sync', authenticate , syncRouter.router,()=>{/* #swagger.tags = ['Syn
app.use('/stats', authenticate , statsRouter,()=>{/* #swagger.tags = ['Stats']*/}); // mount the API router at /stats, with JWT middleware
app.use('/backup', authenticate , backupRouter.router,()=>{/* #swagger.tags = ['Backup']*/}); // mount the API router at /backup, with JWT middleware
app.use('/logs', authenticate , logRouter.router,()=>{/* #swagger.tags = ['Logs']*/}); // mount the API router at /logs, with JWT middleware
app.use('/utils', authenticate, utilsRouter, ()=>{/* #swagger.tags = ['Utils']*/}); // mount the API router at /utils, with JWT middleware
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');