diff options
author | Hristo Venev <hristo@venev.name> | 2019-03-19 01:05:24 +0200 |
---|---|---|
committer | Hristo Venev <hristo@venev.name> | 2019-03-19 01:05:24 +0200 |
commit | fab2654c10cf0d0f440970d55bbe5841d93a4ae3 (patch) | |
tree | 9ebaabebea3cc67c8ff5dda3676f830bebc91ca3 | |
parent | d260190b3074d764cf2423b7187dc58cd37b9a7c (diff) |
Use curl command instead of library.
Rust packaging is complicated and I don't like it.
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/config.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 47 |
3 files changed, 25 insertions, 29 deletions
@@ -10,7 +10,6 @@ serde = { version = "1.0.89" } serde_derive = { version = "1.0.89" } serde_json = { version = "1.0.39" } chrono = { version = "0.4.6", default-features = false } -curl = { version = "0.4.17", default-features = false } [profile.release] panic = "abort" diff --git a/src/config.rs b/src/config.rs index 9124902..4a3c385 100644 --- a/src/config.rs +++ b/src/config.rs @@ -34,6 +34,8 @@ pub struct Config { pub ifname: String, #[serde(default = "default_wg_command")] pub wg_command: String, + #[serde(default = "default_curl_command")] + pub curl_command: String, #[serde(flatten)] pub peers: PeerConfig, @@ -60,6 +62,10 @@ fn default_wg_command() -> String { "wg".to_owned() } +fn default_curl_command() -> String { + "curl".to_owned() +} + fn default_min_keepalive() -> u32 { 10 } diff --git a/src/main.rs b/src/main.rs index deea336..f0fa41b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,7 @@ pub struct Device { update_config: config::UpdateConfig, sources: Vec<Source>, current: wg::Config, + curl_command: String, } impl Device { @@ -46,6 +47,7 @@ impl Device { update_config: c.update, sources: c.sources.into_iter().map(Source::new).collect(), current, + curl_command: c.curl_command, } } @@ -98,8 +100,7 @@ impl Device { continue; } - let r = fetch_source(&src.config.url); - let r = match r { + let r = match fetch_source(&self.curl_command, &src.config.url) { Ok(r) => { eprintln!("<6>Updated [{}]", &src.config.url); src.data = Some(r); @@ -144,34 +145,24 @@ impl Device { } } -fn fetch_source(url: &str) -> io::Result<proto::Source> { - use curl::easy::Easy; - - let mut res = Vec::<u8>::new(); - - { - let mut req = Easy::new(); - req.url(url)?; - - { - let mut tr = req.transfer(); - tr.write_function(|data| { - res.extend_from_slice(data); - Ok(data.len()) - })?; - tr.perform()?; - } - - let code = req.response_code()?; - if code != 0 && code != 200 { - return Err(io::Error::new( - io::ErrorKind::Other, - format!("HTTP error {}", code), - )); - } +fn fetch_source(curl_command: &str, url: &str) -> io::Result<proto::Source> { + use std::process::{Command, Stdio}; + + let out = Command::new(curl_command) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .arg("--fail") + .arg("--fail-early") + .arg("--") + .arg(url) + .output()?; + + if !out.status.success() { + return Err(io::Error::new(io::ErrorKind::Other, format!("Failed to download [{}]", url))); } - let mut de = serde_json::Deserializer::from_slice(&res); + let mut de = serde_json::Deserializer::from_slice(&out.stdout); let r = serde::Deserialize::deserialize(&mut de)?; Ok(r) } |