ipify_async/
lib.rs

1use reqwest;
2use std::net::Ipv4Addr;
3
4type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync + 'static>>;
5
6pub async fn get_ip() -> Result<Ipv4Addr> {
7    let ip: Vec<u8> = reqwest::get("https://api.ipify.org")
8        .await?
9        .text()
10        .await?
11        .split(".")
12        .map(|x| x.parse::<u8>().unwrap())
13        .collect();
14    Ok(Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]))
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20
21    #[tokio::test]
22    async fn ensure_public() {
23        // Simple test to determine the IP returned by IPify is not private.
24        let ip = get_ip().await;
25        assert_eq!(ip.unwrap().is_private(), false);
26    }
27}