Repository navigation
Conversation
BKPepe
left a comment
There was a problem hiding this comment.
I'm not entirely convinced that opening a pull request with such a large number of files is the best approach. When reviewing some of the files, I also noticed quite a few comments explaining what the code does, rather than focusing on the implementation itself. Is this a side effect of AI-assisted development, or is there a particular reason for this approach? I think it would be worth keeping the changes as focused and straightforward as possible to make the review easier.
Also, could we consider supporting notification methods other than Telegram?
| CATEGORY:=Network | ||
| TITLE:=Per-device traffic monitoring and control | ||
| URL:=https://github.com/YusDyr/luci-app-trafficctl | ||
| DEPENDS:=+conntrack +curl +tc +iw |
There was a problem hiding this comment.
The default limiter mode creates a netdev nft table, which needs kmod-nft-netdev; firewall4 doesn't pull it in, and the nft add table netdev ... failure is sent to /dev/null. The fw3 path likewise uses -m hashlimit without depending on iptables-mod-hashlimit. Declare both as conditional deps.
| DEPENDS:=+conntrack +curl +tc +iw | |
| DEPENDS:=+conntrack +curl +tc +iw \ | |
| +PACKAGE_firewall4:kmod-nft-netdev \ | |
| +PACKAGE_firewall:iptables-mod-hashlimit |
Generated by Claude Code
There was a problem hiding this comment.
| # | ||
| # START/STOP/USE_PROCD are consumed by the sourced /etc/rc.common framework. | ||
| # shellcheck disable=SC2034 | ||
| START=95 |
There was a problem hiding this comment.
The header says this service is "not enabled at boot on purpose", but default_postinst runs enable on every shipped init script, and START makes that create an S95 link. Drop START so enable never creates a boot link.
| START=95 |
Generated by Claude Code
There was a problem hiding this comment.
| [ "$(uci -q get trafficctl.metrics.enabled 2>/dev/null)" = "1" ] \ | ||
| || deny "trafficctl metrics disabled" | ||
|
|
||
| if [ -n "$TOKEN" ]; then |
There was a problem hiding this comment.
With the shipped empty token, enabling metrics serves device names, MACs and traffic to anyone who can reach uhttpd, with no authentication at all. The commit message also says the exporter is "protected by a shared token". Deny when no token is configured, rather than skipping the check.
Generated by Claude Code
There was a problem hiding this comment.
| if [ -z "$IP" ] || [ -z "$RATE" ]; then | ||
| echo '{"ok":false,"msg":"usage: trafficctl-ratelimit.sh <target> <rate_kbit> [label] [each|shared]"}' | ||
| exit 1 | ||
| fi |
There was a problem hiding this comment.
RATE/RATE_UP are never checked to be numeric. tctl_rate_bytes does shell arithmetic on them, so a value like abc evaluates to 0, gets clamped to 1 byte/s, and is applied and persisted as a near-total block. That limit is still reported as success (trafficctl-shape.sh has the same gap).
| if [ -z "$IP" ] || [ -z "$RATE" ]; then | |
| echo '{"ok":false,"msg":"usage: trafficctl-ratelimit.sh <target> <rate_kbit> [label] [each|shared]"}' | |
| exit 1 | |
| fi | |
| if [ -z "$IP" ] || [ -z "$RATE" ]; then | |
| echo '{"ok":false,"msg":"usage: trafficctl-ratelimit.sh <target> <rate_kbit> [label] [each|shared]"}' | |
| exit 1 | |
| fi | |
| case "$RATE:$RATE_UP" in | |
| *[!0-9:]*) | |
| echo '{"ok":false,"msg":"rate must be a whole number of kbit/s"}' | |
| exit 1 | |
| ;; | |
| esac |
Generated by Claude Code
There was a problem hiding this comment.
| [ -n "$param" ] && [ "$param" != "0" ] && \ | ||
| tctl_ratelimit_add "$ip" "$param" "$(tctl_ratelimit_comment "$ip")" "$mode" "$param_up" 2>/dev/null || \ | ||
| logger -t trafficctl "Failed to restore ratelimit for $ip (${param}kbit)" |
There was a problem hiding this comment.
a && b && c || logger also logs "Failed to restore" when param is empty or 0 and nothing was attempted. Use an if so the logger only runs when tctl_ratelimit_add fails; the pfw_limit branch at lines 98-100 has the same problem.
| [ -n "$param" ] && [ "$param" != "0" ] && \ | |
| tctl_ratelimit_add "$ip" "$param" "$(tctl_ratelimit_comment "$ip")" "$mode" "$param_up" 2>/dev/null || \ | |
| logger -t trafficctl "Failed to restore ratelimit for $ip (${param}kbit)" | |
| if [ -n "$param" ] && [ "$param" != "0" ]; then | |
| tctl_ratelimit_add "$ip" "$param" "$(tctl_ratelimit_comment "$ip")" "$mode" "$param_up" 2>/dev/null || \ | |
| logger -t trafficctl "Failed to restore ratelimit for $ip (${param}kbit)" | |
| fi |
Generated by Claude Code
There was a problem hiding this comment.
Solved by moving the restore into trafficctl restore, which skips empty rates without logging, thanks.
Generated by Claude Code
Addresses the automated review on openwrt/packages#31204: - depend on kmod-nft-netdev (firewall4 does not pull it) and log a failed netdev table creation instead of silencing it; report a fw3 host without iptables-mod-hashlimit - trafficctl-cut: drop START/STOP so default_postinst creates no boot link, and remove a link left by earlier packages - metrics CGI answers 403 when metrics.token is empty - ratelimit, shape and portfw limit reject rates that are not whole kbit/s in 1..100000000 - hotplug restore: replace a && b || logger with if-blocks and log a failed or skipped restore Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…dev (#141) Addresses the automated review on openwrt/packages#31204: - depend on kmod-nft-netdev (firewall4 does not pull it) and log a failed netdev table creation instead of silencing it; report a fw3 host without iptables-mod-hashlimit - trafficctl-cut: drop START/STOP so default_postinst creates no boot link, and remove a link left by earlier packages - metrics CGI answers 403 when metrics.token is empty - ratelimit, shape and portfw limit reject rates that are not whole kbit/s in 1..100000000 - hotplug restore: replace a && b || logger with if-blocks and log a failed or skipped restore Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
01aa6b5 to
491fdc7
Compare
openwrt-ai
left a comment
There was a problem hiding this comment.
Commit checks
- 491fdc7 "trafficctl: add new package": the body names only the Telegram bot for notifications, but the commit also adds ntfy, webhook and mail senders and the
trafficctl-notifywatcher (notify.uc). Add one line on them.
Generated by Claude Code
| // The v6 half only after the v4 one: alone it is a block is_blocked counts, | ||
| // so a failed block would read as "already blocked" from then on. | ||
| function block_add(ip, comment) { | ||
| let ok = lib.sh('nft insert rule inet fw4 forward ' + |
There was a problem hiding this comment.
Blocks are inserted at run time into fw4's own inet fw4 table. fw4 flushes and rebuilds that table on every reload (service firewall reload, any LuCI firewall Save & Apply), so every block disappears until the next ifup lan, and is lost for good with persist_rules off. Could the drop rules live in a table of trafficctl's own (as tctl_cut and tctl_pfw do), or be re-added through an fw4 include, so a firewall reload keeps them?
Generated by Claude Code
| # The Telegram options of releases up to 1.30.x move to the notification | ||
| # senders (once: only while the 'notify' section is missing). | ||
| if [ -x /usr/sbin/trafficctl ]; then | ||
| /usr/sbin/trafficctl notify migrate >/dev/null 2>&1 || true | ||
| fi |
There was a problem hiding this comment.
nit: default_postinst already runs 80_trafficctl-notify before this scriptlet, so this second notify migrate never has anything left to do. Drop it here or drop the uci-defaults file.
| # The Telegram options of releases up to 1.30.x move to the notification | |
| # senders (once: only while the 'notify' section is missing). | |
| if [ -x /usr/sbin/trafficctl ]; then | |
| /usr/sbin/trafficctl notify migrate >/dev/null 2>&1 || true | |
| fi |
Generated by Claude Code
There was a problem hiding this comment.
491fdc7 to
d143257
Compare
|
There is no chance this 11K LoC PR will be merged. At the very least the bulk of this should live in its own repo and the new package should have OpenWrt-specific bits and packaging details only. And a new package should also have some prominence before it's considered for addition. |
|
Fair point on the size. I'll restructure it the usual way: the Makefile fetches the tagged release from github.com/YusDyr/luci-app-trafficctl (PKG_SOURCE_URL + PKG_HASH) and installs from the tarball, so this PR shrinks to the Makefile and the OpenWrt-specific packaging. Development and tests stay in that repo. On prominence: the project has been public since May, has 60 stars and 13 forks, and is discussed on the forum at https://forum.openwrt.org/t/250531. If that is not enough yet, I'm fine with closing this PR for now and coming back with the slim version later. |
ca39714 to
28a9d25
Compare
| # Releases up to 1.30.1 gave the cut keeper START=95, so rc.d may | ||
| # still link it for boot. It is started on demand only. | ||
| rm -f /etc/rc.d/S95trafficctl-cut /etc/rc.d/K10trafficctl-cut | ||
| 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 | ||
| # A luci-app-trafficctl up to 1.30.x depends on any trafficctl, so this | ||
| # package can be upgraded on its own. That web interface has no ucode | ||
| # plugin in /usr/share/rpcd/ucode, and its exec plugin sources | ||
| # trafficctl-fw.sh, which 2.0.0 removed, so every page fails. | ||
| if [ ! -e /usr/share/rpcd/ucode/luci.trafficctl.uc ] && | ||
| grep -q 'trafficctl-fw.sh' /usr/libexec/rpcd/luci.trafficctl 2>/dev/null; then | ||
| _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" | ||
| echo "$$_m" | ||
| logger -t trafficctl "$$_m" 2>/dev/null || true | ||
| fi | ||
| # The Telegram options of releases up to 1.30.x move to the notification | ||
| # senders (once: only while the 'notify' section is missing). | ||
| if [ -x /usr/sbin/trafficctl ]; then | ||
| /usr/sbin/trafficctl notify migrate >/dev/null 2>&1 || true | ||
| fi | ||
| # A bot of a release up to 1.30.x lost its files to trafficctl-telegram | ||
| # with this upgrade. Without that package it would run on until the next | ||
| # reboot (and scan for devices, which the watcher took over), and rc.d | ||
| # would keep links to a missing init script. | ||
| if [ ! -e /etc/init.d/trafficctl-telegram ]; then | ||
| ubus call service delete '{"name":"trafficctl-telegram"}' >/dev/null 2>&1 || true | ||
| rm -f /etc/rc.d/S99trafficctl-telegram /etc/rc.d/K10trafficctl-telegram | ||
| # Headless (no luci-app-trafficctl to pull it in): say so where | ||
| # the operator will look. | ||
| if [ "$$(uci -q get trafficctl.telegram.enabled)" = "1" ]; then | ||
| 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 | ||
| fi | ||
| fi |
There was a problem hiding this comment.
These blocks migrate from 1.30.x releases that were never in this feed, so a feed install never reaches that state. Is this needed here? It could stay with the out-of-tree packages that shipped 1.30.x, which would leave the postinst with only what a feed install needs.
Generated by Claude Code
There was a problem hiding this comment.
Agreed, dropped. The postinst now only restarts the running services and re-applies the saved rules. The migrations from 1.30.x and from the old single package stay in the out-of-tree builds that shipped those releases.
| # an image being built, where there are no running services to talk to. | ||
| define Package/trafficctl/postinst | ||
| #!/bin/sh | ||
| chmod 0600 "$${IPKG_INSTROOT}/etc/config/trafficctl" 2>/dev/null || true |
There was a problem hiding this comment.
nit: INSTALL_CONF is install -m0600, so the config already ships as 0600 and this chmod is redundant. The comment at lines 160-161 can go with it.
| chmod 0600 "$${IPKG_INSTROOT}/etc/config/trafficctl" 2>/dev/null || true |
Generated by Claude Code
There was a problem hiding this comment.
Dropped, together with the comment.
| define Package/trafficctl-telegram/prerm | ||
| #!/bin/sh | ||
| if [ -z "$${IPKG_INSTROOT}" ] && [ -x /etc/init.d/trafficctl-telegram ]; then | ||
| /etc/init.d/trafficctl-telegram stop 2>/dev/null || true | ||
| /etc/init.d/trafficctl-telegram disable 2>/dev/null || true | ||
| fi | ||
| exit 0 | ||
| endef | ||
|
|
There was a problem hiding this comment.
default_prerm already stops every init script in the package and disables it on removal. Unlike that code, this block also disables the script during an upgrade. Drop the block.
| define Package/trafficctl-telegram/prerm | |
| #!/bin/sh | |
| if [ -z "$${IPKG_INSTROOT}" ] && [ -x /etc/init.d/trafficctl-telegram ]; then | |
| /etc/init.d/trafficctl-telegram stop 2>/dev/null || true | |
| /etc/init.d/trafficctl-telegram disable 2>/dev/null || true | |
| fi | |
| exit 0 | |
| endef |
Generated by Claude Code
There was a problem hiding this comment.
Dropped; default_prerm handles it.
| DEPENDS:=+trafficctl +curl | ||
| # Versioned, so the opkg/apk solver never pairs this bot with a trafficctl | ||
| # older than its own release (1.30.x has no trafficctl.uc for it to call). | ||
| EXTRA_DEPENDS:=trafficctl (>=$(PKG_VERSION)) |
There was a problem hiding this comment.
>= still allows a newer trafficctl to sit next to an older telegram.uc, even though both come from the same tarball and the bot loads the core's modules. In-tree split packages pin the exact build instead, for example net/bind/Makefile:83. Lines 48, 58 and 80 have the same issue.
| EXTRA_DEPENDS:=trafficctl (>=$(PKG_VERSION)) | |
| EXTRA_DEPENDS:=trafficctl (=$(PKG_VERSION)-r$(PKG_RELEASE)) |
Generated by Claude Code
There was a problem hiding this comment.
Done: all four now pin (=$(PKG_VERSION)-r$(PKG_RELEASE)), as net/bind does.
28a9d25 to
6cc9e96
Compare
openwrt-ai
left a comment
There was a problem hiding this comment.
Commit checks
- 6cc9e96 "trafficctl: add new package" — the body still mentions no notification path except the Telegram bot, which it says "reports new devices", although the
trafficctl-notifywatcher now does the device scan and also sends to ntfy, a webhook and mail. Add one line about the watcher and its senders.
Generated by Claude Code
6cc9e96 to
2bbe028
Compare
| fi | ||
| # The watcher decides for itself whether anything wants the scan. | ||
| if [ -x /etc/init.d/trafficctl-notify ]; then | ||
| /etc/init.d/trafficctl-notify enable 2>/dev/null || true |
There was a problem hiding this comment.
On every upgrade this re-enables trafficctl-notify even when the user disabled it, and on a fresh install default_postinst already enables every shipped init script after this scriptlet runs. Drop the enable.
| /etc/init.d/trafficctl-notify enable 2>/dev/null || true |
Generated by Claude Code
There was a problem hiding this comment.
Agreed. The feed copy no longer enables it: default_postinst does that on a fresh install, and on an upgrade the scriptlet restarts the watcher only when it is enabled, so a watcher the user disabled stays off.
| CATEGORY:=Network | ||
| TITLE:=Per-device traffic monitoring and control | ||
| URL:=https://github.com/YusDyr/luci-app-trafficctl | ||
| DEPENDS:=+conntrack +kmod-nft-netdev +firewall4 +ucode +ucode-mod-fs +ucode-mod-uci +ucode-mod-ubus |
There was a problem hiding this comment.
The description (lines 88-89) says the ntfy and webhook senders need curl, but those senders ship in this core package and only trafficctl-telegram depends on +curl. Is that intended, or should the senders use uclient-fetch from the base image, or the core depend on curl?
Generated by Claude Code
There was a problem hiding this comment.
Intended. Only some senders need curl: ntfy and webhook are opt-in, mail goes through msmtp, and the ubus events and hotplug scripts need no HTTP client, so the core does not pull curl onto routers that use none of them. A sender without curl reports "curl is not installed: opkg/apk install curl". trafficctl-telegram depends on curl because the bot cannot work without it. uclient-fetch is not enough for the senders: they post JSON with a Content-Type and an Authorization header, and uclient-fetch cannot set request headers.
2bbe028 to
9ed765d
Compare
9ed765d to
ba97c4d
Compare
ba97c4d to
a4261db
Compare
|
@BKPepe @GeorgeSapkin I've restructured the PR as discussed. It now holds only net/trafficctl/Makefile (182 lines) and test-version.sh. The Makefile fetches the tagged release v2.3.2 from github.com/YusDyr/luci-app-trafficctl (PKG_SOURCE_URL + PKG_HASH) and installs the trees from the tarball. The code, its tests and CI stay in that repo. The comments that described what the code does are gone; the Makefile keeps four short ones on the non-obvious parts (empty meta packages, the 0600 config, the service restart on upgrade, releasing an internet cut on removal). Yes, I used AI assistance while writing it, and the over-commenting came from that. Notifications are no longer Telegram-only: the trafficctl-notify watcher sends to ntfy, a webhook or mail, and Telegram is one optional package (trafficctl-telegram). Could you take another look? |
|
|
||
| # The config holds the bot and metrics tokens, so it is installed at 0600. | ||
| define Package/trafficctl/install | ||
| $(CP) $(PKG_BUILD_DIR)/trafficctl/files/* $(1)/ |
There was a problem hiding this comment.
The init scripts, UCI config, uci-defaults, hotplug and keep.d files must live in net/trafficctl/files/ and be installed from there, not from the source archive (CONTRIBUTING.md:121-124). With a blind $(CP) of the archive tree, a version bump also ships any new or changed integration file without it ever appearing in review. Move those files here and install the remaining program files (usr/sbin, usr/share/ucode, www/cgi-bin) explicitly; the same applies to the telegram init script copied at line 163.
Generated by Claude Code
There was a problem hiding this comment.
Done in 362b70d: the init scripts, the UCI config, uci-defaults, the hotplug scripts and keep.d now live in net/trafficctl/files/ and are installed from there, the telegram init script included. The program files are installed one by one.
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>
a4261db to
362b70d
Compare
Formality Check: Suggestions AvailableWe checked this pull request against the contribution guidelines. Here is what needs your attention:
|
| # '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 |
There was a problem hiding this comment.
This only migrates 1.30.x Telegram options, and the shipped config already has a notify section, so on a feed install it never does anything. Your reply on the postinst says the 1.30.x migrations stay in the out-of-tree builds; should this uci-defaults script (and its install line in the Makefile) go with them?
Generated by Claude Code
| # 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 |
There was a problem hiding this comment.
nit (optional): #105 here, and #12, #55, #89, #90 and #102 at lines 50, 86, 95 and 118, are issue numbers of the out-of-tree repo, which readers of this feed will take for openwrt/packages issues. Drop them, or link the upstream repo's issues in full.
Generated by Claude Code
📦 Package Details
Maintainer: @YusDyr
Description:
trafficctl does per-device traffic monitoring and control on the router: live rates and byte counters per device from conntrack, internet block per device, WiFi MAC filtering, rate limits per device or subnet (nftables policer, IPv4 and IPv6), tc/HTB shaping, a global internet cut that reverts itself after a deadline, per-device history, and notifications of new devices and applied controls to Telegram, ntfy, a webhook or mail. A Prometheus exporter is included, off by default.
Since 2.0.0 it is one ucode CLI,
/usr/sbin/trafficctl, with its modules in/usr/share/ucode/trafficctl, three procd services and two hotplug hooks; the earlier POSIX sh helpers are gone. It needs firewall4 (OpenWrt 22.03 or later).The same Makefile builds three optional packages, so a router carries only the parts it uses:
trafficctl-shaper(pulls in tc, kmod-sched-core and kmod-ifb for tc/HTB shaping),trafficctl-wifi(iw and hostapd-utils, for the WiFi client list and WiFi MAC filtering) andtrafficctl-telegram(the Telegram bot, which needs curl);trafficctl-fullpulls in all three. Each depends on trafficctl by version. trafficctl-shaper, trafficctl-wifi and trafficctl-full hold no files; without them trafficctl checks for the tools at run time and says which package to install.The packages have no web interface of their own. The frontend is luci-app-trafficctl, which I have opened as a draft in openwrt/luci#9221 that depends on this PR. Both have been developed out of tree at https://github.com/YusDyr/luci-app-trafficctl since May 2026.
Runtime dependencies of trafficctl are conntrack, kmod-nft-netdev, firewall4, ucode, ucode-mod-fs, ucode-mod-uci and ucode-mod-ubus. hostapd-utils is on trafficctl-wifi because it can only be selected with a hostapd/wpad variant; a wired-only build leaves trafficctl-wifi out. The ntfy, webhook and Telegram senders need curl.
/usr/sbin/trafficctltakes positional commands and the bot's process is started by its init script; neither has a --version flag, so test-version.sh accepts both packages explicitly.🔬 Run Testing Details
On this router I upgraded from 1.30.0 to 2.1.1 and then to 2.1.2 with opkg. The project's CI also installs it and upgrades it from 1.30.x in x86-64 containers on 22.03, 23.05, 24.10, 25.12 and snapshot, and runs the limiter and the shaper against a real kernel, for example this run.
✅ Formalities
messages.
I wrote much of this package with Claude Code (Anthropic) as a coding assistant, including the commit message and a first draft of this description, and had it open this PR as a draft at my request. I reviewed and tested the result myself, run it on my own router, and will answer review comments myself.