Skip to content

Commit

Permalink
Use IPInfo as secondary source (#2683)
Browse files Browse the repository at this point in the history
  • Loading branch information
enoch85 authored Sep 22, 2024
1 parent 0bd74fb commit 7b1130a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
23 changes: 18 additions & 5 deletions lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ HTTP2_CONF="/etc/apache2/mods-available/http2.conf"
# GeoBlock
GEOBLOCK_MOD_CONF="/etc/apache2/conf-available/geoblock.conf"
GEOBLOCK_MOD="/etc/apache2/mods-available/maxminddb.load"
GEOBLOCK_DIR="/usr/share/GeoIP"
# PHP-FPM
PHPVER=8.3
PHP_FPM_DIR=/etc/php/$PHPVER/fpm
Expand Down Expand Up @@ -404,7 +405,8 @@ curl "https://api.metadefender.com/v4/hash/$hash" -H "apikey: $apikey"
download_geoip_mmdb() {
maxmind_geoip
export MwKfcYATm43NMT
export i9HL69SLnp4ymy
export i9HL69SLnp4ymy
export x8v8GyVQg2UejdPh
{
echo "GEOIPUPDATE_ACCOUNT_ID=$MwKfcYATm43NMT"
echo "GEOIPUPDATE_LICENSE_KEY=$i9HL69SLnp4ymy"
Expand All @@ -414,18 +416,29 @@ download_geoip_mmdb() {
echo "GEOIPUPDATE_VERBOSE=1"
} > /tmp/dockerenv
unset MwKfcYATm43NMT
unset i9HL69SLnp4ymy
unset i9HL69SLnp4ymy
install_docker
if docker run --name maxmind --env-file /tmp/dockerenv -v /usr/share/GeoIP:/usr/share/GeoIP ghcr.io/maxmind/geoipupdate
if docker run --name maxmind --env-file /tmp/dockerenv -v "$GEOBLOCK_DIR":"$GEOBLOCK_DIR" ghcr.io/maxmind/geoipupdate
then
docker rm -f maxmind
rm -f /tmp/dockerenv
# Since only one mmdb file can exist at the same time due to Apache "if" confitions, remove IPInfos config
rm -f "$GEOBLOCK_DIR"/IPInfo-Country.mmdb
else
docker rm -f maxmind
rm -f /tmp/dockerenv
msg_box "Update limit for Maxmind GeoDatabase reached! Please try again tomorrow."
return 1
print_text_in_color "$ICyan" "Rate limit for Maxmind GeoDatabase reached! We're now trying to get the Country Database from https://ipinfo.io instead."
if ! curl -sfL https://ipinfo.io/data/free/country.mmdb?token="$x8v8GyVQg2UejdPh" -o "$GEOBLOCK_DIR"/IPInfo-Country.mmdb
then
msg_box "Sorry, we couldn't get the needed IP geolocation database from any source, please try again in 24 hours."
return 1
else
# Since only one mmdb file can exist at the same time due to Apache "if" confitions, remove MaxMinds config
rm -f "$GEOBLOCK_DIR"/GeoLite2-Country.mmdb
return 0
fi
fi
unset x8v8GyVQg2UejdPh
}

# Check if process is runnnig: is_process_running dpkg
Expand Down
22 changes: 16 additions & 6 deletions network/geoblock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ else
# Remove old database files
find /var/scripts -type f -regex \
"$SCRIPTS/202[0-9]-[01][0-9]-Maxmind-Country-IPv[46]\.dat" -delete
find /usr/share/GeoIP -type f -regex \
find "$GEOBLOCK_DIR" -type f -regex \
"*.dat" -delete
rm -f "$GEOBLOCK_DIR"/IPInfo-Country.mmdb
# Remove Apache2 mod
if [ -f "$GEOBLOCK_MOD" ]
then
Expand Down Expand Up @@ -69,8 +70,8 @@ find /usr/share/GeoIP -type f -regex \
# Show successful uninstall if applicable
removal_popup "$SCRIPT_NAME"
# Make sure it's clean from unused packages and files
apt purge libmaxminddb0* libmaxminddb-dev* mmdb-bin* apache2-dev* -y
apt autoremove -y
apt-get purge libmaxminddb0* libmaxminddb-dev* mmdb-bin* apache2-dev* -y
apt-get autoremove -y
check_command systemctl restart apache2
fi

Expand Down Expand Up @@ -221,10 +222,19 @@ fi
cat << GEOBLOCKCONF_CREATE > "$GEOBLOCK_MOD_CONF"
<IfModule mod_maxminddb.c>
MaxMindDBEnable On
MaxMindDBFile DB /usr/share/GeoIP/GeoLite2-Country.mmdb
MaxMindDBEnv MM_CONTINENT_CODE DB/continent/code
MaxMindDBEnv MM_COUNTRY_CODE DB/country/iso_code
# Check for IPinfo mmdb
<IfFile "$GEOBLOCK_DIR/IPInfo-Country.mmdb">
MaxMindDBFile DB $GEOBLOCK_DIR/IPInfo-Country.mmdb
MaxMindDBEnv MM_CONTINENT_CODE DB/continent
MaxMindDBEnv MM_COUNTRY_CODE DB/country
</IfFile>
# Check for Maxmind mmdb
<IfFile "$GEOBLOCK_DIR/GeoLite2-Country.mmdb">
MaxMindDBFile DB $GEOBLOCK_DIR/GeoLite2-Country.mmdb
MaxMindDBEnv MM_CONTINENT_CODE DB/continent/code
MaxMindDBEnv MM_COUNTRY_CODE DB/country/iso_code
</IfFile>
</IfModule>
# Geoblock rules
Expand Down
2 changes: 1 addition & 1 deletion nextcloud_update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ elif [ -f "$GEOBLOCK_MOD" ]
then
if download_geoip_mmdb
then
print_text_in_color "$IGreen" "MaxMind database updated!"
print_text_in_color "$IGreen" "GeoBlock database updated!"
fi
fi

Expand Down

0 comments on commit 7b1130a

Please sign in to comment.