From d59793c752549784bff30a3a8732aaa40caeef30 Mon Sep 17 00:00:00 2001 From: Sukru Senli Date: Tue, 30 Jan 2024 11:02:04 +0100 Subject: [PATCH] testnet: move it to its own package, out of netmode --- netmode/Makefile | 2 +- .../files/etc/hotplug.d/iface/70-shiftrange | 252 ------------------ testnet/Makefile | 26 ++ .../files/etc/hotplug.d/iface/05-testnet | 2 +- .../files/etc/ruleng/internet.json | 0 .../files/etc/uci-defaults/ruleng.internet | 0 {netmode => testnet}/files/sbin/testnet | 13 +- .../files/usr/libexec/rpcd/testnet | 0 8 files changed, 36 insertions(+), 259 deletions(-) delete mode 100644 netmode/files/etc/hotplug.d/iface/70-shiftrange create mode 100644 testnet/Makefile rename {netmode => testnet}/files/etc/hotplug.d/iface/05-testnet (88%) rename {netmode => testnet}/files/etc/ruleng/internet.json (100%) rename {netmode => testnet}/files/etc/uci-defaults/ruleng.internet (100%) rename {netmode => testnet}/files/sbin/testnet (80%) rename {netmode => testnet}/files/usr/libexec/rpcd/testnet (100%) diff --git a/netmode/Makefile b/netmode/Makefile index 98de78bf5..d29d639f8 100644 --- a/netmode/Makefile +++ b/netmode/Makefile @@ -8,7 +8,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=netmode -PKG_VERSION:=0.3.0 +PKG_VERSION:=1.0.1 PKG_RELEASE:=1 PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION) PKG_LICENSE:=GPL-2.0-only diff --git a/netmode/files/etc/hotplug.d/iface/70-shiftrange b/netmode/files/etc/hotplug.d/iface/70-shiftrange deleted file mode 100644 index 5956addda..000000000 --- a/netmode/files/etc/hotplug.d/iface/70-shiftrange +++ /dev/null @@ -1,252 +0,0 @@ -#!/bin/sh -# this scripts shifts the lan network prefixes -# if a wan interface has the same network prefix. - -# do not shift range if the feature is explicitly disabled -[ "$(uci -q get netmode.shiftrange.enabled)" == "0" ] && exit 0 - -. /lib/functions.sh -. /lib/functions/network.sh - -LOCKFILE="/tmp/70-shiftrange.lock" -RESTRICTED_NETS="" -ALL_NETS="" - -##### -##### initial functions -##### - -initial_check() -{ - # run only on ifup - [ "$ACTION" == "ifup" ] || exit 0 - - # run only for uplink (not is_lan) interfaces - local islan="$(uci -q get network.$INTERFACE.is_lan)" - [ "$islan" != "1" ] || exit 0 - - # run only if the uplink interface has a configured protocol - local proto="$(uci -q get network.$INTERFACE.proto)" - [ "$proto" != "none" ] || exit 0 -} - -finish() -{ - lock -u $LOCKFILE - rm -f $LOCKFILE -} - -# just one instance of this script at a time -just_one_instance() -{ - local counter=0 - local limit=10 - - #wait for the lock to become free - while [ -e $LOCKFILE ] ; do - sleep 1 - counter=$((counter + 1)) - [ "$counter" -gt "$limit" ] && exit 1 - done - - lock $LOCKFILE - trap finish EXIT INT TERM -} - - -##### -##### helper functions -##### - -#given a an ip and a mask in the form of "192.168.1.1/24" -#return the network address like "192.168.1.0/24" -get_network_address() -{ - local ip="$1" - [ -z "$ip" ] && return - - local prefix=${ip##*/} - - local ip1=${ip%%.*} ; ip=${ip#*.*} - local ip2=${ip%%.*} ; ip=${ip#*.*} - local ip3=${ip%%.*} ; ip=${ip#*.*} - local ip4=${ip%%/*} - - local ip=$((($ip1 << 24) + ($ip2 << 16) + ($ip3 << 8) + $ip4)) - local mask=$((0xFFFFFFFF >> (32 - $prefix) << (32 - $prefix))) - local network=$(($ip & $mask)) - - local n1=$((($network & 0xFF000000) >> 24)) - local n2=$((($network & 0x00FF0000) >> 16)) - local n3=$((($network & 0x0000FF00) >> 8)) - local n4=$(( $network & 0x000000FF)) - - echo "$n1.$n2.$n3.$n4/$prefix" -} - -#given a network address (192.168.1.0/24) -#find the next network address (192.168.2.0/24) -next_network_address() -{ - local ip=$1 - local prefix=${ip##*/} - - local ip1=${ip%%.*} ; ip=${ip#*.*} - local ip2=${ip%%.*} ; ip=${ip#*.*} - local ip3=${ip%%.*} ; ip=${ip#*.*} - local ip4=${ip%%/*} - - local ip=$((($ip1 << 24) + ($ip2 << 16) + ($ip3 << 8) + $ip4)) - local one="$((1 << (32-$prefix)))" - local new=$(($ip + $one)) - - local n1=$((($new & 0xFF000000) >> 24)) - local n2=$((($new & 0x00FF0000) >> 16)) - local n3=$((($new & 0x0000FF00) >> 8)) - local n4=$(( $new & 0x000000FF)) - - echo "$n1.$n2.$n3.$n4/$prefix" -} - -# given a network address and a prefix (192.168.2.0/24) -# return the first host ip available (192.168.2.1) -first_host_in_network () -{ - local ip=$1 - local prefix=${ip##*/} - - local ip1=${ip%%.*} ; ip=${ip#*.*} - local ip2=${ip%%.*} ; ip=${ip#*.*} - local ip3=${ip%%.*} ; ip=${ip#*.*} - local ip4=${ip%%/*} - - local ip=$((($ip1 << 24) + ($ip2 << 16) + ($ip3 << 8) + $ip4)) - local new=$(($ip + 1)) - - local n1=$((($new & 0xFF000000) >> 24)) - local n2=$((($new & 0x00FF0000) >> 16)) - local n3=$((($new & 0x0000FF00) >> 8)) - local n4=$(( $new & 0x000000FF)) - - echo "$n1.$n2.$n3.$n4" -} - -# given a network address, -# find the next available network address. -shift_range() -{ - local net="$1" - while true ; do - if [ "$RESTRICTED_NETS" == "${RESTRICTED_NETS//$net/}" ] && [ "$ALL_NETS" == "${ALL_NETS//$net/}" ]; then - # found a net that is not in restricted nets nor in all nets - break - fi - net=$(next_network_address $net) - done - - echo "$net" -} - - -##### -##### parse all interfaces section -##### - -# RESTRICTED_NETS = all the IPs on wan interfaces -# ALL_NETS = all the IPs on any interface -parse_interface() -{ - local interface=$1 - local nets="" # "192.168.1.1/24" - local networks="" # "192.168.1.0/24" - - config_get is_lan $interface is_lan - network_get_subnets nets $interface - - for n in $nets ; do - networks="$networks $(get_network_address $n)" - done - - [ "$is_lan" != "1" ] && RESTRICTED_NETS="$RESTRICTED_NETS $networks" - ALL_NETS="$ALL_NETS $networks" -} - -# parse all the interfaces -# get all the IPs on wan interfaces and store them in restrict_nets -# get all the IPs on all interfaces and store them in ALL_NETS -parse_interfaces() -{ - config_foreach parse_interface "interface" -} - - -##### -##### parse all lan interfaces section -##### - -parse_lan() -{ - local interface=$1 - local nets="" - local ips="" - local newips="" - local ips_changed=0 - - [ "$interface" == "loopback" ] && return - config_get is_lan $interface is_lan - [ "$is_lan" == "1" ] || return - - network_get_subnets ips $interface - - for ip in $ips ; do - net="$(get_network_address $ip)" - - if [ "$RESTRICTED_NETS" == "${RESTRICTED_NETS//$net/}" ] ; then - # net is not in restricted nets - # append ip to newips - [ -z "$newips" ] && newips="${ip%/*}" || newips="$newips ${ip%/*}" - continue - fi - - #net is in RESTRICTED_NETS - local newnet=$(shift_range $net) - local newip="$(first_host_in_network $newnet)" - # append newip to newips - [ -z "$newips" ] && newips="$newip" || newips="$newips $newip" - - ips_changed=1 - logger "$0: Changing the ip on interface $interface from $ip to $newip/${newnet##*/}" - echo "$0: Changing the ip on interface $interface from $ip to $newip/${newnet##*/}" >/dev/console - done - - #assign the new ips - if [ "$ips_changed" == "1" ] ; then - uci -q set network.$interface.ipaddr="$newips" - fi -} - -# parse all the interface with is_lan=1 -parse_lans() -{ - config_foreach parse_lan "interface" -} - -##### -##### main -##### - -main() -{ - initial_check - just_one_instance - - config_load network - parse_interfaces - parse_lans - - if [ -n "$(uci changes network)" ] ; then - ubus call uci commit '{"config":"network"}' - fi -} - -main $@ diff --git a/testnet/Makefile b/testnet/Makefile new file mode 100644 index 000000000..9ff1054dd --- /dev/null +++ b/testnet/Makefile @@ -0,0 +1,26 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=testnet +PKG_VERSION:=1.0.0 +PKG_RELEASE:=1 +PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION) +PKG_LICENSE:=GPL-2.0-only +include $(INCLUDE_DIR)/package.mk + +define Package/testnet + CATEGORY:=Utilities + TITLE:=Check WAN IP Connection +endef + +define Package/testnet/description + Check WAN IP connection +endef + +define Build/Compile +endef + +define Package/testnet/install + $(CP) ./files/* $(1)/ +endef + +$(eval $(call BuildPackage,testnet)) diff --git a/netmode/files/etc/hotplug.d/iface/05-testnet b/testnet/files/etc/hotplug.d/iface/05-testnet similarity index 88% rename from netmode/files/etc/hotplug.d/iface/05-testnet rename to testnet/files/etc/hotplug.d/iface/05-testnet index 9c335a90a..d6a5180ea 100644 --- a/netmode/files/etc/hotplug.d/iface/05-testnet +++ b/testnet/files/etc/hotplug.d/iface/05-testnet @@ -1,7 +1,7 @@ #!/bin/sh # do not start testnet if the feature is explicitly disabled -[ "$(uci -q get netmode.testnet.enabled)" = "0" ] && exit 0 +[ "$(uci -q get testnet.global.enabled)" = "0" ] && exit 0 [ "$ACTION" == "ifup" -o "$ACTION" == "ifdown" ] || exit 0 diff --git a/netmode/files/etc/ruleng/internet.json b/testnet/files/etc/ruleng/internet.json similarity index 100% rename from netmode/files/etc/ruleng/internet.json rename to testnet/files/etc/ruleng/internet.json diff --git a/netmode/files/etc/uci-defaults/ruleng.internet b/testnet/files/etc/uci-defaults/ruleng.internet similarity index 100% rename from netmode/files/etc/uci-defaults/ruleng.internet rename to testnet/files/etc/uci-defaults/ruleng.internet diff --git a/netmode/files/sbin/testnet b/testnet/files/sbin/testnet similarity index 80% rename from netmode/files/sbin/testnet rename to testnet/files/sbin/testnet index c6ba2813d..35577d88d 100755 --- a/netmode/files/sbin/testnet +++ b/testnet/files/sbin/testnet @@ -14,14 +14,17 @@ test_connection() { local defroute="$(ip r | grep default | awk '{print$3}' | head -1)" local def6route="$(ip -f inet6 r | grep default | awk '{print$3}')" local ping6dev="$(ip -f inet6 r | grep default | awk '{print$5}')" + local resolvfile="$(uci -q get dhcp.@dnsmasq[0].resolvfile)" if [ -n "$addr" ]; then ping -q -w 5 -c 1 $addr >/dev/null 2>&1 && return 0 elif [ -n "$defroute" ]; then ping -q -w 5 -c 1 $defroute >/dev/null 2>&1 && return 0 - for nmsrv in $(grep nameserver /var/resolv.conf.auto | awk '{print$2}'); do - ping -q -w 5 -c 1 $nmsrv >/dev/null 2>&1 && return 0 - done + if [ -e "$resolvfile" ]; then + for nmsrv in $(grep nameserver "$resolvfile" | awk '{print$2}'); do + ping -q -w 5 -c 1 $nmsrv >/dev/null 2>&1 && return 0 + done + fi elif [ -n "$def6route" -a -n "$ping6dev" ]; then ndisc6 -w 5 -1 $def6route $ping6dev >/dev/null 2>&1 && return 0 fi @@ -32,9 +35,9 @@ internet_test() { local link dest # use the destination address given in config for connectivity check - dest="$(uci -q get netmode.testnet.destination)" + dest="$(uci -q get testnet.global.destination)" # for backwards compatibility - [ -n "$dest" ] || dest="$(uci -q get diagnostics.@connectivity[0].destination)" + [ -n "$dest" ] || dest="$(uci -q get system.@system[0].netping_addr)" test_connection $dest diff --git a/netmode/files/usr/libexec/rpcd/testnet b/testnet/files/usr/libexec/rpcd/testnet similarity index 100% rename from netmode/files/usr/libexec/rpcd/testnet rename to testnet/files/usr/libexec/rpcd/testnet