Files
ntopng-geoip/install-ntopng-geoip.sh

96 lines
3.2 KiB
Bash
Raw Permalink Normal View History

2026-04-19 16:40:22 +01:00
#!/bin/sh
# ========================================================================
# OPNsense ntopng GeoIP installer
# One-command setup: MaxMind City+Country + ipinfo.io ASN
# Silent updater script: /usr/local/bin/ntopng-updategeo.sh
# You can get free key for https://ipinfo.io/ and https://www.maxmind.com/
# Ipinfo has more and acurate records, but Ntopng does not support it
2026-04-19 17:14:09 +01:00
# Run cron every Sunday 1AM: 0 1 * * 7
# Run cron every month 1st at 3:30am: 30 3 1 * *
2026-04-19 16:40:22 +01:00
# ========================================================================
if [ $# -ne 2 ]; then
echo "Usage: $0 MAXMIND_LICENSE_KEY IPINFO_TOKEN"
echo "Example:"
2026-04-19 16:53:24 +01:00
echo " fetch -o - https://raw.githubusercontent.com/ghostersk/ntopng-geoip/refs/heads/main/install-ntopng-geoip.sh | sh -s -- YOUR_MAXMIND_KEY your_ipinfo_token"
2026-04-19 16:40:22 +01:00
exit 1
fi
MAXMIND_KEY="$1"
IPINFO_TOKEN="$2"
echo "=== Setting up ntopng GeoIP updater ==="
# 1. Create config with your keys
cat << EOF > /usr/local/etc/GeoIP.conf
LicenseKey ${MAXMIND_KEY}
IPINFO_TOKEN ${IPINFO_TOKEN}
EOF
2026-04-19 17:14:09 +01:00
# 2. Create Crontab action for web ui
cat << EOF > /usr/local/opnsense/service/conf/actions.d/actions_ntopnggeo.conf
[update_geo]
command:/usr/local/bin/ntopng-updategeo.sh
parameters:
type:script
message:Updating ntopng GeoIP databases
description:ntopng GeoIP Update
EOF
# 3. Restarts Config service
service configd restart
# 4. Create the silent updater script
2026-04-19 16:40:22 +01:00
cat << 'UPDATER' > /usr/local/bin/ntopng-updategeo.sh
#!/bin/sh
# Silent ntopng GeoIP updater - MaxMind City/Country + ipinfo ASN
# No output on success, only critical errors
set -e
GEOIP_DIR="/usr/local/share/ntopng/httpdocs/geoip"
CONF_FILE="/usr/local/etc/GeoIP.conf"
mkdir -p "${GEOIP_DIR}"
cd "${GEOIP_DIR}"
LICENSE_KEY=$(awk -F ' ' '/^#/ {next} $1=="LicenseKey" {print $2}' "${CONF_FILE}")
IPINFO_TOKEN=$(awk -F ' ' '/^#/ {next} $1=="IPINFO_TOKEN" {print $2}' "${CONF_FILE}")
if [ -z "${LICENSE_KEY}" ] || [ -z "${IPINFO_TOKEN}" ]; then
echo "ERROR: Missing LicenseKey or IPINFO_TOKEN in ${CONF_FILE}" >&2
exit 1
fi
# MaxMind City
2026-04-19 16:53:24 +01:00
fetch -o /tmp/GeoLite2-City.tar.gz \
"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=${LICENSE_KEY}&suffix=tar.gz"
tar xzf /tmp/GeoLite2-City.tar.gz -C /tmp
mv /tmp/GeoLite2-City_*/GeoLite2-City.mmdb "${GEOIP_DIR}/" 2>/dev/null || true
rm -f /tmp/GeoLite2-City.tar.gz
rm -rf /tmp/GeoLite2-City_* 2>/dev/null || true
2026-04-19 16:40:22 +01:00
# ipinfo.io ASN (saved as Geolite2-ASN.mmdb instead of maxmind)
fetch -q -o GeoLite2-ASN.mmdb "https://ipinfo.io/data/free/asn.mmdb?token=${IPINFO_TOKEN}"
# Permissions
chown root:wheel *.mmdb 2>/dev/null || true
chmod 444 *.mmdb
# Restart ntopng (silent)
/usr/local/etc/rc.d/ntopng restart >/dev/null 2>&1 || true
UPDATER
2026-04-19 17:14:09 +01:00
# 5. Make executable
2026-04-19 16:40:22 +01:00
chmod 755 /usr/local/bin/ntopng-updategeo.sh
2026-04-19 17:14:09 +01:00
# 6. Run once (silent on success)
2026-04-19 16:40:22 +01:00
echo "→ Running initial update (this may take a few seconds)..."
/usr/local/bin/ntopng-updategeo.sh
echo " Updater installed at: /usr/local/bin/ntopng-updategeo.sh"
echo " Config file: /usr/local/etc/GeoIP.conf"
echo ""
echo "Now add it to Cron (System → Settings → Cron):"
echo " Command: /usr/local/bin/ntopng-updategeo.sh"
echo " Run Weekly or Monthly at any hour you like (e.g. 3:00 AM)"