#!/bin/sh /etc/rc.common # Copyright (C) 2018 Lean # Copyright (C) 2019-2021 Tianling Shen START=90 STOP=10 EXTRA_COMMANDS="check_status" EXTRA_HELP=" check_status Check running status of utils" restart_utils="true" PS="/bin/busybox ps" inital_conf(){ config_load "turboacc" config_get "sw_flow" "config" "sw_flow" "0" config_get "hw_flow" "config" "hw_flow" "0" config_get "sfe_flow" "config" "sfe_flow" "0" config_get "bbr_cca" "config" "bbr_cca" "0" config_get "fullcone_nat" "config" "fullcone_nat" "0" config_get "dns_caching" "config" "dns_caching" "0" config_get "dns_caching_mode" "config" "dns_caching_mode" "0" config_get "dns_caching_dns" "config" "dns_caching_dns" [ ! -e "/lib/modules/$(uname -r)/xt_FLOWOFFLOAD.ko" ] && { sw_flow="0"; hw_flow="0"; } [ ! -e "/lib/modules/$(uname -r)/shortcut-fe-cm.ko" ] && sfe_flow="0" [ ! -e "/lib/modules/$(uname -r)/tcp_bbr.ko" ] && bbr_cca="0" [ ! -e "/lib/modules/$(uname -r)/xt_FULLCONENAT.ko" ] && fullcone_nat="0" } start_pdnsd() { [ -d "/var/run/dnscache" ] || mkdir -p "/var/run/dnscache" cat > "/var/run/dnscache/dnscache.conf" < "/var/dnscache/pdnsd.cache" chown -R nobody.nogroup "/var/dnscache" fi [ -d "/var/sbin" ] || mkdir -p "/var/sbin" cp -a "/usr/sbin/pdnsd" "/var/sbin/dnscache" /var/sbin/dnscache -c "/var/run/dnscache/dnscache.conf" > "/var/log/dnscache.file" 2>&1 & echo "PDNSD: Start DNS Caching" } start_dnsforwarder() { mkdir -p "/var/run/dnscache" cat > "/var/run/dnscache/dnscache.conf" < "/var/log/dnscache.file" 2>&1 & echo "DnsForwarder: Start DNS Caching" } start_dnsproxy() { [ -d "/var/run/dnscache" ] || mkdir -p "/var/run/dnscache" echo -e "${dns_caching_dns//,/\\n}" > "/var/run/dnscache/dnscache.conf" [ -d "/var/sbin" ] || mkdir -p "/var/sbin" cp -a "/usr/bin/dnsproxy" "/var/sbin/dnscache" /var/sbin/dnscache -l "127.0.0.1" -p "5333" -b "tls://9.9.9.9" -f "tls://8.8.8.8" -u "/var/run/dnscache/dnscache.conf" --all-servers --cache --cache-min-ttl=3600 > "/var/log/dnscache.file" 2>&1 & echo "DNSProxy: Start DNS Caching" } stop_dnscache() { $PS -w | grep dnscache | grep -v "grep" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 & $PS -w | grep dnscache-while.sh | grep -v "grep" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 & killall -q -9 dnscache rm -rf "/var/dnscache" "/var/run/dnscache" echo "Stop DNS Caching" } change_dns() { uci -q delete dhcp.@dnsmasq[0].server uci add_list dhcp.@dnsmasq[0].server="127.0.0.1#5333" uci set dhcp.@dnsmasq[0].noresolv="1" uci commit dhcp } revert_dns() { uci -q del_list dhcp.@dnsmasq[0].server="127.0.0.1#5333" uci set dhcp.@dnsmasq[0].resolvfile="/tmp/resolv.conf.d/resolv.conf.auto" uci set dhcp.@dnsmasq[0].noresolv="0" uci commit dhcp } start(){ inital_conf uci set firewall.@defaults[0].flow_offloading="${sw_flow}" uci set firewall.@defaults[0].flow_offloading_hw="${hw_flow}" uci set firewall.@defaults[0].fullcone="${fullcone_nat}" uci commit firewall [ "${sw_flow}" -ne "1" ] && { [ "${sfe_flow}" -eq "1" ] && { [ "$(have_ecm_init)" = "0" ] && { /etc/init.d/shortcut-fe enabled || /etc/init.d/shortcut-fe enable /etc/init.d/shortcut-fe start } } [ "${sfe_flow}" -eq "0" ] && [ -e "/etc/init.d/shortcut-fe" ] && [ "$(have_ecm_init)" = "0" ] && \ /etc/init.d/shortcut-fe enabled && { /etc/init.d/shortcut-fe stop 2>"/dev/null" /etc/init.d/shortcut-fe disable } } if [ "${bbr_cca}" -eq "1" ]; then sysctl -w net.ipv4.tcp_congestion_control="bbr" else sysctl -w net.ipv4.tcp_congestion_control="cubic" fi if [ "${dns_caching}" -eq "1" ]; then stop_dnscache sleep 1 rm -f "/var/log/dnscache.file" if [ "${dns_caching_mode}" = "1" ]; then start_pdnsd elif [ "${dns_caching_mode}" = "2" ]; then start_dnsforwarder elif [ "${dns_caching_mode}" = "3" ]; then start_dnsproxy fi change_dns /usr/share/dnscache/dnscache-while.sh > "/var/log/dnscache.file" 2>&1 & else stop_dnscache revert_dns fi if [ "${restart_utils}" = "true" ]; then /etc/init.d/dnsmasq restart >"/dev/null" 2>&1 && echo "DNSMASQ change" /etc/init.d/firewall restart >"/dev/null" 2>&1 fi } stop(){ inital_conf uci set firewall.@defaults[0].flow_offloading="${sw_flow}" uci set firewall.@defaults[0].flow_offloading_hw="${hw_flow}" uci set firewall.@defaults[0].fullcone="${fullcone_nat}" uci commit firewall [ "${sfe_flow}" -ne "1" ] && { [ -e "/etc/init.d/shortcut-fe" ] && [ "$(have_ecm_init)" = "0" ] && { /etc/init.d/shortcut-fe stop 2>"/dev/null" /etc/init.d/shortcut-fe enabled && /etc/init.d/shortcut-fe disable } } stop_dnscache revert_dns if [ "${restart_utils}" = "true" ]; then /etc/init.d/dnsmasq restart >"/dev/null" 2>&1 && echo "DNSMASQ revert" /etc/init.d/firewall restart >"/dev/null" 2>&1 fi } restart(){ restart_utils="false" stop start /etc/init.d/dnsmasq restart >"/dev/null" 2>&1 && echo "DNSMASQ restart" /etc/init.d/firewall restart >"/dev/null" 2>&1 } have_ecm_init() { [ -e "/etc/init.d/qca-nss-ecm" ] && echo 1 && return echo 0 } ecm_mode(){ config_load "ecm" config_get front_end global acceleration_engine case $front_end in auto | nss | sfe | hybrid) [ -d /sys/kernel/debug/ecm/ecm_nss_ipv4 ] && echo 'NSS: Enabled ' || echo 'NSS: Disabled ' [ -d /sys/kernel/debug/ecm/ecm_sfe_ipv4 ] && echo 'SFE: Enabled' || echo 'SFE: Disabled' ;; *) echo 'Unknown' esac } check_status(){ case "$1" in "fastpath") if [ "$(cat "/sys/module/xt_FLOWOFFLOAD/refcnt" 2>"/dev/null" || echo 0)" -ne "0" ]; then echo -n "Flow Offloading" exit 0 elif lsmod | grep -q "ecm"; then echo -n "QCA-ECM Engine: "$(ecm_mode) exit 0 elif lsmod | grep -q "shortcut_fe_cm"; then echo -n "Shortcut-FE" exit 0 else exit 1 fi ;; "fullconenat") [ "$(cat "/sys/module/xt_FULLCONENAT/refcnt" 2>"/dev/null" || echo 0)" -ne "0" ] && \ exit 0 || exit 1 ;; "bbr") [ "x$(cat "/proc/sys/net/ipv4/tcp_congestion_control" 2>"/dev/null")" = "xbbr" ] && \ exit 0 || exit 1 ;; "dns") pgrep "dnscache" >"/dev/null" && exit 0 || exit 1 ;; *) exit 2 ;; esac }