diff options
Diffstat (limited to 'src/client/connector')
| -rw-r--r-- | src/client/connector/httpconnector.rs | 92 |
1 files changed, 71 insertions, 21 deletions
diff --git a/src/client/connector/httpconnector.rs b/src/client/connector/httpconnector.rs index dbd094c..5973410 100644 --- a/src/client/connector/httpconnector.rs +++ b/src/client/connector/httpconnector.rs @@ -35,23 +35,35 @@ pub struct HTTPConnector { impl HTTPConnector { pub fn new(pull_url: &str, ntfy_url: &str, ip_getter_url: &str) -> result::Result<Self, HTTPConnectorError> { - Ok(Self { - network_enforcer: Executor::new(&NETWORK_T, ["true"]).unwrap(), // .unwrap() should never panic - prev: String::new(), - pull_url: Url::parse(pull_url)?, - ntfy_url: Url::parse(ntfy_url)?, - ip_getter_url: match Url::parse(ip_getter_url) { - Ok(url) => url, - Err(_) => Url::parse((pull_url.to_string() + ip_getter_url).as_str())?, - }, - client: match Client::builder().build() { - Ok(c) => c, - Err(_) => match Client::builder().danger_accept_invalid_certs(true).build() { + Ok( + Self { + network_enforcer: Executor::new( + &NETWORK_T, + ["true"], + ) + .unwrap(), // .unwrap() should never panic + prev: String::new(), + pull_url: Url::parse(pull_url)?, + ntfy_url: Url::parse(ntfy_url)?, + ip_getter_url: match Url::parse(ip_getter_url) { + Ok(url) => url, + Err(_) => Url::parse((pull_url.to_string() + ip_getter_url).as_str())?, + }, + client: match Client::builder().build() { Ok(c) => c, - Err(_) => Client::builder().danger_accept_invalid_certs(true).danger_accept_invalid_hostnames(true).build()?, + Err(_) => match Client::builder() + .danger_accept_invalid_certs(true) + .build() + { + Ok(c) => c, + Err(_) => Client::builder() + .danger_accept_invalid_certs(true) + .danger_accept_invalid_hostnames(true) + .build()?, + }, }, }, - }) + ) } #[allow(clippy::only_used_in_recursion)] @@ -97,11 +109,28 @@ impl Connection for HTTPConnector { // consider StatusCode::NOT_MODIFIED fn update(&self) -> Result<Self::Ok, Self::Err> { - self.handle_response(|| self.client.get(self.pull_url.as_str())) + self.handle_response( + || { + self.client + .get( + self.pull_url + .as_str(), + ) + }, + ) } fn ntfy(&self, data: String) -> std::result::Result<(), Self::Err> { - self.handle_response(|| self.client.post(self.ntfy_url.as_str()).body(data.to_owned()))?; + self.handle_response( + || { + self.client + .post( + self.ntfy_url + .as_str(), + ) + .body(data.to_owned()) + }, + )?; Ok(()) } @@ -115,14 +144,35 @@ impl Connection for HTTPConnector { } fn force_network(&mut self) { - _ = self.network_enforcer.run(); + _ = self + .network_enforcer + .run(); } fn get_ip(&self) -> Option<IpAddr> { - match self.handle_response(|| self.client.get(self.ip_getter_url.as_str())) { - Result::Ok(ip) => Some(ip.parse().ok()?), - Result::Err(_) => match self.handle_response(|| self.client.get("ifconfig.me")) { - Ok(ip) => Some(ip.parse().ok()?), + match self.handle_response( + || { + self.client + .get( + self.ip_getter_url + .as_str(), + ) + }, + ) { + Result::Ok(ip) => Some( + ip.parse() + .ok()?, + ), + Result::Err(_) => match self.handle_response( + || { + self.client + .get("ifconfig.me") + }, + ) { + Ok(ip) => Some( + ip.parse() + .ok()?, + ), Err(_) => None, }, } |
