Skip to content

Commit ba97c4d

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 - 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 <yusdyr@gmail.com>
1 parent ea3e1b6 commit ba97c4d

2 files changed

Lines changed: 200 additions & 0 deletions

File tree

‎net/trafficctl/Makefile‎

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
include $(TOPDIR)/rules.mk
4+
5+
PKG_NAME:=trafficctl
6+
PKG_VERSION:=2.3.2
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:=575ffd2b8c4942e61a3e8b4718be9977f2ed7eeef20e548427408fd7144530ad
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+
define Package/trafficctl/Default
21+
SECTION:=net
22+
CATEGORY:=Network
23+
URL:=https://github.com/YusDyr/luci-app-trafficctl
24+
PKGARCH:=all
25+
endef
26+
27+
# trafficctl-shaper, trafficctl-wifi and trafficctl-full hold no files: they
28+
# pull in the tools of the optional parts, which trafficctl checks for at
29+
# run time.
30+
define Package/trafficctl
31+
$(call Package/trafficctl/Default)
32+
TITLE:=Per-device traffic monitoring and control
33+
DEPENDS:=+conntrack +kmod-nft-netdev +firewall4 +ucode +ucode-mod-fs +ucode-mod-uci +ucode-mod-ubus
34+
endef
35+
36+
define Package/trafficctl-shaper
37+
$(call Package/trafficctl/Default)
38+
TITLE:=Traffic shaping (tc/HTB) for trafficctl
39+
DEPENDS:=+trafficctl +tc +kmod-sched-core +kmod-ifb
40+
EXTRA_DEPENDS:=trafficctl (=$(PKG_VERSION)-r$(PKG_RELEASE))
41+
endef
42+
43+
define Package/trafficctl-wifi
44+
$(call Package/trafficctl/Default)
45+
TITLE:=WiFi clients and WiFi blocking for trafficctl
46+
DEPENDS:=+trafficctl +iw +hostapd-utils
47+
EXTRA_DEPENDS:=trafficctl (=$(PKG_VERSION)-r$(PKG_RELEASE))
48+
endef
49+
50+
define Package/trafficctl-telegram
51+
$(call Package/trafficctl/Default)
52+
TITLE:=Telegram bot for trafficctl
53+
DEPENDS:=+trafficctl +curl
54+
EXTRA_DEPENDS:=trafficctl (=$(PKG_VERSION)-r$(PKG_RELEASE))
55+
endef
56+
57+
define Package/trafficctl-full
58+
$(call Package/trafficctl/Default)
59+
TITLE:=trafficctl with every optional part
60+
DEPENDS:=+trafficctl +trafficctl-shaper +trafficctl-wifi +trafficctl-telegram
61+
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))
62+
endef
63+
64+
define Package/trafficctl/description
65+
Per-device traffic monitoring (conntrack), rate limiting (nftables),
66+
internet blocking, a global internet cut, port-forward control, device
67+
notifications and a Prometheus exporter. Optional parts are packages of
68+
their own: trafficctl-shaper (traffic shaping with tc/HTB), trafficctl-wifi
69+
(WiFi clients and WiFi MAC filtering) and trafficctl-telegram (the Telegram
70+
bot), or trafficctl-full for all of them. The web interface is
71+
luci-app-trafficctl. The ntfy, webhook and Telegram notification senders
72+
need curl.
73+
endef
74+
75+
define Package/trafficctl-shaper/description
76+
Traffic shaping for trafficctl: per-device tc/HTB queues on the LAN device
77+
(download) and an IFB device (upload). Pulls in tc, kmod-sched-core and
78+
kmod-ifb; holds no files of its own.
79+
endef
80+
81+
define Package/trafficctl-wifi/description
82+
WiFi support for trafficctl: the WiFi clients with their band and signal,
83+
and WiFi MAC filtering through the radio's ACL. Pulls in iw and
84+
hostapd-utils; holds no files of its own. Without hostapd-utils a WiFi block
85+
is a temporary (one hour) hostapd ban over ubus.
86+
endef
87+
88+
define Package/trafficctl-full/description
89+
trafficctl with every optional part: trafficctl-shaper, trafficctl-wifi and
90+
trafficctl-telegram. Holds no files of its own; install trafficctl and only
91+
the parts you need instead for a smaller install.
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+
define Package/trafficctl/conffiles
101+
/etc/config/trafficctl
102+
endef
103+
104+
define Build/Compile
105+
endef
106+
107+
# The config holds the bot and metrics tokens, so it is installed at 0600.
108+
define Package/trafficctl/install
109+
$(CP) $(PKG_BUILD_DIR)/trafficctl/files/* $(1)/
110+
$(INSTALL_CONF) $(PKG_BUILD_DIR)/trafficctl/files/etc/config/trafficctl $(1)/etc/config/
111+
endef
112+
113+
# Restart the running services so that they pick up the upgraded scripts.
114+
define Package/trafficctl/postinst
115+
#!/bin/sh
116+
if [ -z "$${IPKG_INSTROOT}" ]; then
117+
if [ -f /var/run/trafficctl/cut.state ] && [ -x /etc/init.d/trafficctl-cut ]; then
118+
/etc/init.d/trafficctl-cut restart 2>/dev/null || true
119+
fi
120+
if [ -x /etc/init.d/trafficctl-notify ] && /etc/init.d/trafficctl-notify enabled; then
121+
/etc/init.d/trafficctl-notify restart 2>/dev/null || true
122+
fi
123+
if [ -x /etc/init.d/trafficctl-history ]; then
124+
if [ "$$(uci -q get trafficctl.history.enabled)" = "1" ]; then
125+
/etc/init.d/trafficctl-history enable 2>/dev/null || true
126+
fi
127+
if /etc/init.d/trafficctl-history enabled; then
128+
/etc/init.d/trafficctl-history restart 2>/dev/null || true
129+
fi
130+
fi
131+
if [ -x /usr/sbin/trafficctl ]; then
132+
TCTL_NOTIFY=0 /usr/sbin/trafficctl restore >/dev/null 2>&1 || true
133+
fi
134+
if [ -f /usr/share/rpcd/ucode/luci.trafficctl.uc ]; then
135+
/etc/init.d/rpcd reload 2>/dev/null || true
136+
fi
137+
fi
138+
exit 0
139+
endef
140+
141+
# Release a global internet cut on removal: its drop rule must not outlive
142+
# the package.
143+
define Package/trafficctl/prerm
144+
#!/bin/sh
145+
if [ -z "$${IPKG_INSTROOT}" ]; then
146+
/usr/sbin/trafficctl cut release >/dev/null 2>&1 || true
147+
rm -f /var/run/trafficctl/cut.state /etc/trafficctl/cut.state
148+
nft delete table inet tctl_cut 2>/dev/null || true
149+
fi
150+
exit 0
151+
endef
152+
153+
define Package/trafficctl-telegram/postinst
154+
#!/bin/sh
155+
if [ -z "$${IPKG_INSTROOT}" ] && [ -x /etc/init.d/trafficctl-telegram ] && [ "$$(uci -q get trafficctl.telegram.enabled)" = "1" ]; then
156+
/etc/init.d/trafficctl-telegram enable 2>/dev/null || true
157+
/etc/init.d/trafficctl-telegram restart 2>/dev/null || true
158+
fi
159+
exit 0
160+
endef
161+
162+
define Package/trafficctl-telegram/install
163+
$(CP) $(PKG_BUILD_DIR)/trafficctl/files-telegram/* $(1)/
164+
endef
165+
166+
define Package/trafficctl-shaper/install
167+
true
168+
endef
169+
170+
define Package/trafficctl-wifi/install
171+
true
172+
endef
173+
174+
define Package/trafficctl-full/install
175+
true
176+
endef
177+
178+
$(eval $(call BuildPackage,trafficctl))
179+
$(eval $(call BuildPackage,trafficctl-shaper))
180+
$(eval $(call BuildPackage,trafficctl-wifi))
181+
$(eval $(call BuildPackage,trafficctl-telegram))
182+
$(eval $(call BuildPackage,trafficctl-full))

‎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+
# trafficctl-wifi and trafficctl-full hold no files.
9+
case "$PKG_NAME" in
10+
trafficctl | trafficctl-telegram | trafficctl-shaper | trafficctl-wifi | trafficctl-full)
11+
exit 0
12+
;;
13+
14+
*)
15+
echo "Untested package: $PKG_NAME" >&2
16+
exit 1
17+
;;
18+
esac

0 commit comments

Comments
 (0)