From a741015a5979b2a18e997b1efe39f604c98812ca Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Mon, 1 Mar 2021 14:08:39 +0100 Subject: [PATCH] Beginning of gandi livedns implementation (WIP). --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 92 +++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 67 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5619ced..f290654 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,7 @@ dependencies = [ "reqwest", "ron", "serde", + "serde_json", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 6c4adc7..2480de3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ edition = "2018" 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. [profile.release] diff --git a/src/main.rs b/src/main.rs index 84306f6..e6eadc8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,22 +3,25 @@ * Some inspiration: https://github.com/rmarchant/gandi-ddns/blob/master/gandi_ddns.py */ -use std::{ thread, time, fs::File, net::{ IpAddr, Ipv4Addr } }; +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 }; type Result = std::result::Result>; #[derive(Debug)] -struct GetRealIpError; +struct Error { + message: String +} -impl std::fmt::Display for GetRealIpError { +impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "SuperErrorSideKick is here!") + f.write_str(&self.message) } } -impl std::error::Error for GetRealIpError {} +impl std::error::Error for Error {} #[derive(Debug, Deserialize, Serialize)] struct Config { @@ -71,22 +74,10 @@ fn main() -> Result<()> { fn check_and_update_dns(api_key: &str) { /* - let url = "https://api.gandi.net/v5/livedns/domains"; - let client = reqwest::blocking::Client::new(); - - match client.get(url).header("Authorization", format!("Apikey {}", api_key)).send() { - Ok(resp) => - if resp.status().is_success() { - let content = resp.text().unwrap(); - println!("Content:\n{:?}", content); - } else { - println!("Request unsuccessful:\n{:#?}", resp); - }, - Err(error) => - println!("Error during request: {:?}", error) - } */ - dbg!(get_real_ip()); + //dbg!(get_real_ip()); + + get_current_record_ip(api_key); } fn get_real_ip() -> Result { @@ -100,25 +91,72 @@ fn get_real_ip() -> Result { let content = resp.text().unwrap(); match content.parse::() { Ok(IpAddr::V4(ip_v4)) => Ok(ip_v4), - /*Err(_)*/ _ => Err(Box::new(GetRealIpError)) + /*Err(_)*/ _ => Err(Box::new(Error { message: String::from("Can't parse IPv4 from ipify") })) } //println!("Content:\n{:?}", content); } else { - println!("Request unsuccessful:\n{:#?}", resp); - Err(Box::new(GetRealIpError)) + //println!("Request unsuccessful:\n{:#?}", resp); + Err(Box::new(Error { message: format!("Request unsuccessful: {:#?}", resp) })) }, Err(error) => { - println!("Error during request: {:?}", error); - Err(Box::new(GetRealIpError)) + //println!("Error during request: {:?}", error); + Err(Box::new(Error { message: format!("Error during request: {:?}", error) })) } } } -fn get_current_record_ip() { +fn request_livedns_gandi(api_key: &str, url_fragment: &str) -> 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() { + 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 update_record_ip() { +fn get_current_record_ip(api_key: &str) -> Result { + request_livedns_gandi(api_key, "domains/euphorik.ch/records/home/A"); // TODO... + // .map() + //.map(|json_value| json_value["rrset_values"][0].as_str().unwrap()) + + + + //let url = "https://api.gandi.net/v5/livedns/domains/euphorik.ch/records"; + //let url = "https://api.gandi.net/v5/livedns/domains"; // ?sharing_id=" + //let url = "https://api.gandi.net/v5/organization/organizations"; + //let url = "https://api.gandi.net/v5/livedns/domains/euphorik.ch/records/home/A"; + + /* + let client = reqwest::blocking::Client::new(); + + match client.get(url).header("Authorization", format!("Apikey {}", api_key)).send() { + Ok(resp) => + if resp.status().is_success() { + let content = resp.text().unwrap(); + + let json: Value = serde_json::from_str(&content).unwrap(); + let prout = json["rrset_values"][0].as_str().unwrap(); + println!("IP: {}", prout); + + println!("Content:\n{}", serde_json::to_string_pretty(&json).unwrap()); + } else { + println!("Request unsuccessful:\n{:#?}", resp); + }, + Err(error) => + println!("Error during request: {:?}", error) + } + */ +} + +fn update_record_ip() { } -- 2.45.1