-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenwrt_adblock.sh
157 lines (144 loc) · 4.17 KB
/
openwrt_adblock.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
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
#!/usr/bin/env ash
#
# ------------------------------------------------------------------------------
#
# YouTube BlockList Adguard Home collect Script
#
# AUTHOR: Mariel de Jesus ™ <[email protected]>
# MAINTAINERS:
#
# ------------------------------------------------------------------------------
#
# DESCRIPTION:
#
# This script gets the list of youtube video advertising urls from the local
# YouTube list ListBlock on the WEB and inserts it in the Adblock-OpenWRT
# blacklist.
#
# ------------------------------------------------------------------------------
#
# LICENSE:
# This program is Free Software, you can redistribute and / or modify it under
# the terms of the GNU General Public License Version 3 published by Free
# Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; Without even implying guarantees of MERCHANTABILITY or
# ADAPTATION TO AN PRIVATE PURPOSE. See the GNU General Public License (GPL)
# for more details (https://www.gnu.org/licenses/gpl-3.0.html).
#
# ------------------------------------------------------------------------------
#
# DEPENDENCIES:
#
# perl curl grep
#
# ------------------------------------------------- -----------------------------
VERSION=33
CRONTAB="/etc/crontabs/root"
SCRIPT="$(basename $0)"
function echov(){
echo -e "\033[01;32m$1...\033[00;37m"
}
#Verify if is root
if [[ "$(id -u)" != "0" ]]; then
echov "This script requires root or sudo"
echo ""
echov "Exiting"
exit 1
fi
function install() {
echov "Checking dependencies"
#Install dependencies
PKG=""
if [[ ! -e "/usr/bin/perl" ]]; then PKG="$PKG perl"
fi
if [[ ! -e "/usr/bin/curl" ]]; then
PKG="$PKG curl"
fi
if [[ ! -e "/usr/bin/grep" ]]; then
PKG="$PKG grep"
fi
if [[ ! -z "$PKG" ]]; then
echov "Installing dependencies"
opkg update > /dev/null 2>&1
opkg install $PKG > /dev/null 2>&1
fi
move_rename
echov "Installation complete"
}
function upgrade() {
#Check remote version
echov "Checking updates"
REMOTE=$(curl https://gitlab.com/marieldejesus12/youtube-listblock/-/raw/master/$SCRIPT | grep 'VERSION=' | grep -v 'sed' | sed 's/^.*=//') > /dev/null 2>&1
#Update if available update
if [[ $VERSION -lt $REMOTE ]]; then
echov "Updates found, updating"
curl https://gitlab.com/marieldejesus12/youtube-listblock/-/raw/master/$SCRIPT -o $0
echov "Upgrade $0 complete, checking installation"
$0 install
else
echov "No updates required"
fi
}
function move_rename() {
#Move script to /usr/bin
if [[ "$0" != "/usr/bin/openwrt_adblock.sh" ]]; then
echov "Moving script to /usr/bin"
mv $0 /usr/bin/openwrt_adblock.sh
chmod +x /usr/bin/openwrt_adblock.sh
SCRIPT="openwrt_adblock.sh"
fi
#Configure cron
if [[ "$(grep $SCRIPT $CRONTAB)" != "" ]]; then
TESTCRON=$(grep "30 02 * * * /usr/bin/$SCRIPT upgrade" $CRONTAB)
if [[ "$TESTCRON" = "" ]]; then
echov "Adjusting crontabs"
grep -v $SCRIPT $CRONTAB > /tmp/crontabs
mv /tmp/crontabs $CRONTAB
echo "30 02 * * * /usr/bin/$SCRIPT upgrade" >> $CRONTAB
echo "30 03 * * * /usr/bin/$SCRIPT update" >> $CRONTAB
/etc/init.d/cron restart
fi
else
echov "Adjusting crontabs"
echo "30 02 * * * /usr/bin/$SCRIPT upgrade" >> $CRONTAB
echo "30 03 * * * /usr/bin/$SCRIPT update" >> $CRONTAB
/etc/init.d/cron restart
fi
}
function update() {
echov "Updating lists"
ADBLOCK=/etc/adblock/adblock.blacklist
LOCALLIST=/www/youtube.txt
FILETMP=/tmp/youtube.txt
#copy files to blacklist
if [[ -e $LOCALLIST ]]; then
cat $LOCALLIST >> $ADBLOCK
fi
curl https://gitlab.com/marieldejesus12/youtube-listblock/-/raw/master/youtube.txt -o $FILETMP
cat $FILETMP >> $ADBLOCK
rm $FILETMP
#delete duplicates
perl -i -ne 'print if ! $x{$_}++' $ADBLOCK
#Sort list
cat $ADBLOCK | sort > /tmp/sort_list.txt
mv /tmp/sort_list.txt $ADBLOCK
#Update ADBLOCK
/etc/init.d/adblock reload
echov "Update lists complete"
exit
}
case $1 in
install )
install;;
upgrade )
upgrade;;
update )
update;;
* )
echov "Usage $SCRIPT [install|update|upgrade]";
echo "";
echov "Invalid command, try again";
exit 1;;
esac