prt-core
Core library for prt — a real-time network port monitor for macOS and Linux.
What it does
prt-core provides platform-independent logic for:
- Scanning network ports (TCP/UDP) via
lsofon macOS or/procon Linux - Tracking connection changes over time (New → Unchanged → Gone) with
first_seenaging - Enrichment — known port names (~170 built-in + user overrides), suspicious connection detection, container awareness
- Filtering by port, PID, process name, service, protocol, state, user, or
!(suspicious) - Sorting by any column, ascending or descending
- Exporting to JSON or CSV
- Killing processes by PID (SIGTERM / SIGKILL)
- Alerts — configurable rules with bell/highlight actions
- Firewall — generate iptables/pfctl block/unblock commands
- Bandwidth — system-wide RX/TX rate tracking
- Containers — Docker/Podman container name resolution
- Namespaces — Linux network namespace grouping
- Process detail — CWD, environment, open files, CPU, RSS
- i18n — runtime-switchable localization (English, Russian, Chinese) backed by
AtomicU8 - Config — TOML-based configuration from
~/.config/prt/
Architecture
platform::scan_ports()
→ Session::refresh()
→ scanner::diff_entries() (New / Unchanged / Gone + first_seen carry-forward)
→ enrich: service names, suspicious flags, containers
→ retain: drop Gone entries older than 5s
→ bandwidth.sample(): RX/TX rate delta
→ scanner::sort_entries()
→ (frontend layer)
→ alerts::evaluate()
→ scanner::filter_indices()
→ UI renders
| Platform | Method | Performance |
|---|---|---|
| macOS | lsof -F structured output |
2 batch ps calls per cycle |
| Linux | /proc/net/tcp, /proc/net/udp via procfs crate |
Zero subprocess overhead |
Quick start
use scanner;
use ExportFormat;
let entries = scan.expect;
let json = export.unwrap;
println!;
Session-based scanning
For continuous monitoring with change tracking:
use Session;
let mut session = new;
session.refresh.expect;
for entry in &session.entries
Modules
| Module | Description |
|---|---|
model |
Core types: PortEntry, TrackedEntry, ViewMode, DetailTab, SortState |
core::scanner |
Scan, diff, sort, filter, export |
core::session |
Refresh cycle state machine with enrichment pipeline |
core::alerts |
Alert rule evaluation (port, process, state, connections_gt) |
core::suspicious |
Suspicious connection heuristics (3 rules) |
core::bandwidth |
System-wide RX/TX rate (Linux: /proc/net/dev, macOS: netstat -ib) |
core::container |
Docker/Podman resolution via batched CLI calls |
core::namespace |
Linux network namespace grouping |
core::process_detail |
CWD, env, open files, CPU %, RSS |
core::firewall |
iptables/pfctl block/unblock command generation |
core::killer |
SIGTERM / SIGKILL |
known_ports |
Port → service name database (~170 entries + config overrides) |
config |
TOML config loading (known_ports, alert rules) |
i18n |
EN / RU / ZH runtime switching |
platform |
macOS (lsof) / Linux (/proc) |
i18n
use ;
set_lang;
let s = strings;
println!; // "退出"
Platform support
| OS | Method | Notes |
|---|---|---|
| macOS 10.15+ | lsof -F + batch ps |
Pre-installed, no extra deps |
| Linux | /proc/net/tcp, /proc/net/udp via procfs |
Requires /proc filesystem |