summaryrefslogtreecommitdiff
path: root/src/client/connector/httpconnector.rs
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-09-14 22:09:09 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-09-14 22:09:09 +0800
commit1c10b521e86a3f20ff5f237219bb12cac8fb331a (patch)
tree684fef11a2d859ecef05dcb519ee8001e2594bc5 /src/client/connector/httpconnector.rs
parentc02e74bea833224e2ff35c6da51ff30119d7b924 (diff)
chore: remove extra connector{s,}
Diffstat (limited to 'src/client/connector/httpconnector.rs')
-rw-r--r--src/client/connector/httpconnector.rs117
1 files changed, 117 insertions, 0 deletions
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: <HTTPConnector as Connection>::Ok,
+}
+
+impl HTTPConnector {
+ 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_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<F: Fn() -> RequestBuilder>(
+ &self,
+ resp: F,
+ ) -> Result<<HTTPConnector as Connection>::Ok, <HTTPConnector as Connection>::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::Ok, Self::Err> {
+ self.handle_response(|| self.client.get(self.pull_url.as_str()))
+ }
+
+ fn ntfy(&self, data: String) -> Result<Self::Ok, Self::Err> {
+ 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<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()?),
+ Err(_) => None,
+ },
+ }
+ }
+}