diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-09-21 23:08:41 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-09-21 23:08:41 +0800 |
| commit | 22c98311a8e8f4a1c94b0e4ac0f36b4e016a2bf4 (patch) | |
| tree | 6453e6991b39e05bda169bd13ceb4cf0f2ccecbd /src/client | |
| parent | 2e1d12ec90ea12e8824d79dfc31b5b3cae397df8 (diff) | |
format
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/client.rs | 33 | ||||
| -rw-r--r-- | src/client/connector.rs | 42 | ||||
| -rw-r--r-- | src/client/connector/httpconnector.rs | 92 | ||||
| -rw-r--r-- | src/client/main.rs | 76 |
4 files changed, 183 insertions, 60 deletions
diff --git a/src/client/client.rs b/src/client/client.rs index 9080cf1..780d586 100644 --- a/src/client/client.rs +++ b/src/client/client.rs @@ -24,7 +24,28 @@ Username: {} Real Name: {} Languages: {:?} Environment Variables: {:?}"#, - self.inner.ip, self.inner.hostname, self.inner.device_name, self.inner.os, self.inner.cpu_arch, self.inner.distro, self.inner.desktop_env, self.inner.username, self.inner.realname, self.inner.langs, self.inner.env + self.inner + .ip, + self.inner + .hostname, + self.inner + .device_name, + self.inner + .os, + self.inner + .cpu_arch, + self.inner + .distro, + self.inner + .desktop_env, + self.inner + .username, + self.inner + .realname, + self.inner + .langs, + self.inner + .env ) } } @@ -34,7 +55,9 @@ impl<'a, C: Connection> Client<'a, C> { let c = Self { inner: remote_control_rs::client::Client { ip: connector.get_ip(), - hostname: gethostname().into_string().unwrap_or_default(), + hostname: gethostname() + .into_string() + .unwrap_or_default(), device_name: whoami::devicename(), os: std::env::consts::OS, cpu_arch: std::env::consts::ARCH, @@ -50,14 +73,16 @@ impl<'a, C: Connection> Client<'a, C> { }, connector, }; - c.connector.init(&c)?; + c.connector + .init(&c)?; Ok(c) } pub fn enter_loop(mut self) -> ! { loop { sleep(Duration::from_millis(500)); - self.connector.tick(); + self.connector + .tick(); } } } diff --git a/src/client/connector.rs b/src/client/connector.rs index 22a9f5a..c1a5f7f 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -34,18 +34,24 @@ pub trait Connection { fn get_ip(&self) -> Option<IpAddr>; fn force_network(&mut self) { - _ = Executor::new(NETWORK_T.as_ref(), ["true"], ()); + _ = Executor::new( + NETWORK_T.as_ref(), + ["true"], + (), + ); } fn init<'a>(&self, c: &Client<'a, Self>) -> std::result::Result<(), Self::Err> where Self: std::marker::Sized, { - self.ntfy(format!( - r#"New client: + self.ntfy( + format!( + r#"New client: {c} "#, - )) + ), + ) } fn reinit(&mut self) { @@ -67,17 +73,39 @@ pub trait Connection { return; } - let mut it = s.as_ref().split_whitespace(); + let mut it = s + .as_ref() + .split_whitespace(); let task = match it.next() { Some(t) => t, None => return, }; // FIXME: handle the errors... peak prod code - self.ntfy(Executor::new((task.try_into() as result::Result<Task, IntoTaskErr>).unwrap().as_ref(), it, ()).run().as_ref().unwrap().clone()).unwrap(); + self.ntfy( + Executor::new( + (task.try_into() as result::Result<Task, IntoTaskErr>) + .unwrap() + .as_ref(), + it, + (), + ) + .run() + .as_ref() + .unwrap() + .clone(), + ) + .unwrap(); } Result::Err(e) => { #[cfg(debug_assertions)] - std::eprintln!("[{}:{}:{}] {} = {:#?}", std::file!(), std::line!(), std::column!(), std::stringify!(e), &&e as &dyn std::fmt::Debug,); + std::eprintln!( + "[{}:{}:{}] {} = {:#?}", + std::file!(), + std::line!(), + std::column!(), + std::stringify!(e), + &&e as &dyn std::fmt::Debug, + ); } } } diff --git a/src/client/connector/httpconnector.rs b/src/client/connector/httpconnector.rs index 67741e9..c0dbce0 100644 --- a/src/client/connector/httpconnector.rs +++ b/src/client/connector/httpconnector.rs @@ -36,23 +36,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.as_ref(), ["true"], ()), // .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.as_ref(), + ["true"], + (), + ), // .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)] @@ -98,11 +110,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(()) } @@ -116,14 +145,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, }, } diff --git a/src/client/main.rs b/src/client/main.rs index 0669197..699e17e 100644 --- a/src/client/main.rs +++ b/src/client/main.rs @@ -1,5 +1,12 @@ -#![allow(incomplete_features, clippy::redundant_field_names)] -#![feature(associated_type_defaults, never_type, lazy_type_alias)] +#![allow( + incomplete_features, + clippy::redundant_field_names +)] +#![feature( + associated_type_defaults, + never_type, + lazy_type_alias +)] use crate::client::Client; use crate::connector::httpconnector::HTTPConnector; @@ -14,34 +21,47 @@ pub(crate) mod connector; fn main() { #[cfg(not(debug_assertions))] { - _ = Signals::new([ - SIGHUP, - SIGINT, - SIGQUIT, - SIGTRAP, - SIGABRT, - SIGPIPE, - SIGALRM, - SIGTERM, - SIGUSR1, - SIGUSR2, - SIGCHLD, - SIGCONT, - SIGTSTP, - SIGTTIN, - SIGTTOU, - SIGURG, - SIGXCPU, - SIGXFSZ, - SIGVTALRM, - SIGPROF, - SIGWINCH, - #[cfg(any(target_os = "linux", target_os = "android"))] - SIGSYS, - ]); // discard err + _ = Signals::new( + [ + SIGHUP, + SIGINT, + SIGQUIT, + SIGTRAP, + SIGABRT, + SIGPIPE, + SIGALRM, + SIGTERM, + SIGUSR1, + SIGUSR2, + SIGCHLD, + SIGCONT, + SIGTSTP, + SIGTTIN, + SIGTTOU, + SIGURG, + SIGXCPU, + SIGXFSZ, + SIGVTALRM, + SIGPROF, + SIGWINCH, + #[cfg( + any( + target_os = "linux", + target_os = "android" + ) + )] + SIGSYS, + ], + ); // discard err } let ip = "http://127.0.0.1:8081"; - let client = Client::new(HTTPConnector::new(ip, ip, "/").unwrap()).unwrap(); + let client = Client::new( + HTTPConnector::new( + ip, ip, "/", + ) + .unwrap(), + ) + .unwrap(); client.enter_loop(); } |
