-
Notifications
You must be signed in to change notification settings - Fork 1
/
hosts-creator.sh
executable file
·128 lines (105 loc) · 4.06 KB
/
hosts-creator.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
#!/bin/sh
#
# Made by XDream8
#
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
GREEN='\033[1;32m'
CYAN='\033[0;36m'
NC='\033[0m'
check_dep() {
if [ ! "$(command -v $1)" ]; then
printf '%b\n' "${RED}$2${NC}"
exit 1
fi
}
startupcheck() {
[ -d "$current_dir/backups" ] || (printf '%b\n' "${BLUE}creating backups directory${NC}" && mkdir $current_dir/backups)
[ -f "$current_dir/backups/$backupfilename.old" ] && printf '%b\n' "${BLUE}there is already 2 backups no need for another${NC}"
[ -f "$current_dir/backups/$backupfilename.old" ] && no_need="1" || no_need="0"
if [ "$no_need" -eq "0" ]; then
[ -f "$current_dir/backups/$backupfilename" ] && (printf '%b\n' "${BLUE}renaming old backup and copying new $syshosts_file${NC}" && mv $current_dir/backups/$backupfilename $current_dir/backups/$backupfilename.old && cp $syshosts_file $current_dir/backups/$backupfilename)
fi
[ -f "$current_dir/backups/$backupfilename" ] || (printf '%b\n' "${BLUE}backing up $syshosts_file${NC}" && cp $syshosts_file $current_dir/backups/$backupfilename)
[ -f "$current_dir/$newhostsfn" ] && printf '%b\n' "${RED}removing old $newhostsfn file${NC}"
[ -f "$current_dir/$newhostsfn" ] && rm $current_dir/$newhostsfn
}
downloadhosts() {
# number
n=0
printf '%b\n' "${BLUE}Downloading host lists${NC}"
for i in $HOSTS; do
n=$(awk "BEGIN {print $n+1}")
printf '%b\n' "${CYAN}$n) ${YELLOW}downloading $i${NC}"
$downloader $i >>$current_dir/$newhostsfn
done
}
edithostsfile() {
# comments
if [ $RM_COMMENTS = 1 ]; then
printf '%b' "${BLUE}removing comments${NC}"
awk '!/^#/' $current_dir/$newhostsfn >tmp && (mv -f tmp $current_dir/$newhostsfn && printf '%b' "${BLUE}: ${GREEN}done${NC}")
fi
# trailing spaces
if [ $RM_TRAILING_SPACES = 1 ]; then
if [ $RM_COMMENTS = 1 ]; then
printf '\n%b' "${BLUE}removing trailing spaces${NC}"
else
printf '%b' "${BLUE}removing trailing spaces${NC}"
fi
awk '{gsub(/^ +| +$/,"")}1' $current_dir/$newhostsfn >tmp && (mv -f tmp $current_dir/$newhostsfn && printf '%b' "${BLUE}: ${GREEN}done${NC}")
fi
# duplicate lines
if [ $RM_DUPLICATE_LINES = 1 ]; then
if [ $RM_TRAILING_SPACES = 1 ]; then
printf '\n%b' "${BLUE}removing duplicate lines${NC}"
elif [ $RM_COMMENTS = 0 ] && [ $RM_TRAILING_SPACES = 0 ]; then
printf '%b' "${BLUE}removing duplicate lines${NC}"
fi
awk '!seen[$0]++' $current_dir/$newhostsfn >tmp && (mv -f tmp $current_dir/$newhostsfn && printf '%b\n' "${BLUE}: ${GREEN}done${NC}")
fi
}
checksize() {
size=$(du -sh "$current_dir/$newhostsfn" | awk '/[MK]/{print $0}')
if [ "$(printf '%s\n' "${size}" | awk '/M/{print $0}')" ]; then
if [ "$(printf '%s\n' "${size}" | awk '{print $0}' | awk '{print ($0+0)}')" -gt "60" ]; then
printf '%b\n' "${RED}your new hosts file is bigger than 60M${NC}"
fi
fi
}
replacehosts() {
if [ "$(command -v rdo)" ]; then
sudo=rdo
elif [ "$(command -v doas)" ]; then
sudo=doas
else
sudo=sudo
fi
printf '\n%b\n' "${BLUE}replacing /etc/hosts with the new one${NC}"
printf '%b' "${YELLOW}"
$sudo mv -iv $current_dir/$newhostsfn $syshosts_file || printf '%b\n' "${RED}error: couldn't replace /etc/hosts with the new hosts file"
exit 1
printf '%b' "${NC}"
}
main() {
current_dir=$(pwd)
[ -f "$current_dir/config" ] && . $current_dir/config
[ -z "$syshosts_file" ] && syshosts_file=/etc/hosts
[ -z "$backupfilename" ] && backupfilename=hosts.backup
[ -z "$newhostsfn" ] && newhostsfn=hosts-new
[ -z "$downloader" ] && downloader=curl
[ -z "$replacehosts" ] && replacehosts=1
check_dep $downloader "$downloader is missing, exiting!"
check_dep awk "awk is required, exiting!"
startupcheck
printf '%s\n' "$RESOLVE_HOST" >$current_dir/$newhostsfn
downloadhosts
edithostsfile
checksize
if [ $replacehosts = 1 ]; then
replacehosts
fi
}
main
exit 0