-// 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<T> = std::result::Result<T, Box<dyn std::error::Error>>;
+
+#[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<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)
+ }
+ }
+ }
}
-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<Ipv4Addr> {
+
+ 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::<IpAddr>() {
+ 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() {