diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-09-14 21:41:15 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-09-14 21:41:15 +0800 |
| commit | 6bdc5a7c6d9d986115e4e7a5214234c10ce123b2 (patch) | |
| tree | da9d9a4c458af97de2e78fc55a7c0f20a607f0c4 /src/client/connector | |
| parent | bebc63d7bcaaf30f9ffe0a58548250c8cc7189d4 (diff) | |
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
Diffstat (limited to 'src/client/connector')
| -rw-r--r-- | src/client/connector/connectors/httpconnector.rs | 24 | ||||
| -rw-r--r-- | src/client/connector/manager.rs | 15 |
2 files changed, 23 insertions, 16 deletions
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: <HTTPConnector as Connection>::Ok, } impl HTTPConnector { - pub fn new(pull_url: &str, ntfy_url: &str) -> std::result::Result<Self, url::ParseError> { + pub fn new( + pull_url: &str, + ntfy_url: &str, + ip_getter_url: &str, + ) -> std::result::Result<Self, url::ParseError> { + // 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<IpAddr> { + 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<C: Connection> { - connector: C, -} - -impl<C: Connection> ConnectorManager<C> { - pub fn new(connector: C) -> Self { - Self { connector } - } - - pub fn enter_loop(&mut self) { - self.connector.r#loop(); - } -} |
