Files
MikrotikManager/backend/src/services/traffic-flow-dest.ts
T
Denozordec 589fc782f9
Docker images / prepare-release (push) Successful in 11s
Docker images / backend-test (push) Successful in 10m29s
Docker images / frontend-image (push) Successful in 2m13s
Docker images / updater-image (push) Successful in 40s
Docker images / netflow-agent-image (push) Successful in 32s
Docker images / backend-image (push) Successful in 1m59s
Docker images / publish-release (push) Successful in 12s
Docker images / notify-webhook (push) Successful in 2s
fix(map): исправить трафик и карточки карты сети
2026-11-26 10:34:20 +07:00

162 lines
5.6 KiB
TypeScript

import { applicationName } from "./traffic-flow-apps.js"
import { isIsoCountry, isNamedInternetService, OTHER_SERVICE, resolveFlowBrand } from "./traffic-flow-brands.js"
import { classifyFlowDst, type FlowClassification } from "./traffic-flow-classify.js"
import { resolveFlowIp } from "./traffic-flow-geoip.js"
import { canonicalFactIface } from "./traffic-flow-ifindex.js"
import {
canonicalIp,
isNonPublicIp,
isUnspecifiedIp,
resolveFlowEndpoints,
type FlowEndpoints,
type InternetDestCtx,
} from "./traffic-flow-ip.js"
import type { FlowIpMeta } from "./traffic-flow-ripe.js"
import {
flowOursHosts,
resolveClient,
type FlowTopology,
} from "./traffic-flow-topology.js"
/** Aggregate DL/UL is relative to the exporter's WAN or its bound clients. */
export function flowRateDirection(
serverId: number,
row: { src: string; dst: string; inIface: string; outIface: string },
topo: FlowTopology | null,
exporterHost?: string,
): "rx" | "tx" | null {
const ingress = canonicalFactIface(serverId, row.inIface)
const egress = canonicalFactIface(serverId, row.outIface)
const wan = topo?.wanIfaces.get(serverId)
const clients = topo?.clientIfaces.get(serverId)
const download = Boolean(wan?.has(ingress) || clients?.has(egress))
const upload = Boolean(wan?.has(egress) || clients?.has(ingress))
if (download !== upload) return download ? "rx" : "tx"
if (isUnspecifiedIp(row.src) || isUnspecifiedIp(row.dst)) return null
const host = canonicalIp(exporterHost)
if (host && canonicalIp(row.src) === host && canonicalIp(row.dst) !== host) return "tx"
if (host && canonicalIp(row.dst) === host && canonicalIp(row.src) !== host) return "rx"
const srcLocal = isNonPublicIp(row.src), dstLocal = isNonPublicIp(row.dst)
return srcLocal === dstLocal ? null : srcLocal ? "tx" : "rx"
}
export interface InternetDestMeta {
dest: string
ripe: FlowIpMeta | null
classified: FlowClassification
country: string
asn: number
endpoints: FlowEndpoints
}
export function destCtxForIface(
topo: FlowTopology | null | undefined,
serverId: number,
inIface: string,
nat?: Pick<InternetDestCtx, "natSrc" | "natDst" | "natSrcPort" | "natDstPort"> & { outIface?: string },
resolveIface=canonicalFactIface,
): InternetDestCtx {
const name = resolveIface(serverId, inIface) || String(inIface ?? "").trim()
const outName = nat?.outIface ? resolveIface(serverId, nat.outIface) : ""
return {
ours: flowOursHosts(topo),
boundClient: Boolean(
topo && [name, outName].some(iface => iface &&
(resolveClient(topo, serverId, iface) || topo.clientIfaces.get(serverId)?.has(iface))),
),
natSrc: nat?.natSrc,
natDst: nat?.natDst,
natSrcPort: nat?.natSrcPort,
natDstPort: nat?.natDstPort,
}
}
const OTHER_BRAND: FlowClassification = { service: OTHER_SERVICE, category: OTHER_SERVICE }
function isTunnelProto(proto: number, dstPort: number, srcPort: number): boolean {
if (proto === 47 || proto === 50) return true
return applicationName(proto, dstPort, srcPort) === "WireGuard"
}
/** Бренд интернет-dest: ASN/CIDR до skip DNS. GRE/ESP/WG — не сервис. */
export function classifyInternetBrand(
dst: string,
proto: number,
dstPort: number,
srcPort: number,
ripe: FlowIpMeta | null,
): FlowClassification | null {
if (isTunnelProto(proto, dstPort, srcPort)) return null
const brand = resolveFlowBrand(dst, ripe?.asn ?? 0, ripe?.holder ?? "", proto, dstPort, srcPort)
if (brand && isNamedInternetService(brand.service, brand.category)) return brand
const app = applicationName(proto, dstPort, srcPort)
if (app === "DNS" || app === "SSH" || app === "BGP") return null
return null
}
/** Тот же классификатор, что аналитика (GeoLite2 ASN + catalog). Туннель → Прочее. */
export function mapInternetBrand(
dst: string,
proto: number,
dstPort: number,
srcPort: number,
ripe: FlowIpMeta | null,
): FlowClassification {
if (isTunnelProto(proto, dstPort, srcPort)) return OTHER_BRAND
const classified = classifyFlowDst(dst, proto, dstPort, srcPort, ripe, { ignoreTunnelProto: true })
if (isNamedInternetService(classified.service, classified.category)) return classified
return { service: classified.category, category: classified.category }
}
export function resolveInternetDest(opts: {
src: string
dst: string
proto: number
srcPort: number
dstPort: number
serverId: number
inIface: string
outIface?: string
topo?: FlowTopology | null
natSrc?: string
natDst?: string
natSrcPort?: number
natDstPort?: number
resolveIface?:typeof canonicalFactIface
resolveIp?:(ip:string)=>FlowIpMeta|null
classify?:typeof classifyFlowDst
}): InternetDestMeta {
const ctx = destCtxForIface(opts.topo, opts.serverId, opts.inIface, {
outIface: opts.outIface,
natSrc: opts.natSrc,
natDst: opts.natDst,
natSrcPort: opts.natSrcPort,
natDstPort: opts.natDstPort,
},opts.resolveIface)
const endpoints = resolveFlowEndpoints({
src: opts.src,
dst: opts.dst,
srcPort: opts.srcPort,
dstPort: opts.dstPort,
ctx,
})
const dest = endpoints.internetPeer
if (!dest) {
return { dest: "", ripe: null, classified: OTHER_BRAND, country: "", asn: 0, endpoints }
}
const ripe = (opts.resolveIp??resolveFlowIp)(dest)
const classified = (opts.classify??classifyFlowDst)(
dest,
opts.proto,
endpoints.peerPort,
endpoints.otherPort,
ripe,
{ ignoreTunnelProto: true },
)
const country = ripe?.ok && isIsoCountry(ripe.country)
? ripe.country
: (ripe?.ok ? "" : "unknown")
const asn = ripe?.ok && ripe.asn ? ripe.asn : 0
return { dest, ripe, classified, country, asn, endpoints }
}