Skip to content

Commit b13a2e7

Browse files
committed
module_wide: introducing logging system
1 parent ed78543 commit b13a2e7

File tree

4 files changed

+49
-19
lines changed

4 files changed

+49
-19
lines changed

module/action.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ echo "
1313
╚────────────────────────────────────────╝
1414
"
1515
sleep 0.5
16-
echo "- Upgrading Defenses 🛡️"
17-
sleep 0.2
1816
echo "- Preparing New weapons 🔫"
1917

2018
sh "$MODDIR/system/bin/rmlwk" --update-hosts || abort "- Failed to update hosts."

module/common/install.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ else
152152
done > "$config_file"
153153
fi
154154

155+
mkdir /sdcard/Re-Malwack
156+
155157
# set permissions
156158
chmod 644 $MODPATH/system/etc/hosts
157159
chmod 755 $MODPATH/system/bin/rmlwk

module/service.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
#!/system/bin/sh
22

3+
# Logging function
4+
function log_message() {
5+
local message="$1"
6+
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" >> "/sdcard/Re-Malwack/logs.txt"
7+
}
38
# Variables
49
MODDIR="/data/adb/modules/Re-Malwack"
510
HOSTS_FILE="/system/etc/hosts"
6-
string="description=Status: Protection is enabled ✅ | Protection update date: $(date)"
11+
string="description=Status: Protection is enabled ✅ | Last updated: $(date)"
712

813
# Check if hosts file contains blocked entries
914
if grep -q '0.0.0.0' "$HOSTS_FILE"; then
1015
# Update the module description
1116
sed -i "s/^description=.*/$string/g" "$MODDIR/module.prop"
17+
log_message "Protection is enabled ✅ | Last updated: $(date)"
18+
1219
else
1320
# Update the module description if no entries are blocked
14-
sed -i "s/^description=.*/description=Status: disabled due to no blocked entries ❌/g" "$MODDIR/module.prop"
21+
sed -i "s/^description=.*/description=Status: Protection disabled due to no blocked entries ❌/g" "$MODDIR/module.prop"
22+
log_message "Status: Protection disabled due to no blocked entries"
1523
fi

module/system/bin/rmlwk

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/system/bin/sh
22

3+
# Define a logging function
4+
function log_message() {
5+
local message="$1"
6+
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" >> "/sdcard/Re-Malwack/logs.txt"
7+
}
8+
39
# Define functions
410
function malvack_banner() {
511
clear
@@ -46,6 +52,7 @@ tmp_hosts="/data/local/tmp/hosts"
4652
[ -z "$MAGISKTMP" ] && malvack_banner
4753

4854
function install_hosts() {
55+
log_message "Starting to install hosts."
4956
# Prepare original hosts
5057
cp -f "$hosts_file" "${tmp_hosts}0"
5158

@@ -88,20 +95,16 @@ function install_hosts() {
8895
fi
8996
fi
9097

91-
if [ -n "$social_whitelist" ]; then
92-
echo "$social_whitelist" | sed '/^[[:space:]]*#/d; /^[[:space:]]*$/d' | awk '{print "0.0.0.0 " $0}' > "${tmp_hosts}w"
93-
awk 'NR==FNR {seen[$0]=1; next} !seen[$0]' "${tmp_hosts}w" "$hosts_file" > "$tmp_hosts"
94-
cat "$tmp_hosts" > "$hosts_file"
95-
fi
96-
9798
# Update config
9899
[ -n "$block_type" ] && sed -i "s/^block_${block_type}=.*/block_${block_type}=1/" /data/adb/Re-Malwack/config.sh
99100

100101
# Clean up
101102
rm -f "${tmp_hosts}"* 2>/dev/null
103+
log_message "Successfully installed hosts."
102104
}
103105

104106
function remove_hosts() {
107+
log_message "Starting to remove hosts."
105108
# Prepare original hosts
106109
cp -f "$hosts_file" "${tmp_hosts}0"
107110

@@ -111,7 +114,7 @@ function remove_hosts() {
111114
# Remove from hosts file
112115
awk 'NR==FNR {seen[$0]=1; next} !seen[$0]' "${tmp_hosts}1" "${tmp_hosts}0" > "$hosts_file"
113116

114-
# Restore to default entires if hosts file is empty
117+
# Restore to default entries if hosts file is empty
115118
if [ ! -s "$hosts_file" ]; then
116119
echo "- Warning: Hosts file is empty. Restoring default entries."
117120
echo -e "127.0.0.1 localhost\n::1 localhost" > "$hosts_file"
@@ -122,6 +125,7 @@ function remove_hosts() {
122125

123126
# Clean up
124127
rm -f "${tmp_hosts}"* 2>/dev/null
128+
log_message "Successfully removed hosts."
125129
}
126130

127131
function block_content() {
@@ -136,9 +140,10 @@ function block_content() {
136140
nuke_if_we_dont_have_internet
137141
if [ ! -f "${cache_hosts}1" ] || [ "$status" = "update" ]; then
138142
mkdir -p "$persist_dir/cache/$block_type"
143+
log_message "Downloading hosts for $block_type."
139144
fetch "${cache_hosts}1" https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/${block_type}-only/hosts &>/dev/null
140145
if [ "$block_type" = "porn" ]; then
141-
fetch "${cache_hosts}2" https://raw.githubusercontent.com/johnlouie09/Anti-Porn-HOSTS-File/refs/heads/master/HOSTS.txt &>/dev/null &
146+
fetch "${cache_hosts}2" https://raw.githubusercontent.com/johnlouie09/Anti-Porn-HOSTS-File/refs/heads/master/HOSTS.txt &>/dev/null &
142147
fetch "${cache_hosts}3" https://www.someonewhocares.org/hosts/hosts &>/dev/null &
143148
wait
144149
fi
@@ -156,6 +161,7 @@ function tolower() {
156161
}
157162

158163
function abort() {
164+
log_message "Aborting: $1"
159165
echo "- $1"
160166
sleep 0.5
161167
exit 1
@@ -181,19 +187,22 @@ function ask() {
181187
function fetch() {
182188
PATH=/data/adb/ap/bin:/data/adb/ksu/bin:/data/adb/magisk:/data/data/com.termux/files/usr/bin:$PATH
183189
if command -v curl >/dev/null 2>&1; then
184-
curl -o "$@" || abort "Failed to download $2."
190+
curl -o "$@" || { log_message "Failed to download $2."; abort "Failed to download $2."; }
185191
else
186-
busybox wget --no-check-certificate -O - "$@" || abort "Failed to download $2."
192+
busybox wget --no-check-certificate -O - "$@" || { log_message "Failed to download $2."; abort "Failed to download $2."; }
187193
fi
188194
}
189195

190196
function update_status() {
191197
if grep -q '0.0.0.0' "$hosts_file"; then
192198
string="description=Status: Protection is enabled ✅ | Last updated: $(date)"
199+
status="Protection is enabled ✅ | Last updated: $(date)"
193200
else
194201
string="description=Status: Protection is disabled due to reset ❌"
202+
status="Protection is disabled due to reset ❌"
195203
fi
196204
sed -i "s/^description=.*/$string/g" $MODDIR/module.prop
205+
log_message "$status"
197206
}
198207

199208
# Check Root
@@ -204,11 +213,12 @@ fi
204213
# Main Logic
205214
case "$(tolower "$1")" in
206215
--reset)
216+
log_message "Reverting the changes."
207217
echo "- Reverting the changes..."
208218
echo "127.0.0.1 localhost\n::1 localhost" > "$hosts_file"
209219
chmod 644 "$hosts_file"
210220
update_status
211-
echo "- Successfully reverted changes."
221+
log_message "Successfully reverted changes." && echo "- Successfully reverted changes."
212222
;;
213223
--block-porn|--block-gambling|--block-fakenews|--block-social)
214224
local block_type=${1#--block-}
@@ -221,15 +231,15 @@ case "$(tolower "$1")" in
221231
else
222232
echo "- Removing block entries for ${block_type} sites."
223233
block_content "$block_type" 0
224-
echo "- Unblocked ${block_type} sites successfully."
234+
log_message "Unblocked ${block_type} sites successfully." && echo "- Unblocked ${block_type} sites successfully."
225235
fi
226236
else
227237
if [ "$block_toggle" = 1 ]; then
228238
echo "- $block_type block is already enabled"
229239
else
230240
echo "- Adding block entries for ${block_type} sites."
231241
block_content "$block_type" 1
232-
echo "- Blocked ${block_type} sites successfully."
242+
log_message "Blocked ${block_type} sites successfully." && echo "- Blocked ${block_type} sites successfully."
233243
fi
234244
fi
235245
update_status
@@ -248,11 +258,12 @@ case "$(tolower "$1")" in
248258
# Add domain to whitelist.txt and remove from hosts
249259
grep -qx "$domain" "$persist_dir/whitelist.txt" && echo "$domain is already whitelisted" || echo "$domain" >> "$persist_dir/whitelist.txt"
250260
sed -i "/0\.0\.0\.0 $domain/d" "$hosts_file" 2>/dev/null
261+
log_message "Added $domain to whitelist."
251262
else
252263
# Remove domain from whitelist.txt if found
253264
if grep -qxF "$domain" "$persist_dir/whitelist.txt"; then
254265
sed -i "/^$(printf '%s' "$domain" | sed 's/[]\/$*.^|[]/\\&/g')$/d" "$persist_dir/whitelist.txt";
255-
echo "- $domain removed from whitelist."
266+
log_message "Removed $domain from whitelist." && echo "- $domain removed from whitelist."
256267
else
257268
echo "- $domain isn't in whitelist."
258269
fi
@@ -277,34 +288,44 @@ case "$(tolower "$1")" in
277288
else
278289
echo "0.0.0.0 $domain" >> "$hosts_file" && echo "- Blacklisted $domain."
279290
update_status
291+
log_message "Blacklisted $domain."
280292
fi
281293
else
282294
# Remove domain from blacklist.txt if found
283295
if grep -qxF "$domain" "$persist_dir/blacklist.txt"; then
284296
sed -i "/^$(printf '%s' "$domain" | sed 's/[]\/$*.^|[]/\\&/g')$/d" "$persist_dir/blacklist.txt";
285-
echo "- $domain removed from blacklist."
297+
log_message "Removed $domain from blacklist." && echo "- $domain removed from blacklist."
286298
else
287299
echo "- $domain isn't in blacklist."
288300
fi
289301
fi
290302
fi
291303
;;
292304
--update-hosts)
305+
log_message "Starting to update hosts."
293306
echo "- Downloading updates, Please wait."
294307
nuke_if_we_dont_have_internet
295308

296309
# Update Re-Malwack general hosts
297310
fetch "${tmp_hosts}1" https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts &>/dev/null &
311+
log_message "Downloading hosts1."
298312
fetch "${tmp_hosts}2" https://raw.githubusercontent.com/hagezi/dns-blocklists/main/hosts/pro.plus-compressed.txt &>/dev/null &
313+
log_message "Downloading hosts2."
299314
fetch "${tmp_hosts}3" https://o0.pages.dev/Pro/hosts.txt &>/dev/null &
315+
log_message "Downloading hosts3."
300316
fetch "${tmp_hosts}4" https://raw.githubusercontent.com/r-a-y/mobile-hosts/master/AdguardDNS.txt &>/dev/null &
317+
log_message "Downloading hosts4."
301318
fetch "${tmp_hosts}5" https://raw.githubusercontent.com/r-a-y/mobile-hosts/refs/heads/master/AdguardMobileAds.txt &>/dev/null &
319+
log_message "Downloading hosts5."
302320
fetch "${tmp_hosts}6" https://raw.githubusercontent.com/r-a-y/mobile-hosts/refs/heads/master/AdguardMobileSpyware.txt &>/dev/null &
321+
log_message "Downloading hosts6."
303322

304323
# Update hosts for global whitelist
305324
mkdir -p "$persist_dir/cache/whitelist"
306325
fetch "$persist_dir/cache/whitelist/whitelist.txt" https://raw.githubusercontent.com/ZG089/Re-Malwack/main/whitelist.txt &>/dev/null &
326+
log_message "Downloading whitelist."
307327
fetch "$persist_dir/cache/whitelist/social-whitelist.txt" https://raw.githubusercontent.com/ZG089/Re-Malwack/main/social-whitelist.txt &>/dev/null &
328+
log_message "Downloading social whitelist."
308329

309330
# Update hosts for custom block
310331
[ -d "$persist_dir/cache/porn" ] && block_content "porn" "update" &
@@ -322,6 +343,7 @@ case "$(tolower "$1")" in
322343
[ "$block_gambling" = 1 ] && block_content "gambling"
323344
[ "$block_fakenews" = 1 ] && block_content "fakenews"
324345
update_status
346+
log_message "Successfully updated hosts."
325347
echo "- Done."
326348
;;
327349
--help|-h|*)

0 commit comments

Comments
 (0)