|
| 1 | +#!/system/bin/sh |
| 2 | + |
| 3 | +MODPATH="${0%/*}" |
| 4 | +LOGFILE="$MODPATH/service.log" |
| 5 | +FLAGFILE="/dev/.tcp_module_log_cleared" |
| 6 | +MAX_LOG_LINES=200 |
| 7 | +DEBOUNCE_TIME=10 |
| 8 | + |
| 9 | +# Clear log on first run after boot |
| 10 | +if [ ! -f "$FLAGFILE" ]; then |
| 11 | + rm -f "$LOGFILE" >/dev/null 2>&1 |
| 12 | + touch "$FLAGFILE" >/dev/null 2>&1 |
| 13 | +fi |
| 14 | + |
| 15 | +# On startup, reset description to default |
| 16 | +if [ -f "$MODPATH/module.prop" ]; then |
| 17 | + sed -i 's|^description=.*|description=TCP Optimisations & update tcp_cong_algo based on interface|' "$MODPATH/module.prop" |
| 18 | +fi |
| 19 | + |
| 20 | +log_print() { |
| 21 | + local timestamp |
| 22 | + timestamp=$(date +'%Y-%m-%d %H:%M:%S') |
| 23 | + echo "$timestamp - $1" >> "$LOGFILE" |
| 24 | + |
| 25 | + line_count=$(wc -l < "$LOGFILE" 2>/dev/null | awk '{print $1}') |
| 26 | + if [ "$line_count" -gt "$MAX_LOG_LINES" ]; then |
| 27 | + tail -n "$((MAX_LOG_LINES / 2))" "$LOGFILE" > "${LOGFILE}.tmp" |
| 28 | + mv "${LOGFILE}.tmp" "$LOGFILE" |
| 29 | + fi |
| 30 | +} |
| 31 | + |
| 32 | +# Run commands as root using su -c |
| 33 | +run_as_root() { |
| 34 | + if [ "$(id -u)" -eq 0 ]; then |
| 35 | + sh -c "$1" |
| 36 | + else |
| 37 | + su -c "$1" |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +update_description() { |
| 43 | + local iface="$1" |
| 44 | + local algo="$2" |
| 45 | + local icon="🌐" |
| 46 | + |
| 47 | + case "$iface" in |
| 48 | + wlan) icon="🛜" ;; |
| 49 | + mobile) icon="📶" ;; |
| 50 | + esac |
| 51 | + |
| 52 | + local desc="TCP Optimisations & update tcp_cong_algo based on interface | iface: $iface $icon | algo: $algo" |
| 53 | + run_as_root "sed -i \"s/^description=.*/description=$desc/\" \"$MODPATH/module.prop\"" |
| 54 | +} |
| 55 | + |
| 56 | +kill_tcp_connections() { |
| 57 | + if [ -f "$MODPATH/kill_connections" ]; then |
| 58 | + log_print "Killing all TCP connections (IPv4 and IPv6) due to congestion change" |
| 59 | + |
| 60 | + # Kill all IPv4 connections (destination 0.0.0.0/0) |
| 61 | + run_as_root "ss -K dst 0.0.0.0/0" # Kill all IPv4 connections |
| 62 | + |
| 63 | + # Kill all IPv6 connections (destination ::/0) |
| 64 | + run_as_root "ss -K dst ::/0" # Kill all IPv6 connections |
| 65 | + fi |
| 66 | +} |
| 67 | + |
| 68 | +set_congestion() { |
| 69 | + local algo="$1" |
| 70 | + local mode="$2" |
| 71 | + if grep -qw "$algo" /proc/sys/net/ipv4/tcp_available_congestion_control; then |
| 72 | + run_as_root "echo \"$algo\" > /proc/sys/net/ipv4/tcp_congestion_control 2>/dev/null" |
| 73 | + log_print "Applied congestion control: $algo ($mode)" |
| 74 | + kill_tcp_connections |
| 75 | + update_description "$mode" "$algo" |
| 76 | + else |
| 77 | + log_print "Unavailable algorithm: $algo" |
| 78 | + fi |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +get_active_iface() { |
| 83 | + iface=$(ip route get 192.0.2.1 2>/dev/null | awk '/dev/ {for(i=1;i<=NF;i++) if($i=="dev") print $(i+1)}') |
| 84 | + echo "$iface" |
| 85 | +} |
| 86 | + |
| 87 | +last_mode="" |
| 88 | +change_time=0 |
| 89 | + |
| 90 | +while true; do |
| 91 | + iface=$(get_active_iface) |
| 92 | + |
| 93 | + new_mode="none" |
| 94 | + case "$iface" in |
| 95 | + wlan*) new_mode="wifi" ;; |
| 96 | + rmnet_data*) new_mode="mobile" ;; |
| 97 | + *) new_mode="none" ;; |
| 98 | + esac |
| 99 | + |
| 100 | + current_time=$(date +%s) |
| 101 | + |
| 102 | + if [ "$new_mode" != "$last_mode" ]; then |
| 103 | + if [ "$((current_time - change_time))" -ge "$DEBOUNCE_TIME" ]; then |
| 104 | + applied=0 |
| 105 | + if [ "$new_mode" = "wifi" ]; then |
| 106 | + for algo in bbr reno cubic; do |
| 107 | + if [ -f "$MODPATH/wlan_$algo" ]; then |
| 108 | + set_congestion "$algo" "$new_mode" |
| 109 | + applied=1 |
| 110 | + break |
| 111 | + fi |
| 112 | + done |
| 113 | + [ "$applied" -eq 0 ] && set_congestion cubic "$new_mode" |
| 114 | + elif [ "$new_mode" = "mobile" ]; then |
| 115 | + for algo in bbr reno cubic; do |
| 116 | + if [ -f "$MODPATH/rmnet_data_$algo" ]; then |
| 117 | + set_congestion "$algo" "$new_mode" |
| 118 | + applied=1 |
| 119 | + break |
| 120 | + fi |
| 121 | + done |
| 122 | + [ "$applied" -eq 0 ] && set_congestion cubic "$new_mode" |
| 123 | + fi |
| 124 | + last_mode="$new_mode" |
| 125 | + change_time="$current_time" |
| 126 | + fi |
| 127 | + fi |
| 128 | + |
| 129 | + sleep 5 |
| 130 | +done |
0 commit comments