idiomatic paths and result type for checking new dns address
This commit is contained in:
		| @ -9,14 +9,8 @@ | |||||||
| //! | //! | ||||||
| //! The domain for dyndns updates is stored in /var/lib/peachcloud/config.yml | //! 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 | //! 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::ffi::OsStr; | ||||||
|  | use std::{fs, fs::OpenOptions, io::Write, process::Command, str::FromStr}; | ||||||
|  |  | ||||||
| use chrono::prelude::*; | use chrono::prelude::*; | ||||||
| use jsonrpc_client_core::{expand_params, jsonrpc_client}; | use jsonrpc_client_core::{expand_params, jsonrpc_client}; | ||||||
| @ -24,11 +18,8 @@ use jsonrpc_client_http::HttpTransport; | |||||||
| use log::{debug, info}; | use log::{debug, info}; | ||||||
| use regex::Regex; | 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::get_dyndns_server_address; | ||||||
|  | use crate::{config_manager, error::PeachError}; | ||||||
|  |  | ||||||
| /// constants for dyndns configuration | /// constants for dyndns configuration | ||||||
| pub const TSIG_KEY_PATH: &str = "/var/lib/peachcloud/peach-dyndns/tsig.key"; | 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<String, PeachError> | |||||||
|     // save new TSIG key |     // save new TSIG key | ||||||
|     save_dyndns_key(&key)?; |     save_dyndns_key(&key)?; | ||||||
|     // save new configuration values |     // 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 { |     match set_config_result { | ||||||
|         Ok(_) => { |         Ok(_) => { | ||||||
|             let response = "success".to_string(); |             let response = "success".to_string(); | ||||||
| @ -115,8 +107,7 @@ fn get_public_ip_address() -> Result<String, PeachError> { | |||||||
| /// Reads dyndns configurations from config.yml | /// Reads dyndns configurations from config.yml | ||||||
| /// and then uses nsupdate to update the IP address for the configured domain | /// and then uses nsupdate to update the IP address for the configured domain | ||||||
| pub fn dyndns_update_ip() -> Result<bool, PeachError> { | pub fn dyndns_update_ip() -> Result<bool, PeachError> { | ||||||
|  |     let peach_config = config_manager::load_peach_config()?; | ||||||
|     let peach_config = load_peach_config()?; |  | ||||||
|     info!( |     info!( | ||||||
|         "Using config: |         "Using config: | ||||||
|     dyn_tsig_key_path: {:?} |     dyn_tsig_key_path: {:?} | ||||||
| @ -226,18 +217,14 @@ pub fn get_num_seconds_since_successful_dns_update() -> Result<Option<i64>, Peac | |||||||
| /// and has successfully run recently (in the last six minutes) | /// and has successfully run recently (in the last six minutes) | ||||||
| pub fn is_dns_updater_online() -> Result<bool, PeachError> { | pub fn is_dns_updater_online() -> Result<bool, PeachError> { | ||||||
|     // first check if it is enabled in peach-config |     // 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; |     let is_enabled = peach_config.dyn_enabled; | ||||||
|     // then check if it has successfully run within the last 6 minutes (60*6 seconds) |     // 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 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) => { |         Some(seconds) => seconds < (60 * 6), | ||||||
|             seconds < (60 * 6) |  | ||||||
|         } |  | ||||||
|         // if the value is None, then the last time it ran successfully is unknown |         // if the value is None, then the last time it ran successfully is unknown | ||||||
|         None => { |         None => false, | ||||||
|             false |  | ||||||
|         } |  | ||||||
|     }; |     }; | ||||||
|     // debug log |     // debug log | ||||||
|     info!("is_dyndns_enabled: {:?}", is_enabled); |     info!("is_dyndns_enabled: {:?}", is_enabled); | ||||||
| @ -260,11 +247,10 @@ pub fn get_dyndns_subdomain(dyndns_full_domain: &str) -> Option<String> { | |||||||
| } | } | ||||||
|  |  | ||||||
| // helper function which checks if a dyndns domain is new | // helper function which checks if a dyndns domain is new | ||||||
| pub fn check_is_new_dyndns_domain(dyndns_full_domain: &str) -> bool { | pub fn check_is_new_dyndns_domain(dyndns_full_domain: &str) -> Result<bool, PeachError> { | ||||||
|     // TODO: return `Result<bool, PeachError>` and replace `unwrap` with `?` operator |     let peach_config = config_manager::load_peach_config()?; | ||||||
|     let peach_config = load_peach_config().unwrap(); |  | ||||||
|     let previous_dyndns_domain = peach_config.dyn_domain; |     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 { | jsonrpc_client!(pub struct PeachDynDnsClient { | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user