X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=src%2Fmain.rs;h=f9d8f4ed2a37bf33f46c53f74a35b4a56d75ad22;hb=e45d8f67336160ae61a9dc24f899752f5199a01b;hp=67c251a9b668376c6eaf081fa6151b63160bedbd;hpb=f66ea654402cb0a44d075466a1a55a89b5512e4a;p=gandi_dns_update.git diff --git a/src/main.rs b/src/main.rs index 67c251a..f9d8f4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,70 +1,27 @@ /* * API Reference: https://api.gandi.net/docs/livedns/ + * * Some similar implementations: * - https://github.com/rmarchant/gandi-ddns/blob/master/gandi_ddns.py * - https://github.com/brianhp2/gandi-automatic-dns * - * TODO: - * - Log to stdout with (at least) timestamps. - * - Renew function. */ #![cfg_attr(debug_assertions, allow(unused_variables, unused_imports, dead_code))] -use std::{ fs::File, net::{ IpAddr, Ipv4Addr }, thread, time }; -use ron::{ de::from_reader, ser::to_writer }; -use serde::{ Deserialize, Serialize }; +use std::{ net::{ IpAddr, Ipv4Addr }, thread, time }; use serde_json::{ Value, json }; -// A generic result of type 'T'. -type Result = std::result::Result>; - -#[derive(Debug)] -struct Error { - message: String -} +mod error; +mod config; -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "Error: {}", &self.message) - } -} - -impl std::error::Error for Error { } - -#[derive(Debug, Clone, Deserialize, Serialize)] -struct Config { - delay_between_check: time::Duration, - api_key: String, - fqdn: String, - domains: Vec, - ttl: i32 -} - -impl Config { - fn default() -> Self { - Config { delay_between_check: time::Duration::from_secs(120), api_key: String::from(""), fqdn: String::from(""), domains: Vec::new(), ttl: 300 } - } - - fn read(file_path: &str) -> Result { - match File::open(file_path) { - Ok(file) => from_reader(file).map_err(|e| e.into()), - // The file doesn't exit -> create it with default values. - Err(_) => { - let file = File::create(file_path)?; - let default_config = Config::default(); - to_writer(file, &default_config)?; - Ok(default_config) - } - } - } -} +use crate::error::{ Result, Error }; +use crate::config::Config; const FILE_CONF: &str = "config.ron"; fn main() -> Result<()> { - - println!("GANDI DynDNS"); + println!("=== GANDI LiveDNS updater ==="); let config = Config::read(FILE_CONF)?; @@ -73,8 +30,8 @@ fn main() -> Result<()> { loop { let time_beginning_loop = time::Instant::now(); - if let Err(err) = check_and_update_dns(&config.api_key, &config.fqdn, &config.domains, config.ttl) { - println!("!! Error: {}", err); + if let Err(err) = check_and_update_dns(&config.api_key, &config.domains, config.ttl) { + println!("!! {}", err); } let elapsed = time::Instant::now() - time_beginning_loop; @@ -86,15 +43,15 @@ fn main() -> Result<()> { } } -fn check_and_update_dns(api_key: &str, fqdn: &str, domains: &Vec, ttl: i32) -> Result<()> { +fn check_and_update_dns(api_key: &str, domains: &Vec<(String, String)>, ttl: i32) -> Result<()> { let real_ip = get_real_ip()?; - for domain in domains { - let current_ip = get_current_record_ip(api_key, domain, fqdn)?; + for (hostname, domain) in domains { + let current_ip = get_current_record_ip(api_key, hostname, domain)?; if real_ip != current_ip { println!("IP addresses don't match for domain {}: real = {}, dns = {}. Renewing DNS...", domain, real_ip, current_ip); - update_record_ip(api_key, domain, fqdn, real_ip, ttl)?; + update_record_ip(api_key, hostname, domain, real_ip, ttl)?; println!("Renewing of {} successfully", domain); } } @@ -103,7 +60,6 @@ fn check_and_update_dns(api_key: &str, fqdn: &str, domains: &Vec, ttl: i } fn get_real_ip() -> Result { - let url = "https://api.ipify.org"; let client = reqwest::blocking::Client::new(); @@ -111,48 +67,20 @@ fn get_real_ip() -> Result { Ok(resp) => if resp.status().is_success() { let content = resp.text().unwrap(); - match content.parse::() { + match content.parse() { Ok(IpAddr::V4(ip_v4)) => Ok(ip_v4), _ => Err(Box::new(Error { message: String::from("Can't parse IPv4 from ipify") })) } } else { - Err(Box::new(Error { message: format!("Request unsuccessful: {:#?}", resp) })) + Err(Box::new(Error { message: format!("Request unsuccessful to {}: {:#?}", url, resp) })) }, Err(error) => { - Err(Box::new(Error { message: format!("Error during request: {:?}", error) })) + Err(Box::new(Error { message: format!("Error during request to {}: {:?}", url, error) })) } } } -enum Method { - Put(String), - Get -} - -fn request_livedns_gandi(api_key: &str, url_fragment: &str, method: Method) -> Result { - let url = format!("https://api.gandi.net/v5/livedns/{}", url_fragment); - let client = reqwest::blocking::Client::new(); - - let request_builder = - match method { - Method::Put(body) => client.put(url).body(body), - Method::Get => client.get(url) - }; - - match request_builder.header("Authorization", format!("Apikey {}", api_key)).send() { - Ok(resp) => - if resp.status().is_success() { - let content = resp.text().unwrap(); - Ok(serde_json::from_str(&content).unwrap()) - } else { - Err(Box::new(Error { message: format!("Request unsuccessful: {:#?}", resp) })) - }, - Err(error) => - Err(Box::new(Error { message: format!("Error during request: {:?}", error) })) - } -} - fn get_current_record_ip(api_key: &str, name: &str, fqdn: &str) -> Result { let json_value = request_livedns_gandi(api_key, &format!("domains/{}/records/{}/A", fqdn, name), Method::Get)?; @@ -179,3 +107,31 @@ fn update_record_ip(api_key: &str, name: &str, fqdn: &str, ip: Ipv4Addr, ttl: i3 Ok(()) } + +enum Method { + Put(String), + Get +} + +fn request_livedns_gandi(api_key: &str, url_fragment: &str, method: Method) -> Result { + let url = format!("https://api.gandi.net/v5/livedns/{}", url_fragment); + let client = reqwest::blocking::Client::new(); + + let request_builder = + match method { + Method::Put(body) => client.put(&url).body(body), + Method::Get => client.get(&url) + }; + + match request_builder.header("Authorization", format!("Apikey {}", api_key)).send() { + Ok(resp) => + if resp.status().is_success() { + let content = resp.text().unwrap(); + Ok(serde_json::from_str(&content).unwrap()) + } else { + Err(Box::new(Error { message: format!("Request unsuccessful to {}: {:#?}", &url, resp) })) + }, + Err(error) => + Err(Box::new(Error { message: format!("Error during request to {}: {:?}", &url, error) })) + } +}