X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=src%2Fmain.rs;h=84306f6adb8631034e52bd6e29513b480889614a;hb=4d545d80471a5c59f21c3b530a40274b2760c9be;hp=1509a258bdc164947a62b401f399128f636cc15d;hpb=829a38c408dad553be65663ffad2094d8ec76510;p=gandi_dns_update.git diff --git a/src/main.rs b/src/main.rs index 1509a25..84306f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,118 @@ -// API Reference: https://api.gandi.net/docs/livedns/ -// Some inspiration: https://github.com/rmarchant/gandi-ddns/blob/master/gandi_ddns.py +/* + * API Reference: https://api.gandi.net/docs/livedns/ + * Some inspiration: https://github.com/rmarchant/gandi-ddns/blob/master/gandi_ddns.py + */ +use std::{ thread, time, fs::File, net::{ IpAddr, Ipv4Addr } }; +use ron::{ de::from_reader, ser::to_writer }; +use serde::{ Deserialize, Serialize }; -fn main() { - println!("Hello, world!"); +type Result = std::result::Result>; + +#[derive(Debug)] +struct GetRealIpError; + +impl std::fmt::Display for GetRealIpError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "SuperErrorSideKick is here!") + } +} + +impl std::error::Error for GetRealIpError {} + +#[derive(Debug, Deserialize, Serialize)] +struct Config { + delay_between_check: time::Duration, + api_key: String, +} + +impl Config { + fn default() -> Self { + Config { delay_between_check: time::Duration::from_secs(60), api_key: String::from("") } + } + + 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) + } + } + } } -fn get_real_ip() { +const FILE_CONF: &str = "config.ron"; + +fn main() -> Result<()> { + println!("GANDI DynDNS"); + + let config = Config::read(FILE_CONF)?; + + println!("Configuration: {:?}", config); + + loop { + let time_beginning_loop = time::Instant::now(); + + check_and_update_dns(&config.api_key); + + let elapsed = time::Instant::now() - time_beginning_loop; + + if elapsed < config.delay_between_check { + let to_wait = config.delay_between_check - elapsed; + thread::sleep(to_wait); + } + + } +} + +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()); +} + +fn get_real_ip() -> Result { + + let url = "https://api.ipify.org"; + let client = reqwest::blocking::Client::new(); + + match client.get(url).send() { + Ok(resp) => + if resp.status().is_success() { + let content = resp.text().unwrap(); + match content.parse::() { + Ok(IpAddr::V4(ip_v4)) => Ok(ip_v4), + /*Err(_)*/ _ => Err(Box::new(GetRealIpError)) + } + //println!("Content:\n{:?}", content); + } else { + println!("Request unsuccessful:\n{:#?}", resp); + Err(Box::new(GetRealIpError)) + }, + Err(error) => { + println!("Error during request: {:?}", error); + Err(Box::new(GetRealIpError)) + } + } } fn get_current_record_ip() {