You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
haproxy-wi/app/scripts/ansible/roles/geoip.sh.j2

52 lines
1.3 KiB

#!/bin/bash
set -e -o pipefail
country_location="GeoLite2-Country-CSV_*/GeoLite2-Country-Locations-en.csv"
country_ip="GeoLite2-Country-CSV_*/GeoLite2-Country-Blocks-IPv4.csv"
haproxy_dir="{{haproxy_dir}}"
MAXMIND_LICENSE="{{maxmind_key}}"
PROXY="{{PROXY}}"
if [[ $PROXY != "" ]]
then
export http_proxy="$PROXY"
export https_proxy="$PROXY"
fi
cd /tmp
if [[ ! -d geo2lite ]]; then
mkdir geo2lite && cd geo2lite && mkdir subnets
else
cd geo2lite
fi
if [[ -f geoip2lite.zip ]]; then
rm -f geoip2lite.zip
fi
wget "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country-CSV&license_key=${MAXMIND_LICENSE}&suffix=zip" -qO geoip2lite.zip && unzip -oq geoip2lite.zip
function get_ips_by_country {
rm -f subnets/$2.subnets
cat $country_ip |grep $1 |awk -F"," '{print $1}' >> subnets/$2.subnets
}
while read line; do
country_code_dig=$(echo $line |awk -F"," '{print $1}')
country_code_buk=$(echo $line |awk -F"," '{print $5}')
if [[ $country_code_dig != '' ]] && [[ $country_code_buk != '' ]] && [[ $country_code_buk != 'country_iso_code' ]] ; then
get_ips_by_country $country_code_dig $country_code_buk
fi
done < $country_location
if [[ ! -d "$haproxy_dir"/geoip ]]; then
mkdir "$haproxy_dir"/geoip
fi
cp subnets/* "$haproxy_dir"/geoip
systemctl reload haproxy