| Crates.io | rust-network-mgr |
| lib.rs | rust-network-mgr |
| version | 0.1.8 |
| created_at | 2025-04-04 08:25:01.813354+00 |
| updated_at | 2025-04-04 16:27:38.765955+00 |
| description | Linux based network management, packet routing and LAN peers IP monitoring service |
| homepage | |
| repository | https://github.com/sparesparrow/rust-network-mgr |
| max_upload_size | |
| id | 1619667 |
| size | 159,773 |
This document outlines a unified network management service written in Rust. It monitors network interface IP address changes and dynamically updates nftables sets accordingly. Optionally, it can also monitor Docker container lifecycle events and track their IP addresses. It is designed for Linux systems (Ubuntu/Debian) using nftables.
nftables is installed and running.
sudo apt update && sudo apt install nftables
sudo systemctl enable nftables
sudo systemctl start nftables
sudo apt install build-essential pkg-config libmnl-dev
/var/run/docker.sock). This might involve adding the user to the docker group.The network manager service is responsible for:
nftables IP sets based on the nftables_zone configured for each monitored interface.This Rust implementation provides memory safety, improved concurrency handling, and maintainability.
src/main.rs): Central process coordinating all activities, handling signals, and managing the main event loop.src/config.rs): Handles loading and validating network configuration from /etc/rust-network-mgr/config.yaml or a path specified by RUST_NETWORK_MGR_CONFIG.src/network.rs): Uses rtnetlink to detect IP address and interface changes, emitting events.src/nftables.rs): Interacts with nftables via the rustables crate to update IP sets based on network state. Assumes base nftables ruleset (specifically table inet filter) and the relevant sets (e.g., wan_ips, lan_ips) are already defined.src/socket.rs): Listens on /run/rust-network-mgr.sock for commands (reload, status, ping).src/docker.rs): (Optional) Connects to the Docker daemon socket using the bollard crate. Listens for container start, stop, and die events. Inspects started containers to retrieve their IP addresses and updates the application's internal state. Fails gracefully if the Docker socket is inaccessible.graph TD
A[Main Daemon] --> B(Configuration Parser)
A --> C(Network Monitor)
A --> D(NFTables Manager)
A --> E(Control Socket)
A --> J(Docker Monitor)
subgraph "Input/Output"
F[YAML Config] --> B
G[System Netlink] <--> C
H[nftables API] <--> D
I[Unix Socket Commands] <--> E
K[Docker Daemon Socket] <--> J
end
C -- IP/Interface Events --> A
J -- Docker Events --> A
A -- Update Sets --> D
E -- Control Commands --> A
nftables sets (e.g., inet filter/wan_ips) when corresponding interface IPs change. Uses rustables for atomic updates.nftables_zone.reload, status, ping commands./etc/rust-network-mgr/config.yaml) ```yaml
interfaces:
### NFTables Setup Example
This service *expects* a base `nftables` configuration to exist. It only manages the *elements* within predefined sets.
Example base `/etc/nftables.conf` snippet:
```nftables
table inet filter {
set wan_ips {
type ipv4_addr
flags dynamic
}
set lan_ips {
type ipv4_addr
flags dynamic
}
chain input {
type filter hook input priority 0; policy accept;
# Example rule using the set
ip saddr @wan_ips counter drop
}
# ... other chains and rules
}
rust-network-mgr/
├── Cargo.toml
├── src/
│ ├── main.rs
│ ├── network.rs
│ ├── nftables.rs
│ ├── config.rs
│ ├── socket.rs
│ ├── docker.rs # Docker monitoring module
│ └── types.rs
├── tests/
│ ├── basic_tests.rs
│ └── nftables_manager_tests.rs # Requires root/nftables access
├── pkg-files/
│ ├── systemd/
│ │ └── rust-network-mgr.service # Example systemd unit
│ └── config/
│ └── default.yaml # Example config
└── README.md
[dependencies]
# Core Async/Runtime
tokio = { version = "1", features = ["full"] }
futures = "0.3"
# Network Monitoring
rtnetlink = "0.16.0"
netlink-packet-route = "0.22.0"
# NFTables Interaction
rustables = "0.8.6"
# Docker Interaction (Optional Feature)
bollard = "0.15"
# Configuration
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"
directories = "5.0" # For finding config dirs
# Error Handling & Logging
anyhow = "1.0"
thiserror = "1.0"
log = "0.4"
env_logger = "0.10" # Or tracing/tracing-subscriber
# Utilities
nix = "0.27.1" # For Unix socket permissions
(See Cargo.toml for the definitive list)
cargo build --release (Binary at target/release/rust-network-mgr)cargo test (Some tests require sudo and nftables)
sudo cargo test -- --ignoredcargo clippycargo fmt/etc/rust-network-mgr/config.yaml exists or set RUST_NETWORK_MGR_CONFIG./var/run/docker.sock if Docker monitoring is desired.sudo target/release/rust-network-mgrpkg-files/systemd/rust-network-mgr.service to /etc/systemd/system/, then sudo systemctl daemon-reload, sudo systemctl start rust-network-mgr. Check status with sudo systemctl status rust-network-mgr and logs with sudo journalctl -u rust-network-mgr -f.# Send commands using socat (install if needed: sudo apt install socat)
echo "status" | sudo socat - UNIX-CONNECT:/run/rust-network-mgr.sock
echo "reload" | sudo socat - UNIX-CONNECT:/run/rust-network-mgr.sock
echo "ping" | sudo socat - UNIX-CONNECT:/run/rust-network-mgr.sock
nftables sets based on monitored IPs.nftables set updates. Optional features like Docker monitoring should be self-contained and fail gracefully.Current Scope: This tool focuses primarily on monitoring interface IPs via Netlink and updating pre-existing nftables sets based on a simple configuration map (nftables_zone). It does not manage nftables rules, tables, or chains beyond adding/removing IPs from sets. The optional Docker monitoring currently tracks container IPs internally but does not yet integrate them into nftables rules.
While the core focus is narrow, future enhancements could include:
nftables interaction (e.g., managing rules directly).nftables sets.nftables rules/sets (e.g., based on container labels or predefined mappings).(These are ideas, not commitments. Development follows the priorities above.)