Skip to content

Commit 41f10f8

Browse files
committed
Add service.sh and customise.sh
1. Create Base files during installations and check and ignore during update. 2. Check interface and update TCP congestion algorithm. 3. Add debounce logic for quick toggles. 4. Check for change every 5s. 5. Add logging during change.
1 parent 0b28f2f commit 41f10f8

File tree

3 files changed

+198
-5
lines changed

3 files changed

+198
-5
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ In certain kernel, TCP Congestion Algorithm BBR might be enabled. Or you want to
1010
3. Reboot device.
1111

1212
## Note:
13-
1. Default algorithm is **cubic** for **cellular**.
14-
2. Default algorithm is **brr** if exists for **WiFi**. Else **cubic**.
15-
3. You can change algorithm by just renaming the file in same format. Eg: If you want to change WiFi TCP congestion algorithm to **reno**, rename `wlan_{algo}` file to `wlan_reno`.
16-
4. There is an option to kill current tcp connections during algorithm change. This might stop downloads, uploads or other ongoing connections. So apps affected might need to be restarted. To enable create a file named `kill_connections` in module folder. This is disabled by default.
17-
5. Algorithm is applied only if present in kernel.
13+
1. `{algo}` in filename can be any TCP congestion algorithm (cubic, bbr, reno etc..).
14+
2. Default algorithm is **cubic** for **cellular**.
15+
3. Default algorithm is **bbr** if exists for **WiFi**. Else **cubic**.
16+
4. You can change algorithm by just renaming the file in same format. Eg: If you want to change WiFi TCP congestion algorithm to **reno**, rename `wlan_{algo}` file to `wlan_reno`.
17+
5. There is an option to kill current tcp connections during algorithm change. This might stop downloads, uploads or other ongoing connections. So apps affected might need to be restarted. To enable create a file named `kill_connections` in module folder. This is disabled by default.
18+
6. Algorithm is applied only if present in kernel.
19+
7. Module logs are present in `/data/adb/modules/tcp_optimiser/service.log`.

module/customize.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/system/bin/sh
2+
3+
ui_print "- Starting module customization..."
4+
5+
# Detect congestion algorithm
6+
ui_print "- Checking TCP congestion algorithm..."
7+
if grep -qw bbr /proc/sys/net/ipv4/tcp_available_congestion_control; then
8+
CONG="bbr"
9+
ui_print "- Found BBR!"
10+
else
11+
CONG="cubic"
12+
ui_print "- BBR not found. Going with Cubic!"
13+
fi
14+
15+
MODULE_NAME=$(basename "$MODPATH")
16+
MODULE_PATH="/data/adb/modules/$MODULE_NAME"
17+
18+
# Check both live and update folders
19+
check_exists_anywhere() {
20+
local prefix="$1"
21+
22+
# Check in current path
23+
if ls "$MODPATH"/${prefix}_* >/dev/null 2>&1; then
24+
return 0
25+
fi
26+
27+
# Check in Module main path
28+
if ls "$MODULE_PATH"/${prefix}_* >/dev/null 2>&1; then
29+
return 0
30+
fi
31+
32+
return 1
33+
}
34+
35+
create_file_if_needed() {
36+
local prefix="$1"
37+
local suffix="$2"
38+
local target="$MODPATH/${prefix}_${suffix}"
39+
40+
if check_exists_anywhere "$prefix"; then
41+
ui_print "- Skipping $target: another ${prefix}_* file exists."
42+
return
43+
fi
44+
45+
if [ ! -f "$target" ]; then
46+
touch "$target"
47+
ui_print "- Created: $target"
48+
else
49+
ui_print "- Skipped: $target already exists"
50+
fi
51+
}
52+
53+
# Create wlan_* based on BBR availability
54+
if [ "$CONG" = "bbr" ]; then
55+
create_file_if_needed "wlan" "bbr"
56+
else
57+
create_file_if_needed "wlan" "cubic"
58+
fi
59+
60+
# Always create rmnet_data_cubic unless another exists
61+
create_file_if_needed "rmnet_data" "cubic"

module/service.sh

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)