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