Skip to content

Commit

Permalink
Add default mode:white if empty in $pkg_config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
twnesss committed Dec 7, 2024
1 parent 01c74b4 commit d5ea1de
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions box/scripts/box.service
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ prepare_singbox() {
# add exclude_package/include_package for tun
"${yq}" '(.inbounds[] | select(.type == "tun") | .include_package) = []' -i --output-format=json "${sing_config}"
"${yq}" '(.inbounds[] | select(.type == "tun") | .exclude_package) = []' -i --output-format=json "${sing_config}"
[[ ${proxy_mode} = "blacklist" || ${proxy_mode} = "black" ]] && local mode="exclude" || mode="include"
[[ ${proxy_mode} = "blacklist" || ${proxy_mode} = "black" ]] && mode="exclude" || mode="include"
for package in "${packages_list[@]}"; do
"${yq}" eval '(.inbounds[] | select(.type == "tun") | .'${mode}'_package) += ["'${package}'"]' -i --output-format=json "${sing_config}"
done
Expand Down Expand Up @@ -327,23 +327,39 @@ prepare_clash() {
log Info "type [tun] already exists in ${clash_config}"
fi

if [ -z "${proxy_mode}" ]; then
M1=$(busybox awk '!/^ *#/ && /mode:white /{print $0}' "${pkg_config}")
[ -z $M1 ] && printf "\nmode:white" >> "${pkg_config}"
log Warning "prox mode is empty, add mode:white in $pkg_config"
fi

# add exclude-package/include-package for tun
package=$(IFS=","; echo "${packages_list[*]}" | tr ' ' ',')
list_package="${package:-}"
if [ "${proxy_mode}" = "whitelist" ] || [ "${proxy_mode}" = "white" ]; then
mode="include-package"
elif [ "${proxy_mode}" = "blacklist" ] || [ "${proxy_mode}" = "black" ]; then
mode="exclude-package"
list_package="${package}"

if [[ "${proxy_mode}" = "whitelist" || "${proxy_mode}" = "white" ]]; then
mode="include"
elif [[ "${proxy_mode}" = "blacklist" || "${proxy_mode}" = "black" ]]; then
mode="exclude"
fi

# Check if include-package is inside a tun block:
include_package_found=$(busybox awk '/^tun:/{f=1} f && /'"$mode-package:"'/{print $0; exit}' "$clash_config")
if [ -z "$include_package_found" ]; then
sed -i "/^tun:/a \ $mode-package: []" "$clash_config"
fi

sed -i "s/exclude-package:.*/exclude-package: []/g" "${clash_config}"
sed -i "s/include-package:.*/include-package: []/g" "${clash_config}"

# Add packages to the configuration if list_package is not empty
if [ -n "${list_package}" ]; then
sed -i "s/${mode}:.*/${mode}: [\"${list_package//,/\",\"}\"]/g" "${clash_config}"
sed -i "s/${mode}-package:.*/${mode}-package: [\"${list_package//,/\",\"}\"]/g" "${clash_config}"
fi
sed -i "/tun:/ {n;s/enable: false/enable: true/}" "${clash_config}"

sed -i "/tun:/,/enable:/ { /enable: false/ s/enable: false/enable: true/ }" "${clash_config}"
else
sed -i "/tun:/ {n;s/enable: true/enable: false/}" "${clash_config}"
sed -i "/tun:/,/enable:/ { /enable: true/ s/enable: false/enable: false/ }" "${clash_config}"
fi

# sync tproxy/redir port
Expand Down

0 comments on commit d5ea1de

Please sign in to comment.