Skip to content

Commit ca39714

Browse files
committed
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 - 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 reports new devices and accepts block, unblock and throttle commands trafficctl-shaper and trafficctl-wifi hold no files. Without them trafficctl checks for the 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 <yusdyr@gmail.com>
1 parent 1ac5440 commit ca39714

2 files changed

Lines changed: 296 additions & 0 deletions

File tree

‎net/trafficctl/Makefile‎

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
include $(TOPDIR)/rules.mk
4+
5+
PKG_NAME:=trafficctl
6+
PKG_VERSION:=2.2.0
7+
PKG_RELEASE:=1
8+
9+
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
10+
PKG_SOURCE_URL:=https://codeload.github.com/YusDyr/luci-app-trafficctl/tar.gz/v$(PKG_VERSION)?
11+
PKG_HASH:=5e43d524030f1cc2b921dab3e1b1d619604bc8f3f95f9a24d1c0a33eeb984484
12+
PKG_BUILD_DIR:=$(BUILD_DIR)/luci-app-trafficctl-$(PKG_VERSION)
13+
14+
PKG_LICENSE:=Apache-2.0
15+
PKG_LICENSE_FILES:=LICENSE
16+
PKG_MAINTAINER:=Denis Iusupov <yusdyr@gmail.com>
17+
18+
include $(INCLUDE_DIR)/package.mk
19+
20+
# The optional parts are packages of their own, so a router carries only what
21+
# it uses: trafficctl-shaper (tc, kmod-sched-core, kmod-ifb), trafficctl-wifi
22+
# (iw, hostapd-utils) and trafficctl-telegram (curl). shaper and wifi hold no
23+
# files: without them the core checks for the tools at run time, and
24+
# `trafficctl device` reports shaping and wifi_filter.
25+
# hostapd-utils is on trafficctl-wifi, not here: it can only be selected
26+
# together with a hostapd/wpad variant, so a wired-only build leaves out
27+
# trafficctl-wifi instead of the whole package.
28+
# firewall4: blocks are rules in its inet fw4 table, so nftables alone is not
29+
# enough.
30+
# kmod-nft-netdev carries CONFIG_NF_TABLES_NETDEV, the nft family of the
31+
# limiter's table (netdev tm_ratelimit); firewall4 does not pull it in.
32+
define Package/trafficctl
33+
SECTION:=net
34+
CATEGORY:=Network
35+
TITLE:=Per-device traffic monitoring and control
36+
URL:=https://github.com/YusDyr/luci-app-trafficctl
37+
DEPENDS:=+conntrack +kmod-nft-netdev +firewall4 +ucode +ucode-mod-fs +ucode-mod-uci +ucode-mod-ubus
38+
PKGARCH:=all
39+
endef
40+
41+
define Package/trafficctl-shaper
42+
SECTION:=net
43+
CATEGORY:=Network
44+
TITLE:=Traffic shaping (tc/HTB) for trafficctl
45+
URL:=https://github.com/YusDyr/luci-app-trafficctl
46+
DEPENDS:=+trafficctl +tc +kmod-sched-core +kmod-ifb
47+
EXTRA_DEPENDS:=trafficctl (>=$(PKG_VERSION))
48+
PKGARCH:=all
49+
endef
50+
51+
define Package/trafficctl-wifi
52+
SECTION:=net
53+
CATEGORY:=Network
54+
TITLE:=WiFi clients and WiFi blocking for trafficctl
55+
URL:=https://github.com/YusDyr/luci-app-trafficctl
56+
DEPENDS:=+trafficctl +iw +hostapd-utils
57+
EXTRA_DEPENDS:=trafficctl (>=$(PKG_VERSION))
58+
PKGARCH:=all
59+
endef
60+
61+
define Package/trafficctl-telegram
62+
SECTION:=net
63+
CATEGORY:=Network
64+
TITLE:=Telegram bot for trafficctl
65+
URL:=https://github.com/YusDyr/luci-app-trafficctl
66+
DEPENDS:=+trafficctl +curl
67+
# Versioned, so the opkg/apk solver never pairs this bot with a trafficctl
68+
# older than its own release (1.30.x has no trafficctl.uc for it to call).
69+
EXTRA_DEPENDS:=trafficctl (>=$(PKG_VERSION))
70+
PKGARCH:=all
71+
endef
72+
73+
define Package/trafficctl/description
74+
Per-device traffic monitoring (conntrack), rate limiting (nftables),
75+
internet blocking, a global internet cut, device notifications and a
76+
Prometheus exporter. Optional parts: trafficctl-shaper (traffic shaping
77+
with tc/HTB), trafficctl-wifi (WiFi clients and WiFi MAC filtering) and
78+
trafficctl-telegram (the Telegram bot). The web interface is
79+
luci-app-trafficctl. The ntfy, webhook and Telegram senders need curl.
80+
endef
81+
82+
define Package/trafficctl-shaper/description
83+
Traffic shaping for trafficctl: per-device tc/HTB queues on the LAN device
84+
(download) and an IFB device (upload). Pulls in tc, kmod-sched-core and
85+
kmod-ifb; holds no files of its own.
86+
endef
87+
88+
define Package/trafficctl-wifi/description
89+
WiFi support for trafficctl: WiFi clients with band and signal, and WiFi
90+
MAC filtering through the radio's ACL. Pulls in iw and hostapd-utils;
91+
holds no files of its own.
92+
endef
93+
94+
define Package/trafficctl-telegram/description
95+
A Telegram bot for trafficctl: device status, and block, WiFi block,
96+
limit and shape buttons per device. Configured in /etc/config/trafficctl
97+
(section telegram) or from luci-app-trafficctl.
98+
endef
99+
100+
# Only list files that ship in the package and may be user-edited.
101+
# shapes.json / telegram_known.json are runtime state created by scripts; they
102+
# survive sysupgrade via /lib/upgrade/keep.d/trafficctl instead.
103+
define Package/trafficctl/conffiles
104+
/etc/config/trafficctl
105+
endef
106+
107+
define Build/Compile
108+
endef
109+
110+
define Package/trafficctl/install
111+
$(INSTALL_DIR) $(1)/usr/sbin
112+
$(INSTALL_BIN) $(PKG_BUILD_DIR)/trafficctl/files/usr/sbin/trafficctl $(1)/usr/sbin/
113+
$(INSTALL_DIR) $(1)/usr/share/ucode/trafficctl
114+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/lib.uc $(1)/usr/share/ucode/trafficctl/
115+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/net.uc $(1)/usr/share/ucode/trafficctl/
116+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/report.uc $(1)/usr/share/ucode/trafficctl/
117+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/ctl.uc $(1)/usr/share/ucode/trafficctl/
118+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/notify.uc $(1)/usr/share/ucode/trafficctl/
119+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/cut.uc $(1)/usr/share/ucode/trafficctl/
120+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/portfw.uc $(1)/usr/share/ucode/trafficctl/
121+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/history.uc $(1)/usr/share/ucode/trafficctl/
122+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/limit.uc $(1)/usr/share/ucode/trafficctl/
123+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/shape.uc $(1)/usr/share/ucode/trafficctl/
124+
$(INSTALL_DIR) $(1)/etc/config
125+
$(INSTALL_CONF) $(PKG_BUILD_DIR)/trafficctl/files/etc/config/trafficctl $(1)/etc/config/
126+
$(INSTALL_DIR) $(1)/etc/init.d
127+
$(INSTALL_BIN) $(PKG_BUILD_DIR)/trafficctl/files/etc/init.d/trafficctl-cut $(1)/etc/init.d/
128+
$(INSTALL_BIN) $(PKG_BUILD_DIR)/trafficctl/files/etc/init.d/trafficctl-history $(1)/etc/init.d/
129+
$(INSTALL_BIN) $(PKG_BUILD_DIR)/trafficctl/files/etc/init.d/trafficctl-notify $(1)/etc/init.d/
130+
$(INSTALL_DIR) $(1)/etc/uci-defaults
131+
$(INSTALL_BIN) $(PKG_BUILD_DIR)/trafficctl/files/etc/uci-defaults/80_trafficctl-notify $(1)/etc/uci-defaults/
132+
$(INSTALL_DIR) $(1)/etc/hotplug.d/dhcp
133+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/etc/hotplug.d/dhcp/99-trafficctl-newdevice $(1)/etc/hotplug.d/dhcp/
134+
$(INSTALL_DIR) $(1)/etc/hotplug.d/iface
135+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/etc/hotplug.d/iface/99-trafficctl-shapes $(1)/etc/hotplug.d/iface/
136+
$(INSTALL_DIR) $(1)/lib/upgrade/keep.d
137+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/lib/upgrade/keep.d/trafficctl $(1)/lib/upgrade/keep.d/
138+
$(INSTALL_DIR) $(1)/www/cgi-bin
139+
$(INSTALL_BIN) $(PKG_BUILD_DIR)/trafficctl/files/www/cgi-bin/trafficctl-metrics $(1)/www/cgi-bin/
140+
endef
141+
142+
# The config holds the Telegram bot token and the metrics token, so it must not
143+
# be world-readable.
144+
#
145+
# Services follow their uci flag, so the state after an install or an upgrade
146+
# is the state after the next reboot. rc.d is never disabled here:
147+
# default_postinst enables every init script on a fresh install, and each
148+
# service checks its flag before it starts. A running history sampler is
149+
# restarted so that it picks up the upgraded scripts.
150+
#
151+
# Guarded on IPKG_INSTROOT: when it is set the package is being unpacked into
152+
# an image being built, where there are no running services to talk to.
153+
define Package/trafficctl/postinst
154+
#!/bin/sh
155+
chmod 0600 "$${IPKG_INSTROOT}/etc/config/trafficctl" 2>/dev/null || true
156+
if [ -z "$${IPKG_INSTROOT}" ]; then
157+
nft delete table inet trafficctl_mon 2>/dev/null || true
158+
# Releases up to 1.30.1 gave the cut keeper START=95, so rc.d may
159+
# still link it for boot. It is started on demand only.
160+
rm -f /etc/rc.d/S95trafficctl-cut /etc/rc.d/K10trafficctl-cut
161+
if [ -f /var/run/trafficctl/cut.state ] && [ -x /etc/init.d/trafficctl-cut ]; then
162+
/etc/init.d/trafficctl-cut restart 2>/dev/null || true
163+
fi
164+
# A luci-app-trafficctl up to 1.30.x depends on any trafficctl, so this
165+
# package can be upgraded on its own. That web interface has no ucode
166+
# plugin in /usr/share/rpcd/ucode, and its exec plugin sources
167+
# trafficctl-fw.sh, which 2.0.0 removed, so every page fails.
168+
if [ ! -e /usr/share/rpcd/ucode/luci.trafficctl.uc ] &&
169+
grep -q 'trafficctl-fw.sh' /usr/libexec/rpcd/luci.trafficctl 2>/dev/null; then
170+
_m="The installed luci-app-trafficctl predates trafficctl 2.0.0 and does not work with it: upgrade luci-app-trafficctl too (opkg upgrade luci-app-trafficctl, or apk upgrade luci-app-trafficctl) unless this run already does"
171+
echo "$$_m"
172+
logger -t trafficctl "$$_m" 2>/dev/null || true
173+
fi
174+
# The Telegram options of releases up to 1.30.x move to the notification
175+
# senders (once: only while the 'notify' section is missing).
176+
if [ -x /usr/sbin/trafficctl ]; then
177+
/usr/sbin/trafficctl notify migrate >/dev/null 2>&1 || true
178+
fi
179+
# A bot of a release up to 1.30.x lost its files to trafficctl-telegram
180+
# with this upgrade. Without that package it would run on until the next
181+
# reboot (and scan for devices, which the watcher took over), and rc.d
182+
# would keep links to a missing init script.
183+
if [ ! -e /etc/init.d/trafficctl-telegram ]; then
184+
ubus call service delete '{"name":"trafficctl-telegram"}' >/dev/null 2>&1 || true
185+
rm -f /etc/rc.d/S99trafficctl-telegram /etc/rc.d/K10trafficctl-telegram
186+
# Headless (no luci-app-trafficctl to pull it in): say so where
187+
# the operator will look.
188+
if [ "$$(uci -q get trafficctl.telegram.enabled)" = "1" ]; then
189+
logger -t trafficctl "The Telegram bot is stopped: it is the package trafficctl-telegram now; install it (opkg install trafficctl-telegram, or apk add trafficctl-telegram) to run it again" 2>/dev/null || true
190+
fi
191+
fi
192+
# The watcher decides for itself whether anything wants the scan.
193+
if [ -x /etc/init.d/trafficctl-notify ]; then
194+
/etc/init.d/trafficctl-notify enable 2>/dev/null || true
195+
/etc/init.d/trafficctl-notify restart 2>/dev/null || true
196+
fi
197+
if [ -x /etc/init.d/trafficctl-history ]; then
198+
if [ "$$(uci -q get trafficctl.history.enabled)" = "1" ]; then
199+
/etc/init.d/trafficctl-history enable 2>/dev/null || true
200+
fi
201+
if /etc/init.d/trafficctl-history enabled; then
202+
/etc/init.d/trafficctl-history restart 2>/dev/null || true
203+
fi
204+
fi
205+
# Every saved block, limit, shape and port control goes back in force, as
206+
# the `ifup lan` hook does. Up to 1.30.x each shape added removed the u32
207+
# filters of the others, so only the last one was in force, and an upgrade
208+
# from there left the rest unshaped until the next reboot. TCTL_NOTIFY=0:
209+
# nothing new is applied, so no notification per device.
210+
if [ -x /usr/sbin/trafficctl ]; then
211+
TCTL_NOTIFY=0 /usr/sbin/trafficctl restore >/dev/null 2>&1 || true
212+
fi
213+
if [ -f /usr/share/rpcd/ucode/luci.trafficctl.uc ]; then
214+
/etc/init.d/rpcd reload 2>/dev/null || true
215+
fi
216+
fi
217+
exit 0
218+
endef
219+
220+
# Fail open on removal. A global internet cut left engaged by a package that is
221+
# going away is a lockout with no UI left to undo it: the drop rule would sit
222+
# in nftables with nothing on the router admitting to owning it. The release
223+
# needs ucode and every module to load, so its effect is then made sure of
224+
# without them: no state for a keeper to re-assert from, and no table.
225+
define Package/trafficctl/prerm
226+
#!/bin/sh
227+
if [ -z "$${IPKG_INSTROOT}" ]; then
228+
/usr/sbin/trafficctl cut release >/dev/null 2>&1 || true
229+
rm -f /var/run/trafficctl/cut.state /etc/trafficctl/cut.state
230+
nft delete table inet tctl_cut 2>/dev/null || true
231+
fi
232+
exit 0
233+
endef
234+
235+
# Restarted, not started: a bot of a release up to 1.30.x runs the device
236+
# scan itself, which the notification watcher took over. The flag rule is
237+
# trafficctl's (above). trafficctl's postinst has run by now: the package
238+
# manager configures a dependency first, so the Telegram options of 1.30.x
239+
# have already moved to the notification senders.
240+
define Package/trafficctl-telegram/postinst
241+
#!/bin/sh
242+
if [ -z "$${IPKG_INSTROOT}" ] && [ -x /etc/init.d/trafficctl-telegram ] && [ "$$(uci -q get trafficctl.telegram.enabled)" = "1" ]; then
243+
/etc/init.d/trafficctl-telegram enable 2>/dev/null || true
244+
/etc/init.d/trafficctl-telegram restart 2>/dev/null || true
245+
fi
246+
exit 0
247+
endef
248+
249+
define Package/trafficctl-telegram/prerm
250+
#!/bin/sh
251+
if [ -z "$${IPKG_INSTROOT}" ] && [ -x /etc/init.d/trafficctl-telegram ]; then
252+
/etc/init.d/trafficctl-telegram stop 2>/dev/null || true
253+
/etc/init.d/trafficctl-telegram disable 2>/dev/null || true
254+
fi
255+
exit 0
256+
endef
257+
258+
define Package/trafficctl-telegram/install
259+
$(INSTALL_DIR) $(1)/usr/share/ucode/trafficctl
260+
$(INSTALL_DATA) $(PKG_BUILD_DIR)/trafficctl/files/usr/share/ucode/trafficctl/telegram.uc $(1)/usr/share/ucode/trafficctl/
261+
$(INSTALL_DIR) $(1)/usr/libexec/trafficctl
262+
$(INSTALL_BIN) $(PKG_BUILD_DIR)/trafficctl/files/usr/libexec/trafficctl/trafficctl-telegram.uc $(1)/usr/libexec/trafficctl/
263+
$(INSTALL_DIR) $(1)/etc/init.d
264+
$(INSTALL_BIN) $(PKG_BUILD_DIR)/trafficctl/files/etc/init.d/trafficctl-telegram $(1)/etc/init.d/
265+
endef
266+
267+
define Package/trafficctl-shaper/install
268+
true
269+
endef
270+
271+
define Package/trafficctl-wifi/install
272+
true
273+
endef
274+
275+
$(eval $(call BuildPackage,trafficctl))
276+
$(eval $(call BuildPackage,trafficctl-shaper))
277+
$(eval $(call BuildPackage,trafficctl-wifi))
278+
$(eval $(call BuildPackage,trafficctl-telegram))

‎net/trafficctl/test-version.sh‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
3+
# shellcheck shell=busybox
4+
5+
# /usr/sbin/trafficctl is a ucode CLI that takes positional commands, and
6+
# /usr/libexec/trafficctl/trafficctl-telegram.uc is the bot's process, run
7+
# by its init script. Neither supports a --version flag; trafficctl-shaper
8+
# and trafficctl-wifi hold no files.
9+
case "$PKG_NAME" in
10+
trafficctl | trafficctl-telegram | trafficctl-shaper | trafficctl-wifi)
11+
exit 0
12+
;;
13+
14+
*)
15+
echo "Untested package: $PKG_NAME" >&2
16+
exit 1
17+
;;
18+
esac

0 commit comments

Comments
 (0)