From 29cc40be4856b772b5988c369fc0c270d98b5235 Mon Sep 17 00:00:00 2001 From: notplants Date: Sat, 18 Dec 2021 11:24:43 -0500 Subject: [PATCH 1/8] Fix setup of nsupdate --- Cargo.lock | 6 +++--- peach-config/src/setup_peach.rs | 1 + peach-dyndns-updater/Cargo.toml | 2 +- peach-lib/Cargo.toml | 2 +- peach-lib/src/config_manager.rs | 7 ++++++- peach-lib/src/dyndns_client.rs | 16 ++++++++-------- peach-web/Cargo.toml | 2 +- 7 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a022f95..34bcd48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2432,7 +2432,7 @@ dependencies = [ [[package]] name = "peach-dyndns-updater" -version = "0.1.6" +version = "0.1.7" dependencies = [ "env_logger 0.6.2", "log 0.4.14", @@ -2454,7 +2454,7 @@ dependencies = [ [[package]] name = "peach-lib" -version = "1.3.1" +version = "1.3.2" dependencies = [ "chrono", "fslock", @@ -2541,7 +2541,7 @@ dependencies = [ [[package]] name = "peach-web" -version = "0.4.15" +version = "0.4.16" dependencies = [ "env_logger 0.8.4", "log 0.4.14", diff --git a/peach-config/src/setup_peach.rs b/peach-config/src/setup_peach.rs index d17ecf8..af58380 100644 --- a/peach-config/src/setup_peach.rs +++ b/peach-config/src/setup_peach.rs @@ -69,6 +69,7 @@ pub fn setup_peach( "libssl-dev", "nginx", "wget", + "dnsutils", "-y", ])?; diff --git a/peach-dyndns-updater/Cargo.toml b/peach-dyndns-updater/Cargo.toml index cd64557..483c9fb 100644 --- a/peach-dyndns-updater/Cargo.toml +++ b/peach-dyndns-updater/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "peach-dyndns-updater" -version = "0.1.6" +version = "0.1.7" authors = ["Max Fowler "] edition = "2018" description = "Sytemd timer which keeps a dynamic dns subdomain up to date with the latest device IP using nsupdate." diff --git a/peach-lib/Cargo.toml b/peach-lib/Cargo.toml index 8085f69..11d696a 100644 --- a/peach-lib/Cargo.toml +++ b/peach-lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "peach-lib" -version = "1.3.1" +version = "1.3.2" authors = ["Andrew Reid "] edition = "2018" diff --git a/peach-lib/src/config_manager.rs b/peach-lib/src/config_manager.rs index fd31a6b..3aab392 100644 --- a/peach-lib/src/config_manager.rs +++ b/peach-lib/src/config_manager.rs @@ -70,7 +70,7 @@ pub fn load_peach_config() -> Result { peach_config = PeachConfig { external_domain: "".to_string(), dyn_domain: "".to_string(), - dyn_dns_server_address: "".to_string(), + dyn_dns_server_address: "http://dynserver.dyn.peachcloud.org".to_string(), dyn_tsig_key_path: "".to_string(), dyn_enabled: false, ssb_admin_ids: Vec::new(), @@ -122,6 +122,11 @@ pub fn get_peachcloud_domain() -> Result, PeachError> { } } +pub fn get_dyndns_server_address() -> Result { + let peach_config = load_peach_config()?; + Ok(peach_config.dyn_dns_server_address) +} + pub fn set_dyndns_enabled_value(enabled_value: bool) -> Result { let mut peach_config = load_peach_config()?; peach_config.dyn_enabled = enabled_value; diff --git a/peach-lib/src/dyndns_client.rs b/peach-lib/src/dyndns_client.rs index ed7eec7..3538590 100644 --- a/peach-lib/src/dyndns_client.rs +++ b/peach-lib/src/dyndns_client.rs @@ -27,9 +27,9 @@ use crate::{ config_manager::{load_peach_config, set_peach_dyndns_config}, error::PeachError, }; +use crate::config_manager::get_dyndns_server_address; /// constants for dyndns configuration -pub const PEACH_DYNDNS_URL: &str = "http://dynserver.dyn.peachcloud.org"; pub const TSIG_KEY_PATH: &str = "/var/lib/peachcloud/peach-dyndns/tsig.key"; pub const PEACH_DYNDNS_CONFIG_PATH: &str = "/var/lib/peachcloud/peach-dyndns"; pub const DYNDNS_LOG_PATH: &str = "/var/lib/peachcloud/peach-dyndns/latest_result.log"; @@ -62,9 +62,9 @@ pub fn save_dyndns_key(key: &str) -> Result<(), PeachError> { pub fn register_domain(domain: &str) -> std::result::Result { debug!("Creating HTTP transport for dyndns client."); let transport = HttpTransport::new().standalone()?; - let http_server = PEACH_DYNDNS_URL; - debug!("Creating HTTP transport handle on {}.", http_server); - let transport_handle = transport.handle(http_server)?; + let http_server = get_dyndns_server_address()?; + debug!("Creating HTTP transport handle on {}.", &http_server); + let transport_handle = transport.handle(&http_server)?; info!("Creating client for peach-dyndns service."); let mut client = PeachDynDnsClient::new(transport_handle); @@ -73,7 +73,7 @@ pub fn register_domain(domain: &str) -> std::result::Result // save new TSIG key save_dyndns_key(&key)?; // save new configuration values - let set_config_result = set_peach_dyndns_config(domain, PEACH_DYNDNS_URL, TSIG_KEY_PATH, true); + let set_config_result = set_peach_dyndns_config(domain, &http_server, TSIG_KEY_PATH, true); match set_config_result { Ok(_) => { let response = "success".to_string(); @@ -87,9 +87,9 @@ pub fn register_domain(domain: &str) -> std::result::Result pub fn is_domain_available(domain: &str) -> std::result::Result { debug!("Creating HTTP transport for dyndns client."); let transport = HttpTransport::new().standalone()?; - let http_server = PEACH_DYNDNS_URL; - debug!("Creating HTTP transport handle on {}.", http_server); - let transport_handle = transport.handle(http_server)?; + let http_server = get_dyndns_server_address()?; + debug!("Creating HTTP transport handle on {}.", &http_server); + let transport_handle = transport.handle(&http_server)?; info!("Creating client for peach_network service."); let mut client = PeachDynDnsClient::new(transport_handle); diff --git a/peach-web/Cargo.toml b/peach-web/Cargo.toml index 648de74..f514236 100644 --- a/peach-web/Cargo.toml +++ b/peach-web/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "peach-web" -version = "0.4.15" +version = "0.4.16" authors = ["Andrew Reid "] edition = "2018" description = "peach-web is a web application which provides a web interface for monitoring and interacting with the PeachCloud device. This allows administration of the single-board computer (ie. Raspberry Pi) running PeachCloud, as well as the ssb-server and related plugins." From b8f394b9018dcc6e6ba8fc9705a22250d0b43e68 Mon Sep 17 00:00:00 2001 From: notplants Date: Wed, 22 Dec 2021 09:59:20 -0500 Subject: [PATCH 2/8] Debugging dyndns --- .gitignore | 1 + peach-lib/src/config_manager.rs | 3 +++ peach-lib/src/dyndns_client.rs | 20 ++++++++++++++------ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 32758b3..41e4e65 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ target *peachdeploy.sh *vpsdeploy.sh +*bindeploy.sh diff --git a/peach-lib/src/config_manager.rs b/peach-lib/src/config_manager.rs index 3aab392..7f48247 100644 --- a/peach-lib/src/config_manager.rs +++ b/peach-lib/src/config_manager.rs @@ -29,6 +29,8 @@ pub struct PeachConfig { #[serde(default)] pub dyn_dns_server_address: String, #[serde(default)] + pub dyn_nameserver: String, + #[serde(default)] pub dyn_tsig_key_path: String, #[serde(default)] // default is false pub dyn_enabled: bool, @@ -71,6 +73,7 @@ pub fn load_peach_config() -> Result { external_domain: "".to_string(), dyn_domain: "".to_string(), dyn_dns_server_address: "http://dynserver.dyn.peachcloud.org".to_string(), + dyn_nameserver: "ns.peachcloud.org".to_string(), dyn_tsig_key_path: "".to_string(), dyn_enabled: false, ssb_admin_ids: Vec::new(), diff --git a/peach-lib/src/dyndns_client.rs b/peach-lib/src/dyndns_client.rs index 3538590..17379cd 100644 --- a/peach-lib/src/dyndns_client.rs +++ b/peach-lib/src/dyndns_client.rs @@ -16,6 +16,7 @@ use std::{ process::{Command, Stdio}, str::FromStr, }; +use std::ffi::OsStr; use chrono::prelude::*; use jsonrpc_client_core::{expand_params, jsonrpc_client}; @@ -113,7 +114,7 @@ fn get_public_ip_address() -> Result { /// Reads dyndns configurations from config.yml /// and then uses nsupdate to update the IP address for the configured domain pub fn dyndns_update_ip() -> Result { - info!("Running dyndns_update_ip"); + let peach_config = load_peach_config()?; info!( "Using config: @@ -121,21 +122,27 @@ pub fn dyndns_update_ip() -> Result { dyn_domain: {:?} dyn_dns_server_address: {:?} dyn_enabled: {:?} + dyn_nameserver: {:?} ", peach_config.dyn_tsig_key_path, peach_config.dyn_domain, peach_config.dyn_dns_server_address, peach_config.dyn_enabled, + peach_config.dyn_nameserver, ); if !peach_config.dyn_enabled { info!("dyndns is not enabled, not updating"); Ok(false) } else { // call nsupdate passing appropriate configs - let mut nsupdate_command = Command::new("/usr/bin/nsupdate") + let mut nsupdate_command = Command::new("/usr/bin/nsupdate"); + nsupdate_command .arg("-k") .arg(&peach_config.dyn_tsig_key_path) - .arg("-v") + .arg("-v"); + let args: Vec<&OsStr> = nsupdate_command.get_args().collect(); + info!("nsupdate_args: {:?}", args); + let mut nsupdate_child = nsupdate_command .stdin(Stdio::piped()) .spawn()?; // pass nsupdate commands via stdin @@ -148,19 +155,20 @@ pub fn dyndns_update_ip() -> Result { update delete {DOMAIN} A update add {DOMAIN} 30 A {PUBLIC_IP_ADDRESS} send", - NAMESERVER = "ns.peachcloud.org", + NAMESERVER = peach_config.dyn_nameserver, ZONE = peach_config.dyn_domain, DOMAIN = peach_config.dyn_domain, PUBLIC_IP_ADDRESS = public_ip_address, ); - let mut nsupdate_stdin = nsupdate_command.stdin.take().ok_or(PeachError::NsUpdate { + info!("ns_commands: {:?}", ns_commands); + let mut nsupdate_stdin = nsupdate_child.stdin.take().ok_or(PeachError::NsUpdate { msg: "unable to capture stdin handle for `nsupdate` command".to_string(), })?; write!(nsupdate_stdin, "{}", ns_commands).map_err(|source| PeachError::Write { source, path: peach_config.dyn_tsig_key_path.to_string(), })?; - let nsupdate_output = nsupdate_command.wait_with_output()?; + let nsupdate_output = nsupdate_child.wait_with_output()?; info!("nsupdate output: {:?}", nsupdate_output); // We only return a successful result if nsupdate was successful if nsupdate_output.status.success() { From 1ea0ea2ed187a55af8c719d73af05484a1be1643 Mon Sep 17 00:00:00 2001 From: notplants Date: Tue, 11 Jan 2022 18:03:07 -0500 Subject: [PATCH 3/8] Fix regression of peach-dyndns-updater --- Cargo.lock | 4 ++-- peach-dyndns-updater/Cargo.toml | 2 +- peach-lib/Cargo.toml | 1 + peach-lib/src/config_manager.rs | 22 +++++++++++++++++++--- peach-lib/src/dyndns_client.rs | 28 ++++++++++++++-------------- peach-web/Cargo.toml | 2 +- peach-web/README.md | 14 ++++++++++++++ 7 files changed, 52 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 34bcd48..6622ccc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2432,7 +2432,7 @@ dependencies = [ [[package]] name = "peach-dyndns-updater" -version = "0.1.7" +version = "0.1.8" dependencies = [ "env_logger 0.6.2", "log 0.4.14", @@ -2541,7 +2541,7 @@ dependencies = [ [[package]] name = "peach-web" -version = "0.4.16" +version = "0.4.17" dependencies = [ "env_logger 0.8.4", "log 0.4.14", diff --git a/peach-dyndns-updater/Cargo.toml b/peach-dyndns-updater/Cargo.toml index 483c9fb..d60c720 100644 --- a/peach-dyndns-updater/Cargo.toml +++ b/peach-dyndns-updater/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "peach-dyndns-updater" -version = "0.1.7" +version = "0.1.8" authors = ["Max Fowler "] edition = "2018" description = "Sytemd timer which keeps a dynamic dns subdomain up to date with the latest device IP using nsupdate." diff --git a/peach-lib/Cargo.toml b/peach-lib/Cargo.toml index 11d696a..aa821d9 100644 --- a/peach-lib/Cargo.toml +++ b/peach-lib/Cargo.toml @@ -17,3 +17,4 @@ rust-crypto = "0.2.36" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" serde_yaml = "0.8" + diff --git a/peach-lib/src/config_manager.rs b/peach-lib/src/config_manager.rs index 7f48247..00fd104 100644 --- a/peach-lib/src/config_manager.rs +++ b/peach-lib/src/config_manager.rs @@ -17,6 +17,11 @@ pub const YAML_PATH: &str = "/var/lib/peachcloud/config.yml"; // lock file (used to avoid race conditions during config reading & writing) pub const LOCK_FILE_PATH: &str = "/var/lib/peachcloud/config.lock"; +// default values +pub const DEFAULT_DYN_SERVER_ADDRESS: &str = "http://dynserver.dyn.peachcloud.org"; +pub const DEFAULT_DYN_NAMESERVER: &str = "ns.peachcloud.org"; + + // we make use of Serde default values in order to make PeachCloud // robust and keep running even with a not fully complete config.yml // main type which represents all peachcloud configurations @@ -29,6 +34,8 @@ pub struct PeachConfig { #[serde(default)] pub dyn_dns_server_address: String, #[serde(default)] + pub dyn_use_custom_server: bool, + #[serde(default)] pub dyn_nameserver: String, #[serde(default)] pub dyn_tsig_key_path: String, @@ -72,8 +79,9 @@ pub fn load_peach_config() -> Result { peach_config = PeachConfig { external_domain: "".to_string(), dyn_domain: "".to_string(), - dyn_dns_server_address: "http://dynserver.dyn.peachcloud.org".to_string(), - dyn_nameserver: "ns.peachcloud.org".to_string(), + dyn_dns_server_address: DEFAULT_DYN_SERVER_ADDRESS.to_string(), + dyn_use_custom_server: false, + dyn_nameserver: DEFAULT_DYN_NAMESERVER.to_string(), dyn_tsig_key_path: "".to_string(), dyn_enabled: false, ssb_admin_ids: Vec::new(), @@ -127,7 +135,15 @@ pub fn get_peachcloud_domain() -> Result, PeachError> { pub fn get_dyndns_server_address() -> Result { let peach_config = load_peach_config()?; - Ok(peach_config.dyn_dns_server_address) + // if the user is using a custom dyn server then load the address from the config + if peach_config.dyn_use_custom_server { + Ok(peach_config.dyn_dns_server_address) + } + // otherwise hardcode the address + else { + Ok(DEFAULT_DYN_SERVER_ADDRESS.to_string()) + } + } pub fn set_dyndns_enabled_value(enabled_value: bool) -> Result { diff --git a/peach-lib/src/dyndns_client.rs b/peach-lib/src/dyndns_client.rs index 17379cd..1698ad0 100644 --- a/peach-lib/src/dyndns_client.rs +++ b/peach-lib/src/dyndns_client.rs @@ -12,6 +12,7 @@ use std::{ fs, fs::OpenOptions, + fs::File, io::Write, process::{Command, Stdio}, str::FromStr, @@ -64,6 +65,7 @@ pub fn register_domain(domain: &str) -> std::result::Result debug!("Creating HTTP transport for dyndns client."); let transport = HttpTransport::new().standalone()?; let http_server = get_dyndns_server_address()?; + info!("Using dyndns http server address: {:?}", http_server); debug!("Creating HTTP transport handle on {}.", &http_server); let transport_handle = transport.handle(&http_server)?; info!("Creating client for peach-dyndns service."); @@ -140,11 +142,10 @@ pub fn dyndns_update_ip() -> Result { .arg("-k") .arg(&peach_config.dyn_tsig_key_path) .arg("-v"); - let args: Vec<&OsStr> = nsupdate_command.get_args().collect(); - info!("nsupdate_args: {:?}", args); - let mut nsupdate_child = nsupdate_command - .stdin(Stdio::piped()) - .spawn()?; + // info!("nsupdate_args: {:?}", args); + // let mut nsupdate_child = nsupdate_command + // .stdin(Stdio::piped()) + // .spawn()?; // pass nsupdate commands via stdin let public_ip_address = get_public_ip_address()?; info!("found public ip address: {}", public_ip_address); @@ -161,15 +162,14 @@ pub fn dyndns_update_ip() -> Result { PUBLIC_IP_ADDRESS = public_ip_address, ); info!("ns_commands: {:?}", ns_commands); - let mut nsupdate_stdin = nsupdate_child.stdin.take().ok_or(PeachError::NsUpdate { - msg: "unable to capture stdin handle for `nsupdate` command".to_string(), - })?; - write!(nsupdate_stdin, "{}", ns_commands).map_err(|source| PeachError::Write { - source, - path: peach_config.dyn_tsig_key_path.to_string(), - })?; - let nsupdate_output = nsupdate_child.wait_with_output()?; - info!("nsupdate output: {:?}", nsupdate_output); + info!("creating nsupdate temp file"); + let temp_file_path = "/var/lib/peachcloud/nsupdate.sh"; + // write ns_commands to temp_file + fs::write(temp_file_path, ns_commands)?; + nsupdate_command.arg(temp_file_path); + let nsupdate_output = nsupdate_command.output()?; + let args: Vec<&OsStr> = nsupdate_command.get_args().collect(); + info!("nsupdate command: {:?}", args); // We only return a successful result if nsupdate was successful if nsupdate_output.status.success() { info!("nsupdate succeeded, returning ok"); diff --git a/peach-web/Cargo.toml b/peach-web/Cargo.toml index f514236..860508d 100644 --- a/peach-web/Cargo.toml +++ b/peach-web/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "peach-web" -version = "0.4.16" +version = "0.4.17" authors = ["Andrew Reid "] edition = "2018" description = "peach-web is a web application which provides a web interface for monitoring and interacting with the PeachCloud device. This allows administration of the single-board computer (ie. Raspberry Pi) running PeachCloud, as well as the ssb-server and related plugins." diff --git a/peach-web/README.md b/peach-web/README.md index 018ab8d..80f123f 100644 --- a/peach-web/README.md +++ b/peach-web/README.md @@ -97,6 +97,20 @@ Remove configuration files (not removed with `apt-get remove`): `peach-web` is built on the Rocket webserver and Tera templating engine. It presents a web interface for interacting with the device. HTML is rendered server-side. Request handlers call JSON-RPC microservices and serve HTML and assets. A JSON API is exposed for remote calls and dynamic client-side content updates (via plain JavaScript following unobstructive design principles). Each Tera template is passed a context object. In the case of Rust, this object is a `struct` and must implement `Serialize`. The fields of the context object are available in the context of the template to be rendered. +### Configuration + +Configuration variables are stored in /var/lib/peachcloud/config.yml. +Peach-web also updates this file when changes are made to configurations via +the web interface. peach-web has no database, so all configurations are stored in this file. + +#### Dynamic DNS Configuration + +Most users will want to use the default PeachCloud dynamic dns server. +If the config dyn_use_custom_server=false, then default values will be used. +If the config dyn_use_custom_server=true, then a value must also be set for dyn_dns_server_address (e.g. "http://peachdynserver.commoninternet.net"). +This value is the URL of the instance of peach-dyndns-server that requests will be sent to for domain registration. +Using a custom value can here can be useful for testing. + ### Licensing AGPL-3.0 From 171d05171013c771320d21384c5260ca1c38edb3 Mon Sep 17 00:00:00 2001 From: notplants Date: Tue, 11 Jan 2022 18:06:51 -0500 Subject: [PATCH 4/8] Fix clippy warmings --- peach-dyndns-updater/src/main.rs | 5 ++--- peach-lib/src/config_manager.rs | 13 +++++-------- peach-lib/src/dyndns_client.rs | 14 ++++++-------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/peach-dyndns-updater/src/main.rs b/peach-dyndns-updater/src/main.rs index fbbc33a..ecde150 100644 --- a/peach-dyndns-updater/src/main.rs +++ b/peach-dyndns-updater/src/main.rs @@ -1,6 +1,5 @@ +use log::info; use peach_lib::dyndns_client::dyndns_update_ip; -use log::{info}; - fn main() { // initalize the logger @@ -9,4 +8,4 @@ fn main() { info!("Running peach-dyndns-updater"); let result = dyndns_update_ip(); info!("result: {:?}", result); -} \ No newline at end of file +} diff --git a/peach-lib/src/config_manager.rs b/peach-lib/src/config_manager.rs index 00fd104..3043b29 100644 --- a/peach-lib/src/config_manager.rs +++ b/peach-lib/src/config_manager.rs @@ -72,11 +72,8 @@ fn save_peach_config(peach_config: PeachConfig) -> Result Result { let peach_config_exists = std::path::Path::new(YAML_PATH).exists(); - let peach_config: PeachConfig; - - // if this is the first time loading peach_config, we can create a default here - if !peach_config_exists { - peach_config = PeachConfig { + let peach_config: PeachConfig = if !peach_config_exists { + PeachConfig { external_domain: "".to_string(), dyn_domain: "".to_string(), dyn_dns_server_address: DEFAULT_DYN_SERVER_ADDRESS.to_string(), @@ -87,7 +84,7 @@ pub fn load_peach_config() -> Result { ssb_admin_ids: Vec::new(), admin_password_hash: "".to_string(), temporary_password_hash: "".to_string(), - }; + } } // otherwise we load peach config from disk else { @@ -95,8 +92,8 @@ pub fn load_peach_config() -> Result { source, path: YAML_PATH.to_string(), })?; - peach_config = serde_yaml::from_str(&contents)?; - } + serde_yaml::from_str(&contents)? + }; Ok(peach_config) } diff --git a/peach-lib/src/dyndns_client.rs b/peach-lib/src/dyndns_client.rs index 1698ad0..3e66222 100644 --- a/peach-lib/src/dyndns_client.rs +++ b/peach-lib/src/dyndns_client.rs @@ -12,9 +12,8 @@ use std::{ fs, fs::OpenOptions, - fs::File, io::Write, - process::{Command, Stdio}, + process::{Command}, str::FromStr, }; use std::ffi::OsStr; @@ -212,7 +211,7 @@ pub fn get_num_seconds_since_successful_dns_update() -> Result, Peac })?; // replace newline if found // TODO: maybe we can use `.trim()` instead - let contents = contents.replace("\n", ""); + let contents = contents.replace('\n', ""); // TODO: consider adding additional context? let time_ran_dt = DateTime::parse_from_rfc3339(&contents).map_err(|source| { PeachError::ParseDateTime { @@ -235,16 +234,15 @@ pub fn is_dns_updater_online() -> Result { let is_enabled = peach_config.dyn_enabled; // then check if it has successfully run within the last 6 minutes (60*6 seconds) let num_seconds_since_successful_update = get_num_seconds_since_successful_dns_update()?; - let ran_recently: bool; - match num_seconds_since_successful_update { + let ran_recently: bool = match num_seconds_since_successful_update { Some(seconds) => { - ran_recently = seconds < (60 * 6); + seconds < (60 * 6) } // if the value is None, then the last time it ran successfully is unknown None => { - ran_recently = false; + false } - } + }; // debug log info!("is_dyndns_enabled: {:?}", is_enabled); info!("dyndns_ran_recently: {:?}", ran_recently); From bdfbd7057f0069368f36d76ab60d786d3dd07799 Mon Sep 17 00:00:00 2001 From: notplants Date: Tue, 11 Jan 2022 18:10:36 -0500 Subject: [PATCH 5/8] Remove commented out code --- peach-lib/src/dyndns_client.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/peach-lib/src/dyndns_client.rs b/peach-lib/src/dyndns_client.rs index 3e66222..c38b852 100644 --- a/peach-lib/src/dyndns_client.rs +++ b/peach-lib/src/dyndns_client.rs @@ -141,10 +141,6 @@ pub fn dyndns_update_ip() -> Result { .arg("-k") .arg(&peach_config.dyn_tsig_key_path) .arg("-v"); - // info!("nsupdate_args: {:?}", args); - // let mut nsupdate_child = nsupdate_command - // .stdin(Stdio::piped()) - // .spawn()?; // pass nsupdate commands via stdin let public_ip_address = get_public_ip_address()?; info!("found public ip address: {}", public_ip_address); From 4adf5547c9888c40fd64da28dd0dad0870c5ddf1 Mon Sep 17 00:00:00 2001 From: glyph Date: Wed, 12 Jan 2022 10:58:11 +0200 Subject: [PATCH 6/8] formatting --- peach-lib/src/config_manager.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/peach-lib/src/config_manager.rs b/peach-lib/src/config_manager.rs index 3043b29..e3894ab 100644 --- a/peach-lib/src/config_manager.rs +++ b/peach-lib/src/config_manager.rs @@ -21,7 +21,6 @@ pub const LOCK_FILE_PATH: &str = "/var/lib/peachcloud/config.lock"; pub const DEFAULT_DYN_SERVER_ADDRESS: &str = "http://dynserver.dyn.peachcloud.org"; pub const DEFAULT_DYN_NAMESERVER: &str = "ns.peachcloud.org"; - // we make use of Serde default values in order to make PeachCloud // robust and keep running even with a not fully complete config.yml // main type which represents all peachcloud configurations @@ -140,7 +139,6 @@ pub fn get_dyndns_server_address() -> Result { else { Ok(DEFAULT_DYN_SERVER_ADDRESS.to_string()) } - } pub fn set_dyndns_enabled_value(enabled_value: bool) -> Result { From 4a27892ab66df2a404c2d06d06db42b240322afc Mon Sep 17 00:00:00 2001 From: glyph Date: Wed, 12 Jan 2022 10:58:36 +0200 Subject: [PATCH 7/8] idiomatic paths and result type for checking new dns address --- peach-lib/src/dyndns_client.rs | 36 +++++++++++----------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/peach-lib/src/dyndns_client.rs b/peach-lib/src/dyndns_client.rs index c38b852..1e1c97f 100644 --- a/peach-lib/src/dyndns_client.rs +++ b/peach-lib/src/dyndns_client.rs @@ -9,14 +9,8 @@ //! //! The domain for dyndns updates is stored in /var/lib/peachcloud/config.yml //! The tsig key for authenticating the updates is stored in /var/lib/peachcloud/peach-dyndns/tsig.key -use std::{ - fs, - fs::OpenOptions, - io::Write, - process::{Command}, - str::FromStr, -}; use std::ffi::OsStr; +use std::{fs, fs::OpenOptions, io::Write, process::Command, str::FromStr}; use chrono::prelude::*; use jsonrpc_client_core::{expand_params, jsonrpc_client}; @@ -24,11 +18,8 @@ use jsonrpc_client_http::HttpTransport; use log::{debug, info}; use regex::Regex; -use crate::{ - config_manager::{load_peach_config, set_peach_dyndns_config}, - error::PeachError, -}; use crate::config_manager::get_dyndns_server_address; +use crate::{config_manager, error::PeachError}; /// constants for dyndns configuration pub const TSIG_KEY_PATH: &str = "/var/lib/peachcloud/peach-dyndns/tsig.key"; @@ -75,7 +66,8 @@ pub fn register_domain(domain: &str) -> std::result::Result // save new TSIG key save_dyndns_key(&key)?; // save new configuration values - let set_config_result = set_peach_dyndns_config(domain, &http_server, TSIG_KEY_PATH, true); + let set_config_result = + config_manager::set_peach_dyndns_config(domain, &http_server, TSIG_KEY_PATH, true); match set_config_result { Ok(_) => { let response = "success".to_string(); @@ -115,8 +107,7 @@ fn get_public_ip_address() -> Result { /// Reads dyndns configurations from config.yml /// and then uses nsupdate to update the IP address for the configured domain pub fn dyndns_update_ip() -> Result { - - let peach_config = load_peach_config()?; + let peach_config = config_manager::load_peach_config()?; info!( "Using config: dyn_tsig_key_path: {:?} @@ -226,18 +217,14 @@ pub fn get_num_seconds_since_successful_dns_update() -> Result, Peac /// and has successfully run recently (in the last six minutes) pub fn is_dns_updater_online() -> Result { // first check if it is enabled in peach-config - let peach_config = load_peach_config()?; + let peach_config = config_manager::load_peach_config()?; let is_enabled = peach_config.dyn_enabled; // then check if it has successfully run within the last 6 minutes (60*6 seconds) let num_seconds_since_successful_update = get_num_seconds_since_successful_dns_update()?; let ran_recently: bool = match num_seconds_since_successful_update { - Some(seconds) => { - seconds < (60 * 6) - } + Some(seconds) => seconds < (60 * 6), // if the value is None, then the last time it ran successfully is unknown - None => { - false - } + None => false, }; // debug log info!("is_dyndns_enabled: {:?}", is_enabled); @@ -260,11 +247,10 @@ pub fn get_dyndns_subdomain(dyndns_full_domain: &str) -> Option { } // helper function which checks if a dyndns domain is new -pub fn check_is_new_dyndns_domain(dyndns_full_domain: &str) -> bool { - // TODO: return `Result` and replace `unwrap` with `?` operator - let peach_config = load_peach_config().unwrap(); +pub fn check_is_new_dyndns_domain(dyndns_full_domain: &str) -> Result { + let peach_config = config_manager::load_peach_config()?; let previous_dyndns_domain = peach_config.dyn_domain; - dyndns_full_domain != previous_dyndns_domain + Ok(dyndns_full_domain != previous_dyndns_domain) } jsonrpc_client!(pub struct PeachDynDnsClient { From c3fbc5cd73f7d21bb34c4c2a853b9b5314e184a3 Mon Sep 17 00:00:00 2001 From: glyph Date: Wed, 12 Jan 2022 10:59:13 +0200 Subject: [PATCH 8/8] try operator for dyns dns domain check --- peach-web/src/routes/settings/dns.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/peach-web/src/routes/settings/dns.rs b/peach-web/src/routes/settings/dns.rs index 4c3f952..6733cfa 100644 --- a/peach-web/src/routes/settings/dns.rs +++ b/peach-web/src/routes/settings/dns.rs @@ -40,7 +40,7 @@ pub fn save_dns_configuration(dns_form: DnsForm) -> Result<(), PeachWebError> { if dns_form.enable_dyndns { let full_dynamic_domain = get_full_dynamic_domain(&dns_form.dynamic_domain); // check if this is a new domain or if its already registered - let is_new_domain = check_is_new_dyndns_domain(&full_dynamic_domain); + let is_new_domain = check_is_new_dyndns_domain(&full_dynamic_domain)?; if is_new_domain { match dyndns_client::register_domain(&full_dynamic_domain) { Ok(_) => {