From 1c10b521e86a3f20ff5f237219bb12cac8fb331a Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sun, 14 Sep 2025 22:09:09 +0800 Subject: chore: remove extra connector{s,} --- src/client/connector.rs | 2 +- src/client/connector/connectors.rs | 1 - src/client/connector/connectors/httpconnector.rs | 117 ----------------------- src/client/connector/httpconnector.rs | 117 +++++++++++++++++++++++ src/client/main.rs | 2 +- 5 files changed, 119 insertions(+), 120 deletions(-) delete mode 100644 src/client/connector/connectors.rs delete mode 100644 src/client/connector/connectors/httpconnector.rs create mode 100644 src/client/connector/httpconnector.rs diff --git a/src/client/connector.rs b/src/client/connector.rs index c7daf96..231b19a 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -1,4 +1,4 @@ -pub mod connectors; +pub mod httpconnector; use std::{net::IpAddr, thread::sleep, time::Duration}; diff --git a/src/client/connector/connectors.rs b/src/client/connector/connectors.rs deleted file mode 100644 index cc3f1e0..0000000 --- a/src/client/connector/connectors.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod httpconnector; diff --git a/src/client/connector/connectors/httpconnector.rs b/src/client/connector/connectors/httpconnector.rs deleted file mode 100644 index 8f06074..0000000 --- a/src/client/connector/connectors/httpconnector.rs +++ /dev/null @@ -1,117 +0,0 @@ -use crate::{connector::Connection, executor::Executor}; -use remote_control_rs::tasks::TASKS; -use reqwest::{ - Error, StatusCode, Url, - blocking::{Client, RequestBuilder}, -}; -use std::{net::IpAddr, thread::sleep, time::Duration}; - -pub struct HTTPConnector { - network_enforcer: 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, - ip_getter_url: &str, - ) -> std::result::Result { - // TODO: proper type - assert_eq!(TASKS[2].name, "network"); - Ok(Self { - network_enforcer: Executor::new(&TASKS[2], ["on".to_string()]).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: Client::new(), - }) - } - - fn handle_response RequestBuilder>( - &self, - resp: F, - ) -> Result<::Ok, ::Err> { - match resp().send() { - Ok(resp) => match resp.status() { - StatusCode::TOO_MANY_REQUESTS => { - sleep(Duration::from_secs(5)); - self.update() - } - status => { - if !status.is_success() { - return Result::Err(Err::StatusCode(status)); - } - match resp.text() { - Ok(text) => Result::Ok(text), - Err(e) => Result::Err(Err::Reqwest(e)), - } - } - }, - Err(e) => Result::Err(Err::Reqwest(e)), - } - } -} - -#[derive(Debug)] -pub enum Err { - Reqwest(Error), - StatusCode(StatusCode), -} - -impl std::fmt::Display for Err { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Err::Reqwest(e) => e.fmt(f), - Err::StatusCode(e) => e.fmt(f), - } - } -} - -impl Connection for HTTPConnector { - type Err = Err; - - // consider StatusCode::NOT_MODIFIED - fn update(&self) -> Result { - self.handle_response(|| self.client.get(self.pull_url.as_str())) - } - - fn ntfy(&self, data: String) -> Result { - self.handle_response(|| { - self.client - .post(self.ntfy_url.as_str()) - .body(data.to_owned()) - }) - } - - fn is_prev(&mut self, str: &Self::Ok) -> bool { - if str == &self.prev { - return true; - } else { - self.prev = str.to_owned() - } - false - } - - fn force_network(&mut self) { - _ = self.network_enforcer.run(); - } - - fn get_ip(&self) -> Option { - 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, - }, - } - } -} diff --git a/src/client/connector/httpconnector.rs b/src/client/connector/httpconnector.rs new file mode 100644 index 0000000..8f06074 --- /dev/null +++ b/src/client/connector/httpconnector.rs @@ -0,0 +1,117 @@ +use crate::{connector::Connection, executor::Executor}; +use remote_control_rs::tasks::TASKS; +use reqwest::{ + Error, StatusCode, Url, + blocking::{Client, RequestBuilder}, +}; +use std::{net::IpAddr, thread::sleep, time::Duration}; + +pub struct HTTPConnector { + network_enforcer: 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, + ip_getter_url: &str, + ) -> std::result::Result { + // TODO: proper type + assert_eq!(TASKS[2].name, "network"); + Ok(Self { + network_enforcer: Executor::new(&TASKS[2], ["on".to_string()]).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: Client::new(), + }) + } + + fn handle_response RequestBuilder>( + &self, + resp: F, + ) -> Result<::Ok, ::Err> { + match resp().send() { + Ok(resp) => match resp.status() { + StatusCode::TOO_MANY_REQUESTS => { + sleep(Duration::from_secs(5)); + self.update() + } + status => { + if !status.is_success() { + return Result::Err(Err::StatusCode(status)); + } + match resp.text() { + Ok(text) => Result::Ok(text), + Err(e) => Result::Err(Err::Reqwest(e)), + } + } + }, + Err(e) => Result::Err(Err::Reqwest(e)), + } + } +} + +#[derive(Debug)] +pub enum Err { + Reqwest(Error), + StatusCode(StatusCode), +} + +impl std::fmt::Display for Err { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Err::Reqwest(e) => e.fmt(f), + Err::StatusCode(e) => e.fmt(f), + } + } +} + +impl Connection for HTTPConnector { + type Err = Err; + + // consider StatusCode::NOT_MODIFIED + fn update(&self) -> Result { + self.handle_response(|| self.client.get(self.pull_url.as_str())) + } + + fn ntfy(&self, data: String) -> Result { + self.handle_response(|| { + self.client + .post(self.ntfy_url.as_str()) + .body(data.to_owned()) + }) + } + + fn is_prev(&mut self, str: &Self::Ok) -> bool { + if str == &self.prev { + return true; + } else { + self.prev = str.to_owned() + } + false + } + + fn force_network(&mut self) { + _ = self.network_enforcer.run(); + } + + fn get_ip(&self) -> Option { + 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, + }, + } + } +} diff --git a/src/client/main.rs b/src/client/main.rs index b603b37..8ba230b 100644 --- a/src/client/main.rs +++ b/src/client/main.rs @@ -3,7 +3,7 @@ use crate::client::Client; use crate::connector::Connection; -use crate::connector::connectors::httpconnector::HTTPConnector; +use crate::connector::httpconnector::HTTPConnector; use signal_hook::consts::signal::*; use signal_hook::iterator::Signals; -- cgit v1.2.3