-
Notifications
You must be signed in to change notification settings - Fork 6
/
autohosts
executable file
·240 lines (186 loc) · 5.93 KB
/
autohosts
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
239
240
#!/bin/bash
VERSION=3.1.0
# get the non-root user, so we can add the custom filters for them
REGUSER=$(last | grep "logged in" | grep -o '^\S*' | head -1)
# check os type for home directory path
if [[ "$OSTYPE" != "darwin"* ]];
then
HOME_USER="/home/$REGUSER/autohosts/"
AUTOHOSTS_PATH="/usr/bin/autohosts"
# use launch daemon instead of cron? (mac only)
USE_LAUNCHD="0"
else
# macos
HOME_USER="/Users/$REGUSER/autohosts/"
AUTOHOSTS_PATH="/usr/local/bin/autohosts"
# use launch daemon instead of cron? (mac only)
USE_LAUNCHD="1"
# if your mdm deployment does this, set to 0
AUTOCREATE_LAUNCHAGENT="1"
fi
# initialize reusable stuff, to make the terminal results prettier
function croncheck() {
crontab -l | grep -q 'autohosts' && EXIST=1 || EXIST=0
}
function green() {
echo -e "\033[32m$1\033[0m"
}
function red() {
echo -e "\033[0;31m$1\033[0m"
}
function brown() {
echo -e "\033[0;33m$1\033[0m"
}
function purple() {
echo -e "\033[0;35m$1\033[0m"
}
# append a notice to the terminal when an upstream list has been added
function append() {
green ">> $1 upstream successfully appended..."
}
# check for the existence of the upstream hosts file before downloading, to prevent malformed data
function upstream(){
# see if we're dealing with a linux-based distro or mac
if [[ "$OSTYPE" != "darwin"* ]];
then
# not a mac, use wget
if [[ "$(wget --spider "$1" 2>&1 | grep '200 OK')" ]];
then
# file exists! collect it
wget "$1" -O ->> /etc/hosts
append "$hosts_source"
else
logger -s "Autohosts update: $1 appears to be offline, skipping..."
fi
else
# is a mac, use curl, no guarantee gnu tools are installed & unnecessary as a dependency, imo
if [[ "$(curl -I "$1" 2>/dev/null | head -n 1 | cut -d$' ' -f2)" == '200' ]];
then
# file exists! collect it
curl "$1" >> /etc/hosts
append "$hosts_source"
else
logger -s "Autohosts update: $1 appears to be offline, skipping..."
fi
fi
}
if [[ $EUID -ne 0 ]]; then
red "This script must be ran as root, or with sudo privileges."
exit 1
fi
### setup ###
# if this is the first run, move to appropriate directory and set a config file
if [ ! -e /etc/autohosts.conf ];
then
# move the script to /usr/bin so it can be executed as an alias
if [ -f autohosts ] && [ ! -f "$AUTOHOSTS_PATH" ];
then
green "Creating an alias for autohosts.."
cp autohosts "$AUTOHOSTS_PATH" && chmod +x "$AUTOHOSTS_PATH"
fi
# migrate custom filters file to the user's home directory
if [ -f custom_filters ] && [ ! -d "$HOME_USER" ];
then
green "Creating custom filters for $REGUSER..."
mkdir -p "$HOME_USER" && cp custom_filters "$HOME_USER"custom_filters && cp whitelist "$HOME_USER"whitelist
green "Creating skeleton upstream file..."
cp hosts_source "$HOME_USER" && chown "$REGUSER:$REGUSER" -R "$HOME_USER"
fi
if [ ! "$USE_LAUNCHD" == "1" ];
then
# double-check to ensure we don't have a conflicting cron
croncheck
if [ "$EXIST" == 0 ];
then
# see if we are root or sudo; if sudo, we need to append sudo to the cron trigger
if [ "$(whoami)" == "root" ] ; then
CRON_PATH="$AUTOHOSTS_PATH"
else
CRON_PATH="sudo $AUTOHOSTS_PATH"
fi
# if no cronjob exists, create one
crontab -l | { cat; echo "22 11 * * 3 $CRON_PATH > /dev/null 2>&1"; } | crontab -
croncheck
if [ "$EXIST" == 1 ];
then
green "Cronjob created..."
else
red "There was a problem creating the cronjob, you might have to create it manually!"
fi
fi
elif [ "$AUTOCREATE_LAUNCHAGENT" == "1" ];
then
cp ./mac-mdm-deployment/Library/LaunchAgents/com.github.angela-d-autohosts.plist /Library/LaunchAgents/com.github.angela-d-autohosts.plist
fi
purple "Setup is complete!"
fi
### end setup ###
purple "== UPDATING =="
# make sure there's an internet connection
if [ ! "$(ping -c 1 '9.9.9.9')" ];
then
# no internet, exit and try again next time
echo "No internet detected, exiting."
exit
fi
# check hosts_source for a list of preferred upstreams
if [ -f "$HOME_USER"hosts_source ];
then
# start with a clean slate
echo "" > /etc/hosts
green "Obtaining hosts lists from preferred upstream sources..."
# loop any hosts specified by the user in hosts_source and do an existence check
for hosts_source in $(cat "$HOME_USER"hosts_source);
do
upstream "$hosts_source"
done
wait
fi
# courtesy check for localhost to prevent loopback breakage if a preferred upstream doesn't specify such
if ! grep -q "127.0.0.1 localhost" /etc/hosts;
then
red "No upstream source has a courtesy entry for localhost! Adding one..."
echo "127.0.0.1 localhost" >> /etc/hosts
echo "255.255.255.255 broadcasthost" >> /etc/hosts
fi
# check for and include any custom filters
if [ -e "$HOME_USER"custom_filters ];
then
green "Processing custom filters..."
cat "$HOME_USER"'custom_filters' >> /etc/hosts
else
brown "No custom filters found at $HOME_USER""custom_filters, skipping..."
fi
# devdom: https://notabug.org/angela/devdom
green "Checking if devdom is installed (will execute devdom hosts update, if so)"
if [ -e /usr/bin/devdom ];
then
# devdom will echo a notice when ran
devdom hosts
else
brown "Devdom is not installed; skipping Devdom hosts."
fi
# apply any whitelisted entries
if [ -s "$HOME_USER"whitelist ];
then
for whitelist in $(cat "$HOME_USER"whitelist);
do
green "Checking for $whitelist.. whitelisting any match."
if [[ "$OSTYPE" != "darwin"* ]];
then
sed -i "/$whitelist/d" /etc/hosts
else
# osx is weird and expects an extension to be called, so lets set to null
sed -i '' "/$whitelist/d" /etc/hosts
fi
done
wait
fi
# set a var that can be later referenced to see if the application has been configured already
echo VERSION=$VERSION > /etc/autohosts.conf
# if this is a mac, refresh the dns cache
if [[ "$OSTYPE" == "darwin"* ]];
then
killall -HUP mDNSResponder
fi
purple "Hosts update complete!"