From: Greg Burri Date: Tue, 13 Jul 2021 17:48:42 +0000 (+0200) Subject: DNS update is now working X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=79787cfdf89c320de9b12080b04f56ac38c39073;p=gandi_dns_update.git DNS update is now working --- diff --git a/deploy.ps1 b/deploy.ps1 new file mode 100644 index 0000000..eb1a5ea --- /dev/null +++ b/deploy.ps1 @@ -0,0 +1,28 @@ +#!/usr/bin/env pwsh + +if ($args.Count -lt 1) { + $scriptName = [Environment]::GetCommandLineArgs()[1] + Write-Output "Usage: $scriptName " + exit 1 +} + +git pull +cargo build --release + +systemctl --user stop gandi_dns_update + +$destination=$args[0] + +if (!(Test-Path -Path $destination)) { + New-Item -ItemType directory -Path $destination +} + +strip target/release/gandi_dns_update # To reduce the executable size. +Copy-Item target/release/gandi_dns_update -Destination $destination + +# Do not overwrite the configuration. +if (!(Test-Path -Path $destination/config.ron)) { + Copy-Item backend/config.ron -Destination $destination +} + +systemctl --user start gandi_dns_update diff --git a/doc/gandi_dns_update.service b/doc/gandi_dns_update.service new file mode 100644 index 0000000..ed32e27 --- /dev/null +++ b/doc/gandi_dns_update.service @@ -0,0 +1,14 @@ +[Unit] +Description=gandi_dns_update + +[Service] +WorkingDirectory=/var/lib/gandi_dns_update +ExecStart=/var/lib/gandi_dns_update +SyslogIdentifier=gandi_dns_update +Restart=always +RestartSec=10 +KillSignal=SIGINT + +[Install] +WantedBy=default.target + diff --git a/src/main.rs b/src/main.rs index c5edfc9..3c3b4f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use std::{ fmt::format, 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>; @@ -36,12 +36,14 @@ impl std::error::Error for Error { } 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,6 +63,7 @@ impl Config { const FILE_CONF: &str = "config.ron"; fn main() -> Result<()> { + println!("GANDI DynDNS"); let config = Config::read(FILE_CONF)?; @@ -70,7 +73,7 @@ fn main() -> Result<()> { 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,17 @@ 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)?; + let current_ip = get_current_record_ip(api_key, domain, fqdn)?; dbg!(domain, current_ip); 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 +127,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 +155,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 +166,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()))?; + + dbg!(json_value); + Ok(()) }