#!/bin/sh # # Copyright (C) 2018-2025 Ycarus (Yannick Chabanois) for OpenMPTCProuter # # This is free software, licensed under the GNU General Public License v2. # See /LICENSE for more information. # # This script check services status and do appropriate actions # If a service is down, force restart it if [ -f /etc/init.d/shadowsocks-libev ] && [ "$(pgrep -f omr-tracker-ss)" = "" ] && [ "$(pgrep -f ss-redir)" = "" ] && [ "$(pgrep -f ss-local)" = "" ] && [ "$(uci -q get shadowsocks-libev.sss0.disabled)" != "1" ] && [ "$(uci -q get shadowsocks-libev.sss0.server)" != "" ] && [ "$(uci -q get shadowsocks-libev.sss0.server)" != "192.18.1.3" ] && [ "$(uci -q get shadowsocks-libev.sss0.key)" != "" ]; then _log "Can't find Shadowsocks, restart it..." /etc/init.d/shadowsocks-libev restart >/dev/null 2>&1 sleep 5 fi if [ -f /etc/init.d/shadowsocks-rust ] && [ "$(pgrep -f omr-tracker-ss)" = "" ] && [ "$(pgrep -f sslocal)" = "" ] && [ "$(pgrep -f sslocal)" = "" ] && [ "$(uci -q get shadowsocks-rust.sss0.disabled)" != "1" ] && [ "$(uci -q get shadowsocks-rust.sss0.server)" != "" ] && [ "$(uci -q get shadowsocks-rust.sss0.server)" != "192.18.1.3" ] && [ "$(uci -q get shadowsocks-rust.sss0.password)" != "" ]; then _log "Can't find Shadowsocks Rust, restart it..." /etc/init.d/shadowsocks-rust restart >/dev/null 2>&1 sleep 5 fi if [ -f /etc/init.d/glorytun ] && [ "$(pgrep glorytun)" = "" ] && [ "$(uci -q get glorytun.vpn.enable)" = "1" ] && [ -f /etc/init.d/glorytun ] && [ "$(uci -q get glorytun.vpn.key)" != "" ]; then _log "Can't find Glorytun, restart it..." /etc/init.d/glorytun restart >/dev/null 2>&1 sleep 5 fi if [ -f /etc/init.d/glorytun-udp ] && [ "$(pgrep glorytun-udp)" = "" ] && [ "$(uci -q get glorytun-usp.vpn.enable)" = "1" ] && [ -f /etc/init.d/glorytun-udp ] && [ "$(uci -q get glorytun-udp.vpn.key)" != "" ]; then _log "Can't find Glorytun UDP, restart it..." /etc/init.d/glorytun-udp restart >/dev/null 2>&1 sleep 5 fi if [ -f /etc/init.d/unbound ] && [ "$(uci -q get unbound.@unbound[0].enabled)" = "1" ] && [ -n "$(uci -q get dhcp.dnsmasq1.server | grep 127.0.0.1#5353)" ]; then if [ "$(pgrep -f unbound)" = "" ]; then _log "Can't find unbound, restart it..." /etc/init.d/unbound restart >/dev/null 2>&1 sleep 5 elif [ "$(uci -q get openmptcprouter.settings.external_check)" != "0" ] && [ "$(uci -q get unbound.ub_main.listen_port)" = "5353" ] && [ -n "$(dig +timeout=4 +tries=2 openmptcprouter.com -p 5353 | grep 'ANSWER: 0')" ]; then _log "Can't resolve via unbound, restart it..." /etc/init.d/unbound restart >/dev/null 2>&1 sleep 5 fi fi if [ -f /etc/init.d/dnsmasq ]; then if [ "$(pgrep -f dnsmasq)" = "" ]; then _log "Can't find dnsmasq, restart it..." /etc/init.d/dnsmasq restart >/dev/null 2>&1 sleep 5 elif [ "$(uci -q get openmptcprouter.settings.external_check)" != "0" ] && [ -n "$(dig +timeout=4 +tries=2 openmptcprouter.com 2>&1 | grep -i 'connection refused')" ]; then _log "Can't resolve via dnsmasq, restart it..." /etc/init.d/dnsmasq restart >/dev/null 2>&1 sleep 5 fi fi if [ "$(pgrep openvpn)" = "" ] && [ -f /etc/init.d/openvpn ]; then openvpn_enable=0 openvpn_enabled() { [ "$(uci -q get openvpn.$1.enabled)" = "1" ] && [ -n "$(uci -q get openvpn.$1.ca)" ] && openvpn_enable=1 } config_load openvpn config_foreach openvpn_enabled openvpn if [ "$openvpn_enable" = "1" ]; then _log "Can't find OpenVPN, restart it" /etc/init.d/openvpn restart >/dev/null 2>&1 sleep 5 fi fi if [ "$(pgrep mlvpn)" = "" ] && [ "$(uci -q get mlvpn.general.enable)" = "1" ] && [ -f /etc/init.d/mlvpn ] && [ "$(uci -q get mlvpn.general.password)" != "" ]; then _log "Can't find MLVPN, restart it..." /etc/init.d/mlvpn restart >/dev/null 2>&1 sleep 5 fi if [ "$(pgrep dsvpn)" = "" ] && [ "$(uci -q get dsvpn.vpn.enable)" = "1" ] && [ -f /etc/init.d/dsvpn ] && [ "$(uci -q get dsvpn.vpn.key)" != "" ]; then _log "Can't find DSVPN, restart it..." /etc/init.d/dsvpn restart >/dev/null 2>&1 sleep 5 fi if [ "$(pgrep -f v2ray)" = "" ] && [ "$(uci -q get v2ray.main.enabled)" = "1" ] && [ -f /etc/init.d/v2ray ]; then _log "Can't find V2Ray, restart it..." /etc/init.d/v2ray restart >/dev/null 2>&1 sleep 5 fi if [ "$(pgrep -f xray)" = "" ] && [ "$(uci -q get xray.main.enabled)" = "1" ] && [ -f /etc/init.d/xray ]; then _log "Can't find XRay, restart it..." /etc/init.d/xray restart >/dev/null 2>&1 sleep 5 fi if [ "$(pgrep miniupnpd)" = "" ] && [ "$(uci -q get upnpd.config.enabled)" = "1" ] && [ -f /etc/init.d/miniupnpd ]; then _log "Can't find miniupnpd, restart it..." /etc/init.d/miniupnpd restart >/dev/null 2>&1 sleep 5 fi if [ "$(pgrep rpcd)" = "" ] && [ -f /etc/init.d/rpcd ]; then _log "Can't find rpcd, restart it..." /etc/init.d/rpcd enable >/dev/null 2>&1 /etc/init.d/rpcd restart >/dev/null 2>&1 sleep 5 fi if [ "$(pgrep dbus)" = "" ] && [ -f /etc/init.d/dbus ]; then _log "Can't find dbus, restart it..." /etc/init.d/dbus enable >/dev/null 2>&1 /etc/init.d/dbus restart >/dev/null 2>&1 sleep 5 fi if [ "$(pgrep uhttpd)" = "" ] && [ -f /etc/init.d/uhttpd ]; then _log "Can't find uhttpd, restart it..." /etc/init.d/uhttpd restart >/dev/null 2>&1 sleep 5 fi if [ "$(uci -q get openmptcprouter.settings.disable_modemmanager)" != "1" ]; then if [ -z "$(pgrep ModemManager)" ] && [ -f /etc/init.d/modemmanager ] && [ -n "$(uci -q show network | grep modemmanager)" ]; then _log "Can't find ModemManager, restart it..." /etc/init.d/modemmanager restart >/dev/null 2>&1 sleep 5 elif [ -n "$(pgrep ModemManager)" ] && [ -f /etc/init.d/modemmanager ] && [ -n "$(uci -q show network | grep modemmanager)" ] && [ -z "$(timeout 2 mmcli -L)" ]; then _log "ModemManager doesn't answer, restart it..." /etc/init.d/modemmanager restart >/dev/null 2>&1 sleep 5 #elif [ -n "$(pgrep ModemManager)" ] && [ -f /etc/init.d/modemmanager ] && [ -z "$(uci -q show network | grep modemmanager)" ]; then # _log "ModemManager not used, stop it..." # /etc/init.d/modemmanager stop >/dev/null 2>&1 # sleep 5 fi /etc/init.d/modemmanager enable >/dev/null 2>&1 fi if [ "$(uci -q get v2ray.main.enabled)" = "1" ] && [ -f /etc/init.d/v2ray ] && [ "$(pgrep -f omr-tracker-v2ray)" = "" ] && [ "$(pgrep -f '/etc/init.d/omr-tracker')" = "" ]; then _log "Can't find omr-tracker-v2ray, restart omr-tracker..." /etc/init.d/omr-tracker restart >/dev/null 2>&1 fi if [ "$(uci -q get xray.main.enabled)" = "1" ] && [ -f /etc/init.d/xray ] && [ "$(pgrep -f omr-tracker-xray)" = "" ] && [ "$(pgrep -f '/etc/init.d/omr-tracker')" = "" ]; then _log "Can't find omr-tracker-xray, restart omr-tracker..." /etc/init.d/omr-tracker restart >/dev/null 2>&1 fi set_get_config() { local server=$1 [ -n "$server" ] && uci -q set openmptcprouter.${server}.get_config=1 } if ([ -f /etc/init.d/shadowsocks-libev ] && [ "$(uci -q get shadowsocks-libev.sss0.disabled)" != "1" ] && [ "$(uci -q get shadowsocks-libev.sss0.key)" = "" ] && [ "$(uci -q get shadowsocks-libev.sss0.server)" != "" ] && [ "$(uci -q get shadowsocks-libev.sss0.server)" != "192.18.1.3" ]) || ([ -f /etc/init.d/shadowsocks-rust ] && [ "$(uci -q get shadowsocks-rust.sss0.disabled)" != "1" ] && [ "$(uci -q get shadowsocks-rust.sss0.password)" = "" ] && [ "$(uci -q get shadowsocks-rust.sss0.server)" != "" ] && [ "$(uci -q get shadowsocks-rust.sss0.server)" != "192.18.1.3" ]); then config_load openmptcprouter config_foreach set_get_config server [ -n "$(uci -q changes openmptcprouter)" ] && uci -q commit openmptcprouter fi restart_omrtracker() { [ -n "$(pgrep -f $1)" ] && return config_get multipath "$1" multipath config_get ifenabled "$1" auto [ -z "$multipath" ] || [ "$multipath" = "off" ] && return [ "$ifenabled" = "0" ] && return /etc/init.d/omr-tracker enable /etc/init.d/omr-tracker start_interface "$1" sleep 10 } set_lan_ips() { config_get ip4table "$1" ip4table config_get device "$1" device config_get proto "$1" proto config_get multipath "$1" multipath # No restrict for interfaces with strong name [ -n "$(echo $device | grep -)" ] && uci -q set openmptcprouter.settings.restrict_to_lan="0" && uci commit openmptcprouter uci -q del_list shadowsocks-libev.ss_rules.ifnames="$device" uci -q del_list shadowsocks-rust.ss_rules.ifnames="$device" uci -q del_list unbound.ub_main.iface_lan="$1" uci -q del_list unbound.ub_main.iface_wan="$1" uci -q del_list dhcp.@dnsmasq[0].interface="$1" uci -q del_list dhcp.@dnsmasq[0].notinterface="$1" if [ "$multipath" != "on" ] && [ "$multipath" != "master" ] && [ -n "$device" ] && [ -z "$(echo $device | grep @)" ] && ([ "$proto" = "dhcp" ] || [ "$proto" = "static" ]); then uci -q add_list shadowsocks-libev.ss_rules.ifnames="$device" uci -q add_list shadowsocks-rust.ss_rules.ifnames="$device" uci -q add_list unbound.ub_main.iface_lan="$1" uci -q add_list dhcp.@dnsmasq[0].interface="$1" elif [ "$multipath" = "on" ] || [ "$multipath" = "master" ]; then uci -q add_list unbound.ub_main.iface_wan="$1" uci -q add_list dhcp.@dnsmasq[0].notinterface="$1" fi } config_load network config_foreach restart_omrtracker interface [ "$(uci -q get openmptcprouter.settings.restrict_to_lan)" = "1" ] && config_foreach set_lan_ips interface [ "$(uci -q get openmptcprouter.settings.restrict_to_lan)" = "0" ] && ([ -n "$(uci -q get shadowsocks-libev.ss_rules.ifnames)" ] || [ -n "$(uci -q get shadowsocks-rust.ss_rules.ifnames)" ]) && { uci -q batch <<-EOF delete shadowsocks-libev.ss_rules.ifnames delete shadowsocks-rust.ss_rules.ifnames delete unbound.ub_main.lan delete unbound.ub_main.wan delete dhcp.@dnsmasq[0].interface delete dhcp.@dnsmasq[0].notinterface EOF } [ -n "$(uci -q changes shadowsocks-libev)" ] && uci -q commit shadowsocks-libev [ -n "$(uci -q changes shadowsocks-rust)" ] && uci -q commit shadowsocks-rust [ -n "$(uci -q changes unbound)" ] && uci -q commit unbound [ -n "$(uci -q changes dhcp)" ] && uci -q commit dhcp multipath_fix() { config_get multipath "$1" multipath [ "$multipath" != "off" ] && return config_get device "$1" device [ "$(echo $device | grep '@')" ] && return interface="$(ifstatus $1 | jsonfilter -q -e '@.l3_device' | tr -d '\n')" [ -n "$interface" ] && [ -z "$(multipath $interface | grep deactivated)" ] && { _log "Fix Multipath status on $1 ($interface)" /etc/init.d/mptcp reload $interface >/dev/null 2>&1 } } if [ "$(uci -q get network.globals.multipath)" = "enable" ]; then config_load network config_foreach multipath_fix interface fi if [ -f /etc/init.d/shadowsocks-libev ] && [ "$(uci -q get shadowsocks-libev.sss0.disabled)" != "1" ] && [ "$(uci -q get shadowsocks-libev.sss0.key)" != "" ] && [ "$(uci -q get shadowsocks-libev.sss0.server)" != "" ] && [ "$(uci -q get shadowsocks-libev.sss0.server)" != "192.168.1.3" ] && [ "$(pgrep -f omr-tracker-ss)" = "" ] && [ "$(pgrep -f '/etc/init.d/omr-tracker')" = "" ]; then _log "Can't find omr-tracker-ss for Shadowsocks libev, restart omr-tracker..." /etc/init.d/omr-tracker restart >/dev/null 2>&1 fi if [ -f /etc/init.d/shadowsocks-rust ] && [ "$(uci -q get shadowsocks-rust.sss0.disabled)" != "1" ] && [ "$(uci -q get shadowsocks-rust.sss0.password)" != "" ] && [ "$(uci -q get shadowsocks-rust.sss0.server)" != "" ] && [ "$(uci -q get shadowsocks-libev.rust.server)" != "192.168.1.3" ] && [ "$(pgrep -f omr-tracker-ss)" = "" ] && [ "$(pgrep -f '/etc/init.d/omr-tracker')" = "" ]; then _log "Can't find omr-tracker-ss for Shadowsocks Rust, restart omr-tracker..." /etc/init.d/omr-tracker restart >/dev/null 2>&1 fi if [ "$(uci -q get glorytun.vpn.enable)" = "1" ] && [ "$(uci -q get glorytun.vpn.key)" = "" ]; then config_load openmptcprouter config_foreach set_get_config server [ -n "$(uci -q changes openmptcprouter)" ] && uci -q commit openmptcprouter fi if [ "$(uci -q get v2ray.main.enabled)" = "1" ] && [ "$(uci -q get v2ray.omrout.s_vless_user_id)" = "" ]; then config_load openmptcprouter config_foreach set_get_config server [ -n "$(uci -q changes openmptcprouter)" ] && uci -q commit openmptcprouter fi if [ "$(uci -q get xray.main.enabled)" = "1" ] && [ "$(uci -q get xray.omrout.s_vless_user_id)" = "" ]; then config_load openmptcprouter config_foreach set_get_config server [ -n "$(uci -q changes openmptcprouter)" ] && uci -q commit openmptcprouter fi if [ -n "$(logread | tail -n 2 | grep 'Ring expansion failed')" ]; then _log "Workaround Ring expansion failed problem" echo 1 > /sys/bus/pci/devices/0000:00:00.0/remove sleep 2 echo 1 > /sys/bus/pci/rescan fi if [ -f /etc/init.d/omr-bypass ] && [ -z "$(pgrep -f omr-bypass)" ] && ( ([ -e /usr/sbin/iptables-nft-save ] && [ "$(iptables-nft-save 2>/dev/null | grep omr-bypass)" = "" ]) || [ "$(iptables-save 2>/dev/null | grep omr-bypass)" = "" ]); then _log "Can't find omr-bypass rules, restart omr-bypass..." /etc/init.d/omr-bypass start >/dev/null 2>&1 sleep 5 fi if [ -f /etc/backup/installed_packages.txt ] && [ -n "$(grep overlay /etc/backup/installed_packages.txt)" ] && ([ "$(uci -q get openmptcprouter.settings.sysupgrade_lc)" = "" ] || [ $(($(date +"%s") + $((30 + RANDOM % 31)) - $(uci -q get openmptcprouter.settings.sysupgrade_lc))) -gt 3600 ]) && [ $(($(date +"%s") - $(stat -c "%Y" /etc/backup/installed_packages.txt))) -gt 86400 ]; then _log "Reinstall packages..." uci -q set openmptcprouter.settings.sysupgrade_lc=$(date +"%s") /etc/init.d/sysupgrade restart >/dev/null 2>&1 sleep 10 fi if [ "$(pgrep -f openmptcprouter-vps)" = "" ] && [ "$(uci -q show openmptcprouter | grep token_error=\'1\')" != "" ]; then /etc/init.d/openmptcprouter-vps token >/dev/null 2>&1 & sleep 5 fi if [ "$(pgrep -f openmptcprouter-vps)" = "" ] && ([ -z "$(uci -q show openmptcprouter | grep get_config)" ] || [ -n "$(uci -q show openmptcprouter | grep get_config=\'1\')" ] || [ -n "$(uci -q show openmptcprouter | grep admin_error=\'1\')" ]); then /etc/init.d/openmptcprouter-vps restart >/dev/null 2>&1 & sleep 5 fi if [ -n "$OMR_TRACKER_INTERFACE" ] && [ "$(uci -q get sqm.${OMR_TRACKER_INTERFACE}.enabled)" = "1" ] && [ "$(uci -q get sqm.${OMR_TRACKER_INTERFACE}.autorate)" = "1" ] && [ -n "$OMR_TRACKER_DEVICE" ] && [ -z $(pgrep -f "config.${OMR_TRACKER_INTERFACE}") ]; then if [ "$(uci -q get sqm.${OMR_TRACKER_INTERFACE}.max_download)" != "0" ] && [ "$(uci -q get sqm.${OMR_TRACKER_INTERFACE}.download)" != "0" ] && [ "$(uci -q get sqm.${OMR_TRACKER_INTERFACE}.max_upload)" != "0" ] && [ "$(uci -q get sqm.${OMR_TRACKER_INTERFACE}.upload)" != "0" ]; then _log "Restart SQM Autorate" /etc/init.d/sqm-autorate restart >/dev/null 2>&1 sleep 5 fi fi #if [ "$(uci -q show openmptcprouter | grep server)" != "" ] && [ "$(uci -q show openmptcprouter | grep password)" != "" ] && [ "$(pgrep -f openmptcprouter-vps)" = "" ] && [ "$(uci -q show openmptcprouter | grep admin_error=\'1\')" = "" ] && ([ "$(uci -q show openmptcprouter | grep set_firewall=\'1\')" != "" ] || (([ -e /usr/sbin/iptables-nft-save ] && [ -z "$(iptables-save 2>/dev/null | grep omr_dst_bypass_${OMR_TRACKER_DEVICE})" ]) || [ -z "$(iptables-save 2>/dev/null | grep omr_dst_bypass_${OMR_TRACKER_DEVICE})" ])); then if [ "$(pgrep -f set_vps_firewall)" = "" ] && [ "$(uci -q show openmptcprouter | grep server)" != "" ] && [ "$(uci -q show openmptcprouter | grep password)" != "" ] && [ "$(pgrep -f openmptcprouter-vps)" = "" ] && [ -z "$(uci -q show openmptcprouter | grep admin_error=\'1\')" ] && [ -n "$(uci -q show openmptcprouter | grep set_firewall=\'1\')" ]; then check_server_fw() { [ "$(uci -q get openmptcprouter.$1.set_firewall)" = "1" ] && { _log "Set firewall on server $1" /etc/init.d/openmptcprouter-vps set_vps_firewall $1 >/dev/null 2>&1 } } config_load openmptcprouter config_foreach check_server_fw server run_fw_include() { [ -n "$1" ] && [ "$(uci -q get firewall.$1.reload)" = "0" ] && sh $(uci -q get firewall.$1.path) >/dev/null 2>&1 } config_load firewall config_foreach run_fw_include include #/etc/init.d/shadowsocks-libev rules_up >/dev/null 2>&1 #/etc/init.d/v2ray rules_up >/dev/null 2>&1 #/etc/init.d/omr-bypass reload_rules >/dev/null 2>&1 #sh /etc/firewall.gre-tunnel >/dev/null 2>&1 /bin/blocklanfw >/dev/null 2>&1 sleep 5 fi if [ "$(uci -q get openmptcprouter.latest_versions.lc)" = "" ] || [ $(($(date +"%s") - $(uci -q get openmptcprouter.latest_versions.lc))) -gt 3600 ]; then distribution="$(ubus call system board | jsonfilter -q -e '@.release.distribution' | tr -d '\n')" version="$(ubus call system board | jsonfilter -q -e '@.release.version' | tr -d '\n')" latestversions="$(curl -4 -s -m 3 -A "${distribution}/${version}" https://www.openmptcprouter.com/version/version.json)" [ -n "$latestversions" ] && { uci -q set openmptcprouter.latest_versions=latest_versions if [ "${distribution}" = "openmptcprouter" ]; then uci -q set openmptcprouter.latest_versions.omr=$(echo $latestversions | jsonfilter -q -e '@.omr')-$(ubus call system board | jsonfilter -e '@.kernel' | cut -d'.' -f1,2) uci -q set openmptcprouter.latest_versions.vps=$(echo $latestversions | jsonfilter -q -e '@.vps') else uci -q del openmptcprouter.latest_versions.omr uci -q del openmptcprouter.latest_versions.vps fi uci -q set openmptcprouter.latest_versions.lc=$(date +"%s") uci -q commit openmptcprouter } fi # Remove old hidden config files find /etc/config/ -mtime +1 -type f -name '\.*' -exec rm {} +