-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaintain.sh
executable file
·72 lines (63 loc) · 1.45 KB
/
maintain.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
#!/bin/bash
# Unify locale settings temporarily to make sort produce the same order
LC_ALL=C
export LC_ALL
help() {
cat <<EOF
usage: $0 [OPTIONS]
--help Show this message
add <DOMAIN, [DOMAIN, ...]> Add domains to the blacklist
remove <DOMAIN, [DOMAIN, ...]> Remove domains from the blacklist
maintain Maintain the list of domains
EOF
}
add_domains() {
echo "Adding $# domains..."
echo "$@" | tr ' ' '\n' >>blocklist.txt
}
remove_domains() {
echo "Removing $# domains..."
for domain in "$@"; do
sed -i "/^${domain}$/d" blocklist.txt
done
}
# Converts uppercase to lowercase, sorts, and removes duplicates and CRs
# The allowlist is then applied to the blocklist
maintain() {
cat allowlist.txt | tr '[:upper:]' '[:lower:]' | sort -f | uniq -i >tmp.txt
sed -i 's/\r$//g' tmp.txt
mv tmp.txt allowlist.txt
cat blocklist.txt | tr '[:upper:]' '[:lower:]' | sort -f | uniq -i >tmp.txt
sed -i 's/\r$//g' tmp.txt
comm -23 tmp.txt allowlist.txt >blocklist.txt
rm -f tmp.txt
echo "Blacklist sorted and de-duplicated!"
}
# Run maintain if no args
if [[ $# -eq 0 ]]; then
maintain
exit 0
fi
# Check args
for opt in "$1"; do
case $opt in
--help)
help
exit 0
;;
maintain)
maintain
exit 0
;;
add)
add_domains "${@:2}"
maintain
exit 0
;;
remove)
remove_domains "${@:2}"
maintain
exit 0
;;
esac
done