summaryrefslogtreecommitdiff
blob: 3fa8db25f5fa93d1073dc50d1ec8a6a2f4f327c9 (plain)
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
#!/bin/bash

source /etc/conf.d/moblock
source /sbin/functions.sh

log_file=/var/log/moblock-update.log
tmp_dir=/tmp/moblock-update.$$ 

typeset -i successful_dl_count=0
typeset -i failed_dl_count=0
typeset -i FAIL=0

cleanup() {
	log_msg "$0 exiting.

-------------------------------------------------------------------------------


"
	rm -rf ${tmp_dir}
}

die() {
	eerror "Update failed: $@" | tee -a ${log_file}
	eerror "See /var/log/moblock-update.log for details."
	cleanup
	exit 1;
}

log_msg() {
	echo -e "$(date): $@" >> ${log_file}
}

init() {
	if [ -z "${BLOCKLISTSERVERS}" ]; then
		eerror "There is no BLOCKLISTSERVERS defined in /etc/conf.d/moblock."
		eindent
		eerror "Please set this variable to the list of servers you wish to"
		eerror "download from."
		eoutdent
		
		FAIL=1
	fi

	if [ -z "${BLOCKLISTS}" ]; then
		eerror "There is no BLOCKLISTS defined in /etc/conf.d/moblock."
		eindent
		eerror "Please set this variable to the lists you wish to use."
		eoutdent
		
		FAIL=1
	fi

	if [ -z "${BLOCKLISTFILE}" ]; then
		eerror "There is no BLOCKLISTFILE defined in /etc/conf.d/moblock."
		eindent
		eerror "Please set this variable to the file you wish to output the"
		eerror "merged block list to."
		eoutdent
		
		FAIL=1
	fi

	if [ -z "${BLOCKLISTDIR}" ]; then
		eerror "There is no BLOCKLISTDIR defined in /etc/conf.d/moblock."
		eindent
		eerror "Please set this variable to the directory you wish to store"
		eerror "the downloaded lists in."
		eoutdent
		
		FAIL=1
	fi

	[ ${FAIL} -eq 0 ] || die "invalid configuration"
}

# Iterate through servers until we get one to work or they all fail.
getAFile() {
	local tmp_file=${tmp_dir}/${1}.wget.log
	for base_url in ${BLOCKLISTSERVERS}; do
		log_msg "Attempting to downloading ${1}.${BLOCKLISTSUFFIX} from location ${base_url}"
		if wget -P ${BLOCKLISTDIR} \
				-N ${base_url}/${1}.${BLOCKLISTSUFFIX} \
				-a ${tmp_file}; then
			rm ${tmp_file}
			return 0
		fi
	done

	log_msg "Failed to download ${1}.${BLOCKLISTSUFFIX}.
${BAD}wget output ---------->${NORMAL}
$(cat ${tmp_file})
${BAD}<---------- end of wget output${NORMAL}"
	rm ${tmp_file}
	return 1
}

getBlocklists() {
	einfo Downloading lists...
	eindent

	for i in ${BLOCKLISTS}; do
		ebegin "Downloading ${i}" | tee -a ${log_file}

		if getAFile $i; then
			successful_dl_count=${successful_dl_count}+1
			eend 0 | tee -a ${log_file}
		else
			failed_dl_count=${failed_dl_count}+1
			eend 1 | tee -a ${log_file}
		fi
	done

	eoutdent

	if [ ${failed_dl_count} -ne 0 ]; then
		if [ ${successful_dl_count} -eq 0 ]; then
			die "All downloads failed"
		else
			ewarn "WARNING: ${failed_dl_count} downloads failed!  See /var/log/moblock-update.log" \
				| tee -a ${log_file}
			ewarn "for details.  Previous blocklists will be used failed items." \
				| tee -a ${log_file}
		fi
	fi
}

mergeFiles() {
	einfo Unpacking and merging lists...
	eindent

	local new_p2p_file=${tmp_dir}/new.p2p

	for i in ${BLOCKLISTS}; do
		ebegin Merging ${i} | tee -a ${log_file}

		gunzip -c ${BLOCKLISTDIR}/${i}.${BLOCKLISTSUFFIX} >> ${new_p2p_file} 2>>${log_file} \
			|| die "Failed to extract list '${i}'"

		eend $? | tee -a ${log_file}
	done

	mv ${new_p2p_file} ${BLOCKLISTFILE}

	eoutdent
}

reloadList() {
	moblock_pid=$(cat /var/run/moblock.pid 2>/dev/null)

	if ps -p ${moblock_pid} > /dev/null 2>&1; then
		einfo "Reloading block list"
		kill -s HUP ${moblock_pid}
		eend $? 
	fi
}

main() {
	mkdir -p ${BLOCKLISTDIR} || die "Failed to create dir ${BLOCKLISTDIR}."
	mkdir -p ${tmp_dir} || die "Failed to create dir ${tmp_dir}"

	einfo "Updating moblock..." | tee -a ${log_file}
	eindent
	log_msg "$0 initiated."

	getBlocklists
	mergeFiles
	reloadList | tee -a ${log_file}

	eoutdent
	if [ ${failed_dl_count} -eq 0 ]; then
		einfo "MoBlock update completed successfully." | tee -a ${log_file}
	else
		ewarn "MoBlock update partially successful." | tee -a ${log_file}
	fi
	cleanup
}

main