From 1a2b58c7071f039acf21df1bf035e777bc3dd532 Mon Sep 17 00:00:00 2001 From: Yoga Pratama Date: Wed, 11 Nov 2026 11:45:28 +0700 Subject: [PATCH] fix(categories): resolve category dictionary mapping and enrich Traffic Categories breakdown --- backend/routes/agentDetailsHandler.js | 18 +- backend/routes/categoryDetail.js | 56 ++++++- backend/routes/dashboard/apps.js | 52 +++--- backend/utils/categoryDictionary.js | 232 ++++++++++++++++++++++++++ proxy/backoneClientStats.js | 14 +- 5 files changed, 338 insertions(+), 34 deletions(-) create mode 100644 backend/utils/categoryDictionary.js diff --git a/backend/routes/agentDetailsHandler.js b/backend/routes/agentDetailsHandler.js index 9cdf633..c2df1b9 100644 --- a/backend/routes/agentDetailsHandler.js +++ b/backend/routes/agentDetailsHandler.js @@ -66,14 +66,20 @@ module.exports = async function agentDetailsHandler(req, res, helpers) { ]); // 1d. Enrich apps with category and favicon from LookupApp + const { resolveAppCategory } = require('../utils/categoryDictionary'); const appLabels = rawAppsFromFlow.map(a => a._id); - const lookups = await LookupApp.find({ label: { $in: appLabels } }).lean(); + const lookups = await LookupApp.find({ + $or: [ + { tag: { $in: appLabels } }, + { label: { $in: appLabels } } + ] + }).lean(); const lookupMap = {}; for (const app of lookups) { - lookupMap[app.label] = { - favicon: app.favicon || app.logo || null, - category: app.application_category?.label || 'Web' - }; + const fav = app.favicon || app.logo || null; + const cat = app.application_category?.label || app.category?.label || null; + if (app.tag) lookupMap[app.tag] = { favicon: fav, category: cat }; + if (app.label) lookupMap[app.label] = { favicon: fav, category: cat }; } // 2. Map devices from Flow aggregation (already unique by src_ip) @@ -127,7 +133,7 @@ module.exports = async function agentDetailsHandler(req, res, helpers) { const top_apps = rawAppsFromFlow.map((a, index) => ({ app_id: index + 1, app_label: a._id, - category: lookupMap[a._id]?.category || 'Web', + category: lookupMap[a._id]?.category || resolveAppCategory(a._id, lookupMap), favicon: lookupMap[a._id]?.favicon || null, download: a.download || 0, upload: a.upload || 0, diff --git a/backend/routes/categoryDetail.js b/backend/routes/categoryDetail.js index 0366352..d9bf661 100644 --- a/backend/routes/categoryDetail.js +++ b/backend/routes/categoryDetail.js @@ -23,17 +23,45 @@ router.get('/', async (req, res) => { const base = buildBaseFilter(req); try { // Lookup all application labels that belong to this category - const appsInCategory = await LookupApp.find({ 'application_category.label': category }).lean(); - const appLabels = appsInCategory.map(app => app.label); + const appsInCategory = await LookupApp.find({ + $or: [ + { 'application_category.label': category }, + { 'category.label': category } + ] + }).lean(); + + const appLabels = new Set(); + for (const app of appsInCategory) { + if (app.tag) { + appLabels.add(app.tag); + appLabels.add(app.tag.replace(/^netify\./i, 'backone.')); + appLabels.add(app.tag.replace(/^netify\./i, 'siab.')); + appLabels.add(app.tag.replace(/^netify\./i, '')); + } + if (app.label) { + appLabels.add(app.label); + } + } - if (appLabels.length === 0) { + const { APP_CATEGORY_MAP } = require('../utils/categoryDictionary'); + for (const [appKey, catName] of Object.entries(APP_CATEGORY_MAP)) { + if (catName.toLowerCase() === category.toLowerCase()) { + appLabels.add(appKey); + appLabels.add(`netify.${appKey}`); + appLabels.add(`backone.${appKey}`); + appLabels.add(`siab.${appKey}`); + } + } + + const labelList = Array.from(appLabels); + if (labelList.length === 0) { return res.json({ ok: true, category, apps: [], devices: [] }); } - const filter = { ...base, app_label: { $in: appLabels } }; + const filter = { ...base, app_label: { $in: labelList } }; // 1. Get Top Apps for this category - const topAppsData = await DeviceAppStat.aggregate([ + let topAppsData = await DeviceAppStat.aggregate([ { $match: filter }, { $group: { _id: '$app_label', @@ -47,6 +75,24 @@ router.get('/', async (req, res) => { { $limit: 200 } ]); + // Fallback to Flow if DeviceAppStat is empty + if (topAppsData.length === 0) { + const { Flow } = require('../models/Schemas'); + topAppsData = await Flow.aggregate([ + { $match: filter }, + { $group: { + _id: '$app_label', + download: { $sum: '$download' }, + upload: { $sum: '$upload' }, + flows: { $sum: 1 }, + agent_uuid: { $first: '$agent_uuid' }, + last_seen: { $max: '$timestamp' } + }}, + { $sort: { download: -1 } }, + { $limit: 200 } + ]); + } + const apps = topAppsData.map(r => ({ app_label: r._id, download: r.download, diff --git a/backend/routes/dashboard/apps.js b/backend/routes/dashboard/apps.js index 64552d3..20d3e62 100644 --- a/backend/routes/dashboard/apps.js +++ b/backend/routes/dashboard/apps.js @@ -4,6 +4,7 @@ const mongoose = require('mongoose'); const { AppStat, ProtocolStat, AppCategoryStat, LookupApp } = require('../../models/Schemas'); const { getTimeFilter, getBaseFilter } = require('./helpers'); const { applySubnetFilterToQuery } = require('../../utils/subnetFilter'); +const { resolveAppCategory } = require('../../utils/categoryDictionary'); // GET /api/dashboard/apps router.get('/apps', async (req, res) => { @@ -31,24 +32,32 @@ router.get('/apps', async (req, res) => { // Fetch lookup metadata (category and favicon) to enrich apps list const labels = result.map(r => r._id); - const lookups = await LookupApp.find({ label: { $in: labels } }).lean(); + const lookups = await LookupApp.find({ + $or: [ + { tag: { $in: labels } }, + { label: { $in: labels } } + ] + }).lean(); const lookupMap = {}; for (const app of lookups) { - lookupMap[app.label] = { - favicon: app.favicon || app.logo || null, - category: app.application_category?.label || null - }; + const fav = app.favicon || app.logo || null; + const cat = app.application_category?.label || app.category?.label || null; + if (app.tag) lookupMap[app.tag] = { favicon: fav, category: cat }; + if (app.label) lookupMap[app.label] = { favicon: fav, category: cat }; } - const formatted = result.map(r => ({ - app_label: r._id, - download: r.download || 0, - upload: r.upload || 0, - total_bytes: r.total_bytes || 0, - flows: r.flows || 0, - category: lookupMap[r._id]?.category || null, - favicon: lookupMap[r._id]?.favicon || null, - })); + const formatted = result.map(r => { + const resolvedCat = lookupMap[r._id]?.category || resolveAppCategory(r._id, lookupMap); + return { + app_label: r._id, + download: r.download || 0, + upload: r.upload || 0, + total_bytes: r.total_bytes || 0, + flows: r.flows || 0, + category: resolvedCat, + favicon: lookupMap[r._id]?.favicon || null, + }; + }); res.json({ ok: true, data: formatted }); } catch (err) { @@ -111,18 +120,23 @@ router.get('/app-categories', async (req, res) => { // Enrich with categories const labels = appResult.map(r => r._id); - const lookups = await LookupApp.find({ label: { $in: labels } }).lean(); + const lookups = await LookupApp.find({ + $or: [ + { tag: { $in: labels } }, + { label: { $in: labels } } + ] + }).lean(); const lookupMap = {}; for (const app of lookups) { - if (app.application_category?.label) { - lookupMap[app.label] = app.application_category.label; - } + const cat = app.application_category?.label || app.category?.label || null; + if (app.tag && cat) lookupMap[app.tag] = cat; + if (app.label && cat) lookupMap[app.label] = cat; } // Group by category const catMap = {}; for (const r of appResult) { - const cat = lookupMap[r._id] || 'Uncategorized'; + const cat = resolveAppCategory(r._id, lookupMap); if (!catMap[cat]) catMap[cat] = { download: 0, upload: 0, flows: 0 }; catMap[cat].download += r.download || 0; catMap[cat].upload += r.upload || 0; diff --git a/backend/utils/categoryDictionary.js b/backend/utils/categoryDictionary.js new file mode 100644 index 0000000..ade3881 --- /dev/null +++ b/backend/utils/categoryDictionary.js @@ -0,0 +1,232 @@ +// backend/utils/categoryDictionary.js +// ───────────────────────────────────────────────────────────────────────────── +// Category Dictionary & Resolvers for BackOne DPI +// Maps category IDs, tags, and app labels to human-readable categories. +// ───────────────────────────────────────────────────────────────────────────── + +const CATEGORY_ID_MAP = { + '1': 'Unclassified', + '2': 'Adult', + '3': 'Advertiser', + '4': 'Arts and Entertainment', + '5': 'Business', + '6': 'Career and Education', + '9': 'Financial', + '10': 'File Sharing', + '11': 'Gambling', + '12': 'Games', + '13': 'Government', + '15': 'Mail', + '16': 'Malware', + '17': 'Messaging', + '18': 'News', + '19': 'Portal', + '20': 'Recreation', + '21': 'Reference', + '23': 'Shopping', + '24': 'Social Media', + '26': 'Sports', + '27': 'Technology', + '28': 'VPN and Proxy', + '29': 'Streaming Media', + '30': 'Cybersecurity', + '31': 'OS/Software Updates', + '32': 'VoIP/Conferencing', + '33': 'Device/IoT', + '34': 'Remote Desktop', + '35': 'CDN', + '36': 'Hosting', + '37': 'ISP/Telco', + '40': 'Identified' +}; + +const APP_CATEGORY_MAP = { + // Streaming Media + 'youtube': 'Streaming Media', + 'netflix': 'Streaming Media', + 'spotify': 'Streaming Media', + 'twitch': 'Streaming Media', + 'disney': 'Streaming Media', + 'disney-plus': 'Streaming Media', + 'googlevideo': 'Streaming Media', + 'vimeo': 'Streaming Media', + 'soundcloud': 'Streaming Media', + 'tiktokv': 'Streaming Media', + 'bigo': 'Streaming Media', + 'vidio': 'Streaming Media', + + // Social Media + 'facebook': 'Social Media', + 'instagram': 'Social Media', + 'tiktok': 'Social Media', + 'twitter': 'Social Media', + 'x': 'Social Media', + 'threads': 'Social Media', + 'reddit': 'Social Media', + 'pinterest': 'Social Media', + 'linkedin': 'Social Media', + 'snapchat': 'Social Media', + + // Messaging + 'whatsapp': 'Messaging', + 'telegram': 'Messaging', + 'discord': 'Messaging', + 'slack': 'Messaging', + 'signal': 'Messaging', + 'signalr': 'Messaging', + 'line': 'Messaging', + 'wechat': 'Messaging', + 'messenger': 'Messaging', + + // VoIP / Conferencing + 'zoom': 'VoIP/Conferencing', + 'teams': 'VoIP/Conferencing', + 'skype': 'VoIP/Conferencing', + 'webex': 'VoIP/Conferencing', + 'google-meet': 'VoIP/Conferencing', + + // Technology + 'google': 'Technology', + 'google-drive': 'Technology', + 'google-ads': 'Technology', + 'google-play': 'Technology', + 'microsoft': 'Technology', + 'apple': 'Technology', + 'apple-updates': 'Technology', + 'bytedance': 'Technology', + 'yahoo': 'Technology', + 'bing': 'Technology', + + // CDN + 'cloudflare': 'CDN', + 'akamai': 'CDN', + 'fastly': 'CDN', + 'cdn77': 'CDN', + 'edgecast': 'CDN', + + // Hosting / Cloud + 'amazon': 'Hosting', + 'amazon-aws': 'Hosting', + 'amazon-s3': 'Hosting', + 'amazon-cloudfront': 'CDN', + 'aws': 'Hosting', + 'azure': 'Hosting', + 'digitalocean': 'Hosting', + + // Games + 'steam': 'Games', + 'epic': 'Games', + 'roblox': 'Games', + 'riot': 'Games', + 'riot-games': 'Games', + 'valorant': 'Games', + 'garena': 'Games', + 'genshin': 'Games', + 'moonton': 'Games', + 'netease': 'Games', + 'netease-games': 'Games', + 'unity': 'Games', + 'playstation': 'Games', + 'xbox': 'Games', + + // Shopping + 'shopee': 'Shopping', + 'shopeepay': 'Shopping', + 'tokopedia': 'Shopping', + 'lazada': 'Shopping', + 'bukalapak': 'Shopping', + 'blibli': 'Shopping', + 'ebay': 'Shopping', + 'alibaba': 'Shopping', + 'aliexpress': 'Shopping', + + // Financial + 'bca': 'Financial', + 'mandiri': 'Financial', + 'bri': 'Financial', + 'bni': 'Financial', + 'cimb': 'Financial', + 'paypal': 'Financial', + 'stripe': 'Financial', + + // OS / Updates + 'windowsupdate': 'OS/Software Updates', + 'android': 'OS/Software Updates', + + // Remote Desktop + 'anydesk': 'Remote Desktop', + 'teamviewer': 'Remote Desktop', + 'rdp': 'Remote Desktop', + 'ssh': 'Remote Desktop', + 'vnc': 'Remote Desktop', + + // Network Infrastructure + 'dns': 'Network Infrastructure', + 'ntp': 'Network Infrastructure', + 'dhcp': 'Network Infrastructure', + 'icmp': 'Network Infrastructure', + 'reverse-dns': 'Network Infrastructure', + 'root-servers': 'Network Infrastructure', + 'dyn-ddns': 'Network Infrastructure', + + // Development + 'github': 'Technology', + 'gitlab': 'Technology', +}; + +function resolveAppCategory(appLabel, lookupMap = {}) { + if (!appLabel || appLabel === 'Unknown' || appLabel === 'backone.unclassified' || appLabel === 'netify.unclassified') { + return 'Unclassified'; + } + + // 1. Check exact match in lookupMap (which contains LookupApp entries) + if (lookupMap[appLabel]?.category) { + return lookupMap[appLabel].category; + } + if (typeof lookupMap[appLabel] === 'string') { + return lookupMap[appLabel]; + } + + // 2. Clean prefix + const clean = String(appLabel).toLowerCase().replace(/^(?:backone|netify|siab|nexus)\./i, ''); + + if (lookupMap[clean]?.category) { + return lookupMap[clean].category; + } + if (typeof lookupMap[clean] === 'string') { + return lookupMap[clean]; + } + + // 3. Fallback dictionary by exact clean key + if (APP_CATEGORY_MAP[clean]) { + return APP_CATEGORY_MAP[clean]; + } + + // 4. Substring match against known apps + for (const [key, cat] of Object.entries(APP_CATEGORY_MAP)) { + if (clean.includes(key)) return cat; + } + + // 5. Port-based categories + if (/^(?:port\s+)?(\d+)$/i.test(clean)) { + const port = parseInt(clean.replace(/^(?:port\s+)?/i, ''), 10); + if ([80, 443, 8080, 8443].includes(port)) return 'Technology'; + if ([53, 67, 68, 123, 161, 162].includes(port)) return 'Network Infrastructure'; + if ([22, 23, 3389, 5900].includes(port)) return 'Remote Desktop'; + if ([25, 110, 143, 465, 587, 993, 995].includes(port)) return 'Mail'; + return 'General Traffic'; + } + + return 'General Traffic'; +} + +function getAppCategoryFallback(appLabel) { + return resolveAppCategory(appLabel, {}); +} + +module.exports = { + CATEGORY_ID_MAP, + APP_CATEGORY_MAP, + resolveAppCategory, + getAppCategoryFallback +}; diff --git a/proxy/backoneClientStats.js b/proxy/backoneClientStats.js index 0cf8493..7a1e0b4 100644 --- a/proxy/backoneClientStats.js +++ b/proxy/backoneClientStats.js @@ -224,10 +224,16 @@ async function syncApplicationDictionary() { icon: app.icon || app.application?.icon || null, logo: app.logo || app.application?.logo || null, application_category: { - id: app.application_category?.id, - name: app.application_category?.name, - label: app.application_category?.label, - tag: app.application_category?.tag + id: app.category?.id || app.application_category?.id || null, + name: app.category?.name || app.application_category?.name || app.category?.label || null, + label: app.category?.label || app.application_category?.label || null, + tag: app.category?.tag || app.application_category?.tag || null + }, + category: { + id: app.category?.id || app.application_category?.id || null, + name: app.category?.name || app.application_category?.name || app.category?.label || null, + label: app.category?.label || app.application_category?.label || null, + tag: app.category?.tag || app.application_category?.tag || null } }))); }