-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeoip-filter.sh
executable file
·357 lines (322 loc) · 11.1 KB
/
geoip-filter.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
#VARIABLES
################
maxMindAccountId=${MAXMIND_ID}
maxMindLicenceKey=${MAXMIND_KEY}
filterType="${FILTER_TYPE,,}"
countryCodes=($COUNTRY_CODES)
subCodes=($SUB_CODES)
extraIPs=($EXTRA_IPS)
searchMode="${SEARCH_MODE,,:-"false"}"
allowStatusCode=${ALLOW_STATUS_CODE:-"200"}
blockStatusCode=${BLOCK_STATUS_CODE:-"404"}
ipListFilename=${IPLIST_FILENAME:-"IPList.conf"}
ipListFilePath="/etc/nginx/conf.d/${ipListFilename}"
defaultConfFilePath="/etc/nginx/conf.d/default.conf"
comparedIPVariable=${COMPARED_IP_VARIABLE:-"http_x_forwarded_for"}
listenPort=${LISTEN_PORT:-"8080"}
lastModifiedFilename=${LASTMODIFIED_FILENAME:-"LastModified.txt"}
lastModifiedDir=${LASTMODIFIED_DIR:-"/geoip"}
lastModifiedFilePath="${lastModifiedDir}/${lastModifiedFilename}"
countryDir=${COUNTRY_DIR:-"${lastModifiedDir}/country"}
subDir=${SUB_DIR:-"${lastModifiedDir}/sub"}
basicAuth="${maxMindAccountId}:${maxMindLicenceKey}"
countryUrl="https://download.maxmind.com/geoip/databases/GeoLite2-Country-CSV/download?suffix=zip"
subUrl="https://download.maxmind.com/geoip/databases/GeoLite2-City-CSV/download?suffix=zip"
yearsOldDate="Sun, 07 Jan 1990 01:00:00 GMT"
#FUNCTIONS
############
country_getRemoteLastModified() {
remoteResponse=$(curl -LISsu "${basicAuth}" "${countryUrl}")
statusCode=$(echo "$remoteResponse" | grep HTTP)
remoteLastModified=$(echo "$remoteResponse" | grep Last-Modified: | sed 's/Last-Modified: //')
if [[ -z $(echo "$statusCode" | grep 200) ]]; then
echo "ERROR: The HEAD request on the GeoLite2 Country database failed with status code ${statusCode}"
exit 1
fi
}
sub_getRemoteLastModified() {
remoteResponse=$(curl -LISsu "${basicAuth}" "${subUrl}")
statusCode=$(echo "$remoteResponse" | grep HTTP)
remoteLastModified=$(echo "$remoteResponse" | grep Last-Modified: | sed 's/Last-Modified: //')
if [[ -z $(echo "$statusCode" | grep 200) ]]; then
echo "ERROR: The HEAD request on the GeoLite2 City database failed with status code ${statusCode}"
exit 1
fi
}
country_getLastModified() {
if [ -f "${lastModifiedDir}/country${lastModifiedFilename}" ]; then
countryLastModified="$(cat "${lastModifiedDir}/country${lastModifiedFilename}")"
else
countryLastModified=${yearsOldDate}
echo "No country${lastModifiedFilename} record found."
fi
country_getRemoteLastModified
}
sub_getLastModified() {
if [ -f "${lastModifiedDir}/sub${lastModifiedFilename}" ]; then
subLastModified="$(cat "${lastModifiedDir}/sub${lastModifiedFilename}")"
else
subLastModified=${yearsOldDate}
echo "No sub${lastModifiedFilename} record found."
fi
sub_getRemoteLastModified
}
country_isDatabaseMissing() {
if ! [[ (-s ${countryDir}/countryList.txt) && (-s ${countryDir}/globalIPList.txt) ]]; then
echo "Local GeoLite2 Country database is missing or empty."
return 0
else
return 1
fi
}
sub_isDatabaseMissing() {
if ! [[ (-s ${subDir}/subList.txt) && (-s ${subDir}/globalIPList.txt) ]]; then
echo "Local GeoLite2 City database is missing or empty."
return 0
else
return 1
fi
}
country_isDatabaseOutOfDate() {
remoteSeconds=$(date -d "$remoteLastModified" -D "%a, %d %b %Y %T" +'%s')
countrySeconds=$(date -d "$countryLastModified" -D "%a, %d %b %Y %T" +'%s')
if ! [[ ${remoteSeconds} -gt ${countrySeconds} ]]; then
return 1
else
return 0
fi
}
sub_isDatabaseOutOfDate() {
remoteSeconds=$(date -d "$remoteLastModified" -D "%a, %d %b %Y %T" +'%s')
subSeconds=$(date -d "$subLastModified" -D "%a, %d %b %Y %T" +'%s')
if ! [[ ${remoteSeconds} -gt ${subSeconds} ]]; then
return 1
else
return 0
fi
}
country_getZip() {
if (country_isDatabaseMissing || country_isDatabaseOutOfDate) ; then
echo "Downloading latest Geolite2 Country database."
mkdir -p ${countryDir}
curl -LSsu "${basicAuth}" "${countryUrl}" --output "${countryDir}/country.zip"
if grep -q "Invalid license key" ${countryDir}/country.zip ; then
echo "ERROR: MaxMind license key is invalid."
rm ${countryDir}/country.zip
return 1
else
echo "${remoteLastModified}" > "${lastModifiedDir}/country${lastModifiedFilename}"
country_unzipAndExtract
fi
else
echo "Not downloading GeoLite2 Country database as local copy is up to date."
echo " Remote GeoLite2 Country database was last updated on ${remoteLastModified}."
echo " Local GeoLite2 Country database version is dated ${countryLastModified}."
echo " If you wish to force fresh download delete country${lastModifiedFilename} and run again."
fi
}
sub_getZip() {
if (sub_isDatabaseMissing || sub_isDatabaseOutOfDate) ; then
echo "Downloading latest Geolite2 sub database."
mkdir -p ${subDir}
curl -LSsu "${basicAuth}" "${subUrl}" --output "${subDir}/sub.zip"
if grep -q "Invalid license key" ${subDir}/sub.zip ; then
echo "ERROR: MaxMind license key is invalid."
rm ${subDir}/sub.zip
return 1
else
echo "${remoteLastModified}" > "${lastModifiedDir}/sub${lastModifiedFilename}"
sub_unzipAndExtract
fi
else
echo "Not downloading GeoLite2 City database as local copy is up to date."
echo " Remote GeoLite2 City database was last updated on ${remoteLastModified}."
echo " Local GeoLite2 City database version is dated ${subLastModified}."
echo " If you wish to force fresh download delete sub${lastModifiedFilename} and run again."
fi
}
country_unzipAndExtract() {
unzip -jd ${countryDir} ${countryDir}/country.zip "*Blocks*.csv" "*Country-Locations-en.csv"
cat ${countryDir}/*Blocks*.csv | cut -d, -f 1-2 > ${countryDir}/globalIPList.txt
cat ${countryDir}/*Locations-en.csv | \
cut -d, -f 1,5,6 | \
sed -r 's/ /-/g' | \
sed -r 's/"//g' > ${countryDir}/countryList.txt
rm ${countryDir}/country.zip ${countryDir}/*Blocks*.csv ${countryDir}/*Locations-en.csv
}
sub_unzipAndExtract() {
unzip -jd ${subDir} ${subDir}/sub.zip "*Blocks*.csv" "*City-Locations-en.csv"
cat ${subDir}/*Blocks*.csv | cut -d, -f 1-2 > ${subDir}/globalIPList.txt
cat ${subDir}/*Locations-en.csv | \
cut -d, -f 1,5,6,7,8,9,10,11 | \
sed -r 's/ /-/g' | \
sed -r 's/"//g' | \
sed -r 's/(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*)/\1,\2-\4,\5\,\2-\6,\7,\8,\3:\8,\5:\8,\7:\8/' | \
sed -r 's/(,[A-Z]*-,)//g' | \
sed -r 's/(,,[A-Za-z-]*:,.*)//g' | \
sed -r 's/(,:.*$)//' > ${subDir}/subList.txt
rm ${subDir}/sub.zip ${subDir}/*Blocks*.csv ${subDir}/*Locations-en.csv
}
country_addIPsToIPList() {
geoNameID=$( grep -hwiF "$1" ${countryDir}/countryList.txt | cut -d, -f1 )
if [ -z "${geoNameID}" ]; then
echo " Country "$1" not found in GeoLite2 Country database, skipping it."
return 0
else
countryAdded+=("$1")
echo " Adding IPs for Country "$1" to ${ipListFilename}."
echo " #$1 IPs" >> ${ipListFilePath}
printf "%s\n" ${geoNameID[@]} > ${countryDir}/geoNameID.txt
grep -hwFf ${countryDir}/geoNameID.txt ${countryDir}/globalIPList.txt | \
cut -d, -f1 | sed -r 's/(^.*)/ "\1" 1;/' >> ${ipListFilePath}
rm ${countryDir}/geoNameID.txt
fi
}
sub_addIPsToIPList() {
geoNameID=$( grep -hwiF "$1" ${subDir}/subList.txt | cut -d, -f1 )
if [ -z "${geoNameID}" ]; then
echo " Location "$1" not found in GeoLite2 City database, skipping it."
return 0
else
subAdded+=("$1")
echo " Adding IPs for Location "$1" to ${ipListFilename}."
echo " #$1 IPs" >> ${ipListFilePath}
printf "%s\n" ${geoNameID[@]} > ${subDir}/geoNameID.txt
grep -hwFf ${subDir}/geoNameID.txt ${subDir}/globalIPList.txt | \
cut -d, -f1 | sed -r 's/(^.*)/ "\1" 1;/' >> ${ipListFilePath}
rm ${subDir}/geoNameID.txt
fi
}
extra_addIPsToIPList() {
if (($#)); then
echo " Adding Extra IPs to ${ipListFilename}."
echo " #Extra IPs" >> ${ipListFilePath}
printf ' "%s" 1;\n' "$@" >> "${ipListFilePath}"
fi
}
getLastModifiedArray=(country_getLastModified sub_getLastModified)
getZipArray=(country_getZip sub_getZip)
updateGeoIPDatabase () {
for index in "$@"; do
${getLastModifiedArray[index]}
${getZipArray[index]}
done
}
startIpListFile() {
if [ -f "${ipListFilePath}" ]; then
mv ${ipListFilePath} ${ipListFilePath}.old
fi
echo "Writing new ${ipListFilename}"
cat << EOF > ${ipListFilePath}
geo \$${comparedIPVariable} \$inIPList {
default 0;
EOF
}
country_loop () {
for code in "$@"; do
country_addIPsToIPList $code
done
}
sub_loop () {
for code in "$@"; do
sub_addIPsToIPList $code
done
}
endIpListFile() {
cat << EOF >> ${ipListFilePath}
}
EOF
}
writeIpList() {
startIpListFile
country_loop "${countryCodes[@]}"
sub_loop "${subCodes[@]}"
extra_addIPsToIPList "${extraIPs[@]}"
endIpListFile
}
writeDefaultConf() {
if [ -f "${defaultConfFilePath}" ]; then
mv ${defaultConfFilePath} ${defaultContFilePath}.old
fi
echo "Writing new default.conf"
cat << EOF > ${defaultConfFilePath}
server {
listen ${listenPort};
location /traefik {
add_header Content-Type "default_type text/plain";
if (\$inIPList = 1) {
return ${filterStatusCode};
}
return ${defaultStatusCode};
}
}
EOF
}
insertLocationList() {
sed -i "1s/^/\n/" ${ipListFilePath}
if ! [ -z "$subAdded" ]; then
subString=$(echo "${subAdded[@]}")
sed -i "1s/^/#Listed Sublocations: ${subString}\n/" ${ipListFilePath}
fi
if ! [ -z "$countryAdded" ]; then
countryString=$(echo "${countryAdded[@]}")
sed -i "1s/^/#Listed Countries: ${countryString}\n/" ${ipListFilePath}
fi
}
#MAIN
#################
#Check mandatory variables
if [ -z "$maxMindAccountId" ]; then
echo "ERROR: The MAXMIND_ID environment variable is empty, exiting script."
exit 1
elif [ -z "$maxMindLicenceKey" ]; then
echo "ERROR: The MAXMIND_KEY environment variable is empty, exiting script."
exit 1
fi
if [ -z "$filterType" ]; then
echo "ERROR: The FILTER_TYPE environment variable is empty, exiting script."
exit 1
elif [ "$filterType" = allow ]; then
filterStatusCode="${allowStatusCode}"
defaultStatusCode="${blockStatusCode}"
elif [ "$filterType" = block ]; then
filterStatusCode="${blockStatusCode}"
defaultStatusCode="${allowStatusCode}"
else
echo "ERROR: The value of FILTER_TYPE environment variable should be 'allow' or 'block', exiting script."
exit 1
fi
if ! [ -z "$countryCodes" ]; then
codesArray[0]=0
else
echo "COUNTRY_CODES environment variable is empty"
echo " Skipping Geolite2 Country database check."
fi
if ! [ -z "$subCodes" ]; then
codesArray[1]=1
else
echo "SUB_CODES environment variable is empty"
echo " Skipping Geolite2 City database check."
fi
if [ ${#codesArray[@]} -gt 0 ]; then
updateGeoIPDatabase "${codesArray[@]}"
if [ "$searchMode" == "true" ]; then
echo "--------------------------------------"
echo "Search mode: Not setting up IP filter, just checking for matches in GeoLite2 database."
./search.sh -c "${COUNTRY_CODES}" -s "${SUB_CODES}"
echo "Exiting script."
exit 1
else
writeIpList
writeDefaultConf
insertLocationList
echo "${ipListFilename} completed."
fi
else
echo "Both the COUNTRY_CODES and SUB_CODES environment variables are empty."
echo " No GeoIP locations available to filter."
echo " Exiting script."
exit 1
fi
exit 0;