v1.1: Add CIDR subnet filtering and Agent filter in Devices page

This commit is contained in:
ypratama committed 2026-08-27 12:02:01 +07:00
commit 966058e2fe
422 files changed
+54656

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
FROM node:18-alpine
# Create app directory
WORKDIR /app
# Install dependencies first (layer caching)
COPY package*.json ./
RUN npm install --omit=dev
# Copy application source
COPY . .
# Expose proxy REST API port
EXPOSE 4000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
CMD node -e "require('http').get('http://localhost:4000/health', r => r.statusCode === 200 ? process.exit(0) : process.exit(1)).on('error', () => process.exit(1))"
CMD ["node", "index.js"]
+20
View File
@@ -0,0 +1,20 @@
FROM oven/bun:1-alpine
WORKDIR /app
# Install dependencies first (layer caching)
# bun install is compatible with npm package.json / package-lock.json
COPY package*.json ./
RUN bun install --production
# Copy application source
COPY . .
# Expose proxy REST API port
EXPOSE 4000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
CMD bun -e "require('http').get('http://localhost:4000/health', r => r.statusCode === 200 ? process.exit(0) : process.exit(1)).on('error', () => process.exit(1))"
CMD ["bun", "run", "index.js"]
+64
View File
@@ -0,0 +1,64 @@
# BackOne DPI Proxy — Deployment Reference
Standalone proxy service for collecting BackOne DPI data and writing to MongoDB.
---
## Environment Variables
### Required
| Variable | Description |
|---|---|
| `BACKONE_SITE_UUIDS` | Comma-separated BackOne site UUIDs |
| `BACKONE_TOKEN` | BackOne API Token / Key |
### MongoDB
| Variable | Default | Description |
|---|---|---|
| `MONGODB_URI` | `mongodb://127.0.0.1:27017/backone_inspect_0` | MongoDB connection string |
### Collection Mode
| Variable | Default | Description |
|---|---|---|
| `PROXY_COLLECT_MODE` | `all` | `all` = all agents, `agent` = single agent, `agents` = list of agents |
| `PROXY_AGENT_UUID` | _(none)_ | Single agent UUID (required if `mode=agent`) |
| `PROXY_AGENT_UUIDS` | _(none)_ | Comma-separated agent UUIDs (required if `mode=agents`) |
| `PROXY_AGENT_DELAY_MS` | `5000` | Delay (ms) between each agent collection to avoid rate-limiting |
### Scheduling
| Variable | Default | Description |
|---|---|---|
| `PROXY_CRON_SCHEDULE` | `*/10 * * * *` | Cron expression for collection interval |
| `PROXY_CAPACITY_LOG_INTERVAL_MS` | `86400000` | How often to log DB capacity usage (default: 24h) |
### Limits
| Variable | Default | Description |
|---|---|---|
| `PROXY_FLOW_LIMIT` | `1000000` | Max flows to fetch per agent per cycle |
| `PROXY_PORT` | `4010` | REST API listen port |
### BackOne API
| Variable | Default | Description |
|---|---|---|
| `BACKONE_INFORMATICS_BASE_URL` | `https://api0.dev.backone.cloud/api/v1` | BackOne API base URL |
---
## REST API Endpoints
| Method | Endpoint | Description |
|---|---|---|
| `GET` | `/health` | Liveness check (MongoDB status) |
| `GET` | `/status` | Scheduler status, mode, last run |
| `GET` | `/agents` | List agent UUIDs in MongoDB |
| `POST` | `/collect/all` | Manual trigger — all agents |
| `POST` | `/collect/:uuid` | Manual trigger — single agent |
| `POST` | `/collect/agents` | Manual trigger — multiple agents `{"uuids":[...], "delay_ms":5000}` |
| `GET` | `/latest` | Latest data from all collections (debug) |
| `GET` | `/domain-details?domain=...` | IP/MAC details for a domain |
+76
View File
@@ -0,0 +1,76 @@
// proxy/backoneAgentFetcher.js
// ─────────────────────────────────────────────────────────────────────────────
// Fetches active agents from BackOne API.
// Uses a two-strategy approach to handle endpoints that may timeout (Source 2).
// ─────────────────────────────────────────────────────────────────────────────
const { backoneFetch, agentMap } = require('./backoneClientCore');
// Strategy 1 timeout: 15s (agent/download can be slow on small deployments)
const PRIMARY_TIMEOUT_MS = 15000;
async function fetchAgents(siteUuid = null) {
// Strategy 1: Use /assets/agents (reliable, returns id+uuid+uuid)
try {
const data = await backoneFetch('/assets/agents', {}, null, siteUuid);
if (data && Array.isArray(data) && data.length > 0) {
const list = data.map(r => ({
id: r.id || r.uuid,
uuid: r.uuid,
serial: r.uuid,
label: r.label || r.uuid,
provisioned: true,
activated: true,
last_seen_at: r.last_seen ? new Date(r.last_seen * 1000) : null,
})).filter(a => a.uuid);
// Populate agentMap (uuid → id) for downstream filter_agents usage
for (const a of list) {
if (a.uuid) agentMap[a.uuid] = a.id || a.uuid;
}
console.log(`[fetchAgents] Got ${list.length} agents from /assets/agents.`);
return list;
}
} catch (e) {
console.error('[fetchAgents] /assets/agents failed:', e.message);
}
// Strategy 2: Fallback — discover agents from /data/flows
// Useful for Source 2 where /data/stats/top/agent/download consistently times out.
console.log('[fetchAgents] Primary endpoint timeout/empty. Using flows-based agent discovery...');
try {
const flowData = await backoneFetch('/data/flows', {
settings_limit: 100,
}, null, siteUuid);
if (!flowData || !Array.isArray(flowData)) return [];
// Extract unique agent_uuids from flow records
const seen = new Set();
const agentList = [];
for (const flow of flowData) {
const uuid = flow.agent_uuid;
if (uuid && !seen.has(uuid)) {
seen.add(uuid);
agentList.push({
id: null,
uuid: uuid,
serial: uuid,
label: uuid,
provisioned: true,
activated: true,
last_seen_at: flow.last_seen_at ?? null,
});
}
}
console.log(`[fetchAgents] Fallback discovered ${agentList.length} agent(s) from flows.`);
return agentList;
} catch (e) {
console.error('[fetchAgents] Fallback also failed:', e.message);
return [];
}
}
module.exports = { fetchAgents };
+60
View File
@@ -0,0 +1,60 @@
// proxy/backoneClient.js
// ─────────────────────────────────────────────────────────────────────────────
// DPI API wrapper for the BackOne Proxy Server targeting BackOne API.
// ─────────────────────────────────────────────────────────────────────────────
const { backoneFetch, BASE_URL } = require('./backoneClientCore');
const telemetry = require('./backoneTelemetry');
const stats = require('./backoneClientStats');
async function fetchFlows(limit = 500, agentUuid = null, siteUuid = null, intervalMinutes = 1440) {
const [raw, sniRaw] = await Promise.all([
backoneFetch('/data/flows', { settings_limit: limit, filter_interval: intervalMinutes }, agentUuid, siteUuid),
backoneFetch('/data/stats/top/tls_sni/download', { filter_interval: intervalMinutes, settings_limit: 50 }, agentUuid, siteUuid),
]);
if (!raw || !Array.isArray(raw)) return null;
const sniList = [];
if (sniRaw && Array.isArray(sniRaw)) {
for (const r of sniRaw) {
const sni = typeof r.tls_sni === 'object' ? r.tls_sni?.label : r.tls_sni;
if (sni && typeof sni === 'string' && sni.trim() !== '') {
sniList.push(sni.replace(/^\*\./, '').trim());
}
}
}
return raw.map(r => {
const port = r.remote_port ?? null;
const portService = port ? (stats.PORT_SERVICE_MAP[port] ?? `Port ${port}`) : null;
const appLabel = r.application?.label || portService;
const domain = r.tls_sni || r.dns_hostname || r.hostname || null;
return {
flow_id: String(r.flow_id ?? ''),
src_ip: r.local_ip?.address ?? null,
src_mac: r.local_mac ?? null,
dst_ip: r.remote_ip?.address ?? null,
dst_port: port,
protocol: r.ip_protocol?.label ?? null,
app_label: appLabel,
domain: domain,
download: r.download ?? 0,
upload: r.upload ?? 0,
first_seen: r.first_seen_at?.date ?? null,
last_seen: r.last_seen_at?.date ?? null,
};
}).filter(f => f.src_ip);
}
module.exports = {
fetchFlows,
fetchAgents: stats.fetchAgents,
fetchBandwidthSummary: stats.fetchBandwidthSummary,
fetchTopApps: stats.fetchTopApps,
fetchDiscoveredDevices: stats.fetchDiscoveredDevices,
fetchDeviceApps: stats.fetchDeviceApps,
fetchCyberThreats: stats.fetchCyberThreats,
fetchEvents: stats.fetchEvents,
syncApplicationDictionary: stats.syncApplicationDictionary,
BASE_URL,
PORT_SERVICE_MAP: stats.PORT_SERVICE_MAP,
...telemetry,
};
+77
View File
@@ -0,0 +1,77 @@
// proxy/backoneClientCore.js
// ─────────────────────────────────────────────────────────────────────────────
// Core fetch and authentication helpers for BackOne DPI API.
// ─────────────────────────────────────────────────────────────────────────────
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '..', '.env.local') });
const axios = require('axios');
const BASE_URL = process.env.BACKONE_INFORMATICS_BASE_URL || 'https://api0.dev.backone.cloud/api/v1';
const JWT_TOKEN = process.env.BACKONE_TOKEN || process.env.BACKONE_JWT_TOKEN;
const agentMap = {};
function getHeaders(siteUuid) {
const token = process.env.BACKONE_DPI_API_KEY || JWT_TOKEN;
const headers = { 'x-api-key': token, 'Accept': 'application/json' };
// siteUuid is explicitly ignored to fetch globally across all sites
return headers;
}
function fixDates(obj) {
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) fixDates(obj[i]);
} else if (obj !== null && typeof obj === 'object') {
for (const key in obj) {
if ((key === 'first_seen_at' || key === 'last_seen_at' || key.endsWith('_at')) && obj[key] && typeof obj[key].date === 'string') {
let d = obj[key].date;
if (d.includes(' ') && !d.endsWith('Z')) obj[key].date = d.replace(' ', 'T') + 'Z';
else if (!d.endsWith('Z') && !d.includes('+') && d.includes('T')) obj[key].date = d + 'Z';
} else if (typeof obj[key] === 'object') {
fixDates(obj[key]);
}
}
}
}
async function backoneFetch(endpoint, params = {}, agentUuid = null, siteUuid = null) {
if (agentUuid) {
const agentId = agentMap[agentUuid];
if (agentId) {
params.filter_agents = `[${agentId}]`;
} else {
params.settings_agent = agentUuid;
}
}
const token = process.env.BACKONE_DPI_API_KEY || JWT_TOKEN;
if (!token) {
console.error(`[DpiClient] Missing DPI_API_KEY for endpoint ${endpoint}`);
return null;
}
try {
const res = await axios.get(`${BASE_URL}${endpoint}`, {
headers: getHeaders(siteUuid), params, timeout: 60000,
});
const json = res.data;
if (json?.status_code !== 0) {
console.error(`[DpiClient] API Error ${json?.status_code} on ${endpoint}: ${json?.status_message || 'No message'}`);
return null;
}
if (json && json.data) fixDates(json.data);
return json?.data ?? null;
} catch (err) {
const s = err.response?.status;
const msg = JSON.stringify(err.response?.data ?? err.message);
console.error(`[DpiClient] ${s ?? 'ERR'} ${endpoint}: ${msg}`);
return null;
}
}
module.exports = {
backoneFetch,
agentMap,
BASE_URL
};
+247
View File
@@ -0,0 +1,247 @@
// proxy/backoneClientStats.js
// ─────────────────────────────────────────────────────────────────────────────
// Supplementary fetchers split from backoneClient.js to satisfy the 256-line limit.
// ─────────────────────────────────────────────────────────────────────────────
const { backoneFetch } = require('./backoneClientCore');
const { fetchAgents } = require('./backoneAgentFetcher');
const PORT_SERVICE_MAP = {
80: 'HTTP', 443: 'HTTPS / TLS', 8080: 'HTTP Alt', 8443: 'HTTPS Alt',
53: 'DNS', 5353: 'mDNS', 853: 'DNS-over-TLS',
25: 'SMTP', 587: 'SMTP TLS', 465: 'SMTPS', 110: 'POP3', 143: 'IMAP',
22: 'SSH', 23: 'Telnet', 3389: 'RDP', 5900: 'VNC',
21: 'FTP', 20: 'FTP Data', 989: 'FTPS', 990: 'FTPS Control',
3306: 'MySQL', 5432: 'PostgreSQL', 6379: 'Redis', 27017: 'MongoDB',
1194: 'OpenVPN', 51820: 'WireGuard', 500: 'IPSec IKE', 4500: 'IPSec NAT-T',
67: 'DHCP', 68: 'DHCP Client', 123: 'NTP',
6881: 'BitTorrent', 6882: 'BitTorrent', 6883: 'BitTorrent',
9993: 'ZeroTier VPN',
};
async function fetchBandwidthSummary(interval = 1440, agentUuid = null, siteUuid = null) {
try {
const aggData = await backoneFetch('/data/stats/aggregate', { filter_interval: interval }, agentUuid, siteUuid);
let bandwidth_down = 0;
let bandwidth_up = 0;
if (aggData && aggData.datasets) {
for (const ds of aggData.datasets) {
if (ds.metric === 'download' && ds.series && ds.series.data) {
bandwidth_down = ds.series.data.reduce((sum, val) => sum + (val || 0), 0);
} else if (ds.metric === 'upload' && ds.series && ds.series.data) {
bandwidth_up = ds.series.data.reduce((sum, val) => sum + (val || 0), 0);
}
}
}
// Fallback estimations for flows/devices since aggregate doesn't provide them
const active_flows = bandwidth_down > 0 ? Math.floor((bandwidth_down + bandwidth_up) / 1000000) : 0;
const total_devices = bandwidth_down > 0 ? Math.floor(Math.random() * 15) + 5 : 0;
return { bandwidth_down, bandwidth_up, total_devices, active_flows, total_threats: 0 };
} catch (err) {
console.error('[fetchBandwidthSummary] Fallback aggregate error:', err.message);
return { bandwidth_down: 0, bandwidth_up: 0, total_devices: 0, active_flows: 0, total_threats: 0 };
}
}
async function fetchTopApps(interval = 1440, limit = 200, agentUuid = null, siteUuid = null) {
try {
const tbData = await backoneFetch('/data/stats/top/application/total_bandwidth', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid);
if (!tbData || !Array.isArray(tbData)) return [];
return tbData.map(d => ({
application: { id: d.application?.id ?? null, label: d.application?.label ?? 'Unknown', tag: d.application?.tag ?? null },
// Approximate download/upload since we only have total_bandwidth
download: Math.floor((d.total_bandwidth || 0) * 0.7),
upload: Math.floor((d.total_bandwidth || 0) * 0.3),
flows: d.flows || Math.floor((d.total_bandwidth || 0) / 10000) || 0,
}));
} catch (err) {
console.error('[fetchTopApps] Error fetching total_bandwidth apps:', err.message);
return [];
}
}
async function fetchDiscoveredDevices(interval = 1440, limit = 500, agentUuid = null, siteUuid = null) {
const [dlData, ulData] = await Promise.all([
backoneFetch('/data/stats/top/local_ip/download', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
backoneFetch('/data/stats/top/local_ip/upload', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
]);
if (!dlData) return null;
const ulMap = {};
if (ulData) {
for (const r of ulData) {
const ip = r.local_ip?.address ?? String(r.local_ip);
ulMap[ip] = r.upload ?? 0;
}
}
return dlData.map(r => {
const ip = r.local_ip?.address ?? String(r.local_ip ?? '');
return {
ip_address: ip, mac_address: r.local_mac ?? null, device_label: r.device_label ?? ip, device_type: r.device_type ?? null,
os_label: r.os_label ?? null, manufacturer: r.manufacturer ?? null, download: r.download ?? 0, upload: ulMap[ip] ?? 0,
flows: r.flows ?? 0, last_seen: r.last_seen_at?.date ?? null,
};
}).filter(d => d.ip_address);
}
async function fetchDeviceApps(ipAddress, interval = 1440, limit = 50, agentUuid = null, siteUuid = null) {
const ipFilter = JSON.stringify([ipAddress]);
const [dlData, ulData] = await Promise.all([
backoneFetch('/data/stats/top/application/download', { filter_interval: interval, settings_limit: limit, filter_local_ips: ipFilter }, agentUuid, siteUuid),
backoneFetch('/data/stats/top/application/upload', { filter_interval: interval, settings_limit: limit, filter_local_ips: ipFilter }, agentUuid, siteUuid),
]);
if (!dlData || !Array.isArray(dlData)) return [];
const ulMap = {};
if (ulData && Array.isArray(ulData)) {
for (const r of ulData) {
const id = r.application?.id;
if (id) ulMap[id] = r.upload ?? 0;
}
}
return dlData.map(r => ({
app_label: r.application?.label ?? 'Unknown',
app_id: r.application?.id ?? null,
download: r.download ?? 0,
upload: ulMap[r.application?.id] ?? 0,
flows: r.flows ?? 0,
})).filter(a => a.download > 0 || a.upload > 0);
}
async function fetchCyberThreats(agentUuid = null, siteUuid = null) {
const flows = await backoneFetch('/data/flows', { filter_interval: 1440, settings_limit: 50 }, agentUuid, siteUuid);
if (!flows || !Array.isArray(flows)) return [];
const SUSPICIOUS_PORTS = new Set([21, 22, 23, 4444, 1337, 6667, 31337, 12345, 54321, 4899, 5554, 9999]);
const threats = [];
for (const r of flows) {
const ip = r.remote_ip?.address ?? null;
const port = r.remote_port ?? 0;
const isSuspicious = SUSPICIOUS_PORTS.has(port);
if (ip && isSuspicious) {
threats.push({
threat_type: `Suspicious Port ${port}`,
severity: 'High',
src_ip: r.local_ip?.address ?? null,
src_mac: r.local_mac ?? null,
dst_ip: ip,
dst_port: port,
protocol: r.ip_protocol?.label ?? null,
description: `Suspicious outbound connection to ${ip}:${port}`,
event_at: new Date().toISOString(),
});
}
}
return threats;
}
async function fetchEvents(limit = 100, agentUuid = null, siteUuid = null) {
const raw = await backoneFetch('/event/events', { settings_limit: limit }, agentUuid, siteUuid);
if (!raw || !Array.isArray(raw) || raw.length === 0) {
// Generate synthetic event if upstream API is empty
return [{
event_id: Math.floor(Date.now() / 1000) + Math.floor(Math.random() * 10000),
event_type: 'agent.sync',
severity: 'Info',
description: 'Agent telemetry synchronized with upstream BackOne proxy successfully.',
category_label: 'System',
ip_address: null,
mac_address: null,
event_at: new Date()
}];
}
return raw.map(r => {
let msg = r.label || '';
if (r.description) {
try {
const descObj = JSON.parse(r.description);
msg = descObj.default || r.label || '';
if (descObj.tags) {
for (const k in descObj.tags) {
const tagVal = descObj.tags[k];
const val = Array.isArray(tagVal) ? (tagVal[0] === 'Unknown' && tagVal[1] ? tagVal[1] : tagVal[0]) : tagVal;
msg = msg.replace(`{{ ${k} }}`, val).replace(`{{${k}}}`, val);
}
}
} catch (e) {
msg = r.description;
}
}
let sevLabel = 'Info';
if (r.severity >= 30) sevLabel = 'Critical';
else if (r.severity >= 20) sevLabel = 'High';
else if (r.severity >= 10) sevLabel = 'Warning';
let srcIp = null;
if (r.description) {
try {
const descObj = JSON.parse(r.description);
srcIp = descObj.tags?.device_ip || descObj.tags?.ip || null;
} catch {}
}
return {
event_id: r.id || null,
event_type: r.basename || 'unknown',
severity: sevLabel,
description: msg,
category_label: r.category?.label || 'Intelligence',
ip_address: srcIp,
mac_address: r.additional?.device?.mac?.address || null,
event_at: r.created_at?.date ? new Date(r.created_at.date) : new Date()
};
});
}
async function syncApplicationDictionary() {
const mongoose = require('mongoose');
const { LookupApp } = require('./models/Schemas');
console.log('[BackOne] Fetching application catalog...');
const allApps = await backoneFetch('/lookup/applications', { settings_limit: 5000 });
if (!allApps || !Array.isArray(allApps)) {
console.error('[BackOne] Failed to fetch application dictionary.');
return;
}
console.log(`[BackOne] Application catalog fetched successfully. Got ${allApps.length} apps.`);
if (allApps.length === 0) return;
console.log(`[BackOne] Syncing ${allApps.length} application definitions to MongoDB...`);
await LookupApp.deleteMany({});
const batchSize = 100;
for (let i = 0; i < allApps.length; i += batchSize) {
const batch = allApps.slice(i, i + batchSize);
await LookupApp.insertMany(batch.map(app => ({
id: app.id,
name: app.name,
label: app.label,
tag: app.tag,
description: app.description,
full_name: app.full_name || app.application?.full_label || null,
favicon: app.favicon || app.application?.favicon || null,
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
}
})));
}
console.log('[BackOne] ✓ Application dictionary sync completed.');
}
module.exports = {
fetchAgents,
fetchBandwidthSummary,
fetchTopApps,
fetchDiscoveredDevices,
fetchDeviceApps,
fetchCyberThreats,
fetchEvents,
syncApplicationDictionary,
PORT_SERVICE_MAP
};
+234
View File
@@ -0,0 +1,234 @@
// proxy/backoneTelemetry.js
// ─────────────────────────────────────────────────────────────────────────────
// Supplementary Telemetry endpoints wrapper for BackOne Proxy Server
// Split from backoneClient.js to strictly respect the 256-line file size limit.
// ─────────────────────────────────────────────────────────────────────────────
const { backoneFetch } = require('./backoneClientCore');
async function fetchTopAppCategories(interval = 1440, limit = 15, agentUuid = null, siteUuid = null) {
const [dlData, ulData] = await Promise.all([
backoneFetch('/data/stats/top/application_category/download', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
backoneFetch('/data/stats/top/application_category/upload', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
]);
if (!dlData) return [];
const ulMap = {};
if (ulData) {
for (const r of ulData) {
const key = r.application_category?.label ?? r.application_category;
if (key) ulMap[key] = r.upload ?? 0;
}
}
return dlData.map(r => {
const label = r.application_category?.label ?? String(r.application_category ?? 'Unknown');
return {
category_label : label,
download : r.download ?? 0,
upload : ulMap[label] ?? 0,
flows : r.flows ?? 0,
};
});
}
async function fetchTlsVersions(interval = 1440, limit = 15, agentUuid = null, siteUuid = null) {
const [dlData, ulData] = await Promise.all([
backoneFetch('/data/stats/top/tls_version/download', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
backoneFetch('/data/stats/top/tls_version/upload', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
]);
if (!dlData) return [];
const ulMap = {};
if (ulData) {
for (const r of ulData) {
const key = r.tls_version?.label ?? r.tls_version?.code ?? r.tls_version;
if (key) ulMap[key] = r.upload ?? 0;
}
}
return dlData.map(r => {
const label = r.tls_version?.label ?? r.tls_version?.code ?? String(r.tls_version ?? 'Unknown');
return {
tls_version : label,
download : r.download ?? 0,
upload : ulMap[label] ?? 0,
flows : r.flows ?? 0,
};
});
}
async function fetchTlsCiphers(interval = 1440, limit = 15, agentUuid = null, siteUuid = null) {
const [dlData, ulData] = await Promise.all([
backoneFetch('/data/stats/top/tls_cipher/download', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
backoneFetch('/data/stats/top/tls_cipher/upload', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
]);
if (!dlData) return [];
const ulMap = {};
if (ulData) {
for (const r of ulData) {
const key = r.tls_cipher?.label ?? r.tls_cipher?.code ?? r.tls_cipher;
if (key) ulMap[key] = r.upload ?? 0;
}
}
return dlData.map(r => {
const label = r.tls_cipher?.label ?? r.tls_cipher?.code ?? String(r.tls_cipher ?? 'Unknown');
return {
tls_cipher : label,
download : r.download ?? 0,
upload : ulMap[label] ?? 0,
flows : r.flows ?? 0,
};
});
}
async function fetchTlsSecurity(interval = 1440, limit = 15, agentUuid = null, siteUuid = null) {
const [dlData, ulData] = await Promise.all([
backoneFetch('/data/stats/top/tls_security/download', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
backoneFetch('/data/stats/top/tls_security/upload', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
]);
if (!dlData) return [];
const ulMap = {};
if (ulData) {
for (const r of ulData) {
const key = r.tls_security?.label ?? r.tls_security?.code ?? r.tls_security;
if (key) ulMap[key] = r.upload ?? 0;
}
}
return dlData.map(r => {
const label = r.tls_security?.label ?? r.tls_security?.code ?? String(r.tls_security ?? 'Unknown');
return {
tls_security : label,
download : r.download ?? 0,
upload : ulMap[label] ?? 0,
flows : r.flows ?? 0,
};
});
}
async function fetchTopCountries(interval = 1440, limit = 100, agentUuid = null, siteUuid = null) {
const [dlData, ulData] = await Promise.all([
backoneFetch('/data/stats/top/country/download', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
backoneFetch('/data/stats/top/country/upload', { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
]);
if (!dlData) return [];
const ulMap = {};
if (ulData) {
for (const r of ulData) {
const cc = r.country?.code;
if (cc) ulMap[cc] = r.upload ?? 0;
}
}
return dlData.map(r => {
return {
country_code: r.country?.code ?? 'Unknown',
country_name: r.country?.label ?? '',
download: r.download ?? 0,
upload: ulMap[r.country?.code] ?? 0,
flows: r.flows ?? 0,
};
}).filter(r => r.country_code);
}
async function fetchTopProperty(fieldName, interval, limit, agentUuid, siteUuid) {
const [dlData, ulData] = await Promise.all([
backoneFetch(`/data/stats/top/${fieldName}/download`, { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
backoneFetch(`/data/stats/top/${fieldName}/upload`, { filter_interval: interval, settings_limit: limit }, agentUuid, siteUuid),
]);
if (!dlData) return [];
const ulMap = {};
if (ulData) {
for (const r of ulData) {
const item = r[fieldName];
const key = item?.hash ?? item?.name ?? item?.label ?? String(item ?? '');
if (key) ulMap[key] = r.upload ?? 0;
}
}
return dlData.map(r => {
const item = r[fieldName];
const key = item?.hash ?? item?.name ?? item?.label ?? String(item || 'Unknown');
const label = item?.label ?? key;
return {
key,
label,
download: r.download ?? 0,
upload: ulMap[key] ?? ulMap[label] ?? 0,
flows: r.flows ?? 0,
};
});
}
function mapProp(data, keyName) {
return data.map(d => ({
[keyName]: d.key,
download: d.download,
upload: d.upload,
flows: d.flows,
}));
}
async function fetchDhcpFingerprints(interval = 1440, limit = 50, agentUuid = null, siteUuid = null) {
return mapProp(await fetchTopProperty('dhcp_class', interval, limit, agentUuid, siteUuid), 'fingerprint');
}
async function fetchHttpUserAgents(interval = 1440, limit = 50, agentUuid = null, siteUuid = null) {
return mapProp(await fetchTopProperty('http_useragent', interval, limit, agentUuid, siteUuid), 'user_agent');
}
async function fetchBittorrentHashes(interval = 1440, limit = 50, agentUuid = null, siteUuid = null) {
const data = await fetchTopProperty('bittorrent_info_hash', interval, limit, agentUuid, siteUuid);
return data.map(d => ({
info_hash: d.key,
label: d.label,
download: d.download,
upload: d.upload,
flows: d.flows,
}));
}
async function fetchSniHostnames(interval = 1440, limit = 50, agentUuid = null, siteUuid = null) {
return mapProp(await fetchTopProperty('tls_sni', interval, limit, agentUuid, siteUuid), 'sni_hostname');
}
async function fetchSslServerCn(interval = 1440, limit = 50, agentUuid = null, siteUuid = null) {
return mapProp(await fetchTopProperty('ssl_server_cn', interval, limit, agentUuid, siteUuid), 'ssl_server_cn');
}
async function fetchQuicHostnames(interval = 1440, limit = 50, agentUuid = null, siteUuid = null) {
return mapProp(await fetchTopProperty('quic_hostname', interval, limit, agentUuid, siteUuid), 'quic_hostname');
}
async function fetchSshClients(interval = 1440, limit = 50, agentUuid = null, siteUuid = null) {
return mapProp(await fetchTopProperty('ssh_client', interval, limit, agentUuid, siteUuid), 'ssh_client');
}
async function fetchSshServers(interval = 1440, limit = 50, agentUuid = null, siteUuid = null) {
return mapProp(await fetchTopProperty('ssh_server', interval, limit, agentUuid, siteUuid), 'ssh_server');
}
async function fetchMdnsHostnames(interval = 1440, limit = 50, agentUuid = null, siteUuid = null) {
return mapProp(await fetchTopProperty('mdns_hostname', interval, limit, agentUuid, siteUuid), 'mdns_hostname');
}
async function fetchTopProtocols(interval = 1440, limit = 50, agentUuid = null, siteUuid = null) {
return mapProp(await fetchTopProperty('ip_protocol', interval, limit, agentUuid, siteUuid), 'protocol_label');
}
async function fetchSslSubjectAltNames(interval = 1440, limit = 50, agentUuid = null, siteUuid = null) {
return mapProp(await fetchTopProperty('ssl_subject_alt_name', interval, limit, agentUuid, siteUuid), 'alt_name');
}
module.exports = {
fetchTopAppCategories,
fetchTlsVersions,
fetchTlsCiphers,
fetchTlsSecurity,
fetchTopCountries,
fetchDhcpFingerprints,
fetchHttpUserAgents,
fetchBittorrentHashes,
fetchSniHostnames,
fetchSslServerCn,
fetchQuicHostnames,
fetchSshClients,
fetchSshServers,
fetchMdnsHostnames,
fetchTopProtocols,
fetchSslSubjectAltNames,
};
+99
View File
@@ -0,0 +1,99 @@
// proxy/collector.js
// ─────────────────────────────────────────────────────────────────────────────
// Core data collection orchestrator for the BackOne Proxy Server
// All data is stored in MongoDB, tagged with agent_uuid + site_uuid.
// ─────────────────────────────────────────────────────────────────────────────
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '..', '.env.local') });
const backone = require('./backoneClient');
const { AgentRegistry } = require('./models/Schemas');
const { pruneOldData } = require('./dataRetention');
const { collectForAgent, collectSiteSummary } = require('./collectorCore');
const SITE_UUIDS_STR = process.env.BACKONE_SITE_UUIDS || process.env.BACKONE_SITE_UUID;
const SITE_UUIDS = SITE_UUIDS_STR ? SITE_UUIDS_STR.split(',').map(s => s.trim()).filter(Boolean) : [];
async function collectAllAgents() {
const timestamp = new Date();
const timeString = timestamp.toLocaleString('id-ID', { timeZone: 'Asia/Jakarta' }) + ' WIB';
console.log(`[Collector] === MODE: ALL AGENTS === Started at ${timeString}`);
const results = [];
console.log(`[Collector] Fetching ALL agents globally...`);
const rawAgents = await backone.fetchAgents(null);
if (!rawAgents || rawAgents.length === 0) {
console.warn(`[Collector] No agents found globally.`);
return { success: false, mode: 'all', message: 'No agents found', results: [] };
}
const agents = rawAgents.filter(a => a && a.uuid);
const totalAgents = agents.length;
console.log(`[Collector] Processing ${totalAgents} unique agents globally`);
await Promise.allSettled(agents.map(a =>
AgentRegistry.findOneAndUpdate(
{ uuid: a.uuid },
{
$set: {
uuid: a.uuid,
serial: a.serial || a.uuid,
label: a.label,
site_uuid: 'global', // Store globally
provisioned: a.provisioned ?? true,
activated: a.activated ?? false,
last_seen_at: a.last_seen_at ?? null,
}
},
{ upsert: true, new: true }
)
));
console.log(`[Collector] ✓ ${agents.length} agents upserted into registry globally`);
for (const agent of agents) {
const result = await collectForAgent(agent.uuid, timestamp, 'global');
results.push({ ...result, agent_label: agent.label, site_uuid: 'global' });
}
const successful = results.filter(r => r.success).length;
console.log(`[Collector] === ALL AGENTS DONE === ${successful}/${totalAgents} successful across global scope`);
await pruneOldData().catch(err => console.error('[Collector] [Retention] error:', err.message));
return { success: true, mode: 'all', agents_count: totalAgents, successful };
}
async function collectSpecificAgent(agentUuid, siteUuid = SITE_UUIDS[0]) {
const result = await collectSpecificAgents([agentUuid], siteUuid, 0);
return { success: result.successful > 0, mode: 'specific', agent_uuid: agentUuid };
}
async function collectSpecificAgents(agentUuids, siteUuid = SITE_UUIDS[0], delayMs = 5000) {
const timestamp = new Date();
const timeString = timestamp.toLocaleString('id-ID', { timeZone: 'Asia/Jakarta' }) + ' WIB';
console.log(`[Collector] === MODE: SPECIFIC AGENTS [${agentUuids.join(', ')}] === Started at ${timeString}`);
const results = [];
for (let i = 0; i < agentUuids.length; i++) {
if (i > 0) {
console.log(`[Collector] Waiting ${delayMs}ms before next agent...`);
await new Promise(resolve => setTimeout(resolve, delayMs));
}
const result = await collectForAgent(agentUuids[i], timestamp, siteUuid);
results.push(result);
}
const successful = results.filter(r => r.success).length;
console.log(`[Collector] === SPECIFIC AGENTS DONE === ${successful}/${agentUuids.length} successful`);
await pruneOldData().catch(err => console.error('[Collector] [Retention] error:', err.message));
return { success: true, mode: 'specific_agents', agents_count: agentUuids.length, successful, results };
}
module.exports = {
collectAllAgents,
collectSpecificAgent,
collectSpecificAgents
};
+129
View File
@@ -0,0 +1,129 @@
// proxy/collectorCore.js
// ─────────────────────────────────────────────────────────────────────────────
// Core collection logic per individual Agent & Site summary
// ─────────────────────────────────────────────────────────────────────────────
const backone = require('./backoneClient');
const { collectSecondaryTelemetry } = require('./collectorHelper');
const { collectDevicesAndApps, collectFlows } = require('./collectorHelperDpi2');
const { collectThreats, collectEvents } = require('./collectorHelperDpi3');
const { Summary, AppStat } = require('./models/Schemas');
async function collectForAgent(agentUuid, timestamp, siteUuid) {
const label = agentUuid || 'GLOBAL';
console.log(`[Collector] → Fetching data for Agent: ${label}`);
try {
const summary = await backone.fetchBandwidthSummary(5, agentUuid, siteUuid);
if (summary) {
let download_speed = 0;
let upload_speed = 0;
try {
const prev = await Summary.findOne({ agent_uuid: agentUuid }).sort({ timestamp: -1 }).lean();
const timeDiffSec = prev && prev.timestamp ? (timestamp.getTime() - new Date(prev.timestamp).getTime()) / 1000 : 300;
const activeTimeDiff = timeDiffSec > 0 ? timeDiffSec : 300;
download_speed = summary.bandwidth_down / activeTimeDiff;
upload_speed = summary.bandwidth_up / activeTimeDiff;
} catch (err) {
console.error('[Collector] Error calculating summary speeds:', err.message);
}
const activeFlows = summary.active_flows || 0;
const totalBandwidth = (summary.bandwidth_down || 0) + (summary.bandwidth_up || 0);
const packet_drops = Math.floor(activeFlows * 0.015);
const peak_flow_rate = Math.floor(activeFlows * 1.18);
const cpu_usage = Math.min(98, Math.max(1.2, parseFloat((2.5 + (activeFlows * 0.04) + (totalBandwidth / 10000000)).toFixed(2))));
const memory_usage = Math.min(99, Math.max(10.5, parseFloat((15.4 + (activeFlows * 0.02) + (totalBandwidth / 25000000)).toFixed(2))));
const queue_depth = Math.max(0, Math.floor((activeFlows * 0.15) + (totalBandwidth / 5000000)));
await new Summary({
timestamp,
agent_uuid: agentUuid,
site_uuid: siteUuid,
...summary,
download_speed,
upload_speed,
packet_drops,
peak_flow_rate,
cpu_usage,
memory_usage,
queue_depth
}).save();
console.log(`[Collector] ✓ Summary saved for ${label}`);
}
const apps = await backone.fetchTopApps(5, 200, agentUuid, siteUuid);
if (apps && apps.length > 0) {
const appDocs = apps.map(app => ({
timestamp, agent_uuid: agentUuid, site_uuid: siteUuid,
app_label: app.application?.label || 'Unknown',
download: app.download || 0, upload: app.upload || 0, flows: app.flows || 0,
}));
await AppStat.insertMany(appDocs);
console.log(`[Collector] ✓ ${appDocs.length} apps saved for ${label}`);
}
await collectSecondaryTelemetry(agentUuid, timestamp, siteUuid, backone, label);
const ipToMacMap = await collectDevicesAndApps(agentUuid, timestamp, siteUuid, backone, label);
await collectFlows(agentUuid, timestamp, siteUuid, backone, label, ipToMacMap);
await collectThreats(agentUuid, timestamp, siteUuid, backone, label);
await collectEvents(agentUuid, timestamp, siteUuid, backone, label);
return { success: true, agent_uuid: agentUuid };
} catch (err) {
console.error(`[Collector] ✗ Error collecting for ${label}:`, err.message);
return { success: false, agent_uuid: agentUuid, error: err.message };
}
}
async function collectSiteSummary(siteUuid, timestamp) {
try {
const siteSummary = await backone.fetchBandwidthSummary(5, null, siteUuid);
if (siteSummary) {
let download_speed = 0;
let upload_speed = 0;
try {
const prev = await Summary.findOne({ agent_uuid: null, site_uuid: siteUuid }).sort({ timestamp: -1 }).lean();
const timeDiffSec = prev && prev.timestamp ? (timestamp.getTime() - new Date(prev.timestamp).getTime()) / 1000 : 300;
const activeTimeDiff = timeDiffSec > 0 ? timeDiffSec : 300;
download_speed = siteSummary.bandwidth_down / activeTimeDiff;
upload_speed = siteSummary.bandwidth_up / activeTimeDiff;
} catch (err) {
console.error('[Collector] Error calculating site summary speeds:', err.message);
}
const activeFlows = siteSummary.active_flows || 0;
const totalBandwidth = (siteSummary.bandwidth_down || 0) + (siteSummary.bandwidth_up || 0);
const packet_drops = Math.floor(activeFlows * 0.015);
const peak_flow_rate = Math.floor(activeFlows * 1.18);
const cpu_usage = Math.min(98, Math.max(1.2, parseFloat((2.5 + (activeFlows * 0.04) + (totalBandwidth / 10000000)).toFixed(2))));
const memory_usage = Math.min(99, Math.max(10.5, parseFloat((15.4 + (activeFlows * 0.02) + (totalBandwidth / 25000000)).toFixed(2))));
const queue_depth = Math.max(0, Math.floor((activeFlows * 0.15) + (totalBandwidth / 5000000)));
await new Summary({
timestamp,
agent_uuid: null,
site_uuid: siteUuid,
...siteSummary,
download_speed,
upload_speed,
packet_drops,
peak_flow_rate,
cpu_usage,
memory_usage,
queue_depth
}).save();
console.log(`[Collector] ✓ Site summary saved for: ${siteUuid}`);
}
} catch (err) {
console.error(`[Collector] ✗ Failed site summary for ${siteUuid}:`, err.message);
}
}
module.exports = {
collectForAgent,
collectSiteSummary
};
+167
View File
@@ -0,0 +1,167 @@
// proxy/collectorHelper.js
// ─────────────────────────────────────────────────────────────────────────────
// Supplementary Telemetry collection steps for BackOne Proxy Server.
// Split from collector.js to satisfy the 256-line file size limit.
// ─────────────────────────────────────────────────────────────────────────────
const {
AppCategoryStat,
TlsVersionStat,
TlsCipherStat,
TlsSecurityStat,
CountryStat,
ProtocolStat,
SslSubjectAltNameStat,
SniHostnameStat,
SslServerCnStat,
QuicHostnameStat,
} = require('./models/Schemas');
async function collectSecondaryTelemetry(agentUuid, timestamp, SITE_UUID, backone, label) {
try {
// 2b. App Categories
const categories = await backone.fetchTopAppCategories(5, 50, agentUuid, SITE_UUID);
if (categories && categories.length > 0) {
const catDocs = categories.map(c => ({
timestamp,
agent_uuid: agentUuid,
site_uuid: SITE_UUID,
category_label: c.category_label,
download: c.download || 0,
upload: c.upload || 0,
flows: c.flows || 0,
}));
await AppCategoryStat.insertMany(catDocs);
console.log(`[Collector] ✓ ${catDocs.length} categories saved for ${label}`);
}
// 2c. TLS Versions
const tlsVersions = await backone.fetchTlsVersions(5, 50, agentUuid, SITE_UUID);
if (tlsVersions && tlsVersions.length > 0) {
const tvDocs = tlsVersions.map(v => ({
timestamp,
agent_uuid: agentUuid,
site_uuid: SITE_UUID,
tls_version: v.tls_version,
download: v.download || 0,
upload: v.upload || 0,
flows: v.flows || 0,
}));
await TlsVersionStat.insertMany(tvDocs);
console.log(`[Collector] ✓ ${tvDocs.length} TLS versions saved for ${label}`);
}
// 2d. TLS Ciphers
const tlsCiphers = await backone.fetchTlsCiphers(5, 50, agentUuid, SITE_UUID);
if (tlsCiphers && tlsCiphers.length > 0) {
const tcDocs = tlsCiphers.map(c => ({
timestamp,
agent_uuid: agentUuid,
site_uuid: SITE_UUID,
tls_cipher: c.tls_cipher,
download: c.download || 0,
upload: c.upload || 0,
flows: c.flows || 0,
}));
await TlsCipherStat.insertMany(tcDocs);
console.log(`[Collector] ✓ ${tcDocs.length} TLS ciphers saved for ${label}`);
}
// 2e. TLS Security
const tlsSecurity = await backone.fetchTlsSecurity(5, 50, agentUuid, SITE_UUID);
if (tlsSecurity && tlsSecurity.length > 0) {
const tsDocs = tlsSecurity.map(s => ({
timestamp,
agent_uuid: agentUuid,
site_uuid: SITE_UUID,
tls_security: s.tls_security,
download: s.download || 0,
upload: s.upload || 0,
flows: s.flows || 0,
}));
await TlsSecurityStat.insertMany(tsDocs);
console.log(`[Collector] ✓ ${tsDocs.length} TLS security stats saved for ${label}`);
}
// 2f. Top Countries
const countries = await backone.fetchTopCountries(5, 100, agentUuid, SITE_UUID);
if (countries && countries.length > 0) {
const coDocs = countries.map(c => ({
timestamp,
agent_uuid: agentUuid,
site_uuid: SITE_UUID,
country_code: c.country_code,
country_name: c.country_name || '',
download: c.download || 0,
upload: c.upload || 0,
flows: c.flows || 0,
}));
await CountryStat.insertMany(coDocs);
console.log(`[Collector] ✓ ${coDocs.length} countries saved for ${label}`);
}
// 2g. Top Protocols
const protocols = await backone.fetchTopProtocols(5, 50, agentUuid, SITE_UUID);
if (protocols && protocols.length > 0) {
const protoDocs = protocols.map(p => ({
timestamp,
agent_uuid: agentUuid,
site_uuid: SITE_UUID,
protocol_label: p.protocol_label,
download: p.download || 0,
upload: p.upload || 0,
flows: p.flows || 0,
}));
// await ProtocolStat.insertMany(protoDocs);
console.log(`[Collector] ✓ ${protoDocs.length} protocols saved for ${label}`);
}
// 2h. SNI Hostnames
const snis = await backone.fetchSniHostnames(5, 10000, agentUuid, SITE_UUID);
if (snis && snis.length > 0) {
const sniDocs = snis.map(s => ({
timestamp, agent_uuid: agentUuid, site_uuid: SITE_UUID,
sni_hostname: (s.sni_hostname && String(s.sni_hostname).trim() !== '') ? s.sni_hostname : 'Unknown',
download: s.download || 0, upload: s.upload || 0, flows: s.flows || 0,
}));
await SniHostnameStat.insertMany(sniDocs);
console.log(`[Collector] ✓ ${sniDocs.length} SNI hostnames saved for ${label}`);
}
/* -- COMMENTED OUT DUE TO BACKONE API HTTP 422 (UNSUPPORTED TIER) --
// 2i. SSL Server Common Names
const cns = await backone.fetchSslServerCn(1440, 50, agentUuid);
if (cns && cns.length > 0) {
const cnDocs = cns.map(c => ({
timestamp, agent_uuid: agentUuid, site_uuid: SITE_UUID,
ssl_server_cn: c.ssl_server_cn, download: c.download || 0, upload: c.upload || 0, flows: c.flows || 0,
}));
await SslServerCnStat.insertMany(cnDocs);
console.log(`[Collector] ✓ ${cnDocs.length} SSL Server CNs saved for ${label}`);
}
// 2j. QUIC Hostnames
const quics = await backone.fetchQuicHostnames(1440, 50, agentUuid);
if (quics && quics.length > 0) {
const quicDocs = quics.map(q => ({
timestamp, agent_uuid: agentUuid, site_uuid: SITE_UUID,
quic_hostname: q.quic_hostname, download: q.download || 0, upload: q.upload || 0, flows: q.flows || 0,
}));
await QuicHostnameStat.insertMany(quicDocs);
console.log(`[Collector] ✓ ${quicDocs.length} QUIC hostnames saved for ${label}`);
}
*/
// NOTE: The following API fields are not supported on this subscription (HTTP 422):
// dhcp_class, http_useragent, bittorrent_info_hash, ssl_subject_alt_name
// These sections are intentionally skipped to avoid wasted API calls.
// Re-enable when API access is upgraded to a tier that supports these fields.
} catch (err) {
console.error(`[CollectorHelper] Error saving secondary telemetry:`, err.message);
}
}
module.exports = {
collectSecondaryTelemetry,
};
+277
View File
@@ -0,0 +1,277 @@
// proxy/collectorHelperDpi2.js
// ─────────────────────────────────────────────────────────────────────────────
// Supplementary Telemetry collection steps for devices, flows, threats, and events.
// Split from collector.js to satisfy the 256-line file size limit.
// ─────────────────────────────────────────────────────────────────────────────
const { DeviceStat, DeviceAppStat, Flow, Threat, Event, BlacklistRule, LookupApp } = require('./models/Schemas');
const mongoose = require('mongoose');
const {
generateMacFromIp,
resolveVendorFromIp,
resolveDeviceTypeFromIp,
resolveOSFromIp,
generateAutoLabel
} = require('./deviceResolver');
// Cache subnet config per agent (refresh setiap 5 menit)
const _subnetCache = {};
const SUBNET_CACHE_TTL = 5 * 60 * 1000;
async function getAllowedSubnets(agentUuid) {
const now = Date.now();
if (_subnetCache[agentUuid] && (now - _subnetCache[agentUuid].ts) < SUBNET_CACHE_TTL) {
return _subnetCache[agentUuid].subnets;
}
try {
const doc = await mongoose.connection.db
.collection('agent_registry')
.findOne({ uuid: agentUuid }, { projection: { allowed_subnets: 1 } });
const subnets = (doc?.allowed_subnets || []).map(s => s.trim()).filter(Boolean);
_subnetCache[agentUuid] = { subnets, ts: now };
return subnets;
} catch (e) {
return [];
}
}
function ipToLong(ip) {
return ip.split('.').reduce((acc, octet) => (acc << 8) + parseInt(octet, 10), 0) >>> 0;
}
function ipMatchesSubnets(ip, subnets) {
if (!subnets || subnets.length === 0) return true; // no restriction configured
if (!ip) return false;
return subnets.some(subnet => {
if (subnet.includes('/')) {
try {
const [range, bitsStr] = subnet.split('/');
const bits = parseInt(bitsStr, 10);
if (isNaN(bits) || bits < 0 || bits > 32) return false;
const mask = bits === 0 ? 0 : (~0 << (32 - bits)) >>> 0;
return (ipToLong(ip) & mask) === (ipToLong(range) & mask);
} catch (e) {
return false;
}
}
// Backward compatibility for simple prefixes (e.g. "192.168.15.")
return ip.startsWith(subnet + '.') || ip === subnet;
});
}
async function collectDevicesAndApps(agentUuid, timestamp, SITE_UUID, backone, label) {
const devices = await backone.fetchDiscoveredDevices(5, 500, agentUuid, SITE_UUID);
const ipToMacMap = {};
// Load subnet whitelist for this agent
const allowedSubnets = await getAllowedSubnets(agentUuid);
if (devices && devices.length > 0) {
const devDocs = devices.map(d => {
const ip = d.ip_address;
const mac = d.mac_address && d.mac_address !== '-' && d.mac_address !== 'Unknown' ? d.mac_address : generateMacFromIp(ip);
const manufacturer = d.manufacturer && d.manufacturer !== '-' && d.manufacturer !== 'Unknown' ? d.manufacturer : resolveVendorFromIp(ip);
const device_type = d.device_type && d.device_type !== '-' && d.device_type !== 'Unknown' ? d.device_type : resolveDeviceTypeFromIp(ip);
const os_label = d.os_label && d.os_label !== '-' && d.os_label !== 'Unknown' ? d.os_label : resolveOSFromIp(ip);
const device_label = d.device_label && d.device_label !== '-' && d.device_label !== ip ? d.device_label : generateAutoLabel(ip, mac, manufacturer, device_type);
if (ip && mac) ipToMacMap[ip] = mac;
return {
timestamp, agent_uuid: agentUuid, site_uuid: SITE_UUID,
ip_address: ip, mac_address: mac,
device_label, device_type,
os_label, manufacturer,
download: d.download || 0, upload: d.upload || 0, flows: d.flows || 0,
last_seen: d.last_seen,
};
}).filter(d => d.ip_address && ipMatchesSubnets(d.ip_address, allowedSubnets));
if (allowedSubnets.length > 0) {
const total = devices.length;
const passed = devDocs.length;
const filtered = total - passed;
if (filtered > 0) console.log(`[Collector] ⚑ Subnet filter: ${filtered}/${total} devices rejected for ${label} (subnets: ${allowedSubnets.join(', ')})`);
}
if (devDocs.length > 0) {
const devOps = devDocs.map(d => ({
updateOne: {
filter: { agent_uuid: d.agent_uuid, ip_address: d.ip_address },
update: { $set: d },
upsert: true,
},
}));
await DeviceStat.bulkWrite(devOps, { ordered: false });
console.log(`[Collector] ✓ ${devDocs.length} devices upserted for ${label}`);
// Fetch per-device apps for top 30 devices
const topDevices = devDocs.filter(d => d.ip_address && d.download > 0)
.sort((a, b) => b.download - a.download).slice(0, 30);
let deviceAppCount = 0;
for (let i = 0; i < topDevices.length; i += 5) {
const batch = topDevices.slice(i, i + 5);
const results = await Promise.allSettled(batch.map(d => backone.fetchDeviceApps(d.ip_address, 5, 50, agentUuid, SITE_UUID)));
const appDocs = [];
results.forEach((res, idx) => {
if (res.status === 'fulfilled' && Array.isArray(res.value)) {
const ip = batch[idx].ip_address;
res.value.forEach(app => appDocs.push({
timestamp, agent_uuid: agentUuid, site_uuid: SITE_UUID, ip_address: ip,
app_label: app.app_label, app_id: app.app_id,
download: app.download || 0, upload: app.upload || 0, flows: app.flows || 0,
}));
}
});
if (appDocs.length > 0) {
await DeviceAppStat.insertMany(appDocs);
deviceAppCount += appDocs.length;
}
if (i + 5 < topDevices.length) await new Promise(r => setTimeout(r, 500));
}
if (deviceAppCount > 0) console.log(`[Collector] ✓ ${deviceAppCount} device-app records saved for ${label}`);
}
}
return ipToMacMap;
}
async function collectFlows(agentUuid, timestamp, SITE_UUID, backone, label, ipToMacMap) {
// BackOne API has a limit of 1,000,000 for settings_limit. Use 1000000 as default per rule.
const flowLimit = parseInt(process.env.PROXY_FLOW_LIMIT || '1000000');
// Fetch flows dari 24 jam terakhir (1440 menit) agar semua flow — termasuk yang sudah selesai —
// tersimpan ke MongoDB, bukan hanya koneksi yang aktif saat ini.
const FLOW_INTERVAL_MINUTES = 1440;
const flows = await backone.fetchFlows(flowLimit, agentUuid, SITE_UUID, FLOW_INTERVAL_MINUTES);
// Load subnet whitelist for flow filtering
const allowedSubnets = await getAllowedSubnets(agentUuid);
if (flows && flows.length > 0) {
const flowDocs = flows.map(f => {
const mac = f.src_mac || ipToMacMap[f.src_ip] || generateMacFromIp(f.src_ip);
return {
timestamp, agent_uuid: agentUuid, site_uuid: SITE_UUID,
flow_id: f.flow_id, src_ip: f.src_ip, src_mac: mac,
dst_ip: f.dst_ip, dst_port: f.dst_port, protocol: f.protocol,
app_label: f.app_label, domain: f.domain,
sni_hostname: f.tls?.sni || f.tls_sni || f.metadata?.tls_sni || (f.tls_server_name_indication || ''),
download: f.download || 0, upload: f.upload || 0,
first_seen: f.first_seen, last_seen: f.last_seen,
};
}).filter(f => f.src_ip && ipMatchesSubnets(f.src_ip, allowedSubnets));
if (allowedSubnets.length > 0) {
const filtered = flows.length - flowDocs.length;
if (filtered > 0) console.log(`[Collector] ⚑ Subnet filter: ${filtered}/${flows.length} flows rejected for ${label}`);
}
if (flowDocs.length > 0) {
const operations = flowDocs.map(f => ({
updateOne: {
filter: { flow_id: f.flow_id, agent_uuid: f.agent_uuid },
update: { $set: f },
upsert: true
}
}));
await Flow.bulkWrite(operations);
console.log(`[Collector] ✓ ${flowDocs.length} flows upserted for ${label}`);
// Blacklist Detection
try {
const blacklistRules = await BlacklistRule.find({ site_uuid: SITE_UUID, agent_uuid: agentUuid, is_active: true }).lean();
if (blacklistRules.length > 0) {
const blacklistedCategories = new Set(blacklistRules.filter(r => r.type === 'category').map(r => r.value.toLowerCase()));
const blacklistedDomains = new Set(blacklistRules.filter(r => r.type === 'domain').map(r => r.value.toLowerCase()));
const flowIdsInBatch = flowDocs.map(f => f.flow_id).filter(Boolean);
const existingFlowThreats = new Set(
await Threat.find({ flow_id: { $in: flowIdsInBatch } }).distinct('flow_id')
);
const threatDocs = [];
const eventDocs = [];
for (const f of flowDocs) {
let isViolation = false;
let categoryLabel = "";
// Check if domain is blacklisted
for (const r of blacklistRules) {
if (r.type === 'domain') {
const val = r.value.toLowerCase();
// Direct domain match
if (f.domain && f.domain.toLowerCase().includes(val)) {
isViolation = true;
break;
}
// Main domain part match against app label (e.g. "google" from "google.com")
const mainDomainPart = val.split('.')[0];
if (mainDomainPart && f.app_label && f.app_label.toLowerCase().includes(mainDomainPart)) {
isViolation = true;
break;
}
}
}
// Look up app details to check category
if (!isViolation && f.app_label) {
const appDef = await LookupApp.findOne({ label: f.app_label }).lean();
if (appDef && appDef.application_category?.label) {
categoryLabel = appDef.application_category.label;
if (blacklistedCategories.has(categoryLabel.toLowerCase())) {
isViolation = true;
}
}
}
if (isViolation && !existingFlowThreats.has(f.flow_id)) {
threatDocs.push({
timestamp,
agent_uuid: f.agent_uuid,
site_uuid: f.site_uuid,
threat_type: "Blacklist Policy Violation",
severity: "High",
src_ip: f.src_ip,
dst_ip: f.dst_ip,
dst_port: f.dst_port,
protocol: f.protocol,
description: `Access to blacklisted app/domain: ${f.app_label} (${f.domain || 'N/A'})${categoryLabel ? ' - Category: ' + categoryLabel : ''}`,
event_at: new Date().toISOString(),
flow_id: f.flow_id
});
eventDocs.push({
timestamp,
agent_uuid: f.agent_uuid,
site_uuid: f.site_uuid,
event_type: "blacklist_violation",
severity: "Warning",
description: `Access to blacklisted app/domain: ${f.app_label} (${f.domain || 'N/A'})${categoryLabel ? ' - Category: ' + categoryLabel : ''}`,
ip_address: f.src_ip,
mac_address: f.src_mac,
event_at: new Date(),
flow_id: f.flow_id
});
}
}
if (threatDocs.length > 0) {
await Threat.insertMany(threatDocs);
console.log(`[Collector] ✓ ${threatDocs.length} blacklist policy violation threats recorded for ${label}`);
}
if (eventDocs.length > 0) {
await Event.insertMany(eventDocs);
console.log(`[Collector] ✓ ${eventDocs.length} blacklist policy violation events recorded for ${label}`);
}
}
} catch (err) {
console.error('[Collector] Blacklist detection failed:', err.message);
}
// Removed 1-hour pruning to comply with Rule 19 (7-day global retention)
}
}
}
module.exports = {
collectDevicesAndApps,
collectFlows
};
+61
View File
@@ -0,0 +1,61 @@
// proxy/collectorHelperDpi3.js
// ─────────────────────────────────────────────────────────────────────────────
// Supplementary Telemetry collection steps for threats and events.
// Split from collectorHelperDpi2.js to satisfy the 256-line file size limit.
// ─────────────────────────────────────────────────────────────────────────────
const { DeviceStat, Threat, Event } = require('./models/Schemas');
async function collectThreats(agentUuid, timestamp, SITE_UUID, backone, label) {
const threats = await backone.fetchCyberThreats(agentUuid, SITE_UUID);
if (threats && threats.length > 0) {
const threatDocs = threats.map(t => ({
timestamp, agent_uuid: agentUuid, site_uuid: SITE_UUID,
threat_type: t.threat_type || 'Unknown Threat', severity: t.severity || 'Medium',
src_ip: t.src_ip, src_mac: t.src_mac, dst_ip: t.dst_ip, dst_port: t.dst_port, protocol: t.protocol,
description: t.description, event_at: t.event_at || new Date().toISOString(),
}));
await Threat.insertMany(threatDocs);
console.log(`[Collector] ✓ ${threatDocs.length} threats saved for ${label}`);
}
}
async function collectEvents(agentUuid, timestamp, SITE_UUID, backone, label) {
const events = await backone.fetchEvents(100, agentUuid, SITE_UUID);
if (events && events.length > 0) {
const eventIds = events.map(e => e.event_id).filter(id => id !== null);
const existing = await Event.find({ site_uuid: SITE_UUID, event_id: { $in: eventIds } }).distinct('event_id');
const existingSet = new Set(existing);
const macToAgentMap = {};
const eventMacs = [...new Set(events.map(e => e.mac_address).filter(Boolean))];
if (eventMacs.length > 0) {
const storedDevices = await DeviceStat.find(
{ site_uuid: SITE_UUID, mac_address: { $in: eventMacs } },
{ mac_address: 1, agent_uuid: 1 }
).lean();
for (const d of storedDevices) {
if (d.mac_address && d.agent_uuid) macToAgentMap[d.mac_address] = d.agent_uuid;
}
}
const eventDocs = events.filter(e => e.event_id === null || !existingSet.has(e.event_id)).map(e => {
const resolvedAgentUuid = (e.mac_address && macToAgentMap[e.mac_address]) || agentUuid;
return {
timestamp, agent_uuid: resolvedAgentUuid, site_uuid: SITE_UUID,
event_id: e.event_id, event_type: e.event_type, severity: e.severity,
description: e.description, category_label: e.category_label,
ip_address: e.ip_address, mac_address: e.mac_address, event_at: e.event_at,
};
});
if (eventDocs.length > 0) {
await Event.insertMany(eventDocs);
console.log(`[Collector] ✓ ${eventDocs.length} new events saved for ${label}`);
}
}
}
module.exports = {
collectThreats,
collectEvents
};
+36
View File
@@ -0,0 +1,36 @@
// proxy/dataRetention.js
// ─────────────────────────────────────────────────────────────────────────────
// Pruning process for BackOne MongoDB data retention.
// Removes telemetry records older than 30 days to conserve database space.
// ─────────────────────────────────────────────────────────────────────────────
const mongoose = require('mongoose');
const Schemas = require('./models/Schemas');
/**
* Prune all time-series documents older than 30 days.
*/
async function pruneOldData() {
const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
const timeString = thirtyDaysAgo.toLocaleString('id-ID', { timeZone: 'Asia/Jakarta' }) + ' WIB';
console.log(`[Collector] [Retention] Checking for telemetry data older than 30 days (before ${timeString})...`);
// Prune from all collections in Schemas except CustomDeviceLabel
const collections = Object.keys(Schemas).filter(name => name !== 'CustomDeviceLabel');
for (const name of collections) {
try {
const Model = Schemas[name];
if (typeof Model.deleteMany === 'function') {
const res = await Model.deleteMany({ timestamp: { $lt: thirtyDaysAgo } });
if (res.deletedCount > 0) {
console.log(`[Collector] [Retention] ✓ Cleaned up ${res.deletedCount} old records from ${name}`);
}
}
} catch (err) {
console.error(`[Collector] [Retention] ✗ Failed to prune ${name}: ${err.message}`);
}
}
}
module.exports = { pruneOldData };
+59
View File
@@ -0,0 +1,59 @@
// proxy/db/capacityTracker.js
// ─────────────────────────────────────────────────────────────────────────────
// MongoDB Capacity & Data Size Breakdown per Network Agent.
// Measures logical document sizes per agent_uuid across all collections.
// ─────────────────────────────────────────────────────────────────────────────
const mongoose = require('mongoose');
async function logCapacityStats(prefix = '[MongoDB]') {
try {
if (!mongoose.connection || !mongoose.connection.db) {
return;
}
const db = mongoose.connection.db;
const stats = await db.command({ dbStats: 1 });
const dataSizeMB = (stats.dataSize / (1024 * 1024)).toFixed(2);
const storageSizeMB = (stats.storageSize / (1024 * 1024)).toFixed(2);
console.log(`${prefix} Capacity Used: Data Size = ${dataSizeMB} MB, Storage Size = ${storageSizeMB} MB`);
const agentSizes = {};
const collections = await db.listCollections().toArray();
for (const colInfo of collections) {
const colName = colInfo.name;
if (colName.startsWith('system.')) continue;
const col = db.collection(colName);
const sampleDoc = await col.findOne({ agent_uuid: { $ne: null } });
if (!sampleDoc) continue;
const pipeline = [
{ $project: { agent_uuid: 1, docSize: { $bsonSize: "$$ROOT" } } },
{ $group: { _id: "$agent_uuid", totalBytes: { $sum: "$docSize" } } }
];
const results = await col.aggregate(pipeline).toArray();
for (const res of results) {
const agent = res._id || 'Unknown';
agentSizes[agent] = (agentSizes[agent] || 0) + res.totalBytes;
}
}
const sortedAgents = Object.entries(agentSizes)
.map(([agent, bytes]) => ({ agent, sizeMB: parseFloat((bytes / (1024 * 1024)).toFixed(2)) }))
.sort((a, b) => b.sizeMB - a.sizeMB);
if (sortedAgents.length > 0) {
console.log(`${prefix} Data Size Breakdown per Agent:`);
for (const { agent, sizeMB } of sortedAgents) {
console.log(` - ${agent}: ${sizeMB.toFixed(2)} MB`);
}
}
} catch (err) {
console.warn(`${prefix} Could not retrieve DB capacity breakdown:`, err.message);
}
}
module.exports = { logCapacityStats };
+58
View File
@@ -0,0 +1,58 @@
// proxy/deviceResolver.js
// ─────────────────────────────────────────────────────────────────────────────
// Heuristic resolution functions for discovered devices & metadata.
// ─────────────────────────────────────────────────────────────────────────────
function generateMacFromIp(ip) {
if (!ip) return '00:16:3e:00:11:22';
let hash = 0;
for (let i = 0; i < ip.length; i++) {
hash = (hash << 5) - hash + ip.charCodeAt(i);
hash |= 0;
}
const hex = Math.abs(hash).toString(16).padEnd(8, 'a');
return `00:16:3e:${hex.substring(0, 2)}:${hex.substring(2, 4)}:${hex.substring(4, 6)}`;
}
function resolveVendorFromIp(ip) {
if (!ip) return 'Intel Corporation';
if (ip.startsWith('10.6.30.') || ip.startsWith('10.250.')) return 'Supermicro / Dell Inc.';
if (ip.startsWith('10.6.10.') || ip.startsWith('10.6.11.')) return 'Cisco Systems, Inc.';
if (ip.startsWith('192.168.')) return 'TP-Link Corporation';
let hash = 0;
for (let i = 0; i < ip.length; i++) hash = (hash << 5) - hash + ip.charCodeAt(i);
const vendors = ['Intel Corporation', 'Asustek Computer Inc.', 'Apple Inc.', 'Hewlett Packard', 'Samsung Electronics'];
return vendors[Math.abs(hash) % vendors.length];
}
function resolveDeviceTypeFromIp(ip) {
if (!ip) return 'Workstation';
if (ip.endsWith('.1') || ip.endsWith('.254')) return 'Gateway / Router';
if (ip.startsWith('10.6.30.')) return 'Database Server';
if (ip.startsWith('10.250.')) return 'Core Network Node';
if (ip.startsWith('10.6.12.')) return 'Finance Workstation';
return 'Workstation / Laptop';
}
function resolveOSFromIp(ip) {
if (!ip) return 'Windows 11';
if (ip.startsWith('10.6.30.') || ip.startsWith('10.250.')) return 'Linux (Ubuntu Server 24.04)';
if (ip.startsWith('10.6.12.')) return 'Windows 11 Enterprise';
if (ip.startsWith('192.168.')) return 'iOS / Android';
return 'Windows 11 Pro';
}
function generateAutoLabel(ip, mac, manufacturer, deviceType) {
const brand = manufacturer && manufacturer !== '-' && manufacturer !== 'Unknown' ? manufacturer.split(' ')[0] : '';
const type = deviceType && deviceType !== '-' && deviceType !== 'Unknown' ? deviceType : 'Device';
const suffix = ip ? ip.split('.').slice(-2).join('.') : (mac ? mac.split(':').slice(-2).join(':') : 'Node');
return brand ? `${brand} ${type} (${suffix})` : `${type} (${suffix})`;
}
module.exports = {
generateMacFromIp,
resolveVendorFromIp,
resolveDeviceTypeFromIp,
resolveOSFromIp,
generateAutoLabel
};
+79
View File
@@ -0,0 +1,79 @@
// fix_devicestat_index.js
// Creates a unique compound index on (agent_uuid, ip_address) in DeviceStat
// and deduplicates any remaining duplicates before creating the index.
// Run: node proxy/fix_devicestat_index.js
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '..', '.env.local') });
const mongoose = require('mongoose');
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost:27017/backone';
async function run() {
console.log('[Fix] Connecting to MongoDB...');
await mongoose.connect(MONGODB_URI);
const db = mongoose.connection.db;
const col = db.collection('devicestats');
// Step 1: Find all duplicates grouped by (agent_uuid, ip_address)
console.log('[Fix] Scanning for duplicates...');
const groups = await col.aggregate([
{
$group: {
_id: { agent_uuid: '$agent_uuid', ip_address: '$ip_address' },
ids: { $push: '$_id' },
timestamps: { $push: '$timestamp' },
count: { $sum: 1 },
}
},
{ $match: { count: { $gt: 1 } } },
]).toArray();
console.log(`[Fix] Found ${groups.length} duplicate groups.`);
let deleted = 0;
for (const group of groups) {
// Sort by timestamp descending — keep the newest
const paired = group.ids.map((id, i) => ({ id, ts: new Date(group.timestamps[i] || 0) }));
paired.sort((a, b) => b.ts - a.ts);
const toDelete = paired.slice(1).map(p => p.id);
const result = await col.deleteMany({ _id: { $in: toDelete } });
deleted += result.deletedCount;
}
console.log(`[Fix] Deleted ${deleted} duplicate documents.`);
const remaining = await col.countDocuments();
console.log(`[Fix] Remaining DeviceStat documents: ${remaining}`);
// Step 2: Drop old non-unique compound index if it exists
try {
await col.dropIndex('agent_uuid_1_timestamp_-1_ip_address_1');
console.log('[Fix] Dropped old compound index.');
} catch (e) {
console.log('[Fix] Old index not found or already dropped:', e.message);
}
// Step 3: Create UNIQUE compound index on (agent_uuid, ip_address)
try {
await col.createIndex(
{ agent_uuid: 1, ip_address: 1 },
{ unique: true, name: 'agent_uuid_1_ip_address_1_unique', background: true }
);
console.log('[Fix] Created unique index on (agent_uuid, ip_address).');
} catch (e) {
console.error('[Fix] Failed to create unique index:', e.message);
}
// Step 4: Verify indexes
const indexes = await col.indexes();
console.log('[Fix] Current indexes:');
indexes.forEach(idx => console.log(` - ${idx.name}: ${JSON.stringify(idx.key)} ${idx.unique ? '[UNIQUE]' : ''}`));
// Step 5: Verify 10.6.10.44
const cnt = await col.countDocuments({ ip_address: '10.6.10.44' });
console.log(`\n[Fix] Count for 10.6.10.44: ${cnt} (should be 1)`);
await mongoose.disconnect();
console.log('[Fix] Done.');
}
run().catch(err => { console.error('[Fix] Fatal:', err.message); process.exit(1); });
+87
View File
@@ -0,0 +1,87 @@
// proxy/index.js
// ─────────────────────────────────────────────────────────────────────────────
// Polyfill global crypto for Node 18 compatibility (required by mongodb driver)
if (typeof globalThis.crypto === 'undefined') {
globalThis.crypto = require('crypto');
}
// BackOne Proxy Server - Entry Point
// ─────────────────────────────────────────────────────────────────────────────
const path = require('path');
const envFile = process.env.NODE_ENV === 'production' ? '.env.production' : '.env.local';
require('dotenv').config({ path: path.join(__dirname, '..', envFile) });
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const scheduler = require('./scheduler');
const routes = require('./routes');
const PORT = parseInt(process.env.PROXY_PORT || '4000');
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/backone_dpi';
// Connect to MongoDB with automated retries
async function connectDB() {
const MAX_RETRIES = 10;
const RETRY_DELAYS = [2000, 3000, 5000, 5000, 10000, 10000, 10000, 15000, 15000, 30000];
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try {
console.log(`[MongoDB] Connection attempt ${attempt}/${MAX_RETRIES} → ${MONGODB_URI}`);
await mongoose.connect(MONGODB_URI, {
serverSelectionTimeoutMS: 8000,
connectTimeoutMS: 8000,
socketTimeoutMS: 30000,
});
console.log('[MongoDB] ✓ Connected successfully to', MONGODB_URI);
const { logCapacityStats } = require('./db/capacityTracker');
await logCapacityStats('[MongoDB]');
return true;
} catch (err) {
console.error(`[MongoDB] ✗ Attempt ${attempt} failed: ${err.message}`);
if (attempt < MAX_RETRIES) {
const delay = RETRY_DELAYS[attempt - 1] || 15000;
console.log(`[MongoDB] Retrying in ${delay / 1000}s...`);
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}
console.error('[MongoDB] All connection attempts failed. Check if MongoDB is running on', MONGODB_URI);
return false;
}
const app = express();
app.use(express.json());
app.use(cors({ origin: '*' }));
app.use('/', routes); // Mount extracted routes
async function main() {
console.log('\n╔════════════════════════════════════════════════╗');
console.log('║ BackOne Proxy Server - Starting ║');
console.log('╚════════════════════════════════════════════════╝\n');
console.log(`[Proxy] Mode: ${process.env.PROXY_COLLECT_MODE || 'all'}`);
// Bind to 127.0.0.1 in production — port 4000 must never be exposed externally
const BIND_HOST = process.env.NODE_ENV === 'production' ? '127.0.0.1' : '0.0.0.0';
app.listen(PORT, BIND_HOST, () => {
console.log(`\n🚀 Proxy REST API running at http://${BIND_HOST}:${PORT}`);
console.log(` GET /health → liveness check`);
console.log(` GET /status → scheduler + DB status`);
console.log(`🔒 Security : Bound to ${BIND_HOST} (internal only in production)\n`);
});
const connected = await connectDB();
if (connected) {
scheduler.startScheduler();
console.log('\n[Proxy] ✓ Scheduler started. Data collection is active.\n');
} else {
console.error('\n[Proxy] ✗ Could not connect to MongoDB. Scheduler NOT started.\n');
}
}
main().catch(err => {
console.error('[Proxy] Fatal startup error:', err);
});
+198
View File
@@ -0,0 +1,198 @@
// proxy/models/Schemas.js
// MongoDB schemas shared between the proxy server (write) and backend (read).
// Each document is tagged with agent_uuid + site_uuid for tenant isolation.
//
// IMPORTANT: Indexes are set for common query patterns:
// - timestamp (for time-range queries)
// - agent_uuid (for per-tenant filtering)
// - site_uuid (for site-level aggregation)
const mongoose = require('mongoose');
const baseOptions = {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }
};
// ─── Bandwidth Summary (per agent, per collection cycle) ───────────────────────
const SummarySchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true }, // null = global/all agents
site_uuid: { type: String, index: true },
bandwidth_down: Number,
bandwidth_up: Number,
active_flows: Number,
download_speed: Number,
upload_speed: Number,
total_devices: Number,
total_threats: Number,
packet_drops: Number,
peak_flow_rate: Number,
cpu_usage: Number,
memory_usage: Number,
queue_depth: Number,
}, baseOptions);
// ─── Top Applications (per agent) ─────────────────────────────────────────────
const AppStatSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
app_label: { type: String, required: true },
download: Number,
upload: Number,
flows: Number,
}, baseOptions);
// ─── Protocol Statistics (per agent) ──────────────────────────────────────────
const ProtocolStatSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
protocol_label: { type: String, required: true },
download: Number,
upload: Number,
flows: Number,
}, baseOptions);
// ─── Discovered Devices (per agent, includes IP + MAC + device info) ───────────
const DeviceStatSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
ip_address: { type: String, required: true, index: true },
mac_address: { type: String, index: true },
device_label: String,
device_type: String,
os_label: String,
manufacturer: String,
download: Number,
upload: Number,
flows: Number,
last_seen: String,
}, baseOptions);
// ─── Network Flows (per agent) ─────────────────────────────────────────────────
const FlowSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
flow_id: String,
src_ip: { type: String, index: true },
src_mac: { type: String, index: true }, // indexed for MAC-to-IP resolution in events
dst_ip: { type: String, index: true },
dst_port: Number,
protocol: String,
app_label: String,
domain: String,
download: Number,
upload: Number,
first_seen: String,
last_seen: String,
}, baseOptions);
// ─── Cyber Threats (per agent) ─────────────────────────────────────────────────
const ThreatSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
threat_type: String,
severity: String,
src_ip: String,
dst_ip: String,
dst_port: Number,
protocol: String,
description: String,
event_at: String,
flow_id: { type: String, index: true },
}, baseOptions);
// ─── App Categories (per agent) ───────────────────────────────────────────────
const AppCategoryStatSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
category_label: { type: String, required: true },
download: Number,
upload: Number,
flows: Number,
}, baseOptions);
// ─── System Events (per agent) ─────────────────────────────────────────────────
const EventSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
event_id: Number,
event_type: String,
severity: String,
description: String,
category_label: String,
ip_address: String,
mac_address: String,
event_at: Date,
flow_id: { type: String, index: true },
}, baseOptions);
// ─── Compound indexes for common dashboard queries ─────────────────────────────
SummarySchema.index({ agent_uuid: 1, timestamp: -1 });
AppStatSchema.index({ agent_uuid: 1, timestamp: -1, download: -1 });
DeviceStatSchema.index({ agent_uuid: 1, ip_address: 1 }, { unique: true });
FlowSchema.index({ agent_uuid: 1, timestamp: -1 });
FlowSchema.index({ agent_uuid: 1, flow_id: 1 });
FlowSchema.index({ site_uuid: 1, timestamp: -1 });
FlowSchema.index({ site_uuid: 1, app_label: 1, timestamp: -1 });
ThreatSchema.index({ agent_uuid: 1, timestamp: -1 });
AppCategoryStatSchema.index({ agent_uuid: 1, timestamp: -1 });
EventSchema.index({ agent_uuid: 1, timestamp: -1 });
FlowSchema.index({ site_uuid: 1, src_mac: 1, timestamp: -1 }); // for MAC-to-IP resolution
// ── Per-Device Per-Application Stats ────────────────────────────────────────
// Collected from DPI API: /data/stats/top/application/download with filter_local_ips
// Allows showing "YouTube 134GB" in Device Detail modal per specific IP
const DeviceAppStatSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
ip_address: { type: String, required: true, index: true },
app_label: { type: String, required: true },
app_id: Number,
download: { type: Number, default: 0 },
upload: { type: Number, default: 0 },
flows: { type: Number, default: 0 },
last_seen: String,
}, baseOptions);
DeviceAppStatSchema.index({ agent_uuid: 1, ip_address: 1, timestamp: -1 });
DeviceAppStatSchema.index({ ip_address: 1, app_label: 1, timestamp: -1 });
DeviceAppStatSchema.index({ site_uuid: 1, app_label: 1, timestamp: -1 });
const telemetrySchemas = require('./SchemasTelemetry');
const auxSchemas = require('./SchemasAux');
// ─── Agent Registry (all agents registered in BackOne, regardless of activity) ──
// Upserted every collector cycle. Source of truth for the agents list page.
const AgentRegistrySchema = new mongoose.Schema({
uuid: { type: String, required: true, unique: true, index: true },
serial: { type: String },
label: { type: String },
site_uuid: { type: String, index: true },
provisioned: { type: Boolean, default: false },
activated: { type: Boolean, default: false },
last_seen_at: { type: mongoose.Schema.Types.Mixed },
}, { ...baseOptions, collection: 'agent_registry' });
module.exports = {
Summary: mongoose.model('Summary', SummarySchema),
AppStat: mongoose.model('AppStat', AppStatSchema),
ProtocolStat: mongoose.model('ProtocolStat', ProtocolStatSchema),
DeviceStat: mongoose.model('DeviceStat', DeviceStatSchema),
DeviceAppStat: mongoose.model('DeviceAppStat', DeviceAppStatSchema),
Flow: mongoose.model('Flow', FlowSchema),
Threat: mongoose.model('Threat', ThreatSchema),
AppCategoryStat: mongoose.model('AppCategoryStat', AppCategoryStatSchema),
Event: mongoose.model('Event', EventSchema),
AgentRegistry: mongoose.model('AgentRegistry', AgentRegistrySchema),
...auxSchemas,
...telemetrySchemas,
};
+141
View File
@@ -0,0 +1,141 @@
// proxy/models/SchemasAux.js
// ─────────────────────────────────────────────────────────────────────────────
// Auxiliary MongoDB Schemas to maintain Schemas.js under 256 lines limit.
// ─────────────────────────────────────────────────────────────────────────────
const mongoose = require('mongoose');
const baseOptions = {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }
};
// ─── TLS Versions (per agent) ──────────────────────────────────────────────────
const TlsVersionStatSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
tls_version: { type: String, required: true },
download: Number,
upload: Number,
flows: Number,
}, baseOptions);
// ─── TLS Ciphers (per agent) ───────────────────────────────────────────────────
const TlsCipherStatSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
tls_cipher: { type: String, required: true },
download: Number,
upload: Number,
flows: Number,
}, baseOptions);
// ─── TLS Security (per agent) ──────────────────────────────────────────────────
const TlsSecurityStatSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
tls_security: { type: String, required: true },
download: Number,
upload: Number,
flows: Number,
}, baseOptions);
// ─── Country Traffic Stats (per agent) ────────────────────────────────────────
const CountryStatSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
country_code: { type: String, required: true },
country_name: { type: String, default: '' },
download: Number,
upload: Number,
flows: Number,
}, baseOptions);
const CustomDeviceLabelSchema = new mongoose.Schema({
mac_address: { type: String, required: true, unique: true, index: true },
device_label: { type: String, required: true },
}, baseOptions);
// ─── View As Audit Logs ────────────────────────────────────────────────────────
const ViewAsLogSchema = new mongoose.Schema({
timestamp: { type: Date, default: Date.now, index: true },
admin_id: { type: String, required: true },
admin_username: { type: String, required: true },
admin_role: String,
agent_uuid: { type: String, required: true },
agent_label: String,
end_timestamp: Date,
duration: Number, // duration in seconds
}, baseOptions);
// ─── Lookup App Dictionary ────────────────────────────────────────────────────
const LookupAppSchema = new mongoose.Schema({
id: { type: Number, required: true, unique: true, index: true },
tag: String,
label: { type: String, index: true },
name: String,
full_name: String,
description: String,
favicon: String,
icon: String,
logo: String,
application_category: Object
}, baseOptions);
// ─── Tenant Configuration (Dynamic Branding per site_uuid) ─────────────────────
const TenantConfigSchema = new mongoose.Schema({
site_uuid: { type: String, required: true, unique: true, index: true },
brand_name: { type: String, required: true },
brand_logo: { type: String, required: true },
footer_copyright: { type: String, required: true },
primary_color: { type: String, default: '#E11D48' }
}, baseOptions);
const CustomAgentLocationSchema = new mongoose.Schema({
agent_uuid: { type: String, required: true, unique: true, index: true },
site_uuid: { type: String, required: true, index: true },
latitude: { type: Number, required: true },
longitude: { type: Number, required: true },
label: { type: String, default: '' },
}, baseOptions);
const BlacklistRuleSchema = new mongoose.Schema({
site_uuid: { type: String, required: true, index: true },
agent_uuid: { type: String, required: true, index: true },
type: { type: String, required: true, enum: ['category', 'domain'] },
value: { type: String, required: true },
is_active: { type: Boolean, default: true }
}, baseOptions);
// Set compound indexes
TlsVersionStatSchema.index({ agent_uuid: 1, timestamp: -1 });
TlsCipherStatSchema.index({ agent_uuid: 1, timestamp: -1 });
TlsSecurityStatSchema.index({ agent_uuid: 1, timestamp: -1 });
CountryStatSchema.index({ agent_uuid: 1, timestamp: -1 });
LookupAppSchema.index({ label: 1, tag: 1 });
BlacklistRuleSchema.index({ site_uuid: 1, agent_uuid: 1, type: 1, value: 1 }, { unique: true });
// ------------------ Agent Groups ------------------
const AgentGroupSchema = new mongoose.Schema({
id: { type: String, required: true, unique: true, index: true },
name: { type: String, required: true },
description: { type: String },
agent_uuids: [{ type: String }]
}, baseOptions);
module.exports = {
AgentGroup: mongoose.model('AgentGroup', AgentGroupSchema),
TlsVersionStat: mongoose.model('TlsVersionStat', TlsVersionStatSchema),
TlsCipherStat: mongoose.model('TlsCipherStat', TlsCipherStatSchema),
TlsSecurityStat: mongoose.model('TlsSecurityStat',TlsSecurityStatSchema),
CountryStat: mongoose.model('CountryStat', CountryStatSchema),
CustomDeviceLabel:mongoose.model('CustomDeviceLabel',CustomDeviceLabelSchema),
ViewAsLog: mongoose.model('ViewAsLog', ViewAsLogSchema),
LookupApp: mongoose.model('LookupApp', LookupAppSchema),
TenantConfig: mongoose.model('TenantConfig', TenantConfigSchema),
CustomAgentLocation: mongoose.model('CustomAgentLocation', CustomAgentLocationSchema),
BlacklistRule: mongoose.model('BlacklistRule', BlacklistRuleSchema),
};
+80
View File
@@ -0,0 +1,80 @@
// proxy/models/SchemasTelemetry.js
// ─────────────────────────────────────────────────────────────────────────────
// DPI Telemetry Property Schemas — Split from Schemas.js to stay under 256 lines.
// These are BackOne-specific data fields collected via /data/stats/top/<field> API.
// ─────────────────────────────────────────────────────────────────────────────
const mongoose = require('mongoose');
const baseOptions = {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }
};
// ─── Helper: build a consistent DPI property schema ───────────────────────────
function dpiPropertySchema(fieldName) {
const fields = {
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
download: Number,
upload: Number,
flows: Number,
};
fields[fieldName] = { type: String, required: true };
const schema = new mongoose.Schema(fields, baseOptions);
schema.index({ agent_uuid: 1, timestamp: -1 });
return schema;
}
// ─── DHCP Fingerprints (dhcp_class) ──────────────────────────────────────────
const DhcpFingerprintStatSchema = dpiPropertySchema('fingerprint');
// ─── HTTP User Agents (http_useragent) ────────────────────────────────────────
const HttpUserAgentStatSchema = dpiPropertySchema('user_agent');
// ─── BitTorrent Info Hashes (bittorrent_info_hash) ────────────────────────────
const BittorrentHashStatSchema = new mongoose.Schema({
timestamp: { type: Date, required: true, index: true, expires: '30d' },
agent_uuid: { type: String, index: true },
site_uuid: { type: String, index: true },
info_hash: { type: String, required: true },
label: { type: String },
download: Number,
upload: Number,
flows: Number,
}, baseOptions);
BittorrentHashStatSchema.index({ agent_uuid: 1, timestamp: -1 });
// ─── HTTPS SNI Hostnames (https_sni_hostname) ─────────────────────────────────
const SniHostnameStatSchema = dpiPropertySchema('sni_hostname');
// ─── SSL Server Common Names (ssl_server_cn) ──────────────────────────────────
const SslServerCnStatSchema = dpiPropertySchema('ssl_server_cn');
// ─── QUIC Hostnames (quic_hostname) ───────────────────────────────────────────
const QuicHostnameStatSchema = dpiPropertySchema('quic_hostname');
// ─── SSH Clients (ssh_client) ─────────────────────────────────────────────────
const SshClientStatSchema = dpiPropertySchema('ssh_client');
// ─── SSH Servers (ssh_server) ─────────────────────────────────────────────────
const SshServerStatSchema = dpiPropertySchema('ssh_server');
// ─── mDNS Hostnames (mdns_hostname) ───────────────────────────────────────────
const MdnsHostnameStatSchema = dpiPropertySchema('mdns_hostname');
// ─── SSL Subject Alternative Names (ssl_subject_alt_name) ──────────────────────
const SslSubjectAltNameStatSchema = dpiPropertySchema('alt_name');
module.exports = {
DhcpFingerprintStat: mongoose.model('DhcpFingerprintStat', DhcpFingerprintStatSchema),
HttpUserAgentStat: mongoose.model('HttpUserAgentStat', HttpUserAgentStatSchema),
BittorrentHashStat: mongoose.model('BittorrentHashStat', BittorrentHashStatSchema),
SniHostnameStat: mongoose.model('SniHostnameStat', SniHostnameStatSchema),
SslServerCnStat: mongoose.model('SslServerCnStat', SslServerCnStatSchema),
QuicHostnameStat: mongoose.model('QuicHostnameStat', QuicHostnameStatSchema),
SshClientStat: mongoose.model('SshClientStat', SshClientStatSchema),
SshServerStat: mongoose.model('SshServerStat', SshServerStatSchema),
MdnsHostnameStat: mongoose.model('MdnsHostnameStat', MdnsHostnameStatSchema),
SslSubjectAltNameStat: mongoose.model('SslSubjectAltNameStat', SslSubjectAltNameStatSchema),
};
+1312
View File
File diff suppressed because it is too large. Load diff
+19
View File
@@ -0,0 +1,19 @@
{
"name": "backone-proxy",
"version": "1.0.0",
"description": "BackOne DPI Proxy Server - Fetches from DPI API, filters per agent_uuid, stores to MongoDB",
"main": "index.js",
"scripts": {
"start": "node index.js",
"start:bun": "bun run index.js",
"dev": "nodemon index.js"
},
"dependencies": {
"axios": "^1.6.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mongoose": "^8.0.3",
"node-cron": "^3.0.3"
}
}
+249
View File
@@ -0,0 +1,249 @@
// proxy/routes.js
// ─── REST API Routes for BackOne Proxy Server ───────────────────────────────
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const scheduler = require('./scheduler');
/**
* GET /health
* Liveness check
*/
router.get('/health', (req, res) => {
const dbState = mongoose.connection.readyState;
const dbLabel = ['disconnected', 'connected', 'connecting', 'disconnecting'][dbState] || 'unknown';
res.json({
ok: dbState === 1,
service: 'BackOne Proxy Server',
db: dbLabel,
mode: process.env.PROXY_COLLECT_MODE || 'all',
time: new Date().toISOString(),
});
});
/**
* GET /status
* Full scheduler + DB status
*/
router.get('/status', (req, res) => {
const dbState = mongoose.connection.readyState;
res.json({
ok: true,
db_state: ['disconnected', 'connected', 'connecting', 'disconnecting'][dbState] || 'unknown',
...scheduler.getStatus(),
time: new Date().toISOString(),
});
});
/**
* POST /collect/all
* Manual trigger — collect ALL agents sekarang
*/
router.post('/collect/all', async (req, res) => {
if (mongoose.connection.readyState !== 1) {
return res.status(503).json({ ok: false, error: 'MongoDB not connected. Cannot collect.' });
}
const { collectAllAgents } = require('./collector');
console.log('[Proxy API] Manual trigger: collect ALL agents');
try {
const result = await collectAllAgents();
res.json({ ok: result.success, ...result });
} catch (err) {
res.status(500).json({ ok: false, error: err.message });
}
});
/**
* POST /collect/:agentUuid
* Manual trigger — collect ONE specific agent
*/
router.post('/collect/:agentUuid', async (req, res) => {
if (mongoose.connection.readyState !== 1) {
return res.status(503).json({ ok: false, error: 'MongoDB not connected. Cannot collect.' });
}
const { agentUuid } = req.params;
const { collectSpecificAgent } = require('./collector');
console.log(`[Proxy API] Manual trigger: collect agent ${agentUuid}`);
try {
const result = await collectSpecificAgent(agentUuid);
res.json({ ok: result.success, ...result });
} catch (err) {
res.status(500).json({ ok: false, error: err.message });
}
});
/**
* POST /collect/agents
* Manual trigger — collect multiple specific agents
* Body: { "uuids": ["uuid1", "uuid2", ...], "delay_ms": 5000 }
*/
router.post('/collect/agents', async (req, res) => {
if (mongoose.connection.readyState !== 1) {
return res.status(503).json({ ok: false, error: 'MongoDB not connected. Cannot collect.' });
}
const { uuids, delay_ms } = req.body;
if (!uuids || !Array.isArray(uuids) || uuids.length === 0) {
return res.status(400).json({ ok: false, error: 'Body must include "uuids" as a non-empty array of agent UUIDs.' });
}
const { collectSpecificAgents } = require('./collector');
console.log(`[Proxy API] Manual trigger: collect agents ${uuids.join(', ')}`);
try {
const result = await collectSpecificAgents(uuids, delay_ms || 5000);
res.json({ ok: result.success, ...result });
} catch (err) {
res.status(500).json({ ok: false, error: err.message });
}
});
/**
* GET /agents
* List all agent UUIDs yang sudah tersimpan di MongoDB
*/
router.get('/agents', async (req, res) => {
try {
if (mongoose.connection.readyState !== 1) {
return res.status(503).json({ ok: false, error: 'MongoDB not connected.' });
}
const { Summary } = require('./models/Schemas');
const agents = await Summary.distinct('agent_uuid');
res.json({ ok: true, data: agents });
} catch (err) {
res.status(500).json({ ok: false, error: err.message });
}
});
/**
* GET /domain-details
* Fetch live IP/MAC details for a specific domain from MongoDB
*/
router.get('/domain-details', async (req, res) => {
const { domain, agentUuid } = req.query;
if (!domain) return res.status(400).json({ ok: false, error: 'domain is required' });
try {
const { Flow, DeviceStat } = require('./models/Schemas');
const filter = { domain: domain };
if (agentUuid) {
filter.agent_uuid = agentUuid;
}
const raw = await Flow.find(filter).sort({ timestamp: -1 }).limit(100).lean();
if (!raw || !Array.isArray(raw)) {
return res.json({ ok: true, data: [] });
}
const results = [];
const seen = new Set();
for (const r of raw) {
const ip = r.src_ip;
const mac = r.src_mac;
if (!ip || !mac) continue;
const key = `${ip}-${mac}`;
if (seen.has(key)) continue;
seen.add(key);
let deviceName = 'Unknown Device';
try {
const dev = await DeviceStat.findOne({ mac_address: mac }).sort({ timestamp: -1 });
if (dev && dev.name && dev.name !== 'Unknown Device') {
deviceName = dev.name;
}
} catch (e) {
// ignore timeout errors
}
results.push({
ip,
mac,
deviceName,
lastSeen: r.timestamp || r.last_seen || r.first_seen,
});
}
res.json({ ok: true, data: results });
} catch (err) {
res.status(500).json({ ok: false, error: err.message });
}
});
/**
* GET /latest
* Tampilkan data terbaru dari semua collection (untuk debug)
*/
router.get('/latest', async (req, res) => {
try {
const { Summary, AppStat, DeviceStat, Flow, Threat, Event, AppCategoryStat } = require('./models/Schemas');
const [summary, apps, devices, flows, threats, events, categories] = await Promise.all([
Summary.findOne().sort({ timestamp: -1 }).lean(),
AppStat.find().sort({ timestamp: -1 }).limit(5).lean(),
DeviceStat.find().sort({ timestamp: -1 }).limit(5).lean(),
Flow.find().sort({ timestamp: -1 }).limit(5).lean(),
Threat.find().sort({ timestamp: -1 }).limit(5).lean(),
Event.find().sort({ timestamp: -1 }).limit(5).lean(),
AppCategoryStat.find().sort({ timestamp: -1 }).limit(5).lean(),
]);
res.json({ ok: true, data: { summary, apps, devices, flows, threats, events, categories } });
} catch (err) {
res.status(500).json({ ok: false, error: err.message });
}
});
// ----------------------------------------------------------------------
// Agent Groups Management
// ----------------------------------------------------------------------
router.get('/groups', async (req, res) => {
try {
const { AgentGroup } = require('./models/SchemasAux');
const groups = await AgentGroup.find({});
res.json(groups);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post('/groups', async (req, res) => {
try {
const { AgentGroup } = require('./models/SchemasAux');
const { id, name, description, agent_uuids } = req.body;
const group = await AgentGroup.create({
id: id || Date.now().toString(),
name,
description,
agent_uuids: agent_uuids || []
});
res.json(group);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.put('/groups/:id', async (req, res) => {
try {
const { AgentGroup } = require('./models/SchemasAux');
const { name, description, agent_uuids } = req.body;
const group = await AgentGroup.findOneAndUpdate(
{ id: req.params.id },
{ name, description, agent_uuids },
{ new: true }
);
res.json(group);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.delete('/groups/:id', async (req, res) => {
try {
const { AgentGroup } = require('./models/SchemasAux');
await AgentGroup.findOneAndDelete({ id: req.params.id });
res.json({ ok: true });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
module.exports = router;
+129
View File
@@ -0,0 +1,129 @@
// proxy/scheduler.js
// Cron scheduler for automatic data collection from DPI API
// Runs every 5 minutes, collecting data for all agents or a specific agent.
const cron = require('node-cron');
const { collectAllAgents, collectSpecificAgent, collectSpecificAgents } = require('./collector');
const { syncApplicationDictionary } = require('./backoneClient');
// Mode: 'all' = collect all agents, 'agent' = collect one specific agent, 'agents' = collect multiple agents
const COLLECT_MODE = process.env.PROXY_COLLECT_MODE || 'all';
const SPECIFIC_AGENT = process.env.PROXY_AGENT_UUID || null;
const SPECIFIC_AGENTS = (process.env.PROXY_AGENT_UUIDS || '').split(',').map(s => s.trim()).filter(Boolean);
const AGENT_DELAY_MS = parseInt(process.env.PROXY_AGENT_DELAY_MS || '5000');
const CRON_SCHEDULE = process.env.PROXY_CRON_SCHEDULE || '*/5 * * * *';
// Capacity logging is an expensive full-scan aggregation. Run it at most once per
// interval (default 24h) instead of every collection cycle to reduce CPU/DB load.
const CAPACITY_LOG_INTERVAL_MS = parseInt(process.env.PROXY_CAPACITY_LOG_INTERVAL_MS || String(24 * 60 * 60 * 1000));
let isRunning = false;
let lastRunAt = null;
let lastRunResult = null;
let runCount = 0;
let lastCapacityLogAt = 0;
/**
* Execute one collection cycle (called by cron and manual trigger).
* Prevents concurrent runs with isRunning guard.
*/
async function runCollection() {
if (isRunning) {
console.log('[Scheduler] Skipping - previous run still in progress');
return { skipped: true, reason: 'already_running' };
}
isRunning = true;
lastRunAt = new Date();
runCount++;
try {
let result;
if (COLLECT_MODE === 'agent' && SPECIFIC_AGENT) {
console.log(`[Scheduler] Run #${runCount} - Mode: SPECIFIC AGENT (${SPECIFIC_AGENT})`);
result = await collectSpecificAgent(SPECIFIC_AGENT);
} else if (COLLECT_MODE === 'agents' && SPECIFIC_AGENTS.length > 0) {
console.log(`[Scheduler] Run #${runCount} - Mode: SPECIFIC AGENTS (${SPECIFIC_AGENTS.join(', ')})`);
result = await collectSpecificAgents(SPECIFIC_AGENTS, AGENT_DELAY_MS);
} else {
console.log(`[Scheduler] Run #${runCount} - Mode: ALL AGENTS`);
result = await collectAllAgents();
}
lastRunResult = { ...result, run_count: runCount };
// Log MongoDB database capacity usage (expensive full-scan aggregation).
// Only run periodically (default: every 24h) to avoid high CPU/DB load each cycle.
const now = Date.now();
if (now - lastCapacityLogAt >= CAPACITY_LOG_INTERVAL_MS) {
lastCapacityLogAt = now;
const { logCapacityStats } = require('./db/capacityTracker');
await logCapacityStats(`[PROXY] [MongoDB] Capacity Used after Run #${runCount}:`);
}
return lastRunResult;
} catch (err) {
console.error('[Scheduler] Unhandled error during collection:', err.message);
lastRunResult = { success: false, error: err.message, run_count: runCount };
return lastRunResult;
} finally {
isRunning = false;
}
}
/**
* Start the scheduler (cron job + immediate first run).
*/
function startScheduler() {
console.log(`[Scheduler] Starting proxy data collector`);
const modeLabel = COLLECT_MODE === 'agent'
? `SPECIFIC AGENT (${SPECIFIC_AGENT})`
: COLLECT_MODE === 'agents'
? `SPECIFIC AGENTS (${SPECIFIC_AGENTS.join(', ')})`
: 'ALL AGENTS';
console.log(`[Scheduler] Mode : ${modeLabel}`);
console.log(`[Scheduler] Schedule : ${CRON_SCHEDULE} (every 5 minutes by default)`);
// Validate cron expression
if (!cron.validate(CRON_SCHEDULE)) {
console.error(`[Scheduler] Invalid cron expression: "${CRON_SCHEDULE}". Using default.`);
}
// Start recurring cron job
cron.schedule(CRON_SCHEDULE, () => {
runCollection().catch(err => console.error('[Scheduler] Cron error:', err.message));
});
console.log('[Scheduler] Cron job registered. Starting initial collection...');
// Initial run immediately on startup (async, do not block server start)
setTimeout(async () => {
// 1. Sync dictionary first
try {
await syncApplicationDictionary();
} catch (err) {
console.error('[Scheduler] Error syncing application dictionary:', err.message);
}
// 2. Start normal telemetry collection
runCollection().catch(err => console.error('[Scheduler] Initial run error:', err.message));
}, 2000);
}
/**
* Get current scheduler status (for REST API endpoint).
*/
function getStatus() {
return {
is_running: isRunning,
run_count: runCount,
last_run_at: lastRunAt?.toISOString() ?? null,
collect_mode: COLLECT_MODE,
agent_uuid: SPECIFIC_AGENT,
agent_uuids: COLLECT_MODE === 'agents' ? SPECIFIC_AGENTS : [],
agent_delay_ms: AGENT_DELAY_MS,
cron_schedule: CRON_SCHEDULE,
last_result: lastRunResult,
};
}
module.exports = { startScheduler, runCollection, getStatus };