-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.sh
238 lines (211 loc) · 5.96 KB
/
mod.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
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
#!/bin/bash
LOG_FILE="/var/log/pimod.log"
git config --global advice.detachedHead false
help() {
echo "(Re)install Latest Speedtest Mod."
echo "Usage: sudo $0 [up] [un] [db]"
echo "up - update Pi-hole"
echo "un - remove the mod"
echo "db - flush database"
}
download() {
if [ ! -f /usr/local/bin/pihole ]; then
echo "$(date) - Installing Pi-hole..."
curl -sSL https://install.pi-hole.net | sudo bash
fi
if [ -z "${1-}" ] || [ "$1" == "up" ]; then
echo "$(date) - Verifying Dependencies..."
if [ ! -f /etc/apt/sources.list.d/ookla_speedtest-cli.list ]; then
echo "$(date) - Adding speedtest source..."
# https://www.speedtest.net/apps/cli
if [ -e /etc/os-release ]; then
. /etc/os-release
base="ubuntu debian"
os=${ID}
dist=${VERSION_CODENAME}
if [ ! -z "${ID_LIKE-}" ] && [[ "${base//\"/}" =~ "${ID_LIKE//\"/}" ]]; then
os=${ID_LIKE%% *}
[ -z "${UBUNTU_CODENAME-}" ] && UBUNTU_CODENAME=$(/usr/bin/lsb_release -cs)
dist=${UBUNTU_CODENAME}
[ -z "$dist" ] && dist=${VERSION_CODENAME}
fi
wget -O /tmp/script.deb.sh https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh > /dev/null 2>&1
chmod +x /tmp/script.deb.sh
os=$os dist=$dist /tmp/script.deb.sh
rm -f /tmp/script.deb.sh
else
curl -sSLN https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash
fi
fi
PHP_VERSION=$(php -v | tac | tail -n 1 | cut -d " " -f 2 | cut -c 1-3)
apt-get install -y sqlite3 $PHP_VERSION-sqlite3 jq speedtest-cli- speedtest
if [ -f /usr/local/bin/speedtest ]; then
rm -f /usr/local/bin/speedtest
ln -s /usr/bin/speedtest /usr/local/bin/speedtest
fi
echo "$(date) - Downloading Latest Speedtest Mod..."
cd /var/www/html
rm -rf new_admin
git clone --depth=1 https://github.com/arevindh/AdminLTE new_admin
cd new_admin
git fetch --tags -q
latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)
git checkout $latestTag
cd /opt/
rm -rf new_pihole
git clone --depth=1 https://github.com/arevindh/pi-hole new_pihole
cd new_pihole
git fetch --tags -q
latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)
git checkout $latestTag
fi
}
install() {
echo "$(date) - Installing Speedtest Mod..."
cd /opt/
cp pihole/webpage.sh pihole/webpage.sh.org
cp new_pihole/advanced/Scripts/webpage.sh pihole/webpage.sh.mod
rm -rf new_pihole
cd /var/www/html
rm -rf org_admin
rm -rf mod_admin
cp -r new_admin mod_admin
mv admin org_admin
mv new_admin admin
cd - > /dev/null
cp pihole/webpage.sh.mod pihole/webpage.sh
chmod +x pihole/webpage.sh
if [ ! -f /etc/pihole/speedtest.db ]; then
cp scripts/pi-hole/speedtest/speedtest.db /etc/pihole/
echo "$(date) - Initialized Database"
fi
pihole updatechecker local
echo "$(date) - Install Complete"
}
purge() {
rm -rf /opt/pihole/webpage.sh.*
rm -rf /var/www/html/*_admin
exit 0
}
update() {
echo "$(date) - Updating Pi-hole..."
cd /var/www/html/admin
git reset --hard origin/master
git checkout master
PIHOLE_SKIP_OS_CHECK=true sudo -E pihole -up
echo "$(date) - Update Complete"
if [ "${1-}" == "un" ]; then
purge
fi
}
uninstall() {
echo "$(date) - Restoring Pi-hole..."
cd /opt/
if [ ! -f /opt/pihole/webpage.sh.org ]; then
rm -rf org_pihole
git clone https://github.com/pi-hole/pi-hole org_pihole
cd org_pihole
git fetch --tags -q
localVer=$(pihole -v | grep "Pi-hole" | cut -d ' ' -f 6)
remoteVer=$(curl -s https://api.github.com/repos/pi-hole/pi-hole/releases/latest | grep "tag_name" | cut -d '"' -f 4)
if [[ "$localVer" < "$remoteVer" && "$localVer" == *.* ]]; then
remoteVer=$localVer
fi
git checkout -q $remoteVer
cp advanced/Scripts/webpage.sh ../pihole/webpage.sh.org
cd - > /dev/null
rm -rf org_pihole
fi
cd /var/www/html
if [ ! -d /var/www/html/org_admin ]; then
rm -rf org_admin
git clone https://github.com/pi-hole/AdminLTE org_admin
cd org_admin
git fetch --tags -q
localVer=$(pihole -v | grep "AdminLTE" | cut -d ' ' -f 6)
remoteVer=$(curl -s https://api.github.com/repos/pi-hole/AdminLTE/releases/latest | grep "tag_name" | cut -d '"' -f 4)
if [[ "$localVer" < "$remoteVer" && "$localVer" == *.* ]]; then
remoteVer=$localVer
fi
git checkout -q $remoteVer
cd - > /dev/null
fi
if [ "${1-}" == "db" ] && [ -f /etc/pihole/speedtest.db ]; then
mv /etc/pihole/speedtest.db /etc/pihole/speedtest.db.old
echo "$(date) - Configured Database"
fi
echo "$(date) - Uninstalling Current Speedtest Mod..."
if [ -d /var/www/html/admin ]; then
rm -rf mod_admin
mv admin mod_admin
fi
mv org_admin admin
cd /opt/pihole/
cp webpage.sh webpage.sh.mod
mv webpage.sh.org webpage.sh
chmod +x webpage.sh
echo "$(date) - Uninstall Complete"
}
restore() {
if [ ! -d /var/www/html/${1}_admin ] || [ ! -f /opt/pihole/webpage.sh.${1} ]; then
echo "$(date) - A restore is not needed or one failed."
else
echo "$(date) - Restoring Files..."
cd /var/www/html
rm -rf admin
mv ${1}_admin admin
cd /opt/pihole/
mv webpage.sh.${1} webpage.sh
echo "$(date) - Files Restored"
fi
}
abort() {
echo "$(date) - Process Aborted" | sudo tee -a /var/log/pimod.log
case ${1-} in
up | un | db)
restore mod
;;
*)
restore org
;;
esac
pihole restartdns
echo "$(date) - Please try again or try manually."
exit 1
}
clean() {
rm -rf /var/www/html/mod_admin
rm -f /opt/pihole/webpage.sh.mod
pihole restartdns
exit 0
}
main() {
printf "Thanks for using Speedtest Mod!\nScript by @ipitio\n\n"
op=${1-}
if [ "$op" == "-h" ] || [ "$op" == "--help" ]; then
help
exit 0
fi
if [ $EUID != 0 ]; then
sudo "$0" "$@"
exit $?
fi
set -Eeuo pipefail
trap '[ "$?" -eq "0" ] && clean || abort $op' EXIT
db=$([ "$op" == "up" ] && echo "${3-}" || [ "$op" == "un" ] && echo "${2-}" || echo "$op")
download $op
uninstall $db
case $op in
un)
purge
;;
up)
update ${2-}
;&
*)
install
;;
esac
exit 0
}
main "$@" 2>&1 | sudo tee -- "$LOG_FILE"