1use std::time::Duration;
2
3use serde::Deserialize;
4use surf::{Client, Config};
5#[derive(Deserialize)]
6pub struct IpInfo {
7 #[serde(rename = "Answer")]
8 pub answer: String,
9}
10
11pub async fn get_public_ip() -> Result<String, surf::Error> {
12 let client: Client = Config::new()
13 .set_timeout(Some(Duration::from_secs(5)))
14 .try_into()
15 .unwrap();
16 let res: IpInfo = client
17 .get("https://api.duckduckgo.com/?q=ip&format=json")
18 .recv_json()
19 .await?;
20 let ip = res.answer.split(" ").collect::<Vec<&str>>()[4];
21 Ok(ip.to_string())
22}