idiomatic paths and result type for checking new dns address

This commit is contained in:
glyph 2022-01-12 10:58:36 +02:00
parent 4adf5547c9
commit 4a27892ab6
1 changed files with 11 additions and 25 deletions

View File

@ -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<String, PeachError>
// 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<String, PeachError> {
/// 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<bool, PeachError> {
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<Option<i64>, Peac
/// and has successfully run recently (in the last six minutes)
pub fn is_dns_updater_online() -> Result<bool, PeachError> {
// 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<String> {
}
// 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<bool, PeachError>` and replace `unwrap` with `?` operator
let peach_config = load_peach_config().unwrap();
pub fn check_is_new_dyndns_domain(dyndns_full_domain: &str) -> Result<bool, PeachError> {
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 {