A Rust library to query IP addresses using the ipquery.io API.
- Query details for a specific IP address
- Bulk query multiple IP addresses
- Fetch your own public IP address
To use this crate, add the following to your Cargo.toml
:
[dependencies]
ipapi = "0.1.0"
tokio = { version = "1.0", features = ["full"] }
The query_ip
function retrieves information about a specific IP address, including its ISP, location, and risk data.
use ipapi::query_ip;
use tokio;
#[tokio::main]
async fn main() {
let ip_info = query_ip("8.8.8.8").await.unwrap();
println!("{:#?}", ip_info);
}
IPInfo {
ip: "8.8.8.8",
isp: Some(ISPInfo { asn: Some("AS15169"), org: Some("Google LLC"), isp: Some("Google LLC") }),
location: Some(LocationInfo {
country: Some("United States"),
country_code: Some("US"),
city: Some("Mountain View"),
state: Some("California"),
zipcode: Some("94035"),
latitude: Some(37.386),
longitude: Some(-122.0838),
timezone: Some("America/Los_Angeles"),
localtime: Some("2024-11-09T12:45:32"),
}),
risk: Some(RiskInfo {
is_mobile: Some(false),
is_vpn: Some(false),
is_tor: Some(false),
is_proxy: Some(false),
is_datacenter: Some(true),
risk_score: Some(0),
}),
}
The query_bulk
function allows you to query information for multiple IP addresses at once.
use ipapi::query_bulk;
use tokio;
#[tokio::main]
async fn main() {
let ip_infos = query_bulk(&["8.8.8.8", "1.1.1.1"]).await.unwrap();
for info in ip_infos {
println!("{:#?}", info);
}
}
IPInfo {
ip: "8.8.8.8",
...
}
IPInfo {
ip: "1.1.1.1",
...
}
The query_own_ip
function retrieves the public IP address of the current machine.
use ipapi::query_own_ip;
use tokio;
#[tokio::main]
async fn main() {
let ip = query_own_ip().await.unwrap();
println!("Your IP Address: {}", ip);
}
Your IP Address: 203.0.113.45
pub async fn query_ip(ip: &str) -> Result<IPInfo, reqwest::Error>
Fetches detailed information about a specific IP address, including its ISP, location, and risk information.
ip
: A string slice representing the IP address to query.
Ok(IPInfo)
on success, containing details about the IP address.Err(reqwest::Error)
if the network request or JSON deserialization fails.
let result = query_ip("8.8.8.8").await?;
println!("{:?}", result);
pub async fn query_bulk(ips: &[&str]) -> Result<Vec<IPInfo>, reqwest::Error>
Fetches information for multiple IP addresses at once. Useful for batch processing.
ips
: A slice of string slices representing the list of IP addresses to query.
Ok(Vec<IPInfo>)
on success, containing details for each IP address.Err(reqwest::Error)
if the network request or JSON deserialization fails.
let ips = ["8.8.8.8", "1.1.1.1"];
let results = query_bulk(&ips).await?;
println!("{:?}", results);
pub async fn query_own_ip() -> Result<String, reqwest::Error>
Fetches the public IP address of the current machine. Useful for determining your own IP address.
Ok(String)
containing the public IP address.Err(reqwest::Error)
if the network request fails.
let ip = query_own_ip().await?;
println!("Your IP Address: {}", ip);
To run the tests for this crate:
cargo test
Contributions are welcome! Feel free to open issues or submit pull requests on the GitHub repository.
This project is licensed under the MIT License. See the LICENSE file for details.