use regex::Regex; use reqwest::blocking::Client; use crate::errors::Error; pub fn get_public_ip_address() -> Result { //see https://wiki.ubuntuusers.de/FritzBox/Skripte/ let resp = Client::new() .post("http://fritz.box:49000/igdupnp/control/WANIPConn1") .header("Content-Type", "text/xml; charset=utf-8") .header( "SoapAction", "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress", ) .body(" ") .send() .map_err(|_| Error::new("Error fetching result via Http"))?; if resp.status() != 200 { return Err(Error::new(&format!("Status: {}", resp.status()))); } let xml = resp.text().map_err(|_| Error::new("No data"))?; let regex = Regex::new(r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}") .map_err(|_| Error::new("Regex parsing error"))?; let text = regex .captures(&xml) .map(|cap| cap.get(0)) .flatten() .map(|cap| cap.as_str()) .map(|str| String::from(str)) .ok_or(Error::new("Regex no match")); text }