Fail-closed egress protection for Linux proxy gateways. nftables and a pinned
TCX eBPF egress program enforce approved direct destinations independently. The
Linux kernel keeps the TCX link attached across process exits and nftables
reloads, preventing direct traffic leaks if the proxy crashes or firewall rules
are flushed.
LAN clients ─▶ Proxy (TPROXY / TUN) ─▶ nftables guard ─▶ TCX eBPF egress ─▶ WAN
│ │
drops unapproved independent kernel
direct egress fail-closed LPM check
Clatch uses Go for configuration, kernel state, and operations, and C for the eBPF packet classifier. Project source is licensed under GPL-2.0-only; dependencies retain their upstream licenses.
- Linux kernel 6.6 or newer with pinned TCX eBPF support and
bpffs. - Host utilities:
nft,ip,tc,sysctl(plusconntrackfor optionalclatch health --conntrack). - Linux systemd gateway target (Debian 13+, Ubuntu 24.04+/26.04+, etc.):
static IPv4 configured via either
ifupdown(/etc/network/interfaces) orsystemd-networkd/ Netplan,systemd,ping,traceroute, and one supported backend (xray-core,v2ray-core,mihomoTPROXY/TUN withtun.device: clatch-tun, or nativev2rayATPROXY/TUN). - OpenWrt target (25.12+):
netifd,firewall4,ip-full,tc-tiny, and optionallykmod-nft-tproxyfor LAN capture. - macOS / non-Linux offline verifier:
clatchalso builds natively on macOS (make macos) for offline config/profile validation, nftables rendering, LPM explanation, schema export, and starter generation; Linux-only runtime commands display a clear platform hint. - IPv4-only egress: public IPv6 egress is intentionally disabled.
Run clatch doctor on any target host to verify all prerequisites in one pass.
Prepare your static IPv4 interface (ifupdown or systemd-networkd) and proxy
service first, then:
# 1. Generate starter profile and policy files (auto-detects local IPv4 network on Linux)
./clatch-linux-amd64 init profile --backend xray-core --output /root/gateway-profile.json
./clatch-linux-amd64 init policy --output /root/gateway-policy.json
# 2. Edit both files for your proxy paths and approved direct CIDRs, then check readiness
./clatch-linux-amd64 doctor --profile /root/gateway-profile.json
# 3. Review the installation plan (no changes written yet)
./clatch-linux-amd64 install plan /root/gateway-profile.json
# 4. Apply with a persistent local backup directory (arms a 10-minute rollback timer)
./clatch-linux-amd64 install apply /root/gateway-profile.json /root/clatch-install
# 5. Verify guard enforcement and DNS/HTTPS health, then confirm
clatch guard verify
clatch health
clatch install confirmExample templates for all supported backends are provided in deploy/debian/:
- standalone.example.json (Xray TPROXY)
- mihomo-tun.example.json (Mihomo managed TUN,
device: clatch-tun) - v2raya.example.json (Native v2rayA)
- policy.example.json (Standalone CIDR policy source)
- health.example.json (Health check defaults)
install apply leaves an originally stopped proxy service stopped; start it and
verify DNS before confirming. To roll back manually (even after confirmation):
/root/clatch-install/clatch-recover install rollback /root/clatch-install# Safely upgrade /usr/local/sbin/clatch (backs up previous binary and verifies guard)
./clatch-linux-amd64 upgrade
# Preview uninstall actions (dry run), then remove cleanly with --yes
clatch uninstall
clatch uninstall --yes
clatch uninstall --keep-config --yesAll operational commands default to /etc/clatch/config.json (override with
--config), print human-readable output by default, and emit structured JSON
with --json or CLATCH_FORMAT=json.
clatch status # Local service, guard, policy, tunnel & pending state
clatch doctor # Full non-destructive host & config readiness checklist
clatch guard verify # Verify live nftables ruleset and pinned TCX program
clatch config check # Validate config.json and approved policy capacity
clatch explain 1.1.1.1 --live # Explain why an IP is allowed direct egress or blocked
clatch stats # Show TCX allow/deny packet counters
clatch health # Run UDP/TCP DNS and direct/proxied HTTPS probesclatch health reads defaults from the sibling health.json next to
config.json (for example /etc/clatch/health.json). Explicit CLI flags
override saved values.
{
"expected_exits": ["203.0.113.10"],
"conntrack": true,
"direct_url": "https://www.baidu.com/",
"proxy_url": "https://ip.fm/myip",
"dns_probes": [
{"domain": "www.baidu.com", "type": "A"},
{"domain": "ip.fm", "type": "A"},
{"domain": "www.google.com", "type": "A"},
{"domain": "www.google.com", "type": "AAAA"}
]
}- When
config.jsonhas one local IPv4 address,healthuses it on port 53 as the default DNS endpoint unlessdnsor--dnsis set. direct_url,proxy_url, anddns_probesare optional; omit them to use Clatch's built-in defaults, or customize them for your region and routing rules.- Run health probes from a host whose traffic follows the gateway under test.
On the gateway itself,
--conntrackverifies tracked HTTPS flows (including native v2rayA TUN reply tuples).
For native v2rayA gateways, preview changes first and commit with --apply
(flags may appear before or after the subcommand):
clatch whitelist show
clatch whitelist add 198.51.100.10/32
clatch whitelist add 198.51.100.10/32 --apply
clatch whitelist refresh --applyFor standalone backends (xray-core, v2ray-core, mihomo), update direct
CIDRs atomically through the old/new intersection:
clatch policy stage /etc/clatch/config.json /root/policy-tx /root/new-policy.json
clatch policy commit /etc/clatch/config.json /root/policy-tx- Dual-mode output: Clean aligned text by default; add
--json,--format json, or setCLATCH_FORMAT=jsonfor strict machine-readable JSON on every command (including structured error envelopes on failure). - Self-describing CLI:
clatch version --jsonreports build version, target, and embedded BPF status.clatch help --json(andclatch help COMMAND --json) exports the full command catalog, flags, defaults, mutating/root/platform metadata, and exit codes.clatch schema --listandclatch schema <NAME>print Draft 2020-12 JSON Schemas forinstall-profile,config,policy-source,health, andopenwrt.
- Cross-platform offline verifier (macOS):
- Run
config check,nft render,explain(offline),init,schema,doctor,openwrt check,version, andhelpnatively on macOS before deploying to Linux. Linux-only runtime commands exit cleanly with anUNSUPPORTED_PLATFORMhint.
- Run
- Exit codes:
0succeeded (statuswithout--strictreturns0even on degraded reports; add--strictto fail non-healthy states),1check or operation failed (or unsupported platform),2invalid arguments or unknown command.
See docs/agent-guide.md for a concise AI agent playbook.
The routed WAN profile uses netifd, firewall4, ip-full, and tc-tiny on a
kernel with pinned TCX egress support. It is intentionally IPv4-only. Run the
proxy as a separate service and add each proxy server endpoint to the direct
policy.
- Install the release binary as
/usr/sbin/clatch, then generate and validate the configuration files:See also deploy/openwrt/openwrt.example.json and deploy/openwrt/policy.example.json. Useclatch init openwrt --lan-device br-lan --lan 192.168.56.0/24 --uplink wan:eth1 --output /etc/clatch/openwrt.json clatch init policy --networks 198.18.0.1/32,198.18.0.3/32 --output /etc/clatch/policy.json clatch openwrt check
unmanaged_egress_devicesfor interfaces whose egress remains under the existing firewall (for example, a primary uplink during a guarded backup trial). - Install the embedded
netifdprotocol handlers (clatch_static.shand auto-generatedclatch_dhcp.sh):clatch openwrt install --dry-run clatch openwrt install
- From the local console, configure each external IPv4 interface with
proto=clatch_staticorproto=clatch_dhcpand its exact UCIdevice. Disable separate WAN IPv6 interfaces and flow offloading before bringing up guarded uplinks. - Inspect and verify:
clatch openwrt status clatch openwrt verify /etc/clatch/openwrt.json wan eth1 clatch explain 198.18.0.1 --openwrt /etc/clatch/openwrt.json clatch stats --openwrt /etc/clatch/openwrt.json
- For LAN TCP and UDP capture, install
kmod-nft-tproxyand configure a transparent listener on127.0.0.1:clatch openwrt tproxy-start /etc/clatch/openwrt.json 12345 clatch openwrt tproxy-stop
Go 1.26 or newer builds both static Linux release binaries (CGO_ENABLED=0,
embedded eBPF object) and the macOS offline verifier (make macos). Rebuilding
the eBPF object itself requires Clang with a BPF target, Linux UAPI headers, and
libbpf headers.
make -j2 # Linux amd64 & arm64 binaries with embedded eBPF
make macos # Native macOS offline verifier binary
make testOn macOS or a host without the Linux BPF toolchain, build the eBPF object in the included Debian container first:
make bpf-container
make -j2 VERSION=v0.1.0Release artifacts are written to dist/clatch-linux-amd64,
dist/clatch-linux-arm64, and dist/clatch-darwin-<arch>.
- See docs/verification.md for the tested backends, hardware targets, and packet leak verification matrix.
- Low-level subcommands (
bpf,nft render,native,tproxy,managed-tun) remain available for advanced diagnostics and custom wiring. Inclatch bpf, passing-as the object path selects the embedded eBPF object.