|
4 | 4 | # In case of non userspace networking, |
5 | 5 | # add local subnets to ip rules with higher priority than Tailscale's routing |
6 | 6 | # ============================================================================== |
| 7 | + |
7 | 8 | readonly PROTECTION_RULE_PRIORITY=5000 |
| 9 | +readonly WAIT_DELAY=5 # 5s |
| 10 | +readonly WAIT_COUNT=60 # 60*5s = 300s = 5m |
8 | 11 |
|
9 | 12 | declare -a routes=() |
10 | 13 | declare route family |
11 | 14 | declare response |
12 | 15 | declare wait_counter=0 |
13 | 16 |
|
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 |
17 | 23 | # If it is called after network configuration is changed, we need to drop cached network info |
18 | 24 | bashio::cache.flush_all |
19 | 25 | # 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" |
25 | 33 | fi |
26 | 34 | bashio::log.info "Waiting for the supervisor to be ready..." |
27 | | - sleep 5 |
| 35 | + sleep $WAIT_DELAY |
28 | 36 | done |
29 | 37 | if (( wait_counter != 0 )); then |
30 | 38 | bashio::log.info "Supervisor is ready" |
31 | 39 | fi |
| 40 | +fi |
32 | 41 |
|
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 |
34 | 46 | bashio::log.info \ |
35 | 47 | "Adding local subnets to ip rules with higher priority than Tailscale's routing," \ |
36 | 48 | "to prevent routing local subnets if the same subnet is routed within your tailnet." |
|
53 | 65 | if ! response=$(ip "${family}" rule add to "${route}" priority ${PROTECTION_RULE_PRIORITY} table main 2>&1); then |
54 | 66 | if [[ "${response}" != "RTNETLINK answers: File exists" ]]; then |
55 | 67 | echo "${response}" |
56 | | - bashio::exit.nok |
| 68 | + bashio::exit.nok " Adding route ${route} to ip rules is unsuccessful" |
57 | 69 | else |
58 | 70 | bashio::log.notice " Route ${route} is already added to ip rules" |
59 | 71 | fi |
|
0 commit comments