Merge branch 'master' of gburri.org:gandi_dns_update master
authorGreg Burri <greg.burri@gmail.com>
Sat, 28 Jan 2023 23:11:48 +0000 (00:11 +0100)
committerGreg Burri <greg.burri@gmail.com>
Sat, 28 Jan 2023 23:11:48 +0000 (00:11 +0100)
Cargo.lock
Cargo.toml
src/config.rs [new file with mode: 0644]
src/error.rs [new file with mode: 0644]
src/main.rs

index bdbebd6..b3bd7af 100644 (file)
@@ -566,9 +566,9 @@ dependencies = [
 
 [[package]]
 name = "ron"
-version = "0.6.6"
+version = "0.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "86018df177b1beef6c7c8ef949969c4f7cb9a9344181b92486b23c79995bdaa4"
+checksum = "300a51053b1cb55c80b7a9fde4120726ddf25ca241a1cbb926626f62fb136bff"
 dependencies = [
  "base64 0.13.1",
  "bitflags",
index 9ce91d2..04e8242 100644 (file)
@@ -2,7 +2,7 @@
 name = "gandi_dns_update"
 version = "0.1.0"
 authors = ["Greg Burri <greg.burri@gmail.com>"]
-edition = "2018"
+edition = "2021"
 
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
@@ -11,7 +11,7 @@ reqwest = { version = "0.11", features = ["blocking", "json"] }
 itertools = "0.10"
 serde = { version = "1.0", features = ["derive"] }
 serde_json = "1.0"
-ron = "0.6" # Rust object notation, to load configuration files.
+ron = "0.8" # Rust object notation, to load configuration files.
 
 [profile.release]
 codegen-units = 1
diff --git a/src/config.rs b/src/config.rs
new file mode 100644 (file)
index 0000000..d8a13d9
--- /dev/null
@@ -0,0 +1,38 @@
+use std::{fs::File, time};\r
+\r
+use ron::{de::from_reader, ser::to_writer};\r
+use serde::{Deserialize, Serialize};\r
+\r
+use crate::error::Result;\r
+\r
+#[derive(Debug, Clone, Deserialize, Serialize)]\r
+pub struct Config {\r
+    pub delay_between_check: time::Duration,\r
+    pub api_key: String,\r
+    pub domains: Vec<(String, String)>, // Hostname, domain.\r
+    pub ttl: i32,\r
+}\r
+\r
+impl Config {\r
+    pub fn default() -> Self {\r
+        Config {\r
+            delay_between_check: time::Duration::from_secs(120),\r
+            api_key: String::from(""),\r
+            domains: Vec::new(),\r
+            ttl: 300,\r
+        }\r
+    }\r
+\r
+    pub fn read(file_path: &str) -> Result<Config> {\r
+        match File::open(file_path) {\r
+            Ok(file) => from_reader(file).map_err(|e| e.into()),\r
+            // The file doesn't exit -> create it with default values.\r
+            Err(_) => {\r
+                let file = File::create(file_path)?;\r
+                let default_config = Config::default();\r
+                to_writer(file, &default_config)?;\r
+                Ok(default_config)\r
+            }\r
+        }\r
+    }\r
+}\r
diff --git a/src/error.rs b/src/error.rs
new file mode 100644 (file)
index 0000000..e047f56
--- /dev/null
@@ -0,0 +1,15 @@
+// A generic result of type 'T'.\r
+pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;\r
+\r
+#[derive(Debug)]\r
+pub struct Error {\r
+    pub message: String,\r
+}\r
+\r
+impl std::fmt::Display for Error {\r
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {\r
+        write!(f, "Error: {}", &self.message)\r
+    }\r
+}\r
+\r
+impl std::error::Error for Error {}\r
index 5cc0b99..6665155 100644 (file)
@@ -1,64 +1,28 @@
 /*
  * 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 serde_json::{ Value, json };
+use std::{
+    net::{IpAddr, Ipv4Addr},
+    thread, time,
+};
 
-// A generic result of type 'T'.
-type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
+use serde_json::{json, Value};
 
-#[derive(Debug)]
-struct Error {
-    message: String
-}
+use crate::{
+    config::Config,
+    error::{Error, Result},
+};
 
-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<String>,
-    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<Config> {
-        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)
-            }
-        }
-    }
-}
+mod config;
+mod error;
 
 const FILE_CONF: &str = "config.ron";
 
@@ -67,13 +31,19 @@ fn main() -> Result<()> {
 
     let config = Config::read(FILE_CONF)?;
 
-    println!("Configuration: {:?}", Config { api_key: String::from("*****"), ..config.clone() });
+    println!(
+        "Configuration: {:?}",
+        Config {
+            api_key: String::from("*****"),
+            ..config.clone()
+        }
+    );
 
     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;
@@ -85,16 +55,19 @@ fn main() -> Result<()> {
     }
 }
 
-fn check_and_update_dns(api_key: &str, fqdn: &str, domains: &Vec<String>, 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)?;
-            println!("Renewing of {} successfully", domain);
+            println!(
+                "IP addresses don't match for domain {}: real = {}, dns = {}. Renewing DNS...",
+                domain, real_ip, current_ip
+            );
+            update_record_ip(api_key, hostname, domain, real_ip, ttl)?;
+            println!("Renewing of {}.{} successfully", hostname, domain);
         }
     }
 
@@ -102,79 +75,98 @@ fn check_and_update_dns(api_key: &str, fqdn: &str, domains: &Vec<String>, ttl: i
 }
 
 fn get_real_ip() -> Result<Ipv4Addr> {
-
     let url = "https://api.ipify.org";
     let client = reqwest::blocking::Client::new();
 
     match client.get(url).send() {
-        Ok(resp) =>
+        Ok(resp) => {
             if resp.status().is_success() {
                 let content = resp.text().unwrap();
-                match content.parse::<IpAddr>() {
+                match content.parse() {
                     Ok(IpAddr::V4(ip_v4)) => Ok(ip_v4),
-                    _ => Err(Box::new(Error { message: String::from("Can't parse IPv4 from ipify") }))
+                    _ => Err(Box::new(Error {
+                        message: String::from("Can't parse IPv4 from ipify"),
+                    })),
                 }
             } else {
-                Err(Box::new(Error { message: format!("Request unsuccessful: {:#?}", resp) }))
-            },
-
-        Err(error) => {
-            Err(Box::new(Error { message: format!("Error during request: {:?}", error) }))
+                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),
+        })),
+    }
+}
+
+fn get_current_record_ip(api_key: &str, name: &str, fqdn: &str) -> Result<Ipv4Addr> {
+    let json_value = request_livedns_gandi(
+        api_key,
+        &format!("domains/{}/records/{}/A", fqdn, name),
+        Method::Get,
+    )?;
+
+    match &json_value["rrset_values"][0] {
+        Value::String(ip_str) => Ok(ip_str.parse()?),
+        _ => Result::Err(Box::new(Error {
+            message: format!(
+                "Unable to extract the IP from the JSON answer: {}",
+                json_value
+            ),
+        })),
     }
 }
 
+fn update_record_ip(api_key: &str, name: &str, fqdn: &str, ip: Ipv4Addr, ttl: i32) -> Result<()> {
+    let json_body = json!(
+        {
+            "rrset_values": [ format!("{}", ip) ],
+            "rrset_ttl": ttl
+        }
+    );
+
+    let json_value = request_livedns_gandi(
+        api_key,
+        &format!("domains/{}/records/{}/A", fqdn, name),
+        Method::Put(json_body.to_string()),
+    )?;
+
+    println!("Update response: {}", json_value);
+
+    Ok(())
+}
+
 enum Method {
     Put(String),
-    Get
+    Get,
 }
 
 fn request_livedns_gandi(api_key: &str, url_fragment: &str, method: Method) -> Result<Value> {
     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)
-        };
+    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) =>
+    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<Ipv4Addr> {
-    let json_value = request_livedns_gandi(api_key, &format!("domains/{}/records/{}/A", fqdn, name), Method::Get)?;
-
-    match &json_value["rrset_values"][0] {
-        Value::String(ip_str) =>
-            Ok(ip_str.parse()?),
-        _ =>
-            Result::Err(Box::new(Error { message: format!("Unable to extract the IP from the JSON answer: {}", json_value) }))
-    }
-}
-
-fn update_record_ip(api_key: &str, name: &str, fqdn: &str, ip: Ipv4Addr, ttl: i32) -> Result<()> {
-    let json_body =
-        json!(
-            {
-                "rrset_values": [ format!("{}", ip) ],
-                "rrset_ttl": ttl
+                Err(Box::new(Error {
+                    message: format!("Request unsuccessful to {}: {:#?}", &url, resp),
+                }))
             }
-        );
-
-    let json_value = request_livedns_gandi(api_key, &format!("domains/{}/records/{}/A", fqdn, name), Method::Put(json_body.to_string()))?;
-
-    println!("Update response: {}", json_value);
-
-    Ok(())
+        }
+        Err(error) => Err(Box::new(Error {
+            message: format!("Error during request to {}: {:?}", &url, error),
+        })),
+    }
 }