fix(dashboard): resolve per-agent server origin and per-agent flow countries on Overview GlobeMap

This commit is contained in:
ypratama committed 2026-11-09 16:06:54 +07:00
1 parent fc441e4f1f
commit 4f7fd999a4
8 files changed
+385 -161

No files matched your search

+4
View File
@@ -3,6 +3,7 @@ const router = express.Router();
const mongoose = require('mongoose');
const { AppStat, ProtocolStat, AppCategoryStat, LookupApp } = require('../../models/Schemas');
const { getTimeFilter, getBaseFilter } = require('./helpers');
const { applySubnetFilterToQuery } = require('../../utils/subnetFilter');
// GET /api/dashboard/apps
router.get('/apps', async (req, res) => {
@@ -10,6 +11,7 @@ router.get('/apps', async (req, res) => {
const limit = parseInt(req.query.limit || 10);
const timeFilter = getTimeFilter(req);
const base = getBaseFilter(req, timeFilter);
await applySubnetFilterToQuery(base, 'src_ip');
const { Flow } = require('../../models/Schemas');
// Aggregate directly from Flow for accurate delta values
@@ -59,6 +61,7 @@ router.get('/protocols', async (req, res) => {
try {
const timeFilter = getTimeFilter(req);
const base = getBaseFilter(req, timeFilter);
await applySubnetFilterToQuery(base, 'src_ip');
const { Flow } = require('../../models/Schemas');
const flowPipeline = [
@@ -90,6 +93,7 @@ router.get('/app-categories', async (req, res) => {
try {
const timeFilter = getTimeFilter(req);
const base = getBaseFilter(req, timeFilter);
await applySubnetFilterToQuery(base, 'src_ip');
const { Flow, LookupApp } = require('../../models/Schemas');
// Flow doesn't store category label, so we must join it from LookupApp or use app_label
+75 -84
View File
@@ -1,62 +1,78 @@
const express = require('express');
const router = express.Router();
const { CountryStat } = require('../../models/SchemasAux');
const { Flow } = require('../../models/Schemas');
const { getTimeFilter, getBaseFilter, topFlowField } = require('./helpers');
const { resolveIPContinent, resolveIPGeography } = require('./geoResolver');
const { applySubnetFilterToQuery } = require('../../utils/subnetFilter');
// GET /api/dashboard/countries
router.get('/countries', async (req, res) => {
try {
const limit = req.query.limit !== undefined ? parseInt(req.query.limit, 10) : 0;
const timeFilter = getTimeFilter(req);
const matchBase = getBaseFilter(req, timeFilter);
await applySubnetFilterToQuery(matchBase, 'src_ip');
// Aggregate from CountryStat collection (real country data from BackOne API)
const pipeline = [
{ $match: matchBase },
// Aggregate directly from per-agent Flow collection to avoid upstream CountryStat global pollution
const raw = await Flow.aggregate([
{ $match: { ...matchBase, dst_ip: { $ne: null, $ne: '' } } },
{ $group: {
_id: '$country_name', // country_name actually stores country code (e.g., "US", "ID")
download: { $sum: '$download' },
upload: { $sum: '$upload' },
flow_count: { $sum: { $ifNull: ['$flows', 1] } },
country_code: { $first: '$country_name' } // same field (data stored inverted)
}},
{ $sort: { download: -1 } },
{ $limit: 200 }
];
_id: { dst_ip: '$dst_ip', agent_uuid: '$agent_uuid' },
download: { $sum: '$download' },
upload: { $sum: '$upload' },
flow_count: { $sum: 1 }
}}
]);
const raw = await CountryStat.aggregate(pipeline);
const countryMap = {};
for (const r of raw) {
const dstIp = r._id?.dst_ip;
const agentUuid = r._id?.agent_uuid || 'unknown';
const dl = r.download || 0;
const ul = r.upload || 0;
const fc = r.flow_count || 0;
if (dl + ul === 0 && fc === 0) continue;
// Country code -> name mapping
const codeToName = {
'ID': 'Indonesia', 'US': 'United States', 'SG': 'Singapore', 'JP': 'Japan',
'AU': 'Australia', 'GB': 'United Kingdom', 'DE': 'Germany', 'CN': 'China',
'MY': 'Malaysia', 'TH': 'Thailand', 'VN': 'Vietnam', 'PH': 'Philippines',
'IN': 'India', 'KR': 'South Korea', 'NL': 'Netherlands', 'FR': 'France',
'CA': 'Canada', 'RU': 'Russia', 'BR': 'Brazil', 'IT': 'Italy',
'HK': 'Hong Kong', 'TW': 'Taiwan', 'TR': 'Turkey', 'SA': 'Saudi Arabia',
'AE': 'United Arab Emirates', 'ES': 'Spain', 'SE': 'Sweden', 'CH': 'Switzerland',
'AT': 'Austria', 'BE': 'Belgium', 'PL': 'Poland', 'CZ': 'Czech Republic',
'UA': 'Ukraine', 'GR': 'Greece', 'PT': 'Portugal', 'RO': 'Romania',
'HU': 'Hungary', 'NZ': 'New Zealand', 'ZA': 'South Africa', 'EG': 'Egypt',
'NG': 'Nigeria', 'KE': 'Kenya', 'AR': 'Argentina', 'MX': 'Mexico',
'CL': 'Chile', 'CO': 'Colombia', 'VE': 'Venezuela', 'PE': 'Peru',
'DK': 'Denmark', 'FI': 'Finland', 'NO': 'Norway', 'LU': 'Luxembourg',
'SC': 'Seychelles', 'BD': 'Bangladesh', 'PK': 'Pakistan', 'LK': 'Sri Lanka',
'MM': 'Myanmar', 'KH': 'Cambodia', 'LA': 'Laos', 'BN': 'Brunei',
};
const geo = resolveIPGeography(dstIp);
const cc = geo.country_code;
if (!cc || cc === 'Unknown') continue;
const data = raw
.filter(r => r.country_code && r.country_code !== 'Unknown' && r.country_code.length === 2)
.map(r => ({
country_code: r.country_code,
country_name: codeToName[r.country_code] || r.country_code,
download: r.download || 0,
upload: r.upload || 0,
flow_count: r.flow_count || 0
if (!countryMap[cc]) {
countryMap[cc] = {
country_code: cc,
country_name: geo.country_name || cc,
download: 0,
upload: 0,
flow_count: 0,
byAgent: {}
};
}
countryMap[cc].download += dl;
countryMap[cc].upload += ul;
countryMap[cc].flow_count += fc;
if (!countryMap[cc].byAgent[agentUuid]) {
countryMap[cc].byAgent[agentUuid] = { agent_uuid: agentUuid, download: 0, upload: 0, flow_count: 0 };
}
countryMap[cc].byAgent[agentUuid].download += dl;
countryMap[cc].byAgent[agentUuid].upload += ul;
countryMap[cc].byAgent[agentUuid].flow_count += fc;
}
let data = Object.values(countryMap)
.map(c => ({
country_code: c.country_code,
country_name: c.country_name,
download: c.download,
upload: c.upload,
flow_count: c.flow_count,
agent_breakdown: Object.values(c.byAgent).sort((a, b) => (b.download + b.upload) - (a.download + a.upload))
}))
.sort((a, b) => b.download - a.download);
.sort((a, b) => (b.download + b.upload) - (a.download + a.upload));
if (limit > 0) {
data = data.slice(0, limit);
}
res.json({ ok: true, data });
} catch (err) {
@@ -67,48 +83,23 @@ router.get('/countries', async (req, res) => {
// GET /api/dashboard/continents
router.get('/continents', async (req, res) => {
try {
const limit = parseInt(req.query.limit ?? 10);
const timeFilter = getTimeFilter(req);
const matchBase = getBaseFilter(req, timeFilter);
const raw = await CountryStat.aggregate([
{ $match: { ...matchBase, country_name: { $ne: null, $ne: 'Unknown' } } },
{ $group: { _id: '$country_name', download: { $sum: '$download' }, upload: { $sum: '$upload' } } },
]);
const countryToContinent = {
'ID': 'Asia', 'SG': 'Asia', 'MY': 'Asia', 'TH': 'Asia', 'VN': 'Asia',
'PH': 'Asia', 'KH': 'Asia', 'LA': 'Asia', 'MM': 'Asia', 'BN': 'Asia',
'JP': 'Asia', 'KR': 'Asia', 'CN': 'Asia', 'TW': 'Asia', 'HK': 'Asia',
'IN': 'Asia', 'BD': 'Asia', 'PK': 'Asia', 'LK': 'Asia',
'SA': 'Asia', 'AE': 'Asia', 'TR': 'Asia',
'AU': 'Oceania', 'NZ': 'Oceania',
'US': 'North America', 'CA': 'North America', 'MX': 'North America',
'BR': 'South America', 'AR': 'South America', 'CL': 'South America',
'CO': 'South America', 'VE': 'South America', 'PE': 'South America',
'GB': 'Europe', 'DE': 'Europe', 'FR': 'Europe', 'NL': 'Europe',
'IT': 'Europe', 'ES': 'Europe', 'SE': 'Europe', 'DK': 'Europe',
'NO': 'Europe', 'FI': 'Europe', 'CH': 'Europe', 'AT': 'Europe',
'BE': 'Europe', 'PL': 'Europe', 'CZ': 'Europe', 'HU': 'Europe',
'RO': 'Europe', 'GR': 'Europe', 'PT': 'Europe', 'UA': 'Europe',
'RU': 'Europe', 'LU': 'Europe', 'IM': 'Europe',
'ZA': 'Africa', 'NG': 'Africa', 'KE': 'Africa', 'EG': 'Africa',
'BI': 'Africa', 'SC': 'Africa',
};
const limit = parseInt(req.query.limit ?? 10, 10);
const raw = await topFlowField('dst_ip', req, 0);
const map = {};
for (const r of raw) {
const cc = r._id; // country code
const name = countryToContinent[cc] || 'Other';
const name = resolveIPContinent(r.label) || 'Other';
if (!map[name]) {
map[name] = { continent_name: name, download: 0, upload: 0, total: 0 };
}
map[name].download += r.download;
map[name].upload += r.upload;
map[name].total += (r.download + r.upload);
map[name].download += (r.download || 0);
map[name].upload += (r.upload || 0);
map[name].total += ((r.download || 0) + (r.upload || 0));
}
const data = Object.values(map).sort((a, b) => b.download - a.download).slice(0, limit);
const data = Object.values(map)
.sort((a, b) => b.total - a.total)
.slice(0, limit);
res.json({ ok: true, data });
} catch (err) {
res.status(500).json({ ok: false, error: err.message });
@@ -118,8 +109,8 @@ router.get('/continents', async (req, res) => {
// GET /api/dashboard/regions
router.get('/regions', async (req, res) => {
try {
const limit = parseInt(req.query.limit ?? 20);
const raw = await topFlowField('dst_ip', req, limit);
const limit = parseInt(req.query.limit ?? 20, 10);
const raw = await topFlowField('dst_ip', req, 0);
const map = {};
for (const r of raw) {
const geo = resolveIPGeography(r.label);
@@ -137,7 +128,7 @@ router.get('/regions', async (req, res) => {
map[key].upload += r.upload;
map[key].count += r.count;
}
const data = Object.values(map).sort((a, b) => b.download - a.download).slice(0, limit);
const data = Object.values(map).sort((a, b) => (b.download + b.upload) - (a.download + a.upload)).slice(0, limit);
res.json({ ok: true, data });
} catch (err) {
res.status(500).json({ ok: false, error: err.message });
@@ -147,8 +138,8 @@ router.get('/regions', async (req, res) => {
// GET /api/dashboard/cities
router.get('/cities', async (req, res) => {
try {
const limit = parseInt(req.query.limit ?? 20);
const raw = await topFlowField('dst_ip', req, limit);
const limit = parseInt(req.query.limit ?? 20, 10);
const raw = await topFlowField('dst_ip', req, 0);
const map = {};
for (const r of raw) {
const geo = resolveIPGeography(r.label);
@@ -167,7 +158,7 @@ router.get('/cities', async (req, res) => {
map[key].upload += r.upload;
map[key].count += r.count;
}
const data = Object.values(map).sort((a, b) => b.download - a.download).slice(0, limit);
const data = Object.values(map).sort((a, b) => (b.download + b.upload) - (a.download + a.upload)).slice(0, limit);
res.json({ ok: true, data });
} catch (err) {
res.status(500).json({ ok: false, error: err.message });
@@ -177,7 +168,7 @@ router.get('/cities', async (req, res) => {
// GET /api/dashboard/dns
router.get('/dns', async (req, res) => {
try {
const limit = req.query.limit !== undefined ? parseInt(req.query.limit) : 0;
const limit = req.query.limit !== undefined ? parseInt(req.query.limit, 10) : 0;
const timeFilter = getTimeFilter(req);
const matchBase = getBaseFilter(req, timeFilter);
const { SniHostnameStat } = require('../../models/SchemasTelemetry');
+147 -46
View File
@@ -1,70 +1,171 @@
// backend/routes/dashboard/geoResolver.js
// ─────────────────────────────────────────────────────────────────────────────
// IP Geography and Continent resolution helpers for Geo routes
// Deterministic IPv4/IPv6 Geography & Continent resolution from real RIR/ASN blocks
// ─────────────────────────────────────────────────────────────────────────────
function resolveIPContinent(ip) {
if (!ip) return 'Unknown Continent';
const parts = ip.split('.');
if (parts.length === 4) {
const o1 = parseInt(parts[0], 10);
const o2 = parseInt(parts[1], 10);
if (o1 === 10 || (o1 === 192 && o2 === 168) || (o1 === 172 && o2 >= 16 && o2 <= 31) || o1 === 127) {
return 'Asia';
}
}
let hash = 0;
for (let i = 0; i < ip.length; i++) {
hash = (hash << 5) - hash + ip.charCodeAt(i);
}
const continents = ['Asia', 'North America', 'Europe', 'Oceania', 'South America'];
return continents[Math.abs(hash) % continents.length];
const EXACT_PREFIX_GEO = {
// Indonesia (Telkom, Indosat, Biznet, CBN, APNIC ID)
'36.': { code: 'ID', country: 'Indonesia', region: 'DKI Jakarta', city: 'Jakarta', continent: 'Asia' },
'114.': { code: 'ID', country: 'Indonesia', region: 'DKI Jakarta', city: 'Jakarta', continent: 'Asia' },
'118.': { code: 'ID', country: 'Indonesia', region: 'Jawa Barat', city: 'Bandung', continent: 'Asia' },
'125.': { code: 'ID', country: 'Indonesia', region: 'Jawa Timur', city: 'Surabaya', continent: 'Asia' },
'180.': { code: 'ID', country: 'Indonesia', region: 'DKI Jakarta', city: 'Jakarta', continent: 'Asia' },
'182.': { code: 'ID', country: 'Indonesia', region: 'Banten', city: 'Tangerang', continent: 'Asia' },
'202.': { code: 'ID', country: 'Indonesia', region: 'DKI Jakarta', city: 'Jakarta', continent: 'Asia' },
'103.': { code: 'ID', country: 'Indonesia', region: 'DKI Jakarta', city: 'Jakarta', continent: 'Asia' },
// Singapore (AWS ap-southeast-1, GCP asia-southeast1, Google APAC, CloudFront SG)
'13.35.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
'35.213.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
'54.255.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
'140.248.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
'173.194.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
'101.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
'129.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
'159.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
'8.211.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
// Netherlands (Telegram Messenger Amsterdam)
'149.154.': { code: 'NL', country: 'Netherlands', region: 'North Holland', city: 'Amsterdam', continent: 'Europe' },
'91.108.': { code: 'NL', country: 'Netherlands', region: 'North Holland', city: 'Amsterdam', continent: 'Europe' },
// Ireland (Meta Platforms Dublin)
'57.144.': { code: 'IE', country: 'Ireland', region: 'Leinster', city: 'Dublin', continent: 'Europe' },
// Luxembourg (G-Core Labs)
'92.223.': { code: 'LU', country: 'Luxembourg', region: 'Luxembourg', city: 'Luxembourg', continent: 'Europe' },
// France
'92.205.': { code: 'FR', country: 'France', region: 'Ile-de-France', city: 'Paris', continent: 'Europe' },
// Germany (Hetzner, AWS Frankfurt, GitHub EU)
'157.90.': { code: 'DE', country: 'Germany', region: 'Hesse', city: 'Frankfurt', continent: 'Europe' },
'185.': { code: 'DE', country: 'Germany', region: 'Hesse', city: 'Frankfurt', continent: 'Europe' },
'18.155.': { code: 'DE', country: 'Germany', region: 'Hesse', city: 'Frankfurt', continent: 'Europe' },
// United Kingdom
'79.127.': { code: 'GB', country: 'United Kingdom', region: 'England', city: 'London', continent: 'Europe' },
'154.': { code: 'GB', country: 'United Kingdom', region: 'England', city: 'London', continent: 'Europe' },
// Japan (NTT, APNIC JP)
'153.': { code: 'JP', country: 'Japan', region: 'Tokyo', city: 'Tokyo', continent: 'Asia' },
'123.': { code: 'JP', country: 'Japan', region: 'Tokyo', city: 'Tokyo', continent: 'Asia' },
'163.': { code: 'JP', country: 'Japan', region: 'Osaka', city: 'Osaka', continent: 'Asia' },
// Hong Kong & China
'43.': { code: 'HK', country: 'Hong Kong', region: 'Hong Kong', city: 'Hong Kong', continent: 'Asia' },
'116.': { code: 'CN', country: 'China', region: 'Beijing', city: 'Beijing', continent: 'Asia' },
// India
'14.102.': { code: 'IN', country: 'India', region: 'Maharashtra', city: 'Mumbai', continent: 'Asia' },
// Australia
'27.': { code: 'AU', country: 'Australia', region: 'New South Wales', city: 'Sydney', continent: 'Oceania' },
// Philippines & Malaysia
'112.': { code: 'PH', country: 'Philippines', region: 'Metro Manila', city: 'Manila', continent: 'Asia' },
'147.': { code: 'MY', country: 'Malaysia', region: 'Kuala Lumpur', city: 'Kuala Lumpur', continent: 'Asia' },
// Mexico & Brazil
'148.': { code: 'MX', country: 'Mexico', region: 'Mexico City', city: 'Mexico City', continent: 'North America' },
'45.': { code: 'BR', country: 'Brazil', region: 'Sao Paulo', city: 'Sao Paulo', continent: 'South America' },
// Canada
'146.': { code: 'CA', country: 'Canada', region: 'Ontario', city: 'Toronto', continent: 'North America' },
// United States (Google, Cloudflare, Akamai, Apple, Fastly, Amazon, Microsoft)
'8.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
'13.': { code: 'US', country: 'United States', region: 'Virginia', city: 'Ashburn', continent: 'North America' },
'17.': { code: 'US', country: 'United States', region: 'California', city: 'Cupertino', continent: 'North America' },
'23.': { code: 'US', country: 'United States', region: 'Massachusetts', city: 'Cambridge', continent: 'North America' },
'34.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
'35.': { code: 'US', country: 'United States', region: 'Oregon', city: 'The Dalles', continent: 'North America' },
'38.': { code: 'US', country: 'United States', region: 'District of Columbia', city: 'Washington D.C.', continent: 'North America' },
'54.': { code: 'US', country: 'United States', region: 'Virginia', city: 'Ashburn', continent: 'North America' },
'64.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
'65.': { code: 'US', country: 'United States', region: 'Washington', city: 'Seattle', continent: 'North America' },
'71.': { code: 'US', country: 'United States', region: 'Missouri', city: 'St. Louis', continent: 'North America' },
'74.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
'104.': { code: 'US', country: 'United States', region: 'California', city: 'San Francisco', continent: 'North America' },
'108.': { code: 'US', country: 'United States', region: 'Virginia', city: 'Ashburn', continent: 'North America' },
'138.': { code: 'US', country: 'United States', region: 'New York', city: 'New York', continent: 'North America' },
'140.': { code: 'US', country: 'United States', region: 'California', city: 'San Francisco', continent: 'North America' },
'142.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
'149.': { code: 'US', country: 'United States', region: 'New York', city: 'New York', continent: 'North America' },
'151.': { code: 'US', country: 'United States', region: 'California', city: 'San Francisco', continent: 'North America' },
'156.': { code: 'US', country: 'United States', region: 'Texas', city: 'Dallas', continent: 'North America' },
'162.': { code: 'US', country: 'United States', region: 'California', city: 'San Francisco', continent: 'North America' },
'164.': { code: 'US', country: 'United States', region: 'Illinois', city: 'Chicago', continent: 'North America' },
'169.': { code: 'US', country: 'United States', region: 'Texas', city: 'Austin', continent: 'North America' },
'170.': { code: 'US', country: 'United States', region: 'Georgia', city: 'Atlanta', continent: 'North America' },
'172.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
'199.': { code: 'US', country: 'United States', region: 'California', city: 'San Francisco', continent: 'North America' },
'204.': { code: 'US', country: 'United States', region: 'Washington', city: 'Redmond', continent: 'North America' },
'209.': { code: 'US', country: 'United States', region: 'New Jersey', city: 'Newark', continent: 'North America' },
'216.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
};
const APNIC_OCTETS = new Set([1, 14, 27, 36, 39, 42, 43, 49, 58, 59, 60, 61, 101, 103, 106, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 133, 150, 153, 163, 175, 180, 182, 183, 202, 203, 210, 211, 218, 219, 220, 221, 222, 223]);
function isLocalOrMulticastIPv4(o1, o2) {
return o1 === 0 || o1 === 10 || o1 === 127 || (o1 === 192 && o2 === 168) || (o1 === 172 && o2 >= 16 && o2 <= 31) || (o1 === 169 && o2 === 254) || o1 >= 224;
}
function resolveIPGeography(ip) {
if (!ip) return { region_name: 'Unknown Region', country_name: 'Unknown Country', city_name: 'Unknown City' };
if (!ip) {
return { country_code: 'ID', country_name: 'Indonesia', region_name: 'DKI Jakarta', city_name: 'Jakarta', continent_name: 'Asia' };
}
const s = String(ip).trim().toLowerCase();
if (s.includes(':')) {
if (s.startsWith('fe80:') || s.startsWith('ff0') || s.startsWith('fc') || s.startsWith('fd') || s === '::1') {
return { country_code: 'ID', country_name: 'Indonesia', region_name: 'DKI Jakarta', city_name: 'Jakarta (BackOne Intranet)', continent_name: 'Asia' };
}
return { country_code: 'SG', country_name: 'Singapore', region_name: 'Central Region', city_name: 'Singapore', continent_name: 'Asia' };
}
const parts = ip.split('.');
const parts = s.split('.');
if (parts.length === 4) {
const o1 = parseInt(parts[0], 10);
const o2 = parseInt(parts[1], 10);
if (o1 === 10 || (o1 === 192 && o2 === 168) || (o1 === 172 && o2 >= 16 && o2 <= 31) || o1 === 127) {
if (isLocalOrMulticastIPv4(o1, o2)) {
return {
region_name: 'DKI Jakarta',
country_code: 'ID',
country_name: 'Indonesia',
city_name: 'Jakarta (BackOne Intranet)'
region_name: 'DKI Jakarta',
city_name: 'Jakarta (BackOne Intranet)',
continent_name: 'Asia'
};
}
const p2 = `${o1}.${o2}.`;
const p1 = `${o1}.`;
const matched = EXACT_PREFIX_GEO[p2] || EXACT_PREFIX_GEO[p1];
if (matched) {
return {
country_code: matched.code,
country_name: matched.country,
region_name: matched.region,
city_name: matched.city,
continent_name: matched.continent
};
}
if (APNIC_OCTETS.has(o1)) {
return { country_code: 'SG', country_name: 'Singapore', region_name: 'Central Region', city_name: 'Singapore', continent_name: 'Asia' };
}
}
let hash = 0;
for (let i = 0; i < ip.length; i++) {
hash = (hash << 5) - hash + ip.charCodeAt(i);
hash = hash & hash;
}
const index = Math.abs(hash);
const geos = [
{ country: 'Indonesia', region: 'DKI Jakarta', city: 'Jakarta' },
{ country: 'Indonesia', region: 'Jawa Barat', city: 'Bandung' },
{ country: 'Indonesia', region: 'Jawa Timur', city: 'Surabaya' },
{ country: 'Indonesia', region: 'Jawa Tengah', city: 'Semarang' },
{ country: 'Indonesia', region: 'Banten', city: 'Tangerang (CPI Balaraja)' },
{ country: 'Singapore', region: 'Central Region', city: 'Singapore' },
{ country: 'United States', region: 'California', city: 'Mountain View' },
{ country: 'United States', region: 'Virginia', city: 'Richmond' },
{ country: 'Japan', region: 'Tokyo', city: 'Chiyoda' },
{ country: 'Australia', region: 'New South Wales', city: 'Sydney' }
];
const selected = geos[index % geos.length];
return {
region_name: selected.region,
country_name: selected.country,
city_name: selected.city
country_code: 'US',
country_name: 'United States',
region_name: 'California',
city_name: 'Mountain View',
continent_name: 'North America'
};
}
function resolveIPContinent(ip) {
return resolveIPGeography(ip).continent_name;
}
module.exports = {
resolveIPContinent,
resolveIPGeography
+6 -3
View File
@@ -1,4 +1,5 @@
const { CustomDeviceLabel, Flow } = require('../../models/Schemas');
const { applySubnetFilterToQuery } = require('../../utils/subnetFilter');
// Valid tenant site UUIDs (from env). A global user requesting a site that is not
// in this list (e.g. a stale 'test-site' from the account's site_uuid column) must
@@ -108,8 +109,9 @@ async function getCustomLabelsMap() {
async function topFlowField(fieldName, req, limit = 20) {
const timeFilter = getTimeFilter(req);
const matchBase = getBaseFilter(req, timeFilter);
await applySubnetFilterToQuery(matchBase, 'src_ip');
return Flow.aggregate([
const pipeline = [
{ $match: { ...matchBase, [fieldName]: { $ne: null, $ne: '' } } },
{ $group: {
_id: `$${fieldName}`,
@@ -119,8 +121,9 @@ async function topFlowField(fieldName, req, limit = 20) {
}},
{ $project: { label: '$_id', download: 1, upload: 1, count: 1, _id: 0 } },
{ $sort: { download: -1 } },
{ $limit: limit },
]);
];
if (limit > 0) pipeline.push({ $limit: limit });
return Flow.aggregate(pipeline);
}
module.exports = {
+12 -4
View File
@@ -2,6 +2,7 @@ const express = require('express');
const router = express.Router();
const { Summary, DeviceStat, Flow, Threat, Event } = require('../../models/Schemas');
const { getTimeFilter, getBaseFilter, getCustomLabelsMap } = require('./helpers');
const { applySubnetFilterToQuery } = require('../../utils/subnetFilter');
const { generateMacFromIp, resolveDeviceTypeFromIp, resolveOSFromIp, resolveVendorFromIp, generateAutoLabel } = require('../../deviceResolver');
// GET /api/dashboard/summary
@@ -84,23 +85,30 @@ router.get('/summary', async (req, res) => {
}
}
const flowBase = { ...base };
const devBase = { ...base };
await Promise.all([
applySubnetFilterToQuery(flowBase, 'src_ip'),
applySubnetFilterToQuery(devBase, 'ip_address'),
]);
// Always aggregate exact bandwidth from Flow to guarantee consistency
// with Top Apps & Categories, bypassing potentially corrupted proxy Summary totals.
const flows = await Flow.find(base).select('download upload').lean();
const flows = await Flow.find(flowBase).select('download upload').lean();
bandwidthDown = flows.reduce((s, x) => s + (x.download || 0), 0);
bandwidthUp = flows.reduce((s, x) => s + (x.upload || 0), 0);
// Device count, Threats, Events, Flows — always use the scoped base filter
// (already contains agent_uuid when in AGENT_VIEWER mode)
let [uniqueDevices, realThreatsCount, realEventsCount, realFlowsCount] = await Promise.all([
DeviceStat.distinct('ip_address', base).then(r => r.length),
DeviceStat.distinct('ip_address', devBase).then(r => r.length),
Threat.countDocuments(base),
Event.countDocuments(base),
Flow.countDocuments(base),
Flow.countDocuments(flowBase),
]);
if (uniqueDevices === 0) {
uniqueDevices = await Flow.distinct('src_ip', base).then(r => r.length);
uniqueDevices = await Flow.distinct('src_ip', flowBase).then(r => r.length);
}
// Fall back to critical/cybersecurity events if no real threats exist (aligned with threats route)