X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=src%2Fmain.rs;h=67c251a9b668376c6eaf081fa6151b63160bedbd;hb=f66ea654402cb0a44d075466a1a55a89b5512e4a;hp=c5edfc9f3e232c57fc2fb4a77d0274a444944089;hpb=599d755773a28c1595289ffe2cf635376168ebf9;p=gandi_dns_update.git diff --git a/src/main.rs b/src/main.rs index c5edfc9..67c251a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,10 +11,10 @@ #![cfg_attr(debug_assertions, allow(unused_variables, unused_imports, dead_code))] -use std::{ fmt::format, fs::File, net::{ IpAddr, Ipv4Addr }, thread, time }; +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; +use serde_json::{ Value, json }; // A generic result of type 'T'. type Result = std::result::Result>; @@ -32,16 +32,18 @@ impl std::fmt::Display for Error { impl std::error::Error for Error { } -#[derive(Debug, Deserialize, Serialize)] +#[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(60), api_key: String::from(""), domains: Vec::new() } + 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 { @@ -61,16 +63,17 @@ impl Config { const FILE_CONF: &str = "config.ron"; fn main() -> Result<()> { + println!("GANDI DynDNS"); let config = Config::read(FILE_CONF)?; - println!("Configuration: {:?}", config); + 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.domains) { + if let Err(err) = check_and_update_dns(&config.api_key, &config.fqdn, &config.domains, config.ttl) { println!("!! Error: {}", err); } @@ -83,17 +86,15 @@ fn main() -> Result<()> { } } -fn check_and_update_dns(api_key: &str, domains: &Vec) -> Result<()> { +fn check_and_update_dns(api_key: &str, fqdn: &str, domains: &Vec, ttl: i32) -> Result<()> { let real_ip = get_real_ip()?; - dbg!(&real_ip); for domain in domains { - let current_ip = get_current_record_ip(api_key, domain)?; - dbg!(domain, current_ip); + let current_ip = get_current_record_ip(api_key, domain, fqdn)?; 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()?; + update_record_ip(api_key, domain, fqdn, real_ip, ttl)?; println!("Renewing of {} successfully", domain); } } @@ -124,11 +125,22 @@ fn get_real_ip() -> Result { } } -fn request_livedns_gandi(api_key: &str, url_fragment: &str) -> Result { +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(); - match client.get(url).header("Authorization", format!("Apikey {}", api_key)).send() { + 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(); @@ -141,8 +153,8 @@ fn request_livedns_gandi(api_key: &str, url_fragment: &str) -> Result { } } -fn get_current_record_ip(api_key: &str, name: &str) -> Result { - let json_value = request_livedns_gandi(api_key, &format!("domains/euphorik.ch/records/{}/A", name))?; +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)?; match &json_value["rrset_values"][0] { Value::String(ip_str) => @@ -152,7 +164,18 @@ fn get_current_record_ip(api_key: &str, name: &str) -> Result { } } -fn update_record_ip() -> Result<()> { - // TODO. +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(()) }