From bea5c4e32991ed65791764bc96e0b8fc8b9d6e7c Mon Sep 17 00:00:00 2001 From: Andrea Pesaresi Date: Thu, 12 Nov 2026 09:44:24 +0100 Subject: [PATCH] pbr-1.2.4: add a dnsprefetch service command for cron Rebuilding the domain sets from cron needs two settings today: the PBR_DNSPREFETCH_MODE=standalone variable in the cron line, and _dnsprefetch_flush_sets='1' edited into pbr.user.dnsprefetch. The second does not survive an upgrade, because the file is not a conffile. Add '/etc/init.d/pbr dnsprefetch', which runs the file in standalone mode, and a dnsprefetch_flush_sets option in the config section that turns the flush on. A nightly rebuild then takes: uci set pbr.config.dnsprefetch_flush_sets='1'; uci commit pbr 0 4 * * * /etc/init.d/pbr dnsprefetch Without the option a standalone run only tops up the sets, as before, and without the cron line nothing runs: the prefetch on start and reload is unchanged. Calling the file directly with PBR_DNSPREFETCH_MODE still works. tests/06_shell_wrapper/05_dnsprefetch_command.sh covers the command. Signed-off-by: Andrea Pesaresi --- files/etc/init.d/pbr | 10 ++- files/usr/share/pbr/pbr.user.dnsprefetch | 45 +++++++---- .../05_dnsprefetch_command.sh | 80 +++++++++++++++++++ 3 files changed, 119 insertions(+), 16 deletions(-) create mode 100755 tests/06_shell_wrapper/05_dnsprefetch_command.sh diff --git a/files/etc/init.d/pbr b/files/etc/init.d/pbr index 7134ef5..5a0af6c 100755 --- a/files/etc/init.d/pbr +++ b/files/etc/init.d/pbr @@ -8,6 +8,8 @@ START=20 USE_PROCD=1 if type extra_command 1>/dev/null 2>&1; then + extra_command 'dnsprefetch' "Resolves the domain names in policies again, for a cron job + Empties the domain sets first if dnsprefetch_flush_sets is set" extra_command 'enable_forward' "Enables forwarding from LAN > WAN, can be used if the router is stuck in Disabled state" extra_command 'netifd' "Netifd extensions operations" extra_command 'on_interface_reload' "Run service on indicated interface reload" @@ -19,9 +21,11 @@ if type extra_command 1>/dev/null 2>&1; then extra_command 'version' "Show version information" else # shellcheck disable=SC2034 - EXTRA_COMMANDS='help netifd on_interface_reload status version' + EXTRA_COMMANDS='dnsprefetch help netifd on_interface_reload status version' # shellcheck disable=SC2034 EXTRA_HELP=" +\tdnsprefetch\tResolves the domain names in policies again, for a cron job +\t\tEmpties the domain sets first if dnsprefetch_flush_sets is set \tstatus\tDisplays current routing mode and nft file paths \t\tUse '-d' option for detailed diagnostic output \tsupport\tShow diagnostic info and mask sensitive data @@ -43,6 +47,9 @@ readonly _ucode="ucode -S -L /lib/${packageName} -L /lib/mwan4 /lib/${packageNam stop_forward() { $_ucode stop_forward; } enable_forward() { $_ucode enable_forward; } +dnsprefetch() { + PBR_DNSPREFETCH_MODE='standalone' /bin/sh "/usr/share/${packageName}/pbr.user.dnsprefetch" +} boot() { local enabled @@ -205,6 +212,7 @@ load_validate_config() { 'ipv6_enabled:bool:0' \ 'resolver_set:or("", "none", "dnsmasq.nftset")' \ 'resolver_instance:list(or(integer, string)):*' \ + 'dnsprefetch_flush_sets:bool:0' \ 'verbosity:range(0,2):2' \ 'uplink_mark:regex("[A-Fa-f0-9]{8}"):00010000' \ 'uplink_ip_rules_priority:range(99,32765):30000' \ diff --git a/files/usr/share/pbr/pbr.user.dnsprefetch b/files/usr/share/pbr/pbr.user.dnsprefetch index 5cb12ea..951c435 100644 --- a/files/usr/share/pbr/pbr.user.dnsprefetch +++ b/files/usr/share/pbr/pbr.user.dnsprefetch @@ -30,6 +30,11 @@ # Run this file from cron to rebuild the sets periodically. Once a night, at an # hour nobody is using the network, is the sensible default: # +# 0 4 * * * /etc/init.d/pbr dnsprefetch +# +# The service command runs this file in standalone mode (see below). Calling +# the file directly does the same: +# # 0 4 * * * PBR_DNSPREFETCH_MODE=standalone /bin/sh /usr/share/pbr/pbr.user.dnsprefetch # # Use a shorter interval only if your domains rotate faster than that, and see @@ -37,25 +42,29 @@ # day -- `0 */6 * * *` would run at 06:00, 12:00 and 18:00 as well as at # midnight. # -# PBR_DNSPREFETCH_MODE=standalone is required, and is what makes the file -# runnable on its own: without it the script waits for a pbr run that is not -# happening -- it watches pbr's ruleset file for a rewrite, gives up after -# about three minutes and resolves nothing. It also flushes dnsmasq's cache, -# which a run outside pbr has to do itself: dnsmasq only writes to an nft set -# when it takes an answer from upstream, so lookups served from its cache would -# add nothing at all. A standalone run takes a second or two. +# PBR_DNSPREFETCH_MODE=standalone, which the service command sets for you, is +# required, and is what makes the file runnable on its own: without it the +# script waits for a pbr run that is not happening -- it watches pbr's ruleset +# file for a rewrite, gives up after about three minutes and resolves nothing. +# It also flushes dnsmasq's cache, which a run outside pbr has to do itself: +# dnsmasq only writes to an nft set when it takes an answer from upstream, so +# lookups served from its cache would add nothing at all. A standalone run +# takes a second or two. # # The variable says how the script is being RUN, not what it does to the sets; -# rebuilding them is the separate option below. +# rebuilding them is the separate dnsprefetch_flush_sets option below. # # On its own a standalone run only TOPS UP the sets. An address dnsmasq added # is never removed by another lookup, so a domain that has changed address # leaves the old one behind and the set only grows: harmless until that address # is reused by somebody else, at which point traffic to it still takes this # policy's route. To have each run rebuild the sets from the answers that are -# current now, set _dnsprefetch_flush_sets='1' where it is defined further down -# in this file. That empties the domain sets immediately before resolving them -# again. It touches ONLY the sets dnsmasq populates -- the names are read from +# current now, turn on pbr's dnsprefetch_flush_sets option: +# +# uci set pbr.config.dnsprefetch_flush_sets='1'; uci commit pbr +# +# That empties the domain sets immediately before resolving them again. It +# touches ONLY the sets dnsmasq populates -- the names are read from # pbr's own dnsmasq file -- never the pbr_*_user sets, which hold whatever your # other custom user files put there and which nothing here would put back. It # takes effect in standalone mode only: a start or a reload deliberately keeps @@ -114,8 +123,10 @@ _dnsprefetch_self='/usr/share/pbr/pbr.user.dnsprefetch' # Optional: empty pbr's domain sets before re-resolving, so the run rebuilds # them from the answers that are current now instead of adding to whatever is # already there. Off by default: a prefetch is additive and cannot remove an -# address, which is why a domain set otherwise only grows. Set to '1' to -# enable. +# address, which is why a domain set otherwise only grows. Turned on by +# option dnsprefetch_flush_sets '1' in the config section of /etc/config/pbr, +# which an upgrade of the package leaves alone, unlike this file. The name +# is spelled out because $packageName is not set in this pass (see above). # # It takes effect in standalone mode ONLY (see PBR_DNSPREFETCH_MODE below). A # start or a reload deliberately keeps each set's addresses unless that @@ -124,10 +135,14 @@ _dnsprefetch_self='/usr/share/pbr/pbr.user.dnsprefetch' # undo that. A timed run is the opposite case: it exists to rebuild the sets # and it re-resolves every name immediately. -_dnsprefetch_flush_sets='0' +case "$(uci -q get pbr.config.dnsprefetch_flush_sets)" in + 1|on|true|yes|enabled) _dnsprefetch_flush_sets='1' ;; + *) _dnsprefetch_flush_sets='0' ;; +esac # Standalone mode, for a cron job -- see REFRESHING ON A TIMER above. This says -# how the script is being run; it does not by itself rebuild anything: +# how the script is being run; it does not by itself rebuild anything. The +# 'dnsprefetch' command of /etc/init.d/pbr sets it, or by hand: # 0 4 * * * PBR_DNSPREFETCH_MODE=standalone /bin/sh /usr/share/pbr/pbr.user.dnsprefetch # There is no pbr run to wait for and nothing is waiting on us, so act as the # detached child straight away and skip the barrier below. Without this the diff --git a/tests/06_shell_wrapper/05_dnsprefetch_command.sh b/tests/06_shell_wrapper/05_dnsprefetch_command.sh new file mode 100755 index 0000000..5461720 --- /dev/null +++ b/tests/06_shell_wrapper/05_dnsprefetch_command.sh @@ -0,0 +1,80 @@ +#!/bin/bash +# '/etc/init.d/pbr dnsprefetch' must run pbr.user.dnsprefetch in standalone +# mode, pass its exit status back, and leave ucode and the caller's +# environment alone. +TEST_DIR="$(mktemp -d)" +trap 'rm -rf "$TEST_DIR"' EXIT + +cat << EOF > "$TEST_DIR/mock_ucode" +#!/bin/bash +echo "\$1" >> "$TEST_DIR/ucode_calls" +exit 0 +EOF +chmod +x "$TEST_DIR/mock_ucode" + +cat << EOF > "$TEST_DIR/pbr.user.dnsprefetch" +echo "\${PBR_DNSPREFETCH_MODE:-unset}" >> "$TEST_DIR/prefetch_runs" +exit "\${MOCK_PREFETCH_RC:-0}" +EOF + +mkdir -p "$TEST_DIR/var_run" +touch "$TEST_DIR/ucode_calls" + +cp files/etc/init.d/pbr "$TEST_DIR/pbr_mock.sh" +sed -i "s|/var/run|$TEST_DIR/var_run|g" "$TEST_DIR/pbr_mock.sh" +sed -i "s|/usr/share/\${packageName}/pbr.user.dnsprefetch|$TEST_DIR/pbr.user.dnsprefetch|g" "$TEST_DIR/pbr_mock.sh" +sed -i "s|readonly _ucode=.*|readonly _ucode=\"$TEST_DIR/mock_ucode\"|g" "$TEST_DIR/pbr_mock.sh" +sed -i 's|^\. /lib/functions.sh|# . /lib/functions.sh|' "$TEST_DIR/pbr_mock.sh" +sed -i 's|^\. /lib/functions/network.sh|# . /lib/functions/network.sh|' "$TEST_DIR/pbr_mock.sh" +sed -i 's|^\. "${IPKG_INSTROOT}/usr/share/libubox/jshn.sh"|# . /usr/share/libubox/jshn.sh|' "$TEST_DIR/pbr_mock.sh" + +( + unset PBR_DNSPREFETCH_MODE + + . "$TEST_DIR/pbr_mock.sh" + + case " $EXTRA_COMMANDS " in + *' dnsprefetch '*) ;; + *) + echo "FAIL: dnsprefetch missing from EXTRA_COMMANDS: $EXTRA_COMMANDS" > "$TEST_DIR/result" + exit 1 + ;; + esac + + # Test 1: runs the file in standalone mode and returns its status + if ! dnsprefetch; then + echo "FAIL: dnsprefetch failed although the script returned 0!" > "$TEST_DIR/result" + exit 1 + fi + if [ "$(cat "$TEST_DIR/prefetch_runs" 2>/dev/null)" != "standalone" ]; then + echo "FAIL: script not run once in standalone mode: $(cat "$TEST_DIR/prefetch_runs" 2>/dev/null)" > "$TEST_DIR/result" + exit 1 + fi + + # Test 2: a failing run is reported to the caller (cron mails it) + MOCK_PREFETCH_RC=3 dnsprefetch + rc=$? + if [ "$rc" -ne 3 ]; then + echo "FAIL: exit status 3 of the script came back as $rc!" > "$TEST_DIR/result" + exit 1 + fi + + # Test 3: nothing leaks into ucode or into the caller's environment + if [ -s "$TEST_DIR/ucode_calls" ]; then + echo "FAIL: ucode was called: $(cat "$TEST_DIR/ucode_calls")" > "$TEST_DIR/result" + exit 1 + fi + if [ -n "${PBR_DNSPREFETCH_MODE+set}" ]; then + echo "FAIL: PBR_DNSPREFETCH_MODE leaked into the caller's shell!" > "$TEST_DIR/result" + exit 1 + fi + + echo "PASS" > "$TEST_DIR/result" +) >/dev/null 2>&1 + +if [ "$(cat "$TEST_DIR/result")" = "PASS" ]; then + exit 0 +else + cat "$TEST_DIR/result" >&2 + exit 1 +fi