Skip to content

Commit

Permalink
(adjust) white/black list mode
Browse files Browse the repository at this point in the history
  • Loading branch information
twnesss committed Dec 7, 2024
1 parent 8f0c649 commit 4042065
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
31 changes: 17 additions & 14 deletions box/scripts/box.iptables
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ start_redirect() {

if [ "${iptables}" = "$IPV" ]; then
case "${proxy_mode}" in
blacklist)
blacklist|black)
if [ -z "$(cat "${uid_list[@]}")" ] ; then
${iptables} -t nat -A BOX_LOCAL -p tcp -j REDIRECT --to-ports "${redir_port}"
[ ${network_mode} = "enhance" ] || log Info "Transparent proxy for all apps."
Expand All @@ -349,19 +349,19 @@ start_redirect() {
${iptables} -t nat -A BOX_LOCAL -m owner --uid-owner "${appid}" -j RETURN
done < "${uid_list[@]}"
${iptables} -t nat -A BOX_LOCAL -p tcp -j REDIRECT --to-ports "${redir_port}"
[ ${network_mode} = "enhance" ] || log Info "proxy-mode: ${proxy_mode}, package ${packages_list[*]} no transparent proxy."
[ ${network_mode} = "enhance" ] || log Info "proxy mode: ${proxy_mode} (${packages_list[*]}) no transparent proxy."
fi

if [ "${gid_list}" != "" ] ; then
for gid in ${gid_list[@]} ; do
${iptables} -t nat -A BOX_LOCAL -m owner --gid-owner ${gid} -j RETURN
done
[ ${network_mode} = "enhance" ] || {
[ "${iptables}" = "$IPV" ] && log Info "proxy mode: ${proxy_mode}, GID ${gid_list[*]} no transparent proxy."
[ "${iptables}" = "$IPV" ] && log Info "proxy mode: ${proxy_mode}, GID (${gid_list[*]}) no transparent proxy."
}
fi
;;
whitelist)
whitelist|white)
if [ -z "$(cat "${uid_list[@]}")" ] ; then
${iptables} -t nat -A BOX_LOCAL -p tcp -j REDIRECT --to-ports "${redir_port}"
[ ${network_mode} = "enhance" ] || log Info "Transparent proxy for all apps."
Expand All @@ -371,18 +371,18 @@ start_redirect() {
done < "${uid_list[@]}"
${iptables} -t nat -A BOX_LOCAL -p tcp -m owner --uid-owner 0 -j REDIRECT --to-ports "${redir_port}"
${iptables} -t nat -A BOX_LOCAL -p tcp -m owner --uid-owner 1052 -j REDIRECT --to-ports "${redir_port}"
[ ${network_mode} = "enhance" ] || log Info "proxy-mode: ${proxy_mode}, package ${packages_list[*]} transparent proxy."
[ ${network_mode} = "enhance" ] || log Info "proxy mode: ${proxy_mode} (${packages_list[*]}) transparent proxy."
fi

if [ "${gid_list}" != "" ] ; then
for gid in ${gid_list[@]} ; do
${iptables} -t nat -A BOX_LOCAL -p tcp -m owner --gid-owner ${gid} -j REDIRECT --to-ports ${redir_port}
done
[ ${network_mode} = "enhance" ] || [ "${iptables}" = "$IPV" ] && log Info "proxy mode: ${proxy_mode}, GID ${gid_list[*]} transparent proxy."
[ ${network_mode} = "enhance" ] || [ "${iptables}" = "$IPV" ] && log Info "proxy mode: ${proxy_mode}, GID (${gid_list[*]}) transparent proxy."
fi
;;
*)
log Warning "proxy-mode: ${proxy_mode} < error."
log Warning "proxy mode: ${proxy_mode} < error."
${iptables} -t nat -A BOX_LOCAL -p tcp -j REDIRECT --to-ports "${redir_port}"
[ ${network_mode} = "enhance" ] || log Info "Transparent proxy for all apps."
;;
Expand Down Expand Up @@ -524,7 +524,7 @@ start_tproxy() {
fi

case "${proxy_mode}" in
blacklist)
blacklist|black)
if [ -z "$(cat "${uid_list[@]}")" ] ; then
[ ${network_mode} = "enhance" ] || ${iptables} -t mangle -A BOX_LOCAL -p tcp -j MARK --set-mark "${fwmark}"
${iptables} -t mangle -A BOX_LOCAL -p udp -j MARK --set-mark "${fwmark}"
Expand All @@ -535,17 +535,17 @@ start_tproxy() {
done < "${uid_list[@]}"
[ ${network_mode} = "enhance" ] || ${iptables} -t mangle -A BOX_LOCAL -p tcp -j MARK --set-mark "${fwmark}"
${iptables} -t mangle -A BOX_LOCAL -p udp -j MARK --set-mark "${fwmark}"
[ "${iptables}" = "$IPV" ] && log Info "proxy-mode: ${proxy_mode}, package ${packages_list[*]} no transparent proxy."
[ "${iptables}" = "$IPV" ] && log Info "proxy mode: ${proxy_mode} (${packages_list[*]}) no transparent proxy."
fi

if [ "${gid_list}" != "" ] ; then
for gid in ${gid_list[@]} ; do
${iptables} -t mangle -A BOX_LOCAL -m owner --gid-owner ${gid} -j RETURN
done
[ "${iptables}" = "$IPV" ] && log Info "proxy mode: ${proxy_mode}, GID ${gid_list[*]} no transparent proxy."
[ "${iptables}" = "$IPV" ] && log Info "proxy mode: ${proxy_mode}, GID (${gid_list[*]}) no transparent proxy."
fi
;;
whitelist)
whitelist|white)
if [ -z "$(cat "${uid_list[@]}")" ] ; then
[ ${network_mode} = "enhance" ] || ${iptables} -t mangle -A BOX_LOCAL -p tcp -j MARK --set-mark "${fwmark}"
${iptables} -t mangle -A BOX_LOCAL -p udp -j MARK --set-mark "${fwmark}"
Expand All @@ -559,19 +559,19 @@ start_tproxy() {
${iptables} -t mangle -A BOX_LOCAL -p udp -m owner --uid-owner 0 -j MARK --set-mark "${fwmark}"
[ ${network_mode} = "enhance" ] || ${iptables} -t mangle -A BOX_LOCAL -p tcp -m owner --uid-owner 1052 -j MARK --set-mark "${fwmark}"
${iptables} -t mangle -A BOX_LOCAL -p udp -m owner --uid-owner 1052 -j MARK --set-mark "${fwmark}"
[ "${iptables}" = "$IPV" ] && log Info "proxy-mode: ${proxy_mode}, package ${packages_list[*]} transparent proxy."
[ "${iptables}" = "$IPV" ] && log Info "proxy mode: ${proxy_mode} (${packages_list[*]}) transparent proxy."
fi

if [ "${gid_list}" != "" ] ; then
for gid in ${gid_list[@]} ; do
[ ${network_mode} = "enhance" ] || ${iptables} -t mangle -A BOX_LOCAL -p tcp -m owner --gid-owner ${gid} -j MARK --set-mark "${fwmark}"
${iptables} -t mangle -A BOX_LOCAL -p udp -m owner --gid-owner ${gid} -j MARK --set-mark "${fwmark}"
done
[ "${iptables}" = "$IPV" ] && log Info "proxy mode: ${proxy_mode}, GID ${gid_list[*]} transparent proxy."
[ "${iptables}" = "$IPV" ] && log Info "proxy mode: ${proxy_mode}, GID (${gid_list[*]}) transparent proxy."
fi
;;
*)
log Debug "proxy-mode: ${proxy_mode} < error"
log Debug "proxy mode: ${proxy_mode} < error"
[ ${network_mode} = "enhance" ] || ${iptables} -t mangle -A BOX_LOCAL -p tcp -j MARK --set-mark "${fwmark}"
${iptables} -t mangle -A BOX_LOCAL -p udp -j MARK --set-mark "${fwmark}"
[ "${iptables}" = "$IPV" ] && log Info "transparent proxy for all apps."
Expand Down Expand Up @@ -873,6 +873,9 @@ else
probe_tun_device || log Error "tun device: (${tun_device}) not found"
[ $1 = "renew" ] && log Warning "Cleaning up tun rules."
iptables="$IPV"

[ -n "${packages_list}" ] && log Debug "proxy mode: $proxy_mode (${packages_list[*]})"

if forward -I; then
log Info "Create iptables tun rules done."
else
Expand Down
6 changes: 3 additions & 3 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" ] && local mode="exclude" || mode="include"
[[ ${proxy_mode} = "blacklist" || ${proxy_mode} = "black" ]] && local 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 @@ -330,9 +330,9 @@ prepare_clash() {
# add exclude-package/include-package for tun
package=$(IFS=","; echo "${packages_list[*]}" | tr ' ' ',')
list_package="${package:-}"
if [ "${proxy_mode}" = "whitelist" ]; then
if [ "${proxy_mode}" = "whitelist" ] || [ "${proxy_mode}" = "white" ]; then
mode="include-package"
elif [ "${proxy_mode}" = "blacklist" ]; then
elif [ "${proxy_mode}" = "blacklist" ] || [ "${proxy_mode}" = "black" ]; then
mode="exclude-package"
fi
sed -i "s/exclude-package:.*/exclude-package: []/g" "${clash_config}"
Expand Down

0 comments on commit 4042065

Please sign in to comment.