Skip to content

Commit 0d217c4

Browse files
committed
Add Webui and other features.
1. Add Webui to manage the module. 2. Add Set initcwnd & initrwnd value to max feature. 3. Fix old config not saved during update bug. 4. Fix minor bugs in script. 5. Refactor shell scripts. 6. Update Readme to provide more Information. 7. Fix versioning issue in build.yml.
1 parent 2a4e09e commit 0d217c4

24 files changed

+1250
-49
lines changed

.github/workflows/build.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ jobs:
3232
MINOR="${{ github.event.inputs.minor }}"
3333
3434
IFS='.' read -r CURRENT_MAJOR CURRENT_MINOR <<< "$VERSION"
35-
NEW_VERSION="$((CURRENT_MAJOR + MAJOR)).$((CURRENT_MINOR + MINOR))"
35+
NEW_MAJOR=$((CURRENT_MAJOR + MAJOR))
36+
if [ "$MAJOR" -gt 0 ]; then
37+
NEW_MINOR="$MINOR"
38+
else
39+
NEW_MINOR=$((CURRENT_MINOR + MINOR))
40+
fi
41+
NEW_VERSION="$NEW_MAJOR.$NEW_MINOR"
3642
NEW_VERSIONCODE=$((VERSION_CODE + 1))
3743
3844
echo "new_version=$NEW_VERSION" >> "$GITHUB_ENV"

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,30 @@ A Magisk/KernelSU module to change tcp congestion algorithm based on current act
44
# Why?
55
In certain kernel, TCP Congestion Algorithm BBR might be enabled. Or you want to enable certain algorithm or settings based on what interface you are using. I observed that in my kernel, when i use BBR with WiFi I get 50-60 Mbps more upload speed compared to cubic, but BBR gives bad upload speed in cellular. So I designed this module to switch based on active internet facing interface.
66

7+
# Features
8+
1. Set TCP Congestion Algorithm based on interface(Wi-Fi/Cellular).
9+
2. Auto Change TCP Congestion Algorithm on interface change.
10+
3. Set initcwnd and initrwnd value to max.
11+
712
# How to use
813
1. Install the module.
914
2. It creates 2 files `wlan_{algo}` and `rmnet_data_{algo}` in module folder.
1015
3. Reboot device.
16+
4. Basic Functionality of module must run normally on boot.
17+
18+
# Tuning Module by files [/data/adb/modules/tcp_optimiser]
19+
1. TCP Congestion Algorithm can be changed for given interface by editing `{algo}` part of file name. `wlan_{algo}` for Wi-Fi and `rmnet_data_{algo}` for Cellular.
20+
2. Create an empty file named `initcwnd_initrwnd` to set initcwnd and initrwnd value to max values.
21+
3. Create an empty file named `kill_connections` to kill all connections during switch. [Be carefull!]
22+
4. Create an empty file named `force_apply` to apply changes immediately.
23+
24+
# Tuning Module by WebUI
25+
All the module settings can be controlled using Module WebUI in KSU and APatch or KsuWebUIStandalone app for Magisk.
1126

1227
## Note:
1328
1. `{algo}` in filename can be any TCP congestion algorithm (cubic, bbr, reno etc..).
1429
2. Default algorithm is **cubic** for **cellular**.
1530
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`.
31+
4. There is an option to kill current tcp connections during algorithm change. This will stop downloads, uploads or other ongoing connections. So apps affected might need to be restarted. This is disabled by default.
32+
5. Algorithm is applied only if present in kernel.
33+
6. Module logs are present in `/data/adb/modules/tcp_optimiser/service.log`.

module/customize.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,18 @@ if check_exists_anywhere "kill"; then
8484
else
8585
ui_print " [-] Skipping $MODPATH/kill_connections: file already exists."
8686
fi
87-
fi
87+
fi
88+
89+
if check_exists_anywhere "initcwnd"; then
90+
# If file exists and KSU is true, copy any file from MODULEPATH with the same prefix
91+
if [ "$KSU" = true ]; then
92+
# Find any file starting with ${prefix}_ in MODULEPATH and copy it to MODPATH
93+
source_file=$(find "$MODULE_PATH" -name "initcwnd_initrwnd" -print -quit)
94+
if [ -n "$source_file" ]; then
95+
cp "$source_file" "$MODPATH/"
96+
ui_print " [+] Copied from $MODULE_PATH to $MODPATH: initcwnd_initrwnd"
97+
fi
98+
else
99+
ui_print " [-] Skipping $MODPATH/initcwnd_initrwnd: file already exists."
100+
fi
101+
fi

module/service.sh

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,72 @@
11
#!/system/bin/sh
22

33
MODPATH="${0%/*}"
4-
LOGFILE="$MODPATH/service.log"
5-
FLAGFILE="/dev/.tcp_module_log_cleared"
6-
MAX_LOG_LINES=200
74
DEBOUNCE_TIME=10
85

6+
. $MODPATH/utils.sh # Load utils
7+
98
# Get the list of available congestion control algorithms
109
congestion_algorithms=$(cat /proc/sys/net/ipv4/tcp_available_congestion_control)
1110

12-
# Clear log on first run after boot
13-
if [ ! -f "$FLAGFILE" ]; then
14-
rm -f "$LOGFILE" >/dev/null 2>&1
15-
touch "$FLAGFILE" >/dev/null 2>&1
16-
fi
17-
1811
# On startup, reset description to default
1912
if [ -f "$MODPATH/module.prop" ]; then
2013
default_desc="TCP Optimisations & update tcp_cong_algo based on interface"
2114
sed -i '/^description=/d' "$MODPATH/module.prop" && echo "description=$default_desc" >> "$MODPATH/module.prop"
2215
fi
2316

24-
log_print() {
25-
local timestamp
26-
timestamp=$(date +'%Y-%m-%d %H:%M:%S')
27-
echo "$timestamp - $1" >> "$LOGFILE"
28-
29-
line_count=$(wc -l < "$LOGFILE" 2>/dev/null | awk '{print $1}')
30-
if [ "$line_count" -gt "$MAX_LOG_LINES" ]; then
31-
tail -n "$((MAX_LOG_LINES / 2))" "$LOGFILE" > "${LOGFILE}.tmp"
32-
mv "${LOGFILE}.tmp" "$LOGFILE"
33-
fi
34-
}
35-
36-
# Run commands as root using su -c
37-
run_as_root() {
38-
if [ "$(id -u)" -eq 0 ]; then
39-
sh -c "$1"
40-
else
41-
su -c "$1"
42-
fi
43-
}
44-
4517
update_description() {
4618
local iface="$1"
4719
local algo="$2"
48-
local icon="🌐"
20+
local icon="⁉️"
4921

5022
case "$iface" in
5123
Wi-Fi) icon="🛜" ;;
5224
Cellular) icon="📶" ;;
5325
esac
5426

5527
local desc="TCP Optimisations & update tcp_cong_algo based on interface | iface: $iface $icon | algo: $algo"
56-
run_as_root "sed -i '/^description=/d' \"$MODPATH/module.prop\" && echo \"description=$desc\" >> \"$MODPATH/module.prop\""
28+
sed -i '/^description=/d' "$MODPATH/module.prop" && echo "description=$desc" >> "$MODPATH/module.prop"
5729
}
5830

5931
kill_tcp_connections() {
6032
if [ -f "$MODPATH/kill_connections" ]; then
6133
log_print "Killing all TCP connections (IPv4 and IPv6) due to congestion change"
6234

63-
# Kill all IPv4 connections (destination 0.0.0.0/0)
64-
run_as_root "ss -K dst 0.0.0.0/0" # Kill all IPv4 connections
65-
66-
# Kill all IPv6 connections (destination ::/0)
67-
run_as_root "ss -K dst ::/0" # Kill all IPv6 connections
35+
# Kill all connections
36+
ss -K
37+
fi
38+
}
39+
40+
set_max_initcwnd_initrwnd() {
41+
local active_iface="$1"
42+
if [ -f "$MODPATH/initcwnd_initrwnd" ]; then
43+
maxBufferSize=$(cat /proc/sys/net/ipv4/tcp_rmem | awk '{print $3}')
44+
mtu=$(ip link show "$active_iface" | awk '/mtu/ {print $NF}')
45+
mtu=$((mtu - 40))
46+
maxInitrwndValue=$((maxBufferSize / mtu))
47+
local applied
48+
applied=0
49+
50+
while IFS= read -r line; do
51+
run_as_su "/system/bin/ip route change $line initcwnd 10 initrwnd $maxInitrwndValue"
52+
if [ $? -eq 0 ]; then
53+
applied=1
54+
fi
55+
done <<EOF
56+
$(run_as_su "/system/bin/ip route show | grep \"dev $active_iface\"")
57+
EOF
58+
59+
if [ "$applied" -eq 1 ]; then
60+
log_print "Setting initcwnd = 10; initrwnd = $maxInitrwndValue!"
61+
fi
6862
fi
6963
}
7064

7165
set_congestion() {
7266
local algo="$1"
7367
local mode="$2"
74-
if grep -qw "$algo" /proc/sys/net/ipv4/tcp_available_congestion_control; then
75-
run_as_root "echo \"$algo\" > /proc/sys/net/ipv4/tcp_congestion_control 2>/dev/null"
68+
if echo "$congestion_algorithms" | grep -qw "$algo"; then
69+
echo "$algo" > /proc/sys/net/ipv4/tcp_congestion_control 2>/dev/null
7670
log_print "Applied congestion control: $algo ($mode)"
7771
kill_tcp_connections
7872
update_description "$mode" "$algo"
@@ -81,7 +75,6 @@ set_congestion() {
8175
fi
8276
}
8377

84-
8578
get_active_iface() {
8679
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)}')
8780
echo "$iface"
@@ -102,30 +95,33 @@ while true; do
10295

10396
current_time=$(date +%s)
10497

105-
if [ "$new_mode" != "$last_mode" ]; then
98+
if [ "$new_mode" != "$last_mode" ] || [ -f "$MODPATH/force_apply" ]; then
10699
if [ "$((current_time - change_time))" -ge "$DEBOUNCE_TIME" ]; then
107100
applied=0
108101
if [ "$new_mode" = "Wi-Fi" ]; then
109102
for algo in $congestion_algorithms; do
110103
if [ -f "$MODPATH/wlan_$algo" ]; then
111104
set_congestion "$algo" "$new_mode"
105+
set_max_initcwnd_initrwnd "$iface"
112106
applied=1
113107
break
114108
fi
115109
done
116-
[ "$applied" -eq 0 ] && set_congestion cubic "$new_mode"
110+
[ "$applied" -eq 0 ] && set_congestion cubic "$new_mode" && set_max_initcwnd_initrwnd "$iface"
117111
elif [ "$new_mode" = "Cellular" ]; then
118112
for algo in $congestion_algorithms; do
119113
if [ -f "$MODPATH/rmnet_data_$algo" ]; then
120114
set_congestion "$algo" "$new_mode"
115+
set_max_initcwnd_initrwnd "$iface"
121116
applied=1
122117
break
123118
fi
124119
done
125-
[ "$applied" -eq 0 ] && set_congestion cubic "$new_mode"
120+
[ "$applied" -eq 0 ] && set_congestion cubic "$new_mode" && set_max_initcwnd_initrwnd "$iface"
126121
fi
127122
last_mode="$new_mode"
128123
change_time="$current_time"
124+
rm -f "$MODPATH/force_apply"
129125
fi
130126
fi
131127

module/utils.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
8+
# Clear log on first run after boot
9+
if [ ! -f "$FLAGFILE" ]; then
10+
rm -f "$LOGFILE" >/dev/null 2>&1
11+
touch "$FLAGFILE" >/dev/null 2>&1
12+
fi
13+
14+
log_print() {
15+
message="$1"
16+
17+
timestamp=$(date +'%Y-%m-%d %H:%M:%S')
18+
echo "$timestamp - $message" >> "$LOGFILE"
19+
20+
line_count=$(wc -l < "$LOGFILE" 2>/dev/null)
21+
if [ "$line_count" -gt "$MAX_LOG_LINES" ]; then
22+
tail -n "$((MAX_LOG_LINES / 2))" "$LOGFILE" > "${LOGFILE}.tmp"
23+
mv "${LOGFILE}.tmp" "$LOGFILE"
24+
fi
25+
}
26+
27+
run_as_su() {
28+
local cmd="$*"
29+
su -c "$cmd"
30+
local status=$?
31+
return $status
32+
}

module/webroot/css/common.css

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
:root {
2+
--bg-color: #121212;
3+
--card-bg: #1e1e1e;
4+
--primary: #bb86fc;
5+
--text: #e1e1e1;
6+
--text-secondary: #a0a0a0;
7+
--border-radius: 12px;
8+
--spacing: 16px;
9+
--footer-height: 60px;
10+
}
11+
12+
* {
13+
margin: 0;
14+
padding: 0;
15+
box-sizing: border-box;
16+
}
17+
18+
body {
19+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
20+
background-color: var(--bg-color);
21+
color: var(--text);
22+
padding: var(--spacing);
23+
min-height: 100vh;
24+
padding-bottom: calc(var(--footer-height) + var(--spacing));
25+
position: relative;
26+
}
27+
28+
.container {
29+
max-width: 600px;
30+
margin: 0 auto;
31+
display: grid;
32+
gap: var(--spacing);
33+
}
34+
35+
.card {
36+
background-color: var(--card-bg);
37+
border-radius: var(--border-radius);
38+
padding: var(--spacing);
39+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
40+
}
41+
42+
.card-header {
43+
margin-bottom: 16px;
44+
}
45+
46+
.card-header h3 {
47+
font-size: 1.1rem;
48+
font-weight: 600;
49+
color: var(--text);
50+
}
51+
52+
.footer-nav {
53+
position: fixed;
54+
bottom: 0;
55+
left: 0;
56+
right: 0;
57+
height: var(--footer-height);
58+
background-color: var(--card-bg);
59+
display: flex;
60+
justify-content: space-around;
61+
align-items: center;
62+
box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.2);
63+
}
64+
65+
.nav-item {
66+
flex: 1;
67+
display: flex;
68+
flex-direction: column;
69+
align-items: center;
70+
justify-content: center;
71+
text-decoration: none;
72+
color: var(--text-secondary);
73+
height: 100%;
74+
transition: color 0.3s;
75+
}
76+
77+
.nav-item.active {
78+
color: var(--primary);
79+
}
80+
81+
.nav-icon {
82+
font-size: 1.4rem;
83+
margin-bottom: 4px;
84+
}
85+
86+
.nav-text {
87+
font-size: 0.8rem;
88+
}
89+
90+
.module-content {
91+
display: flex;
92+
flex-direction: column;
93+
align-items: center;
94+
text-align: center;
95+
gap: 0;
96+
}
97+
98+
.module-name {
99+
font-size: 1.25rem;
100+
font-weight: 600;
101+
}
102+
103+
.version {
104+
color: var(--text-secondary);
105+
font-size: 0.9rem;
106+
margin-top: 4px;
107+
margin-bottom: 8px;
108+
}
109+
110+
.link {
111+
color: var(--primary);
112+
text-decoration: none;
113+
padding: 2px 0;
114+
font-size: 0.8rem;
115+
}
116+
117+
.page {
118+
opacity: 0;
119+
transform: translateY(20px);
120+
transition: all 0.4s ease;
121+
}
122+
123+
.page.active {
124+
opacity: 1;
125+
transform: translateY(0);
126+
}

0 commit comments

Comments
 (0)