Network Policy Engine for Linux Routers and Gateways
What is BalanSir • Architecture • Components • Quick Start • Configuration • Building • Testing • Documentation
BalanSir is a network policy engine written in Rust for Linux routers, gateways, and embedded devices. It applies declarative rules to traffic, manages VPN and proxy transport drivers, and exposes a unified control-plane API and WebUI.
It is not a VPN client. It is the layer that decides which traffic goes where and how, then drives the transports and kernel state (nftables/netlink) through a privilege-separated daemon/executor pair.
Status: alpha. The project is under active development; the codebase and this README describe the current
mainbranch. Some components are implemented more deeply than others — see the per-component status below.
| Problem | What BalanSir does today |
|---|---|
| Multiple VPN/proxy configs | Unified transport layer with per-protocol drivers |
| VPN profile selection | VpnPool weighted selection with per-profile health probing |
| Local runtime death | L2 lifecycle watchdog (ADR-033): bounded restart/recovery guard |
| No visibility into routing | Decision traces, /subsystems snapshot, Prometheus metrics |
| Privilege separation | Unprivileged daemon + root executor over authenticated IPC |
┌────────────────────────────────────────────────────────────────┐
│ balansir-daemon (unprivileged) │
│ │
│ PolicyEngine (compiles rules → ActionRequest) VpnPool (L1) │
│ │ │ │
│ ▼ ▼ │
│ Coordinator/Reconciler ──── XrayManager ──── B4/DPI engine │
│ │ │ │ │
│ IPC (postcard + SO_PEERCRED) │ │ │
└───────┼───────────────────────────┼──────────────┼─────────────┘
▼ ▼ ▼
┌────────────────────────────────────────────────────────────────┐
│ balansir-executor (root, minimal) │
│ nftables / ip-rule / netlink / interface / QoS / path-MTU │
└────────────────────────────────────────────────────────────────┘
The project follows the Policy → Mechanism split: drivers and the executor apply
mechanism (nftables, kernel state, processes); the policy engine and control plane make
decisions. Two processes communicate over an authenticated binary IPC protocol
(postcard framing + SO_PEERCRED peer validation, ADR-005/ADR-004/ADR-011).
A declarative rule engine. Desired-state rules (config/balansir.toml, [[rules]])
are compiled into backend-neutral ActionRequests and applied through the executor
(nftables/netlink) by the reconciliation loop. A standalone matcher-based PolicyEngine
(domain/IP/port/protocol matchers) with decision traces also exists and is used by the
stress tests; it is not yet wired into the daemon's runtime policy path.
- Config rule actions (parsed from
[[rules]]):allow,block,reject,log. The broaderActionenum (route/mark/forward/shape/queue) exists at the executor boundary but is not yet reachable from the TOML config. - Health-driven rule fallback (
rule.fallback) is implemented in the standalone engine only, not yet exposed from the TOML config. - There is no GeoIP or latency matcher yet.
VPN profile management lives in balansir-vpn plus the daemon's vpn_manager.rs:
-
Profile probe → TCP connect probe → PathSample → PathHealth → VpnPool.
-
TcpConnectProbeis a bounded TCP-connect reachability check ofserver:port(IPv6-safe). Its result feedsPathHealth(EMA latency, hysteresis, anti-flap cooldown) — the L1 health signal. -
VpnPoolperforms weighted profile selection (health weight + availability bonus − latency/load penalties), enforces a minimum dwell time (default 120 s), and cycles profiles throughHealthy → Degraded → Failed → Cooldown → Recovering → Healthystates. When no eligible profile remains, the active profile is cleared (traffic stays direct). -
The selected profile is handed to
XrayManagervia a pool consumer (apply_pool_profile); the pool is authoritative for selection. -
Geo-spoofing (split tunnel): the Xray component config (
BALANSIR_XRAY_CONFIG) accepts ageo_domainslist. When set, only traffic to those domains is routed through the active VPN outbound; all other traffic goes direct. This gives per-service geo-spoofing (e.g. Spotify, Gemini) without proxying the whole LAN. Empty list = all proxied traffic goes through the VPN (legacy behavior). Example:socks_port = 10808 http_port = 10809 geo_domains = ["spotify.com", "api.spotify.com", "gemini.google.com"]
Supported subscription formats (balansir-vpn importer): vless:// URIs
with type=tcp|ws|grpc|httpupgrade|xhttp and
security=none|tls|reality (security=false is accepted as a none alias;
for WS/HTTPUpgrade/XHTTP TLS configs without sni= the effective SNI is
derived from the host= fronting domain, and the Host header is preserved
end-to-end). xhttp (splithttp) is fully supported (mission §10): mode and
optional extra JSON are passed through to the generated runtime config.
Everything else is rejected with an explicit reason — never silently
imported: hysteria2://, trojan://, vmess://, ss://.
XrayManager runs the active Xray driver (VLESS/Reality), supervises it with
driver.health_check(), and manages per-endpoint PathHealth failover for static
endpoints. For the pool-driven runtime it owns the L2 watchdog (ADR-033).
Health model (ADR-033):
- L1 — remote TCP reachability of
server:port. Per-profile. Selection-relevant. - L2 — local active-driver liveness (
kill(pid,0)+ local SOCKS inbound accept). Lifecycle-relevant. - L3 — real tunneled request. Not implemented.
L2 watchdog (implemented) — a bounded restart/recovery guard owned by
XrayManager for the pool-driven active runtime. It is deliberately separate
from the L1 pool health model and never influences profile ranking/selection:
- A startup grace window (default 10 s) tolerates non-Healthy results while the driver is coming up; after it closes, non-Healthy is treated as evidence.
- On failure outside grace it restarts the same driver (never switches VPN profile), respecting a backoff gap (default 5 s).
- Restarts are bounded: max_restarts (default 3) within a rolling window_ms (default 60 s). When the budget is spent the runtime is exhausted and stopped (traffic direct) — no infinite restart loop. A fresh start grants a new bounded budget.
- Verified by unit tests covering grace, evidence, bounded budget exhaustion, backoff gaps, same-driver restart, exhaustion-stops-runtime, and non-rotation of candidates.
- B4 (
balansir-b4+ daemonb4_engine): pure-Rust NFQUEUE engine (netlink-sys, no libnetfilter-queue dependency) with flow classification, packet-level strategies (MSS/StripSack/TTL), and a catch-all profile fallback. It is a policy-controlled adaptation layer (decides how to deliver a flow), never a policy authority. Enabled viaBALANSIR_B4_CONFIG. - DPI manager (
b4_dpi.rs): installs/removes nftables queue rules via the executor, detects a dead engine, and returns to the direct path on stop (never blackholes). Enabled viaBALANSIR_DPI_CONFIG.
| Driver | Status | Notes |
|---|---|---|
| Xray (VLESS/Reality) | Implemented | Real driver; SOCKS/HTTP inbound, health_check (pid + local accept) |
| B4 | Implemented | NFQUEUE engine + DPI manager |
| DNS forwarder | Implemented | SOCKS5 UDP relay, cache, DNS registry |
| UPnP | Implemented | SSDP discovery + SOAP port mapping |
| Hysteria 2 | Implemented | Config + driver present |
| Tailscale | Experimental | Free functions orchestrating tailscale; not a ComponentDriver |
| WireGuard | Partial | Interface up/addr, but no wg setconf (keys/peers not applied) |
| AmneziaWG | Partial | Same as WireGuard; feature-gated |
Enabled by default: wireguard, xray, hysteria, b4, dns (Cargo feature flags).
balansir-daemon: unprivileged process,current_threadtokio runtime, owns the policy/reconciliation loop, drivers, VPN pool, DNS, B4/DPI, Xray, API server, and the shared subsystem snapshot. Startup config viaBALANSIR_CONFIG(a malformed config is fatal; no config = start empty, ADR-027).balansir-executor: minimal root process, refuses to start unlesseuid == 0, applies nftables/ip-rule/interface/QoS/path-MTU changes, and validates peers over IPC (SO_PEERCRED, GID1500).
Axum HTTP server (balansir-api) with REST + SSE:
- Health/readiness:
/health,/ready,/live,/version,/build-info - State:
/desired,/actual,/state,/drift,/subsystems,/system - Control:
/reconcile,/reload,/drivers,/drivers/:id/restart - VPN:
/vpn/pool,/vpn/pause,/vpn/refresh,/vpn/rotate,/vpn/pin - Xray:
/xray,/xray/pause,/xray/select,/xray/rotate - B4/DPI:
/b4,/b4/pause,/dpi - Path:
/path/decision; QoS:/qos; Interfaces:/interfaces - Events:
/events/stream(SSE); Metrics:/metrics(Prometheus)
balansir-ota provides A/B slot updates (mmcblk0p2/p3, cmdline.txt boot swap),
Ed25519-signed manifests with key rotation, boot-confirmation and rollback.
tools/balansir-image builds/inspects/verifies firmware images.
A Svelte dashboard (webui/) that renders the same PathHealth/subsystem state the
managers use — the UI can never disagree with the runtime decisions.
git clone https://github.com/Egorich-print/BalanSir.git
cd BalanSir
# Release build
make build
# Run tests
make test
# Development (debug) build
make devsudo make installInstalls balansir-daemon, balansir-executor, and balansir-cli, plus example
config and systemd units.
# Configure
sudo nano /etc/balansir/balansir.toml
# Start
sudo systemctl start balansir-executor
sudo systemctl start balansir-daemon# CLI (requires a running daemon socket)
balansir-cli status
balansir-cli explain
# REST API
curl http://127.0.0.1:8080/health
curl http://127.0.0.1:8080/subsystems
balansir-clicommands:status,plan,explain,desired,actual,fingerprint,reload <config.toml>.
A multi-stage Dockerfile and docker-compose.yml are provided. The image is built
from this repository (it is not published to a public registry):
docker compose up -dBalanSir is configured via environment variables and TOML files. The daemon loads a
desired-state policy from BALANSIR_CONFIG (example: config/balansir.toml); the
other components are enabled by their own variables (BALANSIR_B4_CONFIG,
BALANSIR_DPI_CONFIG, BALANSIR_XRAY_CONFIG, BALANSIR_VPN_CONFIG,
BALANSIR_DNS_CONFIG). The API server binds per BALANSIR_API_BIND.
Gateway roles (WAN/LAN) can be pinned to physical hardware instead of kernel
interface names — names (eth0, eth1, enx…) are transient across reboots,
USB reconnects and kernel updates (mission §4–§12):
[network]
lan_subnet = "192.168.3.0/24"
# Hardware-identity matchers (fields AND-combined; >=1 required).
[network.wan_match]
usb = "0bda:8156" # USB vid:pid — Realtek RTL8156
# mac = "00:e0:4c:68:02:24" # matches current OR factory MAC
# driver = "r8152" # kernel driver name
[network.lan_match]
mac = "b8:27:eb:8a:4e:ba" # onboard Ethernet (RPi 3B+, smsc95xx)Resolution priority: hardware matchers → legacy names → auto-detect. Ambiguity (e.g. two identical adapters with a driver-only matcher) or zero matches fails closed with a named explanation — never a guess. The 30s re-check loop resolves roles through the same pipeline every tick, so a USB NIC that reconnects under a new name is followed automatically (hotplug recovery).
Operator diagnostics:
balansir-cli network # table: name/link/role/driver/mac/usb/ipv4
balansir-cli identify # full identity chain per interface
balansir-config save # persist /etc/balansir -> /persistent (survives flash)| Device | Bus | Driver | Identity | Verified |
|---|---|---|---|---|
| Realtek RTL8156 (2.5GbE) | USB | r8152 |
0bda:8156 + MAC |
✅ hardware |
| ASIX AX88179/AX88179A | USB | ax88179_178a |
0b95:1790 + MAC |
✅ module (kernel), |
| RPi 3B+ onboard | USB (LAN9514) | smsc95xx |
0424:ec00 + MAC |
✅ hardware |
Any USB Ethernet adapter supported by the kernel works — matchers accept
arbitrary vid:pid/driver/mac. Interface names are never used as
physical identity.
Rootfs slots (A/B) are replaced wholesale by OTA/factory images. Operator
config lives on the persistent partition (mmcblk0p4):
balansir-config save # /etc/balansir -> /persistent/balansir/config
balansir-config restore # runs automatically at boot before the daemonA factory image plus a populated persistent partition boots straight back into the operator's configuration; a fresh persistent partition yields factory defaults. Secrets are never stored in images or the repository.
[policy]
# "pass" (fail-open, default) or "drop" (fail-closed: single terminal drop)
empty_config_action = "pass"
[[rules]]
id = 1
action = "block"
priority = 100
# Flow-level matchers are optional (src/dst IP, dst port, protocol, domain):
# [[rules]]
# id = 2
# action = "allow"
# priority = 90
# dst_domain = "example.com"
# dst_port = 443
# protocol = "tcp"
[[drivers]]
id = "dns"
action = "start"Device profiles live in config/profiles/ (milkv-duos, x86, fornex-weeb). They
select runtime flavor, memory limits, enabled drivers, firewall budgets, and OTA slot
policy. A Raspberry Pi 3B buildroot image is generated from
buildroot-external/configs/balansir_rpi3b_64_defconfig.
- Rust toolchain (stable)
- For cross-compilation, the relevant cross C toolchain (see
.cargo/config.toml; linkers are configured per target)
make dev # debug build
make build # release build
make test # run tests
make check # cargo check + clippy
make install # system-wide install (Linux)
make uninstallCI cross-builds x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu, and
riscv64gc-unknown-linux-musl. These are build/CI targets — they do not imply
hardware testing. Only the Raspberry Pi 3B (buildroot image + gateway E2E harness)
has been exercised on real hardware.
cargo test --workspace --no-fail-fastCurrent main baseline: 483 tests passing, 0 failing, 4 ignored (root-gated
netns tests). Run the root-gated netns tests with:
sudo cargo test -p balansir-tests -- --ignoredTest surface includes unit tests, the gateway E2E harness (tests/gateway_e2e.sh,
run on the Pi), IPC integration tests, and stress tests.
- Architecture audit
- Technical debt
- Roadmap
- Project state
- Operation status
- Architecture Decision Records — ADR-000 … ADR-033 (notable: ADR-005 privilege separation, ADR-027 startup config, ADR-033 two-level health model)
Licensed under either of:
- Apache License, Version 2.0
- MIT License (LICENSE-MIT)
at your option.