diff --git a/infrastructure_files/download-geolite2.sh b/infrastructure_files/download-geolite2.sh index 2bd1c09045e..7cc5528ae29 100755 --- a/infrastructure_files/download-geolite2.sh +++ b/infrastructure_files/download-geolite2.sh @@ -22,41 +22,95 @@ then exit 1 fi -DATABASE_URL="https://download.maxmind.com/geoip/databases/GeoLite2-City/download?suffix=tar.gz" -SIGNATURE_URL="https://download.maxmind.com/geoip/databases/GeoLite2-City/download?suffix=tar.gz.sha256" - -# Download the database and signature files -echo "Downloading database file..." -DATABASE_FILE=$(curl -s -u "$MM_ACCOUNT_ID":"$MM_LICENSE_KEY" -L -O -J "$DATABASE_URL" -w "%{filename_effective}") -echo "Downloading signature file..." -SIGNATURE_FILE=$(curl -s -u "$MM_ACCOUNT_ID":"$MM_LICENSE_KEY" -L -O -J "$SIGNATURE_URL" -w "%{filename_effective}") - -# Verify the signature -echo "Verifying signature..." -if sha256sum -c --status "$SIGNATURE_FILE"; then - echo "Signature is valid." -else - echo "Signature is invalid. Aborting." +if ! command -v sqlite3 &> /dev/null +then + echo "sqlite3 is not installed or not in PATH, please install with your package manager. e.g. sudo apt install sqlite3" > /dev/stderr exit 1 fi -# Unpack the database file -EXTRACTION_DIR=$(basename "$DATABASE_FILE" .tar.gz) -echo "Unpacking $DATABASE_FILE..." -mkdir -p "$EXTRACTION_DIR" -tar -xzvf "$DATABASE_FILE" - -# Create a SHA256 signature file -MMDB_FILE="GeoLite2-City.mmdb" -cd "$EXTRACTION_DIR" -sha256sum "$MMDB_FILE" > "$MMDB_FILE.sha256" -echo "SHA256 signature created for $MMDB_FILE." -cd - - -# Remove downloaded files -rm "$DATABASE_FILE" "$SIGNATURE_FILE" - -# Done. Print next steps -echo "Process completed successfully." -echo "Now you can place $EXTRACTION_DIR/$MMDB_FILE to 'datadir' of management service." -echo -e "Example:\n\tdocker compose cp $EXTRACTION_DIR/$MMDB_FILE management:/var/lib/netbird/" +download_geolite_mmdb() { + DATABASE_URL="https://download.maxmind.com/geoip/databases/GeoLite2-City/download?suffix=tar.gz" + SIGNATURE_URL="https://download.maxmind.com/geoip/databases/GeoLite2-City/download?suffix=tar.gz.sha256" + + # Download the database and signature files + echo "Downloading database file..." + DATABASE_FILE=$(curl -s -u "$MM_ACCOUNT_ID":"$MM_LICENSE_KEY" -L -O -J "$DATABASE_URL" -w "%{filename_effective}") + echo "Downloading signature file..." + SIGNATURE_FILE=$(curl -s -u "$MM_ACCOUNT_ID":"$MM_LICENSE_KEY" -L -O -J "$SIGNATURE_URL" -w "%{filename_effective}") + + # Verify the signature + echo "Verifying signature..." + if sha256sum -c --status "$SIGNATURE_FILE"; then + echo "Signature is valid." + else + echo "Signature is invalid. Aborting." + exit 1 + fi + + # Unpack the database file + EXTRACTION_DIR=$(basename "$DATABASE_FILE" .tar.gz) + echo "Unpacking $DATABASE_FILE..." + mkdir -p "$EXTRACTION_DIR" + tar -xzvf "$DATABASE_FILE" + + # Create a SHA256 signature file + MMDB_FILE="GeoLite2-City.mmdb" + cd "$EXTRACTION_DIR" + sha256sum "$MMDB_FILE" > "$MMDB_FILE.sha256" + echo "SHA256 signature created for $MMDB_FILE." + cd - + + # Remove downloaded files + rm "$DATABASE_FILE" "$SIGNATURE_FILE" + + # Done. Print next steps + echo "Process completed successfully." + echo "Now you can place $EXTRACTION_DIR/$MMDB_FILE to 'datadir' of management service." + echo -e "Example:\n\tdocker compose cp $EXTRACTION_DIR/$MMDB_FILE management:/var/lib/netbird/" +} + + +download_geolite_csv_and_create_sqlite_db() { + DATABASE_URL="https://download.maxmind.com/geoip/databases/GeoLite2-City-CSV/download?suffix=zip" + SIGNATURE_URL="https://download.maxmind.com/geoip/databases/GeoLite2-City-CSV/download?suffix=zip.sha256" + + + # Download the database file + echo "Downloading database file..." + DATABASE_FILE=$(curl -s -u "$MM_ACCOUNT_ID":"$MM_LICENSE_KEY" -L -O -J "$DATABASE_URL" -w "%{filename_effective}") + echo "Downloading signature file..." + SIGNATURE_FILE=$(curl -s -u "$MM_ACCOUNT_ID":"$MM_LICENSE_KEY" -L -O -J "$SIGNATURE_URL" -w "%{filename_effective}") + + # Verify the signature + echo "Verifying signature..." + if sha256sum -c --status "$SIGNATURE_FILE"; then + echo "Signature is valid." + else + echo "Signature is invalid. Aborting." + exit 1 + fi + + # Unpack the database file + EXTRACTION_DIR=$(basename "$DATABASE_FILE" .zip) + DB_NAME="geonames.db" + + unzip "$DATABASE_FILE" + +# Create SQLite database and import data from CSV +sqlite3 "$DB_NAME" <