WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 771a26c

Browse files
authored
Skip DHCP lease renewal in subnet protection service if nothing has changed (#505)
1 parent 6c1338d commit 771a26c

File tree

4 files changed

+56
-23
lines changed

4 files changed

+56
-23
lines changed

tailscale/rootfs/etc/NetworkManager/dispatcher.d/protect-subnets

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@
22
# shellcheck shell=bash
33
# The shebang 'with-contenv-merge' above is identical with 'with-contenv', but doesn't clear the current environment containing the dispatcher variables
44

5+
function halt-add-on() {
6+
bashio::log.error "Failed to protect subnet routes. Halting add-on to prevent network loss."
7+
echo -n 1 > /run/s6-linux-init-container-results/exitcode
8+
exec /run/s6/basedir/bin/halt
9+
}
10+
511
case "${NM_DISPATCHER_ACTION}" in
6-
up|down|dhcp4-change|dhcp6-change)
12+
up|down)
713
bashio::log.info "Handling Network Manager action ${DEVICE_IP_IFACE-} ${NM_DISPATCHER_ACTION}"
814
unprotect-subnet-routes
915
if ! protect-subnet-routes; then
1016
# Better stop add-on than risking losing all network connections
11-
bashio::log.error "Failed to protect subnet routes. Halting add-on to prevent network loss."
12-
echo -n 1 > /run/s6-linux-init-container-results/exitcode
13-
exec /run/s6/basedir/bin/halt
17+
halt-add-on
18+
fi
19+
;;
20+
dhcp4-change|dhcp6-change)
21+
# Do anything only when the addresses are really changed
22+
if [[ "$(unprotect-subnet-routes test)" != "$(protect-subnet-routes test)" ]]; then
23+
bashio::log.info "Handling Network Manager action ${DEVICE_IP_IFACE-} ${NM_DISPATCHER_ACTION}"
24+
unprotect-subnet-routes
25+
if ! protect-subnet-routes tested; then
26+
# Better stop add-on than risking losing all network connections
27+
halt-add-on
28+
fi
1429
fi
1530
;;
1631
connectivity-change)

tailscale/rootfs/usr/bin/protect-subnet-routes

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,45 @@
44
# In case of non userspace networking,
55
# add local subnets to ip rules with higher priority than Tailscale's routing
66
# ==============================================================================
7+
78
readonly PROTECTION_RULE_PRIORITY=5000
9+
readonly WAIT_DELAY=5 # 5s
10+
readonly WAIT_COUNT=60 # 60*5s = 300s = 5m
811

912
declare -a routes=()
1013
declare route family
1114
declare response
1215
declare wait_counter=0
1316

14-
if bashio::config.false "userspace_networking" && \
15-
(! bashio::config.has_value "accept_routes" || bashio::config.true "accept_routes")
16-
then
17+
if ! [[ "${1-}" =~ ^(|test|tested)$ ]]; then
18+
echo "Usage: $(basename "$0") [test|tested]" 1>&2
19+
exit 1
20+
fi
21+
22+
if [[ "${1-}" != "tested" ]]; then
1723
# If it is called after network configuration is changed, we need to drop cached network info
1824
bashio::cache.flush_all
1925
# It is possible to get "ERROR: Got unexpected response from the API: System is not ready with state: setup"
20-
# So we wait a little, 60*5s = 300s = 5m
21-
while ! bashio::api.supervisor GET "/addons/self/options/config" false &> /dev/null; do
22-
if (( wait_counter++ == 60 )); then
23-
bashio::log.error "Supervisor is unreachable"
24-
bashio::exit.nok
26+
# Test both networking and config Supervisor API availability, these APIs are called in subnet-routes script
27+
# And wait a little on inaccessibility
28+
while ! bashio::api.supervisor GET "/network/interface/default/info" false &> /dev/null || \
29+
! bashio::api.supervisor GET "/addons/self/options/config" false &> /dev/null
30+
do
31+
if (( wait_counter++ == $WAIT_COUNT )); then
32+
bashio::exit.nok "Supervisor is unreachable"
2533
fi
2634
bashio::log.info "Waiting for the supervisor to be ready..."
27-
sleep 5
35+
sleep $WAIT_DELAY
2836
done
2937
if (( wait_counter != 0 )); then
3038
bashio::log.info "Supervisor is ready"
3139
fi
40+
fi
3241

33-
readarray -t routes < <(subnet-routes local)
42+
readarray -t routes < <(subnet-routes local)
43+
if [[ "${1-}" == "test" ]]; then
44+
printf "%s" "${routes[@]/%/$'\n'}"
45+
else
3446
bashio::log.info \
3547
"Adding local subnets to ip rules with higher priority than Tailscale's routing," \
3648
"to prevent routing local subnets if the same subnet is routed within your tailnet."
@@ -53,7 +65,7 @@ then
5365
if ! response=$(ip "${family}" rule add to "${route}" priority ${PROTECTION_RULE_PRIORITY} table main 2>&1); then
5466
if [[ "${response}" != "RTNETLINK answers: File exists" ]]; then
5567
echo "${response}"
56-
bashio::exit.nok
68+
bashio::exit.nok " Adding route ${route} to ip rules is unsuccessful"
5769
else
5870
bashio::log.notice " Route ${route} is already added to ip rules"
5971
fi

tailscale/rootfs/usr/bin/subnet-routes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function appendarray() {
1616
}
1717

1818
if ! [[ "${1-}" =~ ^(local|advertised)$ ]]; then
19-
echo "Usage: subnet-routes local|advertised" 1>&2
19+
echo "Usage: $(basename "$0") local|advertised" 1>&2
2020
exit 1
2121
fi
2222

tailscale/rootfs/usr/bin/unprotect-subnet-routes

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ readonly PROTECTION_RULE_PRIORITY=5000
99
declare -a routes=()
1010
declare route family
1111

12-
if bashio::config.false "userspace_networking" && \
13-
(! bashio::config.has_value "accept_routes" || bashio::config.true "accept_routes")
14-
then
15-
readarray -t routes < <( \
16-
{ ip -4 rule list; ip -6 rule list; } \
17-
| { grep -E "^${PROTECTION_RULE_PRIORITY}:" || true ;} \
18-
| sed -nr 's/^\d+:\s+from all to ([^\s]+) lookup main$/\1/p')
12+
if ! [[ "${1-}" =~ ^(|test)$ ]]; then
13+
echo "Usage: $(basename "$0") [test]" 1>&2
14+
exit 1
15+
fi
16+
17+
readarray -t routes < <( \
18+
{ ip -4 rule list; ip -6 rule list; } \
19+
| { grep -E "^${PROTECTION_RULE_PRIORITY}:" || true ;} \
20+
| sed -nr 's/^\d+:\s+from all to ([^\s]+) lookup main$/\1/p')
21+
22+
if [[ "${1-}" == "test" ]]; then
23+
printf "%s" "${routes[@]/%/$'\n'}"
24+
else
1925
for route in "${routes[@]}"; do
2026
bashio::log.info "Removing route ${route} from ip rules"
2127
if [[ "${route}" =~ .*:.* ]]; then

0 commit comments

Comments
 (0)