-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcustomize.sh
More file actions
328 lines (269 loc) · 8.79 KB
/
customize.sh
File metadata and controls
328 lines (269 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/system/bin/sh
SKIPMOUNT=false
PROPFILE=true
POSTFSDATA=false
LATESTARTSERVICE=false
STEVENBLOCK_DIR="/data/adb/stevenblock"
STEVENBLOCK_CONFIG="$STEVENBLOCK_DIR/config.txt"
HOSTS_TARGET="$MODPATH/system/etc/hosts"
HOSTS_BACKUP="$STEVENBLOCK_DIR/hosts.bak"
HOSTS_TEMP="$STEVENBLOCK_DIR/hosts.tmp"
LIST_1_NAME="Default"
LIST_1_URL="https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
LIST_1_REPO="StevenBlack/hosts"
LIST_1_BRANCH="master"
LIST_2_NAME="Light"
LIST_2_URL="https://raw.githubusercontent.com/mikropsoft/StevenBlock/main/hosts/energized_spark_hosts"
LIST_2_REPO="mikropsoft/StevenBlock"
LIST_2_BRANCH="main"
LIST_3_NAME="Medium"
LIST_3_URL="https://raw.githubusercontent.com/mikropsoft/StevenBlock/main/hosts/energized_blu_hosts"
LIST_3_REPO="mikropsoft/StevenBlock"
LIST_3_BRANCH="main"
LIST_4_NAME="Aggressive"
LIST_4_URL="https://raw.githubusercontent.com/mikropsoft/StevenBlock/main/hosts/energized_ultimate_hosts"
LIST_4_REPO="mikropsoft/StevenBlock"
LIST_4_BRANCH="main"
print_banner() {
ui_print ""
ui_print "🛡️ StevenBlock"
ui_print ""
ui_print "👨💻 Developer : mikropsoft"
ui_print "🌐 GitHub : github.com/mikropsoft/StevenBlock"
ui_print "📢 Telegram : t.me/stevenblockmodule"
ui_print "☕ Coffee : buymeacoffee.com/mikropsoft"
ui_print ""
}
get_last_commit_date() {
local repo="$1"
local branch="$2"
local api_url="https://api.github.com/repos/${repo}/commits?sha=${branch}&per_page=1"
local date_str=""
if command -v curl >/dev/null 2>&1; then
date_str=$(curl -sL --connect-timeout 10 --max-time 15 "$api_url" 2>/dev/null | grep '"date"' | head -1 | sed 's/.*"date"[[:space:]]*:[[:space:]]*"//;s/".*//')
elif command -v wget >/dev/null 2>&1; then
date_str=$(wget -qO- --timeout=15 "$api_url" 2>/dev/null | grep '"date"' | head -1 | sed 's/.*"date"[[:space:]]*:[[:space:]]*"//;s/".*//')
fi
if [ -n "$date_str" ]; then
echo "$date_str" | cut -c1-10
else
echo "❓ Unknown"
fi
}
download_hosts() {
local url="$1"
local output="$2"
local success=0
if command -v curl >/dev/null 2>&1; then
curl -sLf --connect-timeout 15 --max-time 120 --retry 3 --retry-delay 2 -o "$output" "$url" 2>/dev/null && success=1
fi
if [ "$success" -ne 1 ] && command -v wget >/dev/null 2>&1; then
wget -qO "$output" --timeout=120 --tries=3 "$url" 2>/dev/null && success=1
fi
if [ "$success" -ne 1 ]; then
for p in /data/adb/magisk/busybox /data/adb/ksu/bin/busybox /data/adb/ap/bin/busybox; do
if [ -x "$p" ]; then
$p wget -qO "$output" -T 120 "$url" 2>/dev/null && success=1
[ "$success" -eq 1 ] && break
fi
done
fi
return $((1 - success))
}
clean_hosts_file() {
local input="$1"
local output="$2"
printf "127.0.0.1 localhost\n::1 localhost\n" > "$output"
sed -e '/^[[:space:]]*#/d' \
-e '/^[[:space:]]*$/d' \
-e '/^[[:space:]]*::1/d' \
-e '/^[[:space:]]*127\.0\.0\.1[[:space:]]*localhost[[:space:]]*$/d' \
-e '/^[[:space:]]*0\.0\.0\.0[[:space:]]*$/d' \
-e 's/#.*$//' \
-e 's/[[:space:]]*$//' \
-e '/^[[:space:]]*$/d' \
-e 's/^127\.0\.0\.1/0.0.0.0/' \
"$input" >> "$output"
sort -u "$output" -o "$output" 2>/dev/null || {
local temp_sort="${output}.sort"
if sort -u "$output" > "$temp_sort" 2>/dev/null; then
mv "$temp_sort" "$output"
fi
}
}
flush_dns_cache() {
ndc resolver flushdefaultif >/dev/null 2>&1
ndc resolver clearnetdns >/dev/null 2>&1
for svc in netd dns_tls mdnsd; do
if [ -n "$(getprop init.svc.$svc 2>/dev/null)" ]; then
setprop ctl.restart "$svc" 2>/dev/null
fi
done
}
set_hosts_permissions() {
local file="$1"
chown 0:0 "$file" 2>/dev/null
chmod 644 "$file" 2>/dev/null
chcon u:object_r:system_file:s0 "$file" 2>/dev/null
}
save_config() {
local list_num="$1"
local list_name="$2"
local list_url="$3"
mkdir -p "$STEVENBLOCK_DIR"
printf "ACTIVE_LIST=%s\nACTIVE_NAME=%s\nACTIVE_URL=%s\nLAST_UPDATE=%s\n" \
"$list_num" "$list_name" "$list_url" "$(date '+%Y-%m-%d %H:%M:%S')" > "$STEVENBLOCK_CONFIG"
chmod 644 "$STEVENBLOCK_CONFIG" 2>/dev/null
}
load_config() {
ACTIVE_LIST=""
ACTIVE_NAME=""
ACTIVE_URL=""
LAST_UPDATE=""
if [ -f "$STEVENBLOCK_CONFIG" ]; then
. "$STEVENBLOCK_CONFIG"
fi
}
install_hosts_list() {
local list_num="$1"
local list_name="$2"
local list_url="$3"
ui_print ""
ui_print "📥 Downloading $list_name list..."
mkdir -p "$STEVENBLOCK_DIR"
if [ -f "$HOSTS_TARGET" ]; then
cp -f "$HOSTS_TARGET" "$HOSTS_BACKUP" 2>/dev/null
fi
if ! download_hosts "$list_url" "$HOSTS_TEMP"; then
ui_print "❌ Download failed"
if [ -f "$HOSTS_BACKUP" ]; then
cp -f "$HOSTS_BACKUP" "$HOSTS_TARGET" 2>/dev/null
set_hosts_permissions "$HOSTS_TARGET"
ui_print "🔄 Restored previous hosts file"
fi
return 1
fi
local filesize
filesize=$(wc -c < "$HOSTS_TEMP" 2>/dev/null | tr -d ' ')
if [ -z "$filesize" ] || [ "$filesize" -lt 10 ]; then
ui_print "❌ Downloaded file is empty, corrupt, or invalid"
if [ -f "$HOSTS_BACKUP" ]; then
cp -f "$HOSTS_BACKUP" "$HOSTS_TARGET" 2>/dev/null
set_hosts_permissions "$HOSTS_TARGET"
ui_print "🔄 Restored previous hosts file"
fi
rm -f "$HOSTS_TEMP" 2>/dev/null
return 1
elif ! grep -q "0\.0\.0\.0" "$HOSTS_TEMP" 2>/dev/null && ! grep -q "127\.0\.0\.1" "$HOSTS_TEMP" 2>/dev/null; then
ui_print "❌ Downloaded file is empty, corrupt, or invalid"
if [ -f "$HOSTS_BACKUP" ]; then
cp -f "$HOSTS_BACKUP" "$HOSTS_TARGET" 2>/dev/null
set_hosts_permissions "$HOSTS_TARGET"
ui_print "🔄 Restored previous hosts file"
fi
rm -f "$HOSTS_TEMP" 2>/dev/null
return 1
fi
ui_print "🧹 Cleaning hosts file..."
local cleaned_file="${HOSTS_TEMP}.clean"
clean_hosts_file "$HOSTS_TEMP" "$cleaned_file"
mkdir -p "$(dirname "$HOSTS_TARGET")"
cp -f "$cleaned_file" "$HOSTS_TARGET"
set_hosts_permissions "$HOSTS_TARGET"
rm -f "$HOSTS_TEMP" "$cleaned_file" 2>/dev/null
local c1
c1=$(grep -c "^0\.0\.0\.0" "$HOSTS_TARGET" 2>/dev/null || echo "0")
local c2
c2=$(grep -c "^127\.0\.0\.1" "$HOSTS_TARGET" 2>/dev/null || echo "0")
local total_domains=$((c1 + c2))
save_config "$list_num" "$list_name" "$list_url"
ui_print "🔄 Flushing DNS cache..."
flush_dns_cache
ui_print ""
ui_print "✅ Done"
ui_print "🏷️ List : $list_name"
ui_print "🚫 Blocked : $total_domains domains"
ui_print ""
return 0
}
show_menu() {
ui_print "⏳ Fetching update dates..."
ui_print ""
local date1 date2 date3 date4
if ping -c 1 -W 2 api.github.com >/dev/null 2>&1; then
date1=$(get_last_commit_date "$LIST_1_REPO" "$LIST_1_BRANCH")
date2=$(get_last_commit_date "$LIST_2_REPO" "$LIST_2_BRANCH")
date3=$(get_last_commit_date "$LIST_3_REPO" "$LIST_3_BRANCH")
date4=$(get_last_commit_date "$LIST_4_REPO" "$LIST_4_BRANCH")
else
date1="❓ Unknown"
date2="❓ Unknown"
date3="❓ Unknown"
date4="❓ Unknown"
fi
ui_print "1️⃣ Default [$date1]"
ui_print "2️⃣ Light [$date2]"
ui_print "3️⃣ Medium [$date3]"
ui_print "4️⃣ Aggressive [$date4]"
ui_print "5️⃣ Force Update Current List"
ui_print ""
}
force_update() {
load_config
if [ -z "$ACTIVE_URL" ] || [ -z "$ACTIVE_NAME" ]; then
ui_print "⚠️ No active list, installing Default..."
install_hosts_list 1 "$LIST_1_NAME" "$LIST_1_URL"
return
fi
ui_print "🔄 Force updating $ACTIVE_NAME..."
install_hosts_list "$ACTIVE_LIST" "$ACTIVE_NAME" "$ACTIVE_URL"
}
handle_selection() {
local choice="$1"
case "$choice" in
1) install_hosts_list 1 "$LIST_1_NAME" "$LIST_1_URL" ;;
2) install_hosts_list 2 "$LIST_2_NAME" "$LIST_2_URL" ;;
3) install_hosts_list 3 "$LIST_3_NAME" "$LIST_3_URL" ;;
4) install_hosts_list 4 "$LIST_4_NAME" "$LIST_4_URL" ;;
5) force_update ;;
*)
ui_print "⚠️ Invalid selection, using Default"
install_hosts_list 1 "$LIST_1_NAME" "$LIST_1_URL"
;;
esac
}
wait_for_key() {
while true; do
local ev
ev=$(getevent -lc 1 2>/dev/null)
if echo "$ev" | grep -iE 'VOLUMEUP|0073' | grep -iE 'DOWN|00000001' >/dev/null 2>&1; then
echo "up"
return
elif echo "$ev" | grep -iE 'VOLUMEDOWN|0072' | grep -iE 'DOWN|00000001' >/dev/null 2>&1; then
echo "down"
return
fi
done
}
print_banner
show_menu
ui_print "🔊 Volume UP = Next | 🔉 Volume DOWN = Select"
ui_print ""
user_choice=1
while true; do
ui_print "👉 $user_choice"
local pressed
pressed=$(wait_for_key)
if [ "$pressed" = "up" ]; then
user_choice=$((user_choice % 5 + 1))
else
break
fi
done
ui_print ""
ui_print "🎯 Selected: $user_choice"
handle_selection "$user_choice"
ui_print "⚠️ Please REBOOT your device"
ui_print ""
ui_print "🛡️ StevenBlock installation finished"
ui_print ""