Skip to content

Instantly share code, notes, and snippets.

@mmh
Created September 14, 2016 18:45
Show Gist options
  • Save mmh/725b9e1a1e396062b1352e01bef71153 to your computer and use it in GitHub Desktop.
Save mmh/725b9e1a1e396062b1352e01bef71153 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Download, extract and convert maxmind country db to varnish acl format for countries defines in the COUNTRIES array
# DB found here: http://dev.maxmind.com/geoip/geoip2/geolite2/
# [email protected], 2015
set -o errexit
set -o nounset
declare -A COUNTRIES=( [syria]=",SY," [sudan]=",SD," [northkorea]=",KP," [cuba]=",CU," [iran]=",IR,")
TMPDIR="/tmp/maxmind-geoip"
# Cleanup possible old run
if [ -d $TMPDIR ]; then
rm -rf $TMPDIR
fi
mkdir $TMPDIR
wget -q -O $TMPDIR/GeoLite2-Country-CSV.zip http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip
cd $TMPDIR
unzip -q GeoLite2-Country-CSV.zip
cd GeoLite2-Country-CSV_*
for K in "${!COUNTRIES[@]}"; do
OUTFILE=$TMPDIR/$K"_ips.vcl"
# echo ${COUNTRIES[$K]}
COUNTRYID=$(grep ${COUNTRIES[$K]} GeoLite2-Country-Locations-en.csv | cut -d, -f 1)
echo "acl $K {" > $OUTFILE
for BLOCK in $(grep $COUNTRYID GeoLite2-Country-Blocks-IPv4.csv | cut -d, -f 1); do
echo " \"$BLOCK;" >> $OUTFILE
done
echo "}" >> $OUTFILE
perl -p -i -e 's#/#"/#g' $OUTFILE
echo "$OUTFILE generated"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment