-
Notifications
You must be signed in to change notification settings - Fork 4
ANON-289 - Update geoipdb using maxmind #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
web3yurii
wants to merge
1
commit into
main
Choose a base branch
from
ANON-289-update-geoip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| geoipgen/target/ | ||
| geoipgen/CArgo.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Navigate to the directory where the script is located | ||
|
|
||
| ``` | ||
| cd scripts/maint/geoip/maxmind | ||
| ``` | ||
|
|
||
| # Run the script | ||
|
|
||
| ``` | ||
| ./update_geoipdb.sh | ||
| ``` | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [workspace] | ||
|
|
||
| [package] | ||
| name = "geoipgen" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| ipnetwork = "0.20" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| use std::collections::HashMap; | ||
| use std::fs::File; | ||
| use std::io::{BufRead, BufReader, Write}; | ||
| use std::net::IpAddr; | ||
| use ipnetwork::IpNetwork; | ||
|
|
||
| fn ipv4_to_u32(ip: std::net::Ipv4Addr) -> u32 { | ||
| u32::from(ip) | ||
| } | ||
|
|
||
| fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| let loc_file = BufReader::new(File::open("geoip-csv/GeoLite2-Country-Locations-en.csv")?); | ||
| let mut id_map = HashMap::new(); | ||
|
|
||
| for line in loc_file.lines().skip(1) { | ||
| let line = line?; | ||
| let fields: Vec<&str> = line.split(',').collect(); | ||
| if fields.len() > 5 && !fields[0].is_empty() && !fields[4].is_empty() { | ||
| id_map.insert(fields[0].to_string(), fields[4].to_string()); // geoname_id -> iso_code | ||
| } | ||
| } | ||
|
|
||
| let mut geoip = File::create("geoip")?; | ||
| let v4_file = BufReader::new(File::open("geoip-csv/GeoLite2-Country-Blocks-IPv4.csv")?); | ||
| for line in v4_file.lines().skip(1) { | ||
| let line = line?; | ||
| let fields: Vec<&str> = line.split(',').collect(); | ||
| if fields.len() > 1 { | ||
| if let Some(iso) = id_map.get(fields[1]) { | ||
| if let Ok(IpNetwork::V4(net)) = fields[0].parse() { | ||
| let start = ipv4_to_u32(net.network()); | ||
| let end = ipv4_to_u32(net.broadcast()); | ||
| writeln!(geoip, "{},{},{}", start, end, iso)?; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let mut geoip6 = File::create("geoip6")?; | ||
| let v6_file = BufReader::new(File::open("geoip-csv/GeoLite2-Country-Blocks-IPv6.csv")?); | ||
| for line in v6_file.lines().skip(1) { | ||
| let line = line?; | ||
| let fields: Vec<&str> = line.split(',').collect(); | ||
| if fields.len() > 1 { | ||
| if let Some(iso) = id_map.get(fields[1]) { | ||
| if let Ok(IpNetwork::V6(net)) = fields[0].parse() { | ||
| let start = net.network(); | ||
| let end = net.broadcast(); | ||
| writeln!(geoip6, "{},{},{}", start, end, iso)?; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| println!("Done: wrote geoip and geoip6"); | ||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| if [ -z "$MAXMIND_LICENSE_KEY" ]; then | ||
| echo " MAXMIND_LICENSE_KEY env variable is not set" | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ -z "$MAXMIND_ACCOUNT_ID" ]; then | ||
| echo " MAXMIND_ACCOUNT_ID env variable is not set" | ||
| exit 1 | ||
| fi | ||
|
|
||
| LICENSE_KEY="$MAXMIND_LICENSE_KEY" | ||
| ACCOUNT_ID="$MAXMIND_ACCOUNT_ID" | ||
| ZIP_FILE="geoip.zip" | ||
| CSV_DIR="geoip-csv" | ||
| OUT_DIR="../../../../src/config" | ||
|
|
||
| cleanup() { | ||
| echo "Cleaning up..." | ||
| rm -f "$ZIP_FILE" | ||
| rm -rf "$CSV_DIR" | ||
| rm -f geoip geoip6 | ||
| } | ||
| trap cleanup EXIT | ||
|
|
||
| echo "Downloading GeoLite2 CSV..." | ||
| curl -L -o "$ZIP_FILE" -u "$ACCOUNT_ID:$LICENSE_KEY" \ | ||
| "https://download.maxmind.com/geoip/databases/GeoLite2-Country-CSV/download?suffix=zip" | ||
|
|
||
| echo "Extracting CSV files..." | ||
| mkdir -p "$CSV_DIR" | ||
| unzip -j "$ZIP_FILE" '*.csv' -d "$CSV_DIR" | ||
| ls -lh "$CSV_DIR" | ||
|
|
||
| echo "Running geoipgen Rust converter..." | ||
| cargo run --release --manifest-path=geoipgen/Cargo.toml | ||
|
|
||
| echo "Moving output files to $OUT_DIR..." | ||
| mkdir -p "$OUT_DIR" | ||
| mv geoip geoip6 "$OUT_DIR/" | ||
|
|
||
| echo "Done!" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using a dedicated CSV parsing library (e.g., the 'csv' crate) to handle edge cases like quoted fields containing commas for improved data reliability.