+fn check_and_update_dns(api_key: &str, fqdn: &str, domains: &Vec<String>, ttl: i32) -> Result<()> {
+ let real_ip = get_real_ip()?;
+
+ for domain in domains {
+ 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(api_key, domain, fqdn, real_ip, ttl)?;
+ println!("Renewing of {} successfully", domain);
+ }
+ }
+
+ Ok(())
+}
+
+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(Box::new(Error { message: String::from("Can't parse IPv4 from ipify") }))
+ }
+ } else {
+ Err(Box::new(Error { message: format!("Request unsuccessful: {:#?}", resp) }))
+ },
+
+ Err(error) => {
+ Err(Box::new(Error { message: format!("Error during request: {:?}", error) }))
+ }
+ }
+}
+
+enum Method {
+ Put(String),
+ Get
+}
+
+fn request_livedns_gandi(api_key: &str, url_fragment: &str, method: Method) -> Result<Value> {
+ let url = format!("https://api.gandi.net/v5/livedns/{}", url_fragment);
+ let client = reqwest::blocking::Client::new();
+
+ 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();
+ 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 get_current_record_ip(api_key: &str, name: &str, fqdn: &str) -> Result<Ipv4Addr> {
+ 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) =>
+ Ok(ip_str.parse()?),
+ _ =>
+ Result::Err(Box::new(Error { message: format!("Unable to extract the IP from the JSON answer: {}", json_value) }))
+ }
+}
+
+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);