-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocklist-dl.bash
executable file
·139 lines (101 loc) · 3.25 KB
/
blocklist-dl.bash
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
#!/bin/bash
##
## This file is part of the `src-run/user-scripts-server` project.
##
## (c) Rob Frawley 2nd <[email protected]>
##
## For the full copyright and license information, please view the LICENSE.md
## file that was distributed with this source code.
##
#
# configuration variables
#
BLOCKLIST_URL="http://list.iblocklist.com/?list=bt_level2&fileformat=cidr&archiveformat=gz"
BLOCKLIST_EXT_TYPE="cidr"
BLOCKLIST_OUT_ROOT="/tmp"
BLOCKLIST_LOG_ROOT="/tmp"
BLOCKLIST_TMP_ROOT="/tmp"
#
# internal variables
#
BLOCKLIST_OUT="${BLOCKLIST_OUT_ROOT}/blocklist.${BLOCKLIST_EXT_TYPE}.txt"
BLOCKLIST_LOG="${BLOCKLIST_LOG_ROOT}/blocklist.${BLOCKLIST_EXT_TYPE}.log"
function log_start()
{
printf '\n[RUN @ "%s"]\n' "$(date)" >> "${BLOCKLIST_LOG}"
}
function out_title()
{
local format="${1^^}"
shift
printf "${format}\n" "$@" | tee -a "${BLOCKLIST_LOG}"
}
function out_info()
{
local format="${1}"
shift
printf " • ${format}\n" "$@" | tee -a "${BLOCKLIST_LOG}"
}
function out_error()
{
local format="${1}"
shift
printf " • [ERROR] ${format}\n" "$@" | tee -a "${BLOCKLIST_LOG}"
exit 255
}
function main()
{
local path="${BLOCKLIST_TMP_ROOT}/blocklist-download-temporary-work"
local file="${path}/$(basename "${BLOCKLIST_OUT}" .txt).gz"
local cust="${1}"
if [[ "x${cust}" != "xx" ]]; then
BLOCKLIST_OUT="${cust}.${BLOCKLIST_EXT_TYPE}.txt"
BLOCKLIST_LOG="${cust}.${BLOCKLIST_EXT_TYPE}.log"
fi
local logRoot="$(dirname "${BLOCKLIST_LOG}")"
if [[ ! -d "${logRoot}" ]]; then
mkdir -p "${logRoot}"
if [[ $? -ne 0 ]]; then
out_info '[WARNING] Unable to use custom log path: %s' "${BLOCKLIST_LOG}"
BLOCKLIST_LOG="/tmp/blocklist.${BLOCKLIST_EXT_TYPE}.log"
out_info '[WARNING] Using system temporary path instead for logs: %s' "${BLOCKLIST_LOG}"
fi
fi
log_start
out_title 'Updating blocklist:' "${BLOCKLIST_URL}"
out_info 'Logged blocklist stat: %s' "${BLOCKLIST_LOG}"
if [[ ! -d "${path}" ]]; then
mkdir -p "${path}"
if [[ $? -ne 0 ]]; then
out_error 'Unable to create temporary working directory "%s"!' "${path}"
fi
fi
if [[ -f "${file}" ]]; then
rm "${file}"
if [[ $? -ne 0 ]]; then
out_error 'Unable to remove old temporary working file "%s"!' "${file}"
fi
fi
out_info 'Fetch blocklist link: "%s"' "${BLOCKLIST_URL}"
wget -q -O "${file}" "${BLOCKLIST_URL}"
if [[ $? -ne 0 ]]; then
out_error 'Unable to download blocklist "%s"!' "${BLOCKLIST_URL}"
exit
fi
local size="$(gzip -dc "${file}" | grep -E '^[0-9]' | wc -l)"
if [[ ${size} -eq 0 ]]; then
out_error 'Unable to create blocklist as download contains 0 lines!'
exit
fi
out_info 'Write blocklist file: "%s" (%d lines)' "${BLOCKLIST_OUT}" ${size}
gzip -dc "${file}" | grep -E '^[0-9]' | sort -u > "${BLOCKLIST_OUT}"
if [[ $? -ne 0 ]]; then
out_error 'Unable to parse downloaded blocklist "%s"!' "${path}"
fi
out_info 'Clean temporary data: "%s"' "${path}"
rm -fr "${path}"
if [[ $? -ne 0 ]]; then
out_error 'Unable to remove temporary working path "%s"!' "${path}"
fi
}
main "${1:-x}"