aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHristo Venev <hristo@venev.name>2019-03-19 01:05:24 +0200
committerHristo Venev <hristo@venev.name>2019-03-19 01:05:24 +0200
commitfab2654c10cf0d0f440970d55bbe5841d93a4ae3 (patch)
tree9ebaabebea3cc67c8ff5dda3676f830bebc91ca3 /src
parentd260190b3074d764cf2423b7187dc58cd37b9a7c (diff)
Use curl command instead of library.
Rust packaging is complicated and I don't like it.
Diffstat (limited to 'src')
-rw-r--r--src/config.rs6
-rw-r--r--src/main.rs47
2 files changed, 25 insertions, 28 deletions
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)
}