From 6bdc5a7c6d9d986115e4e7a5214234c10ce123b2 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sun, 14 Sep 2025 21:41:15 +0800 Subject: feat: client initialization fetches basic infos refactor: connector manager -> client manager, similar concept, different mentality note: if custom ip fetcher fails then it defaults to ifconfig.me --- src/client/connector/connectors/httpconnector.rs | 24 +++++++++++++++++++++++- src/client/connector/manager.rs | 15 --------------- 2 files changed, 23 insertions(+), 16 deletions(-) delete mode 100644 src/client/connector/manager.rs (limited to 'src/client/connector') diff --git a/src/client/connector/connectors/httpconnector.rs b/src/client/connector/connectors/httpconnector.rs index 0b9d590..8052b25 100644 --- a/src/client/connector/connectors/httpconnector.rs +++ b/src/client/connector/connectors/httpconnector.rs @@ -11,18 +11,28 @@ pub struct HTTPConnector { network_updater: Executor, pull_url: Url, ntfy_url: Url, + ip_getter_url: Url, client: Client, prev: ::Ok, } impl HTTPConnector { - pub fn new(pull_url: &str, ntfy_url: &str) -> std::result::Result { + pub fn new( + pull_url: &str, + ntfy_url: &str, + ip_getter_url: &str, + ) -> std::result::Result { + // TODO: proper type assert_eq!(TASKS[2].name, "network"); Ok(Self { network_updater: Executor::new(&TASKS[2], ["on".to_string()]).unwrap(), 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: Client::new(), }) } @@ -96,4 +106,16 @@ impl Connection for HTTPConnector { fn force_network(&mut self) { _ = self.network_updater.run(); } + + fn get_ip(&self) -> Option { + match self.handle_response(|| self.client.get(self.ip_getter_url.as_str()).send()) { + Result::Ok(ip) => Some(ip.parse().ok()?), + Result::Err(_) => { + match self.handle_response(|| self.client.get("ifconfig.me").send()) { + Ok(ip) => Some(ip.parse().ok()?), + Err(_) => None, + } + } + } + } } diff --git a/src/client/connector/manager.rs b/src/client/connector/manager.rs deleted file mode 100644 index ff52daf..0000000 --- a/src/client/connector/manager.rs +++ /dev/null @@ -1,15 +0,0 @@ -use crate::connector::Connection; - -pub struct ConnectorManager { - connector: C, -} - -impl ConnectorManager { - pub fn new(connector: C) -> Self { - Self { connector } - } - - pub fn enter_loop(&mut self) { - self.connector.r#loop(); - } -} -- cgit v1.2.3