Bonami
28
This is how it looks like my /usr/bin/switchaction/ :
#!/bin/sh
check_other_vpn(){
wg_server=$(uci get wireguard_server.@servers[0].enable)
ov_server=$(uci get vpn_service.global.enable)
client=""
if [ "$1" = "ovpn" ];then
client=$(uci get wireguard.@proxy[0].enable)
else
client=$(uci get glconfig.openvpn.enable)
fi
if [ "$wg_server" = "1" -o "$ov_server" = "1" -o "$client" = "1" ];then
rm /var/lock/switch.lock
exit 0
fi
if [ -f "/etc/config/shadowsocks" ]; then
ss_client=$(uci get shadowsocks.@transparent_proxy[0].main_server)
[ "$ss_client" != "nil" ] && {
rm /var/lock/switch.lock
exit 0
}
fi
if [ -f "/etc/config/ss-service" ]; then
ss_server=$(uci get ss-service.host.enable)
[ "$ss_server" = "1" ] && {
rm /var/lock/switch.lock
exit 0
}
fi
}
set_function(){
# Using lock, avoid restart repeatedly
LOCK=/var/lock/switch.lock
if [ -f "$LOCK" ];then
exit 0
fi
touch $LOCK
model=$(awk -F': ' '/machine/ {print tolower($NF)}' /proc/cpuinfo| cut -d- -f2-)
switch_left=
switch_disabled="0"
switch_enabled=$(uci get glconfig.switch_button.enable)
switch_func=$(uci get glconfig.switch_button.function)
case "$model" in
"ar150")
switch_left=$(grep -o "BTN_8.*hi" /sys/kernel/debug/gpio)
;;
"ar300m")
switch_left=$(grep -o "left.*hi" /sys/kernel/debug/gpio)
;;
"mt300a")
switch_left=$(grep -o "BTN_1.*hi" /sys/kernel/debug/gpio)
;;
"mt300n")
switch_left=$(grep -o "BTN_0.*hi" /sys/kernel/debug/gpio)
;;
"mt300n-v2")
switch_left=$(grep -o "BTN_0.*hi" /sys/kernel/debug/gpio)
;;
"mifi-v3"|\
"ar750s")
switch_left=$(grep -o "right.*lo" /sys/kernel/debug/gpio)
;;
"ar750")
switch_left=$(grep -o "sw1.*lo" /sys/kernel/debug/gpio)
;;
"*")
switch_disabled="1"
;;
esac
if [ "$switch_disabled" = "1" ] || [ "$switch_enabled" != "1" ]; then
rm $LOCK
exit 0
fi
#if switch is on left
if [ -n "$switch_left" ]; then
case "$switch_func" in
"wg")
check_other_vpn wg
wgswitch=`uci get wireguard.@proxy[0].enable`
if [ "$wgswitch" != "1" -o "$wgswitch" != 1 ];then
uci set wireguard.@proxy[0].enable='1'
uci commit wireguard
fi
wgstat=`pidof "wg-crypt-wg0"`
if [ "$wgstat" = "" -o "$wgstat" = " " ];then
/etc/init.d/wireguard restart
fi
;;
"vpn")
check_other_vpn ovpn
vpn_status=$(pidof openvpn)
vpn_cfg=$(uci get glconfig.openvpn.ovpn)
if [ -z $vpn_status ] && [ -n "$vpn_cfg" ]; then
uci set glconfig.openvpn.enable='1'
uci commit glconfig
/etc/init.d/startvpn restart
fi
;;
"*")
;;
esac
else
case "$switch_func" in
"wg")
uci set wireguard.@proxy[0].enable='0'
uci commit wireguard
/etc/init.d/wireguard stop
;;
"vpn")
vpn_en=$(uci get glconfig.openvpn.enable)
if [ "$vpn_en" = "1" ]; then
uci set glconfig.openvpn.enable='0'
uci commit glconfig
/etc/init.d/startvpn stop
fi
;;
"*")
;;
esac
fi
rm $LOCK
}
set_function