Skip to content

Commit

Permalink
refined bfr scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
twnesss committed Dec 4, 2023
1 parent 13f9802 commit 3e5d800
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 74 deletions.
10 changes: 7 additions & 3 deletions box/scripts/box.iptables
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ misc_info() {
log Info "tun_device: $tun_device"
fi
if [ -n "${fake_ip_range}" ] && [ "${bin_name}" = "sing-box" ]; then
log Debug "fakeip inet(v4/v6)_range: ${fake_ip_range}, ${fake_ip6_range}"
log Debug "fake-ip-range: ${fake_ip_range}, ${fake_ip6_range}"
fi
;;
*)
Expand All @@ -65,9 +65,13 @@ misc_info() {

sync_port() {
if [[ "${network_mode}" == "tproxy" && "${proxy_mode}" != "tun" ]]; then
netstat -tnulp | grep -q "${tproxy_port}" || log Warning "tproxy_port: ${tproxy_port} out of sync with config"
if command -v netstat &> /dev/null; then
netstat -tnulp | grep -q "${tproxy_port}" || log Warning "tproxy_port: ${tproxy_port} out of sync with config"
fi
elif [[ "${network_mode}" == "mixed" || "${proxy_mode}" == "tun" ]]; then
ifconfig | grep -q "${tun_device}" || log Warning "tun_device: '${tun_device}' not found"
if command -v ifconfig &> /dev/null; then
ifconfig | grep -q "${tun_device}" || log Warning "tun_device: '${tun_device}' not found"
fi
fi
}

Expand Down
20 changes: 5 additions & 15 deletions box/scripts/box.service
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,6 @@ box_cgroup() {
log Warning "cgroups ${cgroup_attr} is turned off"
sed -i -E "/cgroup_${cgroup_attr}/ s/(true)/false/" "${settings}"
fi
else
log Info "${bin_name} cgroup ${cgroup_attr}: ${cgroup_value}"
fi
}
set_cgroup_config "memcg" "${cgroup_memcg}"
Expand All @@ -432,7 +430,7 @@ box_bin_status() {
fi

log Info "${bin_name} service is running."
log Info "ProxyMode: ${proxy_mode}, $(if [ "${proxy_mode}" != "tun" ]; then echo NetworkMode: ${network_mode}; fi)"
log Info "Proxy-mode: ${proxy_mode}, $(if [ "${proxy_mode}" != "tun" ]; then echo Network-mode: ${network_mode}; fi)"

# Get the memory usage of the binary
rss=$(grep VmRSS /proc/$PID/status | busybox awk '{ print $2 }')
Expand Down Expand Up @@ -508,14 +506,14 @@ start_box() {
echo -e "${yellow}$(getprop persist.sys.timezone)${normal}"
echo -e "${yellow}$(getprop gsm.sim.operator.alpha) / $(getprop gsm.network.type)${normal}"
echo -e "${yellow}$(date)${normal}"
echo -e "${yellow}${box_version}${normal}"
echo -e "${yellow}${box_version}, $(getprop ro.product.cpu.abi)${normal}"
echo -e "${white}--------------------------------------------${normal}"
else
{
echo "$(getprop persist.sys.timezone)"
echo "$(getprop gsm.sim.operator.alpha) / $(getprop gsm.network.type)"
echo "$(date)"
echo "${box_version}"
echo "${box_version}, $(getprop ro.product.cpu.abi)"
echo "--------------------------------------------"
} | tee -a "${box_log}" > /dev/null 2>&1
fi
Expand Down Expand Up @@ -578,12 +576,6 @@ start_box() {
# Execute the box_cgroup, box_run_bin, box_detected_port, box_bin_alive,box_bin_status functions
box_run_bin
box_cgroup
# $bin_name detected port
if [ "${port_detect}" = "true" ]; then
${scripts_dir}/box.tool port
else
log Info "${bin_name} skipped port detection."
fi

count=0
while [ $count -le 10 ]; do
Expand All @@ -592,9 +584,7 @@ start_box() {
count=$((count + 1))
done
box_bin_status

# open the yacd in browser
open_dashboard
true
}

stop_box() {
Expand Down Expand Up @@ -662,7 +652,7 @@ force_stop() {

# Check whether busybox is installed or not on the system using command -v
if ! command -v busybox &> /dev/null; then
log Error "busybox command not found."
log Error "$(which busybox) command not found."
exit 1
fi

Expand Down
43 changes: 4 additions & 39 deletions box/scripts/box.tool
Original file line number Diff line number Diff line change
Expand Up @@ -521,36 +521,6 @@ upyacd() {
fi
}

# Function for detecting ports used by a process
port_detection() {
sleep 1
# Use 'command' function to check availability of 'ss'
if command -v ss > /dev/null ; then
# Use 'awk' with a regular expression to match the process ID
ports=$(ss -antup | busybox awk -v PID="$(busybox pidof "${bin_name}")" '$7 ~ PID {print $5}' | busybox awk -F ':' '{print $2}' | sort -u) >/dev/null 2>&1
# Make a note of the detected ports
if busybox pidof "${bin_name}" >/dev/null 2>&1; then
if [ -t 1 ]; then
echo -n "${orange}${current_time} [Debug]: ${bin_name} port detected:${normal}"
else
echo -n "${current_time} [Debug]: ${bin_name} port detected:" | tee -a "${box_log}" >> /dev/null 2>&1
fi
# write ports
while read -r port; do
sleep 0.5
[ -t 1 ] && (echo -n "${red}${port} $normal") || (echo -n "${port} " | tee -a "${box_log}" >> /dev/null 2>&1)
done <<< "${ports}"
# Add a newline to the output if running in terminal
[ -t 1 ] && echo -e "\033[1;31m""\033[0m" || echo "" >> "${box_log}" 2>&1
else
return 1
fi
else
log Debug "ss command not found, skipping port detection." >&2
return 1
fi
}

# Function to limit cgroup memcg
cgroup_blkio() {
# Check if the cgroup blkio path is set and exists.
Expand Down Expand Up @@ -660,6 +630,9 @@ cgroup_cpuset() {
return 0
}

ip_port=$(if [ "${bin_name}" = "clash" ]; then busybox awk '/external-controller:/ {print $2}' "${clash_config}"; else find /data/adb/box/sing-box/ -maxdepth 1 -type f -name "*.json" -exec busybox awk -F':' '/experimental/,/\}/' {} \; | sed -n 's/.*"external_controller": "\(.*\)",/\1/p'; fi;)
secret=""

case "$1" in
check)
check
Expand Down Expand Up @@ -687,7 +660,6 @@ case "$1" in
if [ -f "${box_pid}" ]; then
if kill -0 "$(<"${box_pid}" 2>/dev/null)"; then
reload
open_dashboard
fi
fi
;;
Expand All @@ -701,7 +673,6 @@ case "$1" in
if [ -f "${box_pid}" ]; then
if kill -0 "$(<"${box_pid}" 2>/dev/null)"; then
reload
open_dashboard
fi
fi
;;
Expand All @@ -710,16 +681,10 @@ case "$1" in
;;
upyacd)
upyacd
if kill -0 "$(<"${box_pid}" 2>/dev/null)"; then
open_dashboard
fi
;;
upyq|upcurl)
$1
;;
port)
port_detection
;;
reload)
reload
;;
Expand All @@ -735,6 +700,6 @@ case "$1" in
;;
*)
echo "${red}$0 $1 no found${normal}"
echo "${yellow}usage${normal}: ${green}$0${normal} {${yellow}check|memcg|cpuset|blkio|geosub|geox|subs|upkernel|upyacd|upyq|upcurl|port|reload|all${normal}}"
echo "${yellow}usage${normal}: ${green}$0${normal} {${yellow}check|memcg|cpuset|blkio|geosub|geox|subs|upkernel|upyacd|upyq|upcurl|reload|all${normal}}"
;;
esac
18 changes: 1 addition & 17 deletions box/settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,4 @@ log() {
# Print messages to a log file
echo "${message}" >> ${box_log} 2>&1
fi
}

# Open yacd on start
# "cmd: Failure calling service activity: Failed transaction" if run through Magisk/KernelSu Manager,
# this is normal if run in terminal/termux
open_dashboard_on_start="false"
ip_port=$(if [ "${bin_name}" = "clash" ]; then busybox awk '/external-controller:/ {print $2}' "${clash_config}"; else find /data/adb/box/sing-box/ -maxdepth 1 -type f -name "*.json" -exec busybox awk -F':' '/experimental/,/\}/' {} \; | sed -n 's/.*"external_controller": "\(.*\)",/\1/p'; fi;)
secret=""
open_dashboard() {
if [[ "${bin_name}" == @(clash|sing-box) ]]; then
if [ "${open_dashboard_on_start}" = "true" ]; then
if [ -t 1 ]; then
su -c am start -a android.intent.action.VIEW -d "http://${ip_port}/ui/#/proxies"
fi
fi
fi
}
}

0 comments on commit 3e5d800

Please sign in to comment.