From 662effebddf711ade0e42574496ed9d0cdc4685d Mon Sep 17 00:00:00 2001 From: llyc Date: Thu, 16 Feb 2023 13:13:11 +0700 Subject: [PATCH] optimize bfm service script --- scripts/src/box.iptables | 17 +++++++++++++++-- scripts/src/box.service | 14 +------------- scripts/src/box.tool | 31 +++++++++++-------------------- 3 files changed, 27 insertions(+), 35 deletions(-) diff --git a/scripts/src/box.iptables b/scripts/src/box.iptables index 030b0e2..15cc60f 100755 --- a/scripts/src/box.iptables +++ b/scripts/src/box.iptables @@ -8,8 +8,8 @@ table="222" fwmark="223" pref="100" -# iptables_version=$(iptables -V | grep -o "v1\.[0-9]") -# if [ "${iptables_version}" = "v1.4" ] ; then +# ipts_version=$(iptables -V | grep -o "v1\.[0-9]") +# if [ "${ipts_version}" = "v1.4" ] ; then # ipts="iptables" # ip6ts="ip6tables" # else @@ -47,6 +47,15 @@ ipv6_enable() { intranet=(0.0.0.0/8 10.0.0.0/8 100.64.0.0/10 127.0.0.0/8 169.254.0.0/16 192.0.0.0/24 192.0.2.0/24 192.88.99.0/24 192.168.0.0/16 198.51.100.0/24 203.0.113.0/24 224.0.0.0/4 240.0.0.0/4 255.255.255.255/32) intranet6=(::/128 ::1/128 ::ffff:0:0/96 100::/64 64:ff9b::/96 2001::/32 2001:10::/28 2001:20::/28 2001:db8::/32 2002::/16 fe80::/10 ff00::/8) +network_port () { + $(/system/bin/netstat -tnulp | grep -q "${tproxy_port}") || \ + log error "tproxy_port out of sync with config" + if [ "${network_mode}" = "mixed" ] || [ "${proxy_mode}" = "core" ] ; then + $(/system/bin/ifconfig | grep -q ${tun_device}) || \ + log error "tun_device: ${tun_device} not found" + fi +} + find_packages_uid() { echo -n "" > ${uid_list} for package in ${packages_list[*]} ; do @@ -405,6 +414,7 @@ stop_tproxy() { if [ "${proxy_mode}" != "core" ] ; then case "$1" in enable) + network_port probe_user_group || log Error "failed to check Box user group, please make sure ${bin_name} core is started." iptables="iptables -w 100" && stop_tproxy >> /dev/null 2>&1 iptables="iptables -w 100" && stop_redirect >> /dev/null 2>&1 @@ -477,6 +487,7 @@ if [ "${proxy_mode}" != "core" ] ; then ${scripts_dir}/box.tool testing ;; renew) + network_port probe_user_group || log Error "failed to check Box user group, please make sure ${bin_name} core is started." log warn "cleaning up iptables transparent proxy rules." iptables="iptables -w 100" && stop_tproxy >> /dev/null 2>&1 @@ -572,6 +583,7 @@ if [ "${proxy_mode}" != "core" ] ; then else case "$1" in enable) + network_port probe_user_group || log Error "failed to check Box user group, please make sure ${bin_name} core is started." log info "proxy_mode: ${proxy_mode}, disable transparent proxy." iptables="iptables -w 100" && stop_tproxy >> /dev/null 2>&1 @@ -597,6 +609,7 @@ else ${scripts_dir}/box.tool testing ;; renew) + network_port probe_user_group || log Error "failed to check Box user group, please make sure ${bin_name} core is started." log warn "cleaning up tun rules." iptables="iptables -w 100" && stop_tproxy >> /dev/null 2>&1 diff --git a/scripts/src/box.service b/scripts/src/box.service index 0c33cc2..783b3f3 100755 --- a/scripts/src/box.service +++ b/scripts/src/box.service @@ -260,17 +260,6 @@ crontab_alive() { crontab_geo } -network_port () { - if [ "${clash_tun_status}" = "false" ] ; then - $(/system/bin/netstat -tnulp | grep -q "${tproxy_port}") || \ - log debug "tproxy_port out of sync with config" - fi - if [ "${network_mode}" = "mixed" ] || [ "${proxy_mode}" = "core" ] ; then - $(/system/bin/ifconfig | grep -q ${tun_device}) || \ - log error "tun_device: ${tun_device} not found" - fi -} - start_box() { echo -n "" > ${logs_file} [ -t 1 ] && echo "\033[1;31m$(date)\033[0m" || echo "$(date)" | tee -a ${logs_file} >> /dev/null 2>&1 @@ -291,7 +280,6 @@ start_box() { crontab_alive || log info "crontab: disable." cgroup_limit detected_port || sleep 1 - # network_port still_alive display_bin_pid } @@ -333,7 +321,7 @@ case "$1" in esac [ $(pidof ${bin_name}) ] && \ bin_usage || log warn "${bin_name} service is stopped" - ;; + ;; reload) if [ "${bin_name}" = "clash" ] ; then temporary_config_file diff --git a/scripts/src/box.tool b/scripts/src/box.tool index b33c7cb..117407f 100755 --- a/scripts/src/box.tool +++ b/scripts/src/box.tool @@ -41,14 +41,8 @@ testing () { fi logs testing "https=" - ipInfo=$(${data_dir}/bin/mlbox -timeout=5 -http="https://ip.tool.lu/" 2>&1 | grep -Ev 'timeout|httpGetResponse') - if echo "${ipInfo}" | grep -qi 'IP'; then - logs succes "done" - else - httpsResp=$(${data_dir}/bin/mlbox -timeout=5 -http="https://api.infoip.io" 2>&1 | grep -Ev 'timeout|httpGetResponse' | grep -E '[1-9][0-9]{0,2}(\.[0-9]{1,3}){3}') - [ -n "${httpsResp}" ] && logs succes "done" || \ - logs failed "failed" - fi + httpsResp=$(${data_dir}/bin/mlbox -timeout=5 -http="https://api.infoip.io" 2>&1 | grep -Ev 'timeout|httpGetResponse' | grep -E '[1-9][0-9]{0,2}(\.[0-9]{1,3}){3}') + [ -n "${httpsResp}" ] && logs succes "done" || logs failed "failed" logs testing "udp=" currentTime=$(${data_dir}/bin/mlbox -timeout=7 -ntp="${ntpip}" | grep -v 'timeout') @@ -63,21 +57,18 @@ testing () { } network_check() { - sleep 0.5 if [ -f ${data_dir}/bin/mlbox ] ; then - ipInfo=$(${data_dir}/bin/mlbox -timeout=5 -http="https://ip.tool.lu/" 2>&1 | grep -Ev 'timeout|httpGetResponse') - if echo "${ipInfo}" | grep -qi 'IP'; then - log info "connected to the internet network" + logs info "check internet connection... " + httpsResp=$(${data_dir}/bin/mlbox -timeout=5 -http="https://api.infoip.io" 2>&1 | grep -Ev 'timeout|httpGetResponse' | grep -E '[1-9][0-9]{0,2}(\.[0-9]{1,3}){3}') + if [ -n "${httpsResp}" ] ; then + logs succes "done" else - httpsResp=$(${data_dir}/bin/mlbox -timeout=5 -http="https://api.infoip.io" 2>&1 | grep -Ev 'timeout|httpGetResponse' | grep -E '[1-9][0-9]{0,2}(\.[0-9]{1,3}){3}') - if [ -n "${httpsResp}" ] ; then - log info "connected to the internet network" - else - log debug "the network is down or very slow" - exit 0 - fi - fi + logs failed "failed" + flags=false + fi fi + [ -t 1 ] && echo "\033[1;31m""\033[0m" || echo "" | tee -a ${logs_file} >> /dev/null 2>&1 + [ "${flags}" != "false" ] || exit 1 } probe_bin_alive() {