Track tcp/udp dports via deny_port_hits, expose aggregate and per-IP ports in UI; install-link re-run refreshes nft rules. Co-authored-by: Cursor <cursoragent@cursor.com>
345 lines
9.2 KiB
TypeScript
345 lines
9.2 KiB
TypeScript
import { and, eq, desc, sql, inArray } from 'drizzle-orm'
|
|
import type { Db } from '../client.js'
|
|
import {
|
|
agentIpBlockStats,
|
|
agentPortBlockStats,
|
|
agentStatsSamples,
|
|
} from '../schema.js'
|
|
|
|
export function insertStatsSample(
|
|
db: Db,
|
|
row: typeof agentStatsSamples.$inferInsert,
|
|
) {
|
|
db.insert(agentStatsSamples).values(row).run()
|
|
}
|
|
|
|
export function listStatsSamples(db: Db, agentId: string, limit = 100) {
|
|
return db
|
|
.select()
|
|
.from(agentStatsSamples)
|
|
.where(eq(agentStatsSamples.agentId, agentId))
|
|
.orderBy(desc(agentStatsSamples.recordedAt))
|
|
.limit(limit)
|
|
.all()
|
|
}
|
|
|
|
export function listRecentStats(db: Db, limit = 500) {
|
|
return db
|
|
.select()
|
|
.from(agentStatsSamples)
|
|
.orderBy(desc(agentStatsSamples.recordedAt))
|
|
.limit(limit)
|
|
.all()
|
|
}
|
|
|
|
export function deleteStatsSamplesForAgent(db: Db, agentId: string) {
|
|
db.delete(agentStatsSamples)
|
|
.where(eq(agentStatsSamples.agentId, agentId))
|
|
.run()
|
|
}
|
|
|
|
export type IpHitInput = { ip: string; packets: number }
|
|
|
|
/** Gap after which a new presence report counts as a re-hit (left EVOFW_HITS). */
|
|
export const PRESENCE_REHIT_STALE_MS = 150_000
|
|
|
|
export type UpsertIpBlockStatsOptions = {
|
|
/**
|
|
* MikroTik EVOFW_HITS presence: refresh last_seen every report while IP is listed;
|
|
* increment packets only on first see or re-hit after stale gap (left the list).
|
|
* Linux keeps absolute counter deltas (default).
|
|
*/
|
|
mode?: 'absolute' | 'presence'
|
|
/** Override re-hit gap (tests). Default PRESENCE_REHIT_STALE_MS. */
|
|
presenceStaleMs?: number
|
|
}
|
|
|
|
/**
|
|
* Upsert per-IP drop counters.
|
|
* - absolute (Linux): agent reports kernel counters; CP accumulates deltas.
|
|
* - presence (MikroTik): last_seen while in HITS; packets = entries into HITS (not per-sync).
|
|
*/
|
|
export function upsertIpBlockStats(
|
|
db: Db,
|
|
agentId: string,
|
|
hits: IpHitInput[],
|
|
now = new Date().toISOString(),
|
|
opts: UpsertIpBlockStatsOptions = {},
|
|
) {
|
|
const mode = opts.mode ?? 'absolute'
|
|
const staleMs = opts.presenceStaleMs ?? PRESENCE_REHIT_STALE_MS
|
|
const nowMs = Date.parse(now)
|
|
|
|
for (const hit of hits) {
|
|
const ip = hit.ip.trim()
|
|
if (!ip) continue
|
|
const reported = Math.max(0, Math.floor(hit.packets))
|
|
const existing = db
|
|
.select()
|
|
.from(agentIpBlockStats)
|
|
.where(
|
|
and(
|
|
eq(agentIpBlockStats.agentId, agentId),
|
|
eq(agentIpBlockStats.ip, ip),
|
|
),
|
|
)
|
|
.get()
|
|
|
|
if (mode === 'presence') {
|
|
if (!existing) {
|
|
db.insert(agentIpBlockStats)
|
|
.values({
|
|
id: crypto.randomUUID(),
|
|
agentId,
|
|
ip,
|
|
packets: 1,
|
|
lastReportedPackets: 1,
|
|
firstSeenAt: now,
|
|
lastSeenAt: now,
|
|
})
|
|
.run()
|
|
continue
|
|
}
|
|
const prevSeen = Date.parse(existing.lastSeenAt)
|
|
const gapMs = Number.isFinite(prevSeen) ? nowMs - prevSeen : staleMs + 1
|
|
const isRehit = gapMs > staleMs
|
|
db.update(agentIpBlockStats)
|
|
.set({
|
|
lastSeenAt: now,
|
|
...(isRehit
|
|
? {
|
|
packets: (existing.packets ?? 0) + 1,
|
|
lastReportedPackets: (existing.lastReportedPackets ?? 0) + 1,
|
|
}
|
|
: {}),
|
|
})
|
|
.where(eq(agentIpBlockStats.id, existing.id))
|
|
.run()
|
|
continue
|
|
}
|
|
|
|
if (!existing) {
|
|
db.insert(agentIpBlockStats)
|
|
.values({
|
|
id: crypto.randomUUID(),
|
|
agentId,
|
|
ip,
|
|
packets: reported,
|
|
lastReportedPackets: reported,
|
|
firstSeenAt: now,
|
|
lastSeenAt: now,
|
|
})
|
|
.run()
|
|
continue
|
|
}
|
|
|
|
const prevReported = existing.lastReportedPackets ?? 0
|
|
const delta =
|
|
reported >= prevReported ? reported - prevReported : reported
|
|
const packets = (existing.packets ?? 0) + delta
|
|
db.update(agentIpBlockStats)
|
|
.set({
|
|
packets,
|
|
lastReportedPackets: reported,
|
|
...(delta > 0 ? { lastSeenAt: now } : {}),
|
|
})
|
|
.where(eq(agentIpBlockStats.id, existing.id))
|
|
.run()
|
|
}
|
|
}
|
|
|
|
export function listIpBlockStats(db: Db, agentId: string, limit = 200) {
|
|
return db
|
|
.select()
|
|
.from(agentIpBlockStats)
|
|
.where(eq(agentIpBlockStats.agentId, agentId))
|
|
.orderBy(desc(agentIpBlockStats.packets))
|
|
.limit(limit)
|
|
.all()
|
|
}
|
|
|
|
export function deleteIpBlockStatsForAgent(db: Db, agentId: string) {
|
|
db.delete(agentIpBlockStats)
|
|
.where(eq(agentIpBlockStats.agentId, agentId))
|
|
.run()
|
|
}
|
|
|
|
/**
|
|
* After nft/ipset counter flush (policy apply), kernel absolutes restart at 0.
|
|
* Traffic detects this via packets_dropped drop; per-IP baselines must reset too,
|
|
* otherwise ip_hits with packets=0 are omitted from the report and lastReported
|
|
* stays high → second epoch deltas are lost (Traffic 14 vs Blocked IPs 7).
|
|
*/
|
|
export function resetIpBlockStatsBaselines(db: Db, agentId: string) {
|
|
db.update(agentIpBlockStats)
|
|
.set({ lastReportedPackets: 0 })
|
|
.where(eq(agentIpBlockStats.agentId, agentId))
|
|
.run()
|
|
}
|
|
|
|
export type PortHitInput = {
|
|
ip: string
|
|
port: number
|
|
protocol: 'tcp' | 'udp' | string
|
|
packets: number
|
|
}
|
|
|
|
/**
|
|
* Upsert per-(ip, proto, port) deny counters (Linux nft absolute deltas).
|
|
* deny_port_hits is not flushed on policy apply (timeout ages elements), so
|
|
* baselines are not auto-reset on Traffic flush — only on stats/reset delete.
|
|
*/
|
|
export function upsertPortBlockStats(
|
|
db: Db,
|
|
agentId: string,
|
|
hits: PortHitInput[],
|
|
now = new Date().toISOString(),
|
|
) {
|
|
for (const hit of hits) {
|
|
const ip = hit.ip.trim()
|
|
const protocol = String(hit.protocol).toLowerCase()
|
|
if (!ip || (protocol !== 'tcp' && protocol !== 'udp')) continue
|
|
const port = Math.floor(hit.port)
|
|
if (port < 1 || port > 65535) continue
|
|
const reported = Math.max(0, Math.floor(hit.packets))
|
|
const existing = db
|
|
.select()
|
|
.from(agentPortBlockStats)
|
|
.where(
|
|
and(
|
|
eq(agentPortBlockStats.agentId, agentId),
|
|
eq(agentPortBlockStats.ip, ip),
|
|
eq(agentPortBlockStats.port, port),
|
|
eq(agentPortBlockStats.protocol, protocol),
|
|
),
|
|
)
|
|
.get()
|
|
|
|
if (!existing) {
|
|
db.insert(agentPortBlockStats)
|
|
.values({
|
|
id: crypto.randomUUID(),
|
|
agentId,
|
|
ip,
|
|
port,
|
|
protocol,
|
|
packets: reported,
|
|
lastReportedPackets: reported,
|
|
firstSeenAt: now,
|
|
lastSeenAt: now,
|
|
})
|
|
.run()
|
|
continue
|
|
}
|
|
|
|
const prevReported = existing.lastReportedPackets ?? 0
|
|
const delta =
|
|
reported >= prevReported ? reported - prevReported : reported
|
|
const packets = (existing.packets ?? 0) + delta
|
|
db.update(agentPortBlockStats)
|
|
.set({
|
|
packets,
|
|
lastReportedPackets: reported,
|
|
...(delta > 0 ? { lastSeenAt: now } : {}),
|
|
})
|
|
.where(eq(agentPortBlockStats.id, existing.id))
|
|
.run()
|
|
}
|
|
}
|
|
|
|
export type PortBlockAggregateRow = {
|
|
port: number
|
|
protocol: string
|
|
packets: number
|
|
lastSeenAt: string
|
|
}
|
|
|
|
export function listPortBlockStatsAggregate(
|
|
db: Db,
|
|
agentId: string,
|
|
limit = 50,
|
|
): PortBlockAggregateRow[] {
|
|
const rows = db
|
|
.select({
|
|
port: agentPortBlockStats.port,
|
|
protocol: agentPortBlockStats.protocol,
|
|
packets: sql<number>`sum(${agentPortBlockStats.packets})`.mapWith(Number),
|
|
lastSeenAt: sql<string>`max(${agentPortBlockStats.lastSeenAt})`,
|
|
})
|
|
.from(agentPortBlockStats)
|
|
.where(eq(agentPortBlockStats.agentId, agentId))
|
|
.groupBy(agentPortBlockStats.port, agentPortBlockStats.protocol)
|
|
.orderBy(sql`sum(${agentPortBlockStats.packets}) desc`)
|
|
.limit(limit)
|
|
.all()
|
|
return rows.map((r) => ({
|
|
port: r.port,
|
|
protocol: r.protocol,
|
|
packets: r.packets ?? 0,
|
|
lastSeenAt: r.lastSeenAt,
|
|
}))
|
|
}
|
|
|
|
export type PortBlockPerIpRow = {
|
|
ip: string
|
|
port: number
|
|
protocol: string
|
|
packets: number
|
|
}
|
|
|
|
/** Raw rows for a set of IPs (caller picks top-N per IP). */
|
|
export function listPortBlockStatsForIps(
|
|
db: Db,
|
|
agentId: string,
|
|
ips: string[],
|
|
): PortBlockPerIpRow[] {
|
|
if (ips.length === 0) return []
|
|
return db
|
|
.select({
|
|
ip: agentPortBlockStats.ip,
|
|
port: agentPortBlockStats.port,
|
|
protocol: agentPortBlockStats.protocol,
|
|
packets: agentPortBlockStats.packets,
|
|
})
|
|
.from(agentPortBlockStats)
|
|
.where(
|
|
and(
|
|
eq(agentPortBlockStats.agentId, agentId),
|
|
inArray(agentPortBlockStats.ip, ips),
|
|
),
|
|
)
|
|
.orderBy(desc(agentPortBlockStats.packets))
|
|
.all()
|
|
}
|
|
|
|
/** Top `perIpLimit` ports per IP, keyed by IP. */
|
|
export function mapTopPortsByIp(
|
|
db: Db,
|
|
agentId: string,
|
|
ips: string[],
|
|
perIpLimit = 5,
|
|
): Map<string, PortBlockPerIpRow[]> {
|
|
const all = listPortBlockStatsForIps(db, agentId, ips)
|
|
const map = new Map<string, PortBlockPerIpRow[]>()
|
|
for (const row of all) {
|
|
const list = map.get(row.ip) ?? []
|
|
if (list.length >= perIpLimit) continue
|
|
list.push(row)
|
|
map.set(row.ip, list)
|
|
}
|
|
return map
|
|
}
|
|
|
|
export function deletePortBlockStatsForAgent(db: Db, agentId: string) {
|
|
db.delete(agentPortBlockStats)
|
|
.where(eq(agentPortBlockStats.agentId, agentId))
|
|
.run()
|
|
}
|
|
|
|
export function resetPortBlockStatsBaselines(db: Db, agentId: string) {
|
|
db.update(agentPortBlockStats)
|
|
.set({ lastReportedPackets: 0 })
|
|
.where(eq(agentPortBlockStats.agentId, agentId))
|
|
.run()
|
|
}
|