From 362b70d1fa49901aca433aa819cb6faf2df31edf Mon Sep 17 00:00:00 2001 From: Denis Iusupov Date: Mon, 30 Nov 2026 12:29:22 -0500 Subject: [PATCH] trafficctl: add new package Add trafficctl, per-device traffic monitoring and control for routers. It is one ucode CLI, /usr/sbin/trafficctl, with its modules in /usr/share/ucode/trafficctl, three procd services and two hotplug hooks: - per-device rates and byte counters from conntrack - internet blocking per device, WiFi MAC filtering, and a global internet cut that reverts itself after a deadline - rate limiting per device or subnet (nftables policer) and traffic shaping with tc/HTB, restored on boot and on interface up - a durable per-device traffic history kept per accounting period - notifications of new devices and other events to ntfy, a webhook, mail or Telegram, sent by the trafficctl-notify watcher - an optional Prometheus exporter at /cgi-bin/trafficctl-metrics, off by default and protected by a shared token Three optional packages come from the same Makefile, so a router carries only the parts it uses: - trafficctl-shaper pulls in tc, kmod-sched-core and kmod-ifb for the traffic shaping - trafficctl-wifi pulls in iw and hostapd-utils for the WiFi client list and WiFi MAC filtering - trafficctl-telegram is a Telegram bot that accepts block, unblock and throttle commands trafficctl-full pulls in trafficctl and all three. trafficctl-shaper, trafficctl-wifi and trafficctl-full hold no files. Without a part trafficctl checks for its tools at run time and says which package to install. It needs firewall4 (OpenWrt 22.03 or later). The packages have no web interface of their own. luci-app-trafficctl in the LuCI feed provides one, and everything here also works from a shell. The sources come from the tagged releases of the upstream project. Project page and changelog: https://github.com/YusDyr/luci-app-trafficctl Signed-off-by: Denis Iusupov --- net/trafficctl/Makefile | 214 ++++++++++++++++++ net/trafficctl/files/trafficctl-cut.init | 30 +++ net/trafficctl/files/trafficctl-history.init | 42 ++++ .../files/trafficctl-newdevice.hotplug | 17 ++ net/trafficctl/files/trafficctl-notify.init | 36 +++ .../files/trafficctl-notify.uci-defaults | 9 + .../files/trafficctl-shapes.hotplug | 18 ++ net/trafficctl/files/trafficctl-telegram.init | 32 +++ net/trafficctl/files/trafficctl.config | 186 +++++++++++++++ net/trafficctl/files/trafficctl.keep | 1 + net/trafficctl/test-version.sh | 18 ++ 11 files changed, 603 insertions(+) create mode 100644 net/trafficctl/Makefile create mode 100755 net/trafficctl/files/trafficctl-cut.init create mode 100755 net/trafficctl/files/trafficctl-history.init create mode 100644 net/trafficctl/files/trafficctl-newdevice.hotplug create mode 100755 net/trafficctl/files/trafficctl-notify.init create mode 100755 net/trafficctl/files/trafficctl-notify.uci-defaults create mode 100644 net/trafficctl/files/trafficctl-shapes.hotplug create mode 100755 net/trafficctl/files/trafficctl-telegram.init create mode 100644 net/trafficctl/files/trafficctl.config create mode 100644 net/trafficctl/files/trafficctl.keep create mode 100644 net/trafficctl/test-version.sh diff --git a/net/trafficctl/Makefile b/net/trafficctl/Makefile new file mode 100644 index 0000000000000..e0c5e38db3c57 --- /dev/null +++ b/net/trafficctl/Makefile @@ -0,0 +1,214 @@ +# SPDX-License-Identifier: Apache-2.0 + +include $(TOPDIR)/rules.mk + +PKG_NAME:=trafficctl +PKG_VERSION:=2.3.2 +PKG_RELEASE:=1 + +PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz +PKG_SOURCE_URL:=https://codeload.github.com/YusDyr/luci-app-trafficctl/tar.gz/v$(PKG_VERSION)? +PKG_HASH:=575ffd2b8c4942e61a3e8b4718be9977f2ed7eeef20e548427408fd7144530ad +PKG_BUILD_DIR:=$(BUILD_DIR)/luci-app-trafficctl-$(PKG_VERSION) + +PKG_LICENSE:=Apache-2.0 +PKG_LICENSE_FILES:=LICENSE +PKG_MAINTAINER:=Denis Iusupov + +include $(INCLUDE_DIR)/package.mk + +define Package/trafficctl/Default + SECTION:=net + CATEGORY:=Network + URL:=https://github.com/YusDyr/luci-app-trafficctl + PKGARCH:=all +endef + +# trafficctl-shaper, trafficctl-wifi and trafficctl-full hold no files: they +# pull in the tools of the optional parts, which trafficctl checks for at +# run time. +define Package/trafficctl + $(call Package/trafficctl/Default) + TITLE:=Per-device traffic monitoring and control + DEPENDS:=+conntrack +kmod-nft-netdev +firewall4 +ucode +ucode-mod-fs +ucode-mod-uci +ucode-mod-ubus +endef + +define Package/trafficctl-shaper + $(call Package/trafficctl/Default) + TITLE:=Traffic shaping (tc/HTB) for trafficctl + DEPENDS:=+trafficctl +tc +kmod-sched-core +kmod-ifb + EXTRA_DEPENDS:=trafficctl (=$(PKG_VERSION)-r$(PKG_RELEASE)) +endef + +define Package/trafficctl-wifi + $(call Package/trafficctl/Default) + TITLE:=WiFi clients and WiFi blocking for trafficctl + DEPENDS:=+trafficctl +iw +hostapd-utils + EXTRA_DEPENDS:=trafficctl (=$(PKG_VERSION)-r$(PKG_RELEASE)) +endef + +define Package/trafficctl-telegram + $(call Package/trafficctl/Default) + TITLE:=Telegram bot for trafficctl + DEPENDS:=+trafficctl +curl + EXTRA_DEPENDS:=trafficctl (=$(PKG_VERSION)-r$(PKG_RELEASE)) +endef + +define Package/trafficctl-full + $(call Package/trafficctl/Default) + TITLE:=trafficctl with every optional part + DEPENDS:=+trafficctl +trafficctl-shaper +trafficctl-wifi +trafficctl-telegram + EXTRA_DEPENDS:=trafficctl (=$(PKG_VERSION)-r$(PKG_RELEASE)), trafficctl-shaper (=$(PKG_VERSION)-r$(PKG_RELEASE)), trafficctl-wifi (=$(PKG_VERSION)-r$(PKG_RELEASE)), trafficctl-telegram (=$(PKG_VERSION)-r$(PKG_RELEASE)) +endef + +define Package/trafficctl/description + Per-device traffic monitoring (conntrack), rate limiting (nftables), + internet blocking, a global internet cut, port-forward control, device + notifications and a Prometheus exporter. Optional parts are packages of + their own: trafficctl-shaper (traffic shaping with tc/HTB), trafficctl-wifi + (WiFi clients and WiFi MAC filtering) and trafficctl-telegram (the Telegram + bot), or trafficctl-full for all of them. The web interface is + luci-app-trafficctl. The ntfy, webhook and Telegram notification senders + need curl. +endef + +define Package/trafficctl-shaper/description + Traffic shaping for trafficctl: per-device tc/HTB queues on the LAN device + (download) and an IFB device (upload). Pulls in tc, kmod-sched-core and + kmod-ifb; holds no files of its own. +endef + +define Package/trafficctl-wifi/description + WiFi support for trafficctl: the WiFi clients with their band and signal, + and WiFi MAC filtering through the radio's ACL. Pulls in iw and + hostapd-utils; holds no files of its own. Without this package a WiFi + block is a temporary (one hour) hostapd ban over ubus. +endef + +define Package/trafficctl-full/description + trafficctl with every optional part: trafficctl-shaper, trafficctl-wifi and + trafficctl-telegram. Holds no files of its own; install trafficctl and only + the parts you need instead for a smaller install. +endef + +define Package/trafficctl-telegram/description + A Telegram bot for trafficctl: device status, and block, WiFi block, + limit and shape buttons per device. Configured in /etc/config/trafficctl + (section telegram) or from luci-app-trafficctl. +endef + +define Package/trafficctl/conffiles +/etc/config/trafficctl +endef + +define Build/Compile +endef + +# The config holds the bot and metrics tokens, so it is installed at 0600. +define Package/trafficctl/install + $(INSTALL_DIR) $(1)/etc/config + $(INSTALL_CONF) ./files/trafficctl.config $(1)/etc/config/trafficctl + $(INSTALL_DIR) $(1)/etc/hotplug.d/dhcp + $(INSTALL_DATA) ./files/trafficctl-newdevice.hotplug $(1)/etc/hotplug.d/dhcp/99-trafficctl-newdevice + $(INSTALL_DIR) $(1)/etc/hotplug.d/iface + $(INSTALL_DATA) ./files/trafficctl-shapes.hotplug $(1)/etc/hotplug.d/iface/99-trafficctl-shapes + $(INSTALL_DIR) $(1)/etc/init.d + $(INSTALL_BIN) ./files/trafficctl-cut.init $(1)/etc/init.d/trafficctl-cut + $(INSTALL_BIN) ./files/trafficctl-history.init $(1)/etc/init.d/trafficctl-history + $(INSTALL_BIN) ./files/trafficctl-notify.init $(1)/etc/init.d/trafficctl-notify + $(INSTALL_DIR) $(1)/etc/uci-defaults + $(INSTALL_BIN) ./files/trafficctl-notify.uci-defaults $(1)/etc/uci-defaults/80_trafficctl-notify + $(INSTALL_DIR) $(1)/lib/upgrade/keep.d + $(INSTALL_DATA) ./files/trafficctl.keep $(1)/lib/upgrade/keep.d/trafficctl + $(INSTALL_DIR) $(1)/usr/sbin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/trafficctl/files/usr/sbin/trafficctl $(1)/usr/sbin/ + $(INSTALL_DIR) $(1)/usr/share/ucode/trafficctl + $(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/ctl.uc $(1)/usr/share/ucode/trafficctl/ + $(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/cut.uc $(1)/usr/share/ucode/trafficctl/ + $(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/history.uc $(1)/usr/share/ucode/trafficctl/ + $(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/lib.uc $(1)/usr/share/ucode/trafficctl/ + $(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/limit.uc $(1)/usr/share/ucode/trafficctl/ + $(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/net.uc $(1)/usr/share/ucode/trafficctl/ + $(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/notify.uc $(1)/usr/share/ucode/trafficctl/ + $(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/portfw.uc $(1)/usr/share/ucode/trafficctl/ + $(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/report.uc $(1)/usr/share/ucode/trafficctl/ + $(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/shape.uc $(1)/usr/share/ucode/trafficctl/ + $(INSTALL_DIR) $(1)/www/cgi-bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/trafficctl/files/www/cgi-bin/trafficctl-metrics $(1)/www/cgi-bin/ +endef + +# Restart the running services so that they pick up the upgraded scripts. +define Package/trafficctl/postinst +#!/bin/sh +if [ -z "$${IPKG_INSTROOT}" ]; then + if [ -f /var/run/trafficctl/cut.state ] && [ -x /etc/init.d/trafficctl-cut ]; then + /etc/init.d/trafficctl-cut restart 2>/dev/null || true + fi + if [ -x /etc/init.d/trafficctl-notify ] && /etc/init.d/trafficctl-notify enabled; then + /etc/init.d/trafficctl-notify restart 2>/dev/null || true + fi + if [ -x /etc/init.d/trafficctl-history ]; then + if [ "$$(uci -q get trafficctl.history.enabled)" = "1" ]; then + /etc/init.d/trafficctl-history enable 2>/dev/null || true + fi + if /etc/init.d/trafficctl-history enabled; then + /etc/init.d/trafficctl-history restart 2>/dev/null || true + fi + fi + if [ -x /usr/sbin/trafficctl ]; then + TCTL_NOTIFY=0 /usr/sbin/trafficctl restore >/dev/null 2>&1 || true + fi + if [ -f /usr/share/rpcd/ucode/luci.trafficctl.uc ]; then + /etc/init.d/rpcd reload 2>/dev/null || true + fi +fi +exit 0 +endef + +# Release a global internet cut on removal: its drop rule must not outlive +# the package. +define Package/trafficctl/prerm +#!/bin/sh +if [ -z "$${IPKG_INSTROOT}" ]; then + /usr/sbin/trafficctl cut release >/dev/null 2>&1 || true + rm -f /var/run/trafficctl/cut.state /etc/trafficctl/cut.state + nft delete table inet tctl_cut 2>/dev/null || true +fi +exit 0 +endef + +define Package/trafficctl-telegram/postinst +#!/bin/sh +if [ -z "$${IPKG_INSTROOT}" ] && [ -x /etc/init.d/trafficctl-telegram ] && [ "$$(uci -q get trafficctl.telegram.enabled)" = "1" ]; then + /etc/init.d/trafficctl-telegram enable 2>/dev/null || true + /etc/init.d/trafficctl-telegram restart 2>/dev/null || true +fi +exit 0 +endef + +define Package/trafficctl-telegram/install + $(INSTALL_DIR) $(1)/etc/init.d + $(INSTALL_BIN) ./files/trafficctl-telegram.init $(1)/etc/init.d/trafficctl-telegram + $(INSTALL_DIR) $(1)/usr/libexec/trafficctl + $(INSTALL_BIN) $(PKG_BUILD_DIR)/trafficctl/files-telegram/usr/libexec/trafficctl/trafficctl-telegram.uc $(1)/usr/libexec/trafficctl/ + $(INSTALL_DIR) $(1)/usr/share/ucode/trafficctl + $(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files-telegram/usr/share/ucode/trafficctl/telegram.uc $(1)/usr/share/ucode/trafficctl/ +endef + +define Package/trafficctl-shaper/install + true +endef + +define Package/trafficctl-wifi/install + true +endef + +define Package/trafficctl-full/install + true +endef + +$(eval $(call BuildPackage,trafficctl)) +$(eval $(call BuildPackage,trafficctl-shaper)) +$(eval $(call BuildPackage,trafficctl-wifi)) +$(eval $(call BuildPackage,trafficctl-telegram)) +$(eval $(call BuildPackage,trafficctl-full)) diff --git a/net/trafficctl/files/trafficctl-cut.init b/net/trafficctl/files/trafficctl-cut.init new file mode 100755 index 0000000000000..13638f9e0fd5f --- /dev/null +++ b/net/trafficctl/files/trafficctl-cut.init @@ -0,0 +1,30 @@ +#!/bin/sh /etc/rc.common +# SPDX-License-Identifier: Apache-2.0 +# shellcheck shell=dash +# Keeper for the global internet cut: enforces the auto-revert deadline and +# re-asserts the rule if the table it lives in is ever taken out from under it. +# +# Not started at boot on purpose. The cut's engaged state lives in tmpfs, so +# after a reboot there is normally nothing to keep; the one case that does +# survive — an operator who ticked "keep after reboot" — is restored from the +# ifup-lan hotplug, which starts this service itself. Hence no START/STOP: +# default_postinst runs `enable` on every init script, and without them that +# creates no rc.d link. A boot-time keeper could also race the restore and +# remove the rule it was about to install. +# +# No respawn either: the loop is meant to end when the cut does, and a +# respawning keeper would restart into an empty state forever. If it dies +# anyway, `trafficctl cut status` notices and starts a fresh one, and it +# enforces the deadline itself in the meantime. +# +# USE_PROCD is consumed by the sourced /etc/rc.common framework. +# shellcheck disable=SC2034 +USE_PROCD=1 + +start_service() { + procd_open_instance cut + procd_set_param command /usr/sbin/trafficctl cut keeper + procd_set_param stdout 1 + procd_set_param stderr 1 + procd_close_instance +} diff --git a/net/trafficctl/files/trafficctl-history.init b/net/trafficctl/files/trafficctl-history.init new file mode 100755 index 0000000000000..6b1fa25ba794b --- /dev/null +++ b/net/trafficctl/files/trafficctl-history.init @@ -0,0 +1,42 @@ +#!/bin/sh /etc/rc.common +# SPDX-License-Identifier: Apache-2.0 +# shellcheck shell=dash +# Sampler and flusher for the durable per-period traffic history. +# +# Why a service and not a cron line: there are TWO cadences and they are not +# interchangeable. Sampling must be frequent — with a conntrack source, a flow +# that starts and finishes between two samples is invisible to all of them — +# and costs a tmpfs write. Flushing touches durable storage and is therefore +# rare. One process runs both; see history.uc for the accuracy +# model that forces the split. +# +# Respawns, unlike the cut keeper: this is a steady-state service, and a +# sampler that quietly died would not fail loudly — it would just leave a +# month with a plausible-looking number in it that is far too low. +# +# Enabled at boot, but the loop itself refuses to do anything while +# trafficctl.history.enabled is 0, and re-reads that on every tick. So +# switching history on in the UI does not require knowing there is a service +# behind it. +# +# START/STOP/USE_PROCD are consumed by the sourced /etc/rc.common framework. +# shellcheck disable=SC2034 +START=96 +STOP=10 +USE_PROCD=1 + +start_service() { + procd_open_instance history + procd_set_param command /usr/sbin/trafficctl history keeper + procd_set_param respawn 3600 5 0 + procd_set_param stdout 1 + procd_set_param stderr 1 + procd_close_instance +} + +service_triggers() { + # A change to the store location or the intervals is picked up by the loop + # itself, but enabling history for the first time should start sampling + # without waiting for a reboot. + procd_add_reload_trigger "trafficctl" +} diff --git a/net/trafficctl/files/trafficctl-newdevice.hotplug b/net/trafficctl/files/trafficctl-newdevice.hotplug new file mode 100644 index 0000000000000..89c0d99e71587 --- /dev/null +++ b/net/trafficctl/files/trafficctl-newdevice.hotplug @@ -0,0 +1,17 @@ +#!/bin/sh +# SPDX-License-Identifier: Apache-2.0 +# shellcheck shell=dash +# Triggered by dnsmasq on DHCP events (add/old/del), with ACTION, MACADDR, +# IPADDR and HOSTNAME in the environment: the new-device notification +# (`trafficctl notify dhcp`) and the default limit for a device seen for the first time. +# `trafficctl newdevice` (trafficctl.ctl) does both. hotplug-call sources this +# in a subshell, so the variables are handed on explicitly. + +[ "$ACTION" = "add" ] || exit 0 +[ -n "$MACADDR" ] || exit 0 + +# HOSTNAME comes from the dnsmasq dhcp hotplug environment, not a POSIX shell +# builtin; shellcheck's SC3028 is a false positive. +# shellcheck disable=SC3028 +exec env ACTION="$ACTION" MACADDR="$MACADDR" IPADDR="$IPADDR" HOSTNAME="$HOSTNAME" \ + /usr/sbin/trafficctl newdevice >/dev/null 2>&1 diff --git a/net/trafficctl/files/trafficctl-notify.init b/net/trafficctl/files/trafficctl-notify.init new file mode 100755 index 0000000000000..8e9f3405b191f --- /dev/null +++ b/net/trafficctl/files/trafficctl-notify.init @@ -0,0 +1,36 @@ +#!/bin/sh /etc/rc.common +# SPDX-License-Identifier: Apache-2.0 +# shellcheck shell=dash +# Device watcher for the notification events (trafficctl.notify). +# +# new_device and known_device have no kernel event of their own: a device +# that keeps a static address or never asks for a lease shows up only in the +# neighbour table and the station lists. This loop scans them every +# notify.scan_interval seconds (`trafficctl notify watch`). The other events +# are emitted by the commands that cause them and need no service. +# +# The service runs only when something wants the scan (`trafficctl notify +# wanted`): notify.scan '1', or 'auto' (the default) with a sender subscribed +# to new_device or known_device, or a script in /etc/hotplug.d/trafficctl. +# Until 1.31 the Telegram bot ran this scan itself, so it stopped with the bot. +# +# START/STOP/USE_PROCD are consumed by the sourced /etc/rc.common framework. +# shellcheck disable=SC2034 +START=96 +STOP=10 +USE_PROCD=1 + +start_service() { + /usr/sbin/trafficctl notify wanted > /dev/null 2>&1 || return 0 + procd_open_instance notify + procd_set_param command /usr/sbin/trafficctl notify watch + procd_set_param respawn 3600 5 0 + procd_set_param stdout 0 + procd_set_param stderr 1 + procd_close_instance +} + +service_triggers() { + # A sender switched on in the UI starts the watcher without a reboot. + procd_add_reload_trigger "trafficctl" +} diff --git a/net/trafficctl/files/trafficctl-notify.uci-defaults b/net/trafficctl/files/trafficctl-notify.uci-defaults new file mode 100755 index 0000000000000..2467a4d6e9adf --- /dev/null +++ b/net/trafficctl/files/trafficctl-notify.uci-defaults @@ -0,0 +1,9 @@ +#!/bin/sh +# Moves the Telegram notification options of releases up to 1.30.x +# (trafficctl.telegram.notify_new_device, notify_known_device, +# notify_template) to the notification layout: a 'notify' section and a +# 'notify_telegram' sender with the same events and template. It runs once, +# only while the 'notify' section is missing, so a config saved in the new +# layout is never touched. See `trafficctl notify migrate`. +[ -x /usr/sbin/trafficctl ] && /usr/sbin/trafficctl notify migrate >/dev/null 2>&1 +exit 0 diff --git a/net/trafficctl/files/trafficctl-shapes.hotplug b/net/trafficctl/files/trafficctl-shapes.hotplug new file mode 100644 index 0000000000000..dcd4e94bd3f3d --- /dev/null +++ b/net/trafficctl/files/trafficctl-shapes.hotplug @@ -0,0 +1,18 @@ +#!/bin/sh +# SPDX-License-Identifier: Apache-2.0 +# shellcheck shell=dash +# Restore persistent rules after LAN interface comes up + +[ "$ACTION" = "ifup" ] && [ "$INTERFACE" = "lan" ] || exit 0 + +# `trafficctl restore` waits up to 10 seconds for the LAN device and for fw4, +# which at boot can load its ruleset after ifup lan, then puts back the saved +# blocks, rate limits and port controls (persist_rules), every shape +# (shapes.json, always saved) and a global cut engaged with "keep after +# reboot". A limit or a shape still live from before (an ifup without a +# reboot) is replaced; each record it could not put back is logged. +# +# TCTL_NOTIFY=0: it puts back what was there before, so no "Limit applied" or +# "Internet blocked" notification per device on every boot or ifup. +# hotplug-call sources this in a subshell, so the exec ends only that. +exec env TCTL_NOTIFY=0 /usr/sbin/trafficctl restore >/dev/null 2>&1 diff --git a/net/trafficctl/files/trafficctl-telegram.init b/net/trafficctl/files/trafficctl-telegram.init new file mode 100755 index 0000000000000..b59a8b7eb3cd4 --- /dev/null +++ b/net/trafficctl/files/trafficctl-telegram.init @@ -0,0 +1,32 @@ +#!/bin/sh /etc/rc.common +# SPDX-License-Identifier: Apache-2.0 +# shellcheck shell=dash +# START/STOP/USE_PROCD are consumed by the sourced /etc/rc.common framework. +# shellcheck disable=SC2034 +START=99 +STOP=10 +USE_PROCD=1 + +start_service() { + local enabled + config_load trafficctl + config_get enabled telegram enabled 0 + [ "$enabled" = "1" ] || return 0 + + procd_open_instance telegram + procd_set_param command /usr/libexec/trafficctl/trafficctl-telegram.uc + procd_set_param stdout 1 + procd_set_param stderr 1 + procd_set_param respawn 3600 5 5 + procd_set_param file /etc/config/trafficctl + procd_close_instance +} + +service_triggers() { + procd_add_reload_trigger "trafficctl" +} + +reload_service() { + stop + start +} diff --git a/net/trafficctl/files/trafficctl.config b/net/trafficctl/files/trafficctl.config new file mode 100644 index 0000000000000..edeba57713f02 --- /dev/null +++ b/net/trafficctl/files/trafficctl.config @@ -0,0 +1,186 @@ +config trafficctl 'main' + option enabled '1' + option default_mode 'limiter' + # Write per-device blocks and rate limits to /etc/trafficctl/rules.json so + # the ifup-lan hook can restore them after a reboot. + # + # On by default since #105. It used to ship off, which made the same + # intention behave two ways: a traffic SHAPE persists unconditionally (the + # shaper has to record which HTB class belongs to which device, so + # shapes.json is always written and always restored), while a block or a + # limit vanished on reboot — and the default mode is the limiter, so the + # out-of-the-box path was the one that forgot. That was an unexamined + # default sitting next to a deliberate one, not a design. + # + # The cost is one small write to /etc per rule CHANGE. Not periodic, so + # unlike the byte counters — which live in tmpfs precisely because they are + # rewritten every poll — this is not a flash-wear concern. + # + # NOT the same switch as the global cut's "keep after reboot": that one is + # its own opt-in because an all-devices cut can lock out whoever + # administers the router through a LAN host, so a reboot must be able to + # undo it. See the cut section below. + option persist_rules '1' + # Extra subnets to monitor (space-separated CIDRs), for downstream-router + # clients that have no kernel route on this router. Subnets with a + # "via" route over a LAN interface are picked up automatically. + option extra_subnets '' + # Resolve names for devices with no DHCP lease here (routed/downstream + # clients) via reverse DNS. rdns_server is a space-separated list of + # resolvers tried in order before the system resolver — point it at the + # downstream router (e.g. a MikroTik) so its lease names get used. + option resolve_names '1' + option rdns_server '' + option rdns_ttl '300' + # Optional netifyd (DPI) integration — adds application labels per device. + # Inactive unless the agent is installed and its socket exists. + option netify_enabled '1' + option netify_socket '/var/run/netifyd/netifyd.sock' + # Where flow telemetry is published. Agent v5's unix socket is a + # request/response API that never pushes flows — the socket SINK does, + # usually tcp://127.0.0.1:1780. Empty = auto-detect from + # /etc/netifyd/netify-sink-socket.json. + option netify_endpoint '' + option netify_interval '45' + # IFB device used to shape upload pre-NAT (needs kmod-ifb). + option shape_ifb 'tctl-ifb0' + # Dashboard defaults for a browser that has not chosen its own. The Poll + # and Window chips still override per-browser — these only decide what a + # fresh session starts with, which is the part an admin can usefully set + # once for a router rather than per person (#12). + # + # poll_interval: seconds between live samples; 0 disables polling. + # A smaller number costs a conntrack read per tick on the router, which + # is the overhead the request was about. + # avg_window: seconds of history the displayed speed is averaged over. + # Samples kept per device are avg_window/poll_interval, so a long window + # with a short poll is the expensive combination. + option poll_interval '2' + option avg_window '15' + +config newdevice 'newdevice' + # Apply a limit automatically the first time a device is seen. Off by + # default: it is the only setting here that restricts a device nobody + # has looked at yet. limit_kbit 0 means "no limit" and makes enabled + # a no-op. An existing limit is never overwritten. + option enabled '0' + option limit_kbit '0' + option limit_mode 'limiter' + +config cut 'cut' + # Global internet cut (all devices at once). Only PREFERENCES live here — + # whether the cut is currently engaged never does, because /etc/config + # survives a reboot and this control must not. It is the one control that + # can lock out its own operator: anyone administering the router through a + # LAN host (Tailscale on a NAS, a tunnel from a LAN box, a jump host) loses + # that path while it is on, and a reboot has to be able to undo it. + # + # persist is its own opt-in and deliberately NOT the global persist_rules + # flag: somebody who turned that on so their rate limits survive a reboot + # must not inherit a persistent internet kill as a side effect. + option persist '0' + # Duration offered by default, in seconds; 0 means indefinite — the cut + # stays on until it is switched off. + # + # Indefinite is the default because it is what the feature is FOR: the + # requester's case (#55) is holding a new device off the internet until it + # has been configured, which has no predictable duration, and a cut that + # auto-released halfway through would silently let the device out. The + # safety net is that the engaged state lives in tmpfs, so a reboot always + # restores the internet — a timer is a convenience on top of that, not the + # thing standing between the operator and a permanent lockout. + option default_duration '0' + +config history 'history' + # Durable per-device traffic history (#89, #90). OFF by default: every + # other persistent store in this app was deliberately kept off the write + # path, and this one writes to flash on a schedule. + option enabled '0' + # Where the period files live. One file per accounting period, replaced + # atomically through a temp file in this same directory — so a network + # mount works, but see the note on `interval` before reaching for one. + option dir '/etc/trafficctl/history' + # day | week | month. The period IS the reset: a new period is a new file. + option period 'month' + # Seconds between flushes to durable storage. Floored at 300. Hourly is + # roughly 24 writes a day of a few KB, which is not a wear problem — this + # is the setting to raise if flash wear is the worry, in preference to + # pointing `dir` at a share that can hang when the NAS sleeps. + option interval '3600' + # Seconds between samples of the live accumulator. tmpfs only, no flash. + # Separate from `interval` on purpose and much shorter: with a conntrack + # byte source, a flow that starts and ends between two samples is + # invisible to both, so an hourly sample would miss most short traffic. + option sample_interval '60' + # How many FINISHED periods to retain. The current one is never a + # retention candidate. + option keep '12' + # Also accumulate per-INTERFACE totals (#102), in their own file per + # period. A second series rather than a breakdown of the device one: these + # are the kernel's interface counters, so they include the router's own + # traffic and broadcast, they legitimately disagree with the sum over + # devices, and under flow offloading they are the MORE accurate of the two + # because they come from the driver rather than from conntrack. They also + # answer what the device series cannot: how much went out of wan1 versus + # wan2. Off is reasonable on a single-WAN router. + option interfaces '1' + +config logging 'logging' + option enabled '1' + option log_file '/tmp/trafficctl/activity.log' + option max_lines '500' + option syslog '1' + option log_blocks '1' + option log_ratelimits '1' + option log_shapes '1' + option log_telegram '1' + option log_config '1' + +config telegram 'telegram' + option enabled '0' + option bot_token '' + option chat_id '' + option poll_interval '3' + option control_enabled '1' + option btn_block_inet '1' + option btn_block_wifi '1' + option btn_limiter '1' + option btn_shaper '1' + +config notify 'notify' + # Notifications: new_device, known_device, limit_applied, block_applied, + # cut_engaged. Each event goes out on ubus (trafficctl.notify), to the + # scripts in /etc/hotplug.d/trafficctl, and to every sender section below + # that is enabled and lists it. enabled '0' silences the senders only. + option enabled '1' + # The device scan behind new_device and known_device (the + # trafficctl-notify service): 'auto' runs it while a sender or a hotplug + # script takes those events, '1' always, '0' never. + option scan 'auto' + # Seconds between scans. + option scan_interval '30' + +# One section per sender: notify_telegram, notify_ntfy, notify_webhook, +# notify_mail. No 'list events' means every event. Telegram uses the bot's +# token and chat unless the section sets bot_token / chat_id; template is the +# new_device message ({{name}}, {{mac}}, {{ip}}, ...; empty = the default). +config notify_telegram 'notify_telegram' + option enabled '1' + list events 'new_device' + option template '' + +config metrics 'metrics' + # Prometheus/OpenMetrics exporter. Off by default: it exposes device + # names, addresses and traffic volumes without a LuCI session. + option enabled '0' + # Shared token for the CGI endpoint, required: with it empty the + # endpoint answers 403. Scrape with ?token=. + option token '' + # Per-device gauges (connections, blocked, limits, name/mac). Costs a + # full summary run per scrape — set 0 for byte counters only. + option state '1' + # Port-forward metrics. + option portfw '1' + # DPI application metrics. Off by default: device x application is a + # cardinality trap on a busy network. + option apps '0' diff --git a/net/trafficctl/files/trafficctl.keep b/net/trafficctl/files/trafficctl.keep new file mode 100644 index 0000000000000..ead2343141b0e --- /dev/null +++ b/net/trafficctl/files/trafficctl.keep @@ -0,0 +1 @@ +/etc/trafficctl/ diff --git a/net/trafficctl/test-version.sh b/net/trafficctl/test-version.sh new file mode 100644 index 0000000000000..2a5bc8aa9efd2 --- /dev/null +++ b/net/trafficctl/test-version.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# shellcheck shell=busybox + +# /usr/sbin/trafficctl is a ucode CLI that takes positional commands, and +# /usr/libexec/trafficctl/trafficctl-telegram.uc is the bot's process, run +# by its init script. Neither supports a --version flag; trafficctl-shaper, +# trafficctl-wifi and trafficctl-full hold no files. +case "$PKG_NAME" in +trafficctl | trafficctl-telegram | trafficctl-shaper | trafficctl-wifi | trafficctl-full) + exit 0 + ;; + +*) + echo "Untested package: $PKG_NAME" >&2 + exit 1 + ;; +esac