mirror of
https://github.com/Ysurac/openmptcprouter-feeds.git
synced 2025-02-15 03:51:51 +00:00
Add choice between ping or dns to omr-tracker
This commit is contained in:
parent
7b94b8742e
commit
8eb08542c0
4 changed files with 61 additions and 26 deletions
|
@ -23,6 +23,9 @@ o.default = "2"
|
||||||
o.datatype = "range(1, 100)"
|
o.datatype = "range(1, 100)"
|
||||||
o.rmempty = false
|
o.rmempty = false
|
||||||
|
|
||||||
|
o = s:option(Value, "type", translate("Type"))
|
||||||
|
o:value("ping","ping")
|
||||||
|
o:value("dns","dns")
|
||||||
|
|
||||||
o = s:option(DynamicList, "hosts", translate("Hosts"))
|
o = s:option(DynamicList, "hosts", translate("Hosts"))
|
||||||
o.placeholder = "4.2.2.1"
|
o.placeholder = "4.2.2.1"
|
||||||
|
|
|
@ -73,7 +73,40 @@ _post_tracking() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
_restart
|
_ping() {
|
||||||
|
local host=$1
|
||||||
|
local deviceip=$2
|
||||||
|
local localip=$3
|
||||||
|
ret=$(ping "${host}" \
|
||||||
|
-I "${deviceip}" \
|
||||||
|
-W "$OMR_TRACKER_TIMEOUT" \
|
||||||
|
-c 1 \
|
||||||
|
-q
|
||||||
|
) && echo "$ret" | grep -sq "0% packet loss" && {
|
||||||
|
if [ "$localip" = "yes" ]; then
|
||||||
|
OMR_TRACKER_LATENCY=$(echo "$ret" | cut -d "/" -s -f4 | cut -d "." -f1)
|
||||||
|
_update_rto "$OMR_TRACKER_LATENCY"
|
||||||
|
fi
|
||||||
|
return
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
_dns() {
|
||||||
|
local host=$1
|
||||||
|
local deviceip=$2
|
||||||
|
ret=$(dig @"${host}" \
|
||||||
|
-b "${deviceip}" \
|
||||||
|
+time="$OTB_TRACKER_TIMEOUT" \
|
||||||
|
+tries=1 \
|
||||||
|
openmptcprouter.com
|
||||||
|
) && echo "$ret" | grep -sq "94.23.252.192" && {
|
||||||
|
OTB_TRACKER_LATENCY=$(echo "$ret" | awk '/Query time/{print $4}')
|
||||||
|
_update_rto "$OTB_TRACKER_LATENCY"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
# main loop
|
# main loop
|
||||||
while true; do
|
while true; do
|
||||||
|
@ -86,36 +119,36 @@ while true; do
|
||||||
if [ -d "/sys/class/net/$OMR_TRACKER_DEVICE" ]; then
|
if [ -d "/sys/class/net/$OMR_TRACKER_DEVICE" ]; then
|
||||||
# retrieve iface ip and gateway
|
# retrieve iface ip and gateway
|
||||||
OMR_TRACKER_DEVICE_IP=$(ip -4 -br addr ls dev "$OMR_TRACKER_DEVICE" | awk -F'[ /]+' '{print $3}')
|
OMR_TRACKER_DEVICE_IP=$(ip -4 -br addr ls dev "$OMR_TRACKER_DEVICE" | awk -F'[ /]+' '{print $3}')
|
||||||
|
if [ -z "$OMR_TRACKER_DEVICE_GATEWAY" ]; then
|
||||||
|
OTB_TRACKER_DEVICE_GATEWAY=$(ip -4 r list dev "$OMR_TRACKER_DEVICE" | grep -v default | awk '/proto static/ {print $1}' | tr -d "\n")
|
||||||
|
fi
|
||||||
|
|
||||||
# execute specific tracker
|
# execute specific tracker
|
||||||
if [ -n "$OMR_TRACKER_DEVICE_IP" ] && [ -n "$OMR_TRACKER_DEVICE_GATEWAY" ]; then
|
if [ -n "$OMR_TRACKER_DEVICE_IP" ] && [ -n "$OMR_TRACKER_DEVICE_GATEWAY" ]; then
|
||||||
retgw=$(ping "$OMR_TRACKER_DEVICE_GATEWAY" \
|
_ping "$OMR_TRACKER_DEVICE_GATEWAY" "$OMR_TRACKER_DEVICE_IP" "no"
|
||||||
-I "$OMR_TRACKER_DEVICE_IP" \
|
status=$?
|
||||||
-W 1 \
|
if ! $(exit $status); then
|
||||||
-c 1 \
|
|
||||||
-q
|
|
||||||
) && echo "$retgw" | grep -sq "0% packet loss" && {
|
|
||||||
# setup loop variable
|
# setup loop variable
|
||||||
tries="$OMR_TRACKER_TRIES"
|
tries="$OMR_TRACKER_TRIES"
|
||||||
ip route replace "$OMR_TRACKER_HOST" via "$OMR_TRACKER_DEVICE_GATEWAY" dev "$OMR_TRACKER_DEVICE" src "$OMR_TRACKER_DEVICE_IP"
|
ip route replace "$OMR_TRACKER_HOST" via "$OMR_TRACKER_DEVICE_GATEWAY" dev "$OMR_TRACKER_DEVICE" src "$OMR_TRACKER_DEVICE_IP"
|
||||||
# loop until tries attempts have been reached
|
# loop until tries attempts have been reached
|
||||||
while [ "$tries" -gt 0 ]; do
|
while [ "$tries" -gt 0 ]; do
|
||||||
ret=$(ping "$OMR_TRACKER_HOST" \
|
if [ "$OMR_TRACKER_TYPE" = "ping" ]; then
|
||||||
-I "$OMR_TRACKER_DEVICE_IP" \
|
_ping "$OMR_TRACKER_HOST" "$OMR_TRACKER_DEVICE_IP" "yes"
|
||||||
-W "$OMR_TRACKER_TIMEOUT" \
|
status=$?
|
||||||
-c 1 \
|
elif [ "$OMR_TRACKER_TYPE" = "dns" ]; then
|
||||||
-q
|
_dns "$OMR_TRACKER_HOST" "$OMR_TRACKER_DEVICE_IP" "yes"
|
||||||
) && echo "$ret" | grep -sq "0% packet loss" && {
|
status=$?
|
||||||
OMR_TRACKER_LATENCY=$(echo "$ret" | cut -d "/" -s -f4 | cut -d "." -f1)
|
fi
|
||||||
_update_rto "$OMR_TRACKER_LATENCY"
|
if ! $(exit $status); then
|
||||||
OMR_TRACKER_STATUS="OK"
|
OMR_TRACKER_STATUS="OK"
|
||||||
break
|
break
|
||||||
}
|
fi
|
||||||
tries=$((tries - 1))
|
tries=$((tries - 1))
|
||||||
OMR_TRACKER_TIMEOUT=$((OMR_TRACKER_TIMEOUT * 2))
|
OMR_TRACKER_TIMEOUT=$((OMR_TRACKER_TIMEOUT * 2))
|
||||||
done
|
done
|
||||||
ip route del "$OMR_TRACKER_HOST" via "$OMR_TRACKER_DEVICE_GATEWAY" dev "$OMR_TRACKER_DEVICE" src "$OMR_TRACKER_DEVICE_IP"
|
ip route del "$OMR_TRACKER_HOST" via "$OMR_TRACKER_DEVICE_GATEWAY" dev "$OMR_TRACKER_DEVICE" src "$OMR_TRACKER_DEVICE_IP"
|
||||||
}
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,5 @@ config defaults 'defaults'
|
||||||
option timeout '1'
|
option timeout '1'
|
||||||
option tries '4'
|
option tries '4'
|
||||||
option interval '2'
|
option interval '2'
|
||||||
|
option type 'ping'
|
||||||
option options ''
|
option options ''
|
||||||
|
|
||||||
config rule_type 'tunnel'
|
|
||||||
option tries '5'
|
|
||||||
|
|
|
@ -11,13 +11,14 @@
|
||||||
|
|
||||||
_validate_section() {
|
_validate_section() {
|
||||||
local tmp_hosts=$hosts tmp_timeout=$timeout tmp_tries=$tries
|
local tmp_hosts=$hosts tmp_timeout=$timeout tmp_tries=$tries
|
||||||
local tmp_interval=$interval tmp_options=$options
|
local tmp_interval=$interval tmp_options=$options tmp_type=$type
|
||||||
|
|
||||||
uci_validate_section omr-tracker "$1" "$2" \
|
uci_validate_section omr-tracker "$1" "$2" \
|
||||||
'hosts:list(host)' \
|
'hosts:list(host)' \
|
||||||
'timeout:uinteger' \
|
'timeout:uinteger' \
|
||||||
'tries:uinteger' \
|
'tries:uinteger' \
|
||||||
'interval:uinteger' \
|
'interval:uinteger' \
|
||||||
|
'type:string' \
|
||||||
'options:string'
|
'options:string'
|
||||||
|
|
||||||
[ -z "$hosts" ] && hosts=$tmp_hosts
|
[ -z "$hosts" ] && hosts=$tmp_hosts
|
||||||
|
@ -25,6 +26,7 @@ _validate_section() {
|
||||||
[ -z "$tries" ] && tries=$tmp_tries
|
[ -z "$tries" ] && tries=$tmp_tries
|
||||||
[ -z "$interval" ] && interval=$tmp_interval
|
[ -z "$interval" ] && interval=$tmp_interval
|
||||||
[ -z "$options" ] && options=$tmp_options
|
[ -z "$options" ] && options=$tmp_options
|
||||||
|
[ -z "$type" ] && options=$tmp_type
|
||||||
}
|
}
|
||||||
|
|
||||||
_launch_tracker() {
|
_launch_tracker() {
|
||||||
|
@ -35,9 +37,8 @@ _launch_tracker() {
|
||||||
local interface_type
|
local interface_type
|
||||||
config_get interface_type "$1" type
|
config_get interface_type "$1" type
|
||||||
|
|
||||||
local hosts timeout tries interval options
|
local hosts timeout tries interval options type
|
||||||
_validate_section "defaults" "defaults"
|
_validate_section "defaults" "defaults"
|
||||||
_validate_section "rule_type" "$interface_type"
|
|
||||||
_validate_section "interface" "$1"
|
_validate_section "interface" "$1"
|
||||||
|
|
||||||
local ifname ip4table
|
local ifname ip4table
|
||||||
|
|
Loading…
Reference in a new issue