incsw / geoip2-rs Goto Github PK
View Code? Open in Web Editor NEWFast GeoIP2 Reader for Rust
License: MIT License
Fast GeoIP2 Reader for Rust
License: MIT License
I'm using a paid download of GeoIP database. Not sure if it's the correct one because the documentation is awful.
IPv4 works fine.
Here is the link
https://download.maxmind.com/app/geoip_download?edition_id=GeoIP2-City&suffix=tar.gz&license_key=[mykey]
Then I load it, and query it.
let buffer = fs::read(file_name).unwrap();
let static_buffer: &'static [u8] = Box::leak(Box::new(buffer));
let reader = Reader::<City>::from_bytes(static_buffer).unwrap();
Later on
match result.city === Returns None
This works on the NodeJS GeoIP package, so I assume it's an issue with this library?
The IP works with the geo ip tester.
2606:2800:220:1:248:1893:25c8:1946, it's Cali address.
let mut ip = req.remote_addr().to_string();
// For testing local
if ip.contains("127.0.0.1") {
// ip = "45.88.221.137".to_string();
ip = "2606:2800:220:1:248:1893:25c8:1946".to_string(); // Is a valid IP from Cali
}
let ip_address: IpAddr = match FromStr::from_str(&ip) {
Ok(ip) => ip,
Err(e) => {
eprintln!("Error parsing ip: {0:?}", e);
eprint!("Error parsing ip: {0:?}", ip);
return Err(ApiError {
message: "Error parsing ip".to_string(),
status: "error-with-ip".to_string(),
});
}
};
let reader = match GEO_IP_CITY.get() {
Some(reader) => reader,
None => {
eprintln!("Error getting GEO_IP_CITY not setup");
return Err(ApiError {
message: "GEO_IP_CITY not setup".to_string(),
status: "error-with-ip".to_string(),
});
}
};
let result: geoip2::City = match reader.lookup(ip_address) {
Ok(result) => result,
Err(e) => {
eprintln!("Error getting result from geo-ip: {0:?}", e);
return Err(ApiError {
message: "Error getting result from geo-ip".to_string(),
status: "error-with-ip".to_string(),
});
}
};
let city = match result.city {
Some(city) => city,
None => {
eprintln!("Error getting city on result for geo-ip, no city was returned");
return Err(ApiError {
message: "Error getting city, required for this action".to_string(),
status: "error-with-ip".to_string(),
});
}
};
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.