Skip to content

Commit

Permalink
Geo2.bin file checks with our server to see if its uptodate, and down…
Browse files Browse the repository at this point in the history
…loads a fresh one if it is old.
  • Loading branch information
thebracket committed Nov 14, 2024
1 parent 97781e9 commit fd9af3b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/rust/lqosd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ timerfd = { workspace = true }
crossbeam-channel = { workspace = true }
arc-swap = { workspace = true }
crossbeam-queue = { workspace = true }
sha256 = "1.5.0"

# For memory debugging
allocative = { version = "0.3.3", features = [ "dashmap" ] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,38 @@ impl GeoTable {
fn download() -> anyhow::Result<()> {
tracing::warn!("Downloading ASN-IP Table");
let file_path = Self::file_path();
let url = "https://stats.libreqos.io/geo2.bin";
let url = "https://insight.libreqos.com/geo2.bin";
let response = reqwest::blocking::get(url)?;
let content = response.bytes()?;
let bytes = &content[0..];
std::fs::write(file_path, bytes)?;
tracing::info!("Downloaded geo2.bin");
Ok(())
}

fn is_file_uptodate() -> anyhow::Result<bool> {
let bytes = std::fs::read(&Self::file_path())?; // Vec<u8>
let hash = sha256::digest(&bytes);

// Using blocking reqwest, retrieve the checksum as a string from https://insight.libreqos.com/geo/checksum
let checksum = reqwest::blocking::get("https://insight.libreqos.com/geo/checksum")?.text()?;
if checksum == hash {
return Ok(true)
}

Ok(false)
}

pub fn load() -> anyhow::Result<Self> {
let path = Self::file_path();
if !path.exists() {
info!("geo.bin not found - trying to download it");
info!("geo2.bin not found - trying to download it");
Self::download()?;
} else {
if !Self::is_file_uptodate().unwrap_or(true) {
info!("geo2.bin is outdated - trying to download it");
Self::download()?;
}
}

// Decompress and deserialize
Expand Down

0 comments on commit fd9af3b

Please sign in to comment.