Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 214 additions & 0 deletions net/trafficctl/Makefile
Original file line number Diff line number Diff line change
@@ -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 <yusdyr@gmail.com>

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))
30 changes: 30 additions & 0 deletions net/trafficctl/files/trafficctl-cut.init
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 42 additions & 0 deletions net/trafficctl/files/trafficctl-history.init
Original file line number Diff line number Diff line change
@@ -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"
}
17 changes: 17 additions & 0 deletions net/trafficctl/files/trafficctl-newdevice.hotplug
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions net/trafficctl/files/trafficctl-notify.init
Original file line number Diff line number Diff line change
@@ -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"
}
9 changes: 9 additions & 0 deletions net/trafficctl/files/trafficctl-notify.uci-defaults
Original file line number Diff line number Diff line change
@@ -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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

exit 0
18 changes: 18 additions & 0 deletions net/trafficctl/files/trafficctl-shapes.hotplug
Original file line number Diff line number Diff line change
@@ -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
Loading