diff --git a/box/scripts/box.iptables b/box/scripts/box.iptables index 610d2ea..d6eaea4 100755 --- a/box/scripts/box.iptables +++ b/box/scripts/box.iptables @@ -139,51 +139,53 @@ intranet6=( ff00::/8 ) -# monitor_local_ip() { - # if [ "${iptables}" != "ip6tables -w 64" ]; then - # local_ips=($(ip a | busybox awk '$1~/inet$/{print $2}')) - # else - # local_ips=($(ip -6 a | busybox awk '$1~/inet6$/{print $2}')) - # fi - # local_ip_number=${#local_ips[@]} - # rules_ips=($(${iptables} -t mangle -nvL FILTER_LOCAL_IP | grep "ACCEPT" | busybox awk '{print $9}')) - # rules_number=$(${iptables} -t mangle -L FILTER_LOCAL_IP | grep "ACCEPT" | wc -l) +monitor_local_ip() { + [ "${iptables}" != "ip6tables -w 64" ] && ipv=4 || ipv=6 + if [ "${iptables}" != "ip6tables -w 64" ]; then + local_ips=($(ip a | busybox awk '$1~/inet$/{print $2}')) + else + local_ips=($(ip -6 a | busybox awk '$1~/inet6$/{print $2}')) + fi + local_ip_number=${#local_ips[@]} + rules_ips=($(${iptables} -t mangle -nvL FILTER_LOCAL_IP | grep "ACCEPT" | busybox awk '{print $9}')) + rules_number=$(${iptables} -t mangle -L FILTER_LOCAL_IP | grep "ACCEPT" | wc -l) - # if [ ${local_ip_number} -ne ${rules_number} ] ; then - # for rules_subnet in "${rules_ips[@]}" ; do - # wait_count=0 - # a_subnet=$(busybox ipcalc -n ${rules_subnet} | busybox awk -F '=' '{print $2}') - # for local_subnet in "${local_ips[@]}" ; do - # b_subnet=$(busybox ipcalc -n ${local_subnet} | busybox awk -F '=' '{print $2}') - # if [ "${a_subnet}" != "${b_subnet}" ] ; then - # wait_count=$((wait_count + 1)) - # if [ ${wait_count} -ge ${local_ip_number} ] ; then - # ${iptables} -t mangle -D FILTER_LOCAL_IP -d ${rules_subnet} -j ACCEPT - # fi - # fi - # done - # done + if [ ${local_ip_number} -ne ${rules_number} ] ; then + for rules_subnet in "${rules_ips[@]}" ; do + wait_count=0 + a_subnet=$(busybox ipcalc -n ${rules_subnet} | busybox awk -F '=' '{print $2}') + for local_subnet in "${local_ips[@]}" ; do + b_subnet=$(busybox ipcalc -n ${local_subnet} | busybox awk -F '=' '{print $2}') + if [ "${a_subnet}" != "${b_subnet}" ] ; then + wait_count=$((wait_count + 1)) + if [ ${wait_count} -ge ${local_ip_number} ] ; then + ${iptables} -t mangle -D FILTER_LOCAL_IP -d ${rules_subnet} -j ACCEPT + fi + fi + done + done - # for subnets in "${local_ips[@]}" ; do - # if ! (${iptables} -t mangle -C FILTER_LOCAL_IP -d ${subnets} -j ACCEPT > /dev/null 2>&1) ; then - # ${iptables} -t mangle -I FILTER_LOCAL_IP -d ${subnets} -j ACCEPT - # fi - # done + for subnets in "${local_ips[@]}" ; do + if ! (${iptables} -t mangle -C FILTER_LOCAL_IP -d ${subnets} -j ACCEPT > /dev/null 2>&1) ; then + ${iptables} -t mangle -I FILTER_LOCAL_IP -d ${subnets} -j ACCEPT + fi + done + log Debug "Filter Local IP($ipv) ${local_ips[*]}" - # unset a_subnet - # unset b_subnet - # [ "${iptables}" != "ip6tables -w 64" ] && log Debug "the iptables rules for bypassing the local IP(4/6) have been updated." - # else - # [ "${iptables}" != "ip6tables -w 64" ] && log Debug "the local IP(4/6) remains unchanged, and no processing has been performed." - # return 1 - # fi + unset a_subnet + unset b_subnet + log Debug "iptables rules for bypassing the local IP($ipv) have been updated." + else + log Debug "local IP($ipv) remains unchanged, and no processing has been performed." + return 1 + fi - # unset local_ips - # unset local_ip_number - # unset rules_ips - # unset rules_number - # unset wait_count -# } + unset local_ips + unset local_ip_number + unset rules_ips + unset rules_number + unset wait_count +} forward() { ${iptables} $1 FORWARD -o "${tun_device}" -j ACCEPT @@ -534,11 +536,11 @@ start_tproxy() { ${iptables} -A OUTPUT -d ::1 -p tcp -m owner --uid-owner "${box_user}" --gid-owner "${box_group}" -m tcp --dport "${tproxy_port}" -j REJECT fi - # # Add filter local IP - # ${iptables} -t mangle -N FILTER_LOCAL_IP - # ${iptables} -t mangle -A PREROUTING -j FILTER_LOCAL_IP - # ${iptables} -t mangle -A OUTPUT -j FILTER_LOCAL_IP - # monitor_local_ip + # Add filter local IP + ${iptables} -t mangle -N FILTER_LOCAL_IP + ${iptables} -t mangle -A PREROUTING -j FILTER_LOCAL_IP + ${iptables} -t mangle -A OUTPUT -j FILTER_LOCAL_IP + monitor_local_ip if [ "${iptables}" != "ip6tables -w 64" ]; then if [ "${bin_name}" = "clash" ]; then @@ -596,12 +598,12 @@ stop_tproxy() { ${iptables} -t mangle -F DIVERT ${iptables} -t mangle -X DIVERT - # # flush filter local IP - # ${iptables} -t mangle -D OUTPUT -j FILTER_LOCAL_IP - # ${iptables} -t mangle -D PREROUTING -j FILTER_LOCAL_IP - # ${iptables} -t mangle -D FILTER_LOCAL_IP - # ${iptables} -t mangle -F FILTER_LOCAL_IP - # ${iptables} -t mangle -X FILTER_LOCAL_IP + # flush filter local IP + ${iptables} -t mangle -D OUTPUT -j FILTER_LOCAL_IP + ${iptables} -t mangle -D PREROUTING -j FILTER_LOCAL_IP + ${iptables} -t mangle -D FILTER_LOCAL_IP + ${iptables} -t mangle -F FILTER_LOCAL_IP + ${iptables} -t mangle -X FILTER_LOCAL_IP # flush QUIC ${iptables} -D OUTPUT -p udp --dport 443 -j REJECT diff --git a/box/scripts/box.service b/box/scripts/box.service index 080f2fe..87586ad 100755 --- a/box/scripts/box.service +++ b/box/scripts/box.service @@ -31,7 +31,7 @@ box_bin_alive() { done "${scripts_dir}/box.iptables" disable >/dev/null 2>&1 [ -f "${box_pid}" ] && rm -f "${box_pid}" - return 1 + exit 1 else return 0 fi @@ -316,7 +316,7 @@ box_run_bin() { nohup busybox setuidgid "${box_user_group}" "${bin_path}" run -D "${box_dir}/${bin_name}" --config-directory "${box_dir}/${bin_name}" > "${box_run}/${bin_name}.log" 2>&1 & PID=$! echo -n $PID > "${box_pid}" - sleep 0.5 + sleep 1 else log Error "$(<"${box_run}/${bin_name}.log")" log Error "configuration failed. Please check the ${box_run}/${bin_name}.log file." @@ -329,7 +329,7 @@ box_run_bin() { nohup busybox setuidgid "${box_user_group}" "${bin_path}" -d "${box_dir}/${bin_name}" -f "${clash_config}" > "${box_run}/${bin_name}.log" 2>&1 & PID=$! echo -n $PID > "${box_pid}" - sleep 0.5 + sleep 1 else log Error "$(<"${box_run}/${bin_name}.log")" log Error "configuration failed. Please check the ${box_run}/${bin_name}.log file." @@ -360,7 +360,7 @@ box_run_bin() { nohup busybox setuidgid "${box_user_group}" "${bin_path}" run -confdir "${box_dir}/${bin_name}" > "${box_run}/${bin_name}.log" 2>&1 & PID=$! echo -n $PID > "${box_pid}" - sleep 0.5 + sleep 1 else log Error "$(<"${box_run}/${bin_name}.log")" log Error "configuration failed. Please check the ${box_run}/${bin_name}.log file." @@ -390,7 +390,7 @@ box_run_bin() { nohup busybox setuidgid "${box_user_group}" "${bin_path}" run -d "${box_dir}/${bin_name}" > "${box_run}/${bin_name}.log" 2>&1 & PID=$! echo -n $PID > "${box_pid}" - sleep 0.5 + sleep 1 else log Error "$(<"${box_run}/${bin_name}.log")" log Error "configuration failed. Please check the ${box_run}/${bin_name}.log file." diff --git a/box/scripts/box.tool b/box/scripts/box.tool index 53a4448..c58af70 100755 --- a/box/scripts/box.tool +++ b/box/scripts/box.tool @@ -204,20 +204,20 @@ update_subs() { "${yq_cmd}" 'del(.outbounds[] | select(.type == "direct" or .type == "block" or .type == "dns" or .type == "selector" or .type == "urltest"))' -i --output-format=json "${sing_provide_config}" # create new outbounds with type: selector, tag: singbox in ${sing_provide_config} - "${yq_cmd}" '.outbounds += [{"tag": "singbox", "type": "selector", "outbounds": [.outbounds[].tag]}]' -i --output-format=json "${sing_provide_config}" + "${yq_cmd}" '.outbounds += [{"tag": "b0x", "type": "selector", "outbounds": [.outbounds[].tag]}]' -i --output-format=json "${sing_provide_config}" - # create new outbounds with type: urltest, tag: singbox[urltest] in ${sing_provide_config} - "${yq_cmd}" '.outbounds += [{"tag": "singbox[urltest]", "type": "urltest", "url": "https://www.gstatic.com/generate_204", "interval": "3m", "outbounds": [.outbounds[].tag]}]' -i --output-format=json "${sing_provide_config}" + # create new outbounds with type: urltest, tag: b0x[urltest] in ${sing_provide_config} + "${yq_cmd}" '.outbounds += [{"tag": "b0x[urltest]", "type": "urltest", "url": "https://www.gstatic.com/generate_204", "interval": "3m", "outbounds": [.outbounds[].tag]}]' -i --output-format=json "${sing_provide_config}" # renew outbounds with tag: singbox in main ${sing_config} dan ${sing_provide_config} - "${yq_cmd}" 'del(.outbounds[].outbounds[] | select(. == "singbox"))' -i --output-format=json "${sing_provide_config}" - "${yq_cmd}" 'del(.outbounds[].outbounds[] | select(. == "singbox"))' -i --output-format=json "${sing_config}" - "${yq_cmd}" '.outbounds[0].outbounds += ["singbox"]' -i --output-format=json "${sing_config}" + "${yq_cmd}" 'del(.outbounds[].outbounds[] | select(. == "b0x"))' -i --output-format=json "${sing_provide_config}" + "${yq_cmd}" 'del(.outbounds[].outbounds[] | select(. == "b0x"))' -i --output-format=json "${sing_config}" + "${yq_cmd}" '.outbounds[0].outbounds += ["b0x"]' -i --output-format=json "${sing_config}" - # renew outbounds with tag: singbox[urltest] in main ${sing_config} dan ${sing_provide_config} - "${yq_cmd}" 'del(.outbounds[].outbounds[] | select(. == "singbox[urltest]"))' -i --output-format=json "${sing_provide_config}" - "${yq_cmd}" 'del(.outbounds[].outbounds[] | select(. == "singbox[urltest]"))' -i --output-format=json "${sing_config}" - "${yq_cmd}" '.outbounds[0].outbounds += ["singbox[urltest]"]' -i --output-format=json "${sing_config}" + # renew outbounds with tag: b0x[urltest] in main ${sing_config} dan ${sing_provide_config} + "${yq_cmd}" 'del(.outbounds[].outbounds[] | select(. == "b0x[urltest]"))' -i --output-format=json "${sing_provide_config}" + "${yq_cmd}" 'del(.outbounds[].outbounds[] | select(. == "b0x[urltest]"))' -i --output-format=json "${sing_config}" + "${yq_cmd}" '.outbounds[0].outbounds += ["b0x[urltest]"]' -i --output-format=json "${sing_config}" log Info "subscription success" log Info "Update subscription $(date +"%F %R")" @@ -270,7 +270,7 @@ update_kernel() { "sing-box") url_down="https://github.com/SagerNet/sing-box/releases" if [ "${singbox_releases}" = "false" ]; then - sing_box_version_temp=$(busybox wget --no-check-certificate -qO- "${url_down}" | grep -oE '/tag/v[0-9]+\.[0-9]+-[a-z0-9]+' | head -1 | busybox awk -F'/' '{print $3}') + sing_box_version_temp=$(busybox wget --no-check-certificate -qO- "${url_down}" | grep -oE '/tag/v[0-9]+\.[0-9].+-[a-z0-9]+' | head -1 | busybox awk -F'/' '{print $3}' | busybox awk -F'"' '{print $1}') else sing_box_version_temp=$(busybox wget --no-check-certificate -qO- "${url_down}" | grep -oE '/tag/v[0-9]+\.[0-9]+\.[0-9]+' | head -1 | busybox awk -F'/' '{print $3}') fi diff --git a/box/settings.ini b/box/settings.ini index 93f6bb2..955d5ab 100755 --- a/box/settings.ini +++ b/box/settings.ini @@ -106,9 +106,9 @@ gray="\033[1;90m" log() { # Selects the text color according to the parameters case $1 in - info) color="${blue}" ;; - error) color="${red}" ;; - warn) color="${yellow}" ;; + Info) color="${blue}" ;; + Error) color="${red}" ;; + Warning) color="${yellow}" ;; *) color="${green}" ;; esac # Add messages to time and parameters