summaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
Diffstat (limited to 'src/client')
-rw-r--r--src/client/connector.rs22
-rw-r--r--src/client/connector/httpconnector.rs104
-rw-r--r--src/client/executor.rs24
-rw-r--r--src/client/main.rs70
4 files changed, 91 insertions, 129 deletions
diff --git a/src/client/connector.rs b/src/client/connector.rs
index 25f8b5c..00d9f3f 100644
--- a/src/client/connector.rs
+++ b/src/client/connector.rs
@@ -27,23 +27,18 @@ 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) {
@@ -89,14 +84,7 @@ pub trait Connection {
}
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 5973410..59ffc9c 100644
--- a/src/client/connector/httpconnector.rs
+++ b/src/client/connector/httpconnector.rs
@@ -35,35 +35,29 @@ 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(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(c) => c,
- Err(_) => match Client::builder()
+ Err(_) => 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()?,
- },
+ .danger_accept_invalid_hostnames(true)
+ .build()?,
},
},
- )
+ })
}
#[allow(clippy::only_used_in_recursion)]
@@ -109,28 +103,24 @@ 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(())
}
@@ -150,25 +140,21 @@ impl Connection for HTTPConnector {
}
fn get_ip(&self) -> Option<IpAddr> {
- match self.handle_response(
- || {
- self.client
- .get(
- self.ip_getter_url
- .as_str(),
- )
- },
- ) {
+ 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")
- },
- ) {
+ Result::Err(_) => match self.handle_response(|| {
+ self.client
+ .get("ifconfig.me")
+ }) {
Ok(ip) => Some(
ip.parse()
.ok()?,
diff --git a/src/client/executor.rs b/src/client/executor.rs
index 8fef869..1e6968c 100644
--- a/src/client/executor.rs
+++ b/src/client/executor.rs
@@ -8,23 +8,19 @@ 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 = Some((self
+ .task
+ .exec)(&self.args));
self.output
.as_ref()
}
diff --git a/src/client/main.rs b/src/client/main.rs
index 68afaa1..0fae222 100644
--- a/src/client/main.rs
+++ b/src/client/main.rs
@@ -22,47 +22,39 @@ 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();
}