summaryrefslogtreecommitdiff
path: root/src/client/connector/httpconnector.rs
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-09-21 23:08:48 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-09-21 23:08:48 +0800
commit0cfc9d0f4acac43ce20b445ac883b7cf0ca1dc79 (patch)
tree14a465146b8858f1f46b6a4552604adfe1328b9e /src/client/connector/httpconnector.rs
parent8cdda8a10c563b3d6e6e306c421583f1ade3e6c1 (diff)
format
Diffstat (limited to 'src/client/connector/httpconnector.rs')
-rw-r--r--src/client/connector/httpconnector.rs92
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,
},
}