first implementation

This commit is contained in:
Tim Wundenberg
2023-04-21 18:10:58 +02:00
parent 90562b3834
commit 3607826c39
6 changed files with 1310 additions and 1 deletions

38
src/public_ip.rs Normal file
View File

@@ -0,0 +1,38 @@
use regex::Regex;
use reqwest::blocking::Client;
use crate::errors::Error;
pub fn get_public_ip_address() -> Result<String, Error> {
//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("<?xml version='1.0' encoding='utf-8'?> <s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'> <s:Body> <u:GetExternalIPAddress xmlns:u='urn:schemas-upnp-org:service:WANIPConnection:1' /> </s:Body> </s:Envelope>")
.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
}