Include 'url' in error message
[gandi_dns_update.git] / src / main.rs
1 /*
2 * API Reference: https://api.gandi.net/docs/livedns/
3 *
4 * Some similar implementations:
5 * - https://github.com/rmarchant/gandi-ddns/blob/master/gandi_ddns.py
6 * - https://github.com/brianhp2/gandi-automatic-dns
7 *
8 */
9
10 #![cfg_attr(debug_assertions, allow(unused_variables, unused_imports, dead_code))]
11
12 use std::{ net::{ IpAddr, Ipv4Addr }, thread, time };
13 use serde_json::{ Value, json };
14
15 mod error;
16 mod config;
17
18 use crate::error::{ Result, Error };
19 use crate::config::Config;
20
21 const FILE_CONF: &str = "config.ron";
22
23 fn main() -> Result<()> {
24 println!("=== GANDI LiveDNS updater ===");
25
26 let config = Config::read(FILE_CONF)?;
27
28 println!("Configuration: {:?}", Config { api_key: String::from("*****"), ..config.clone() });
29
30 loop {
31 let time_beginning_loop = time::Instant::now();
32
33 if let Err(err) = check_and_update_dns(&config.api_key, &config.fqdn, &config.domains, config.ttl) {
34 println!("!! {}", err);
35 }
36
37 let elapsed = time::Instant::now() - time_beginning_loop;
38
39 if elapsed < config.delay_between_check {
40 let to_wait = config.delay_between_check - elapsed;
41 thread::sleep(to_wait);
42 }
43 }
44 }
45
46 fn check_and_update_dns(api_key: &str, fqdn: &str, domains: &Vec<String>, ttl: i32) -> Result<()> {
47 let real_ip = get_real_ip()?;
48
49 for domain in domains {
50 let current_ip = get_current_record_ip(api_key, domain, fqdn)?;
51
52 if real_ip != current_ip {
53 println!("IP addresses don't match for domain {}: real = {}, dns = {}. Renewing DNS...", domain, real_ip, current_ip);
54 update_record_ip(api_key, domain, fqdn, real_ip, ttl)?;
55 println!("Renewing of {} successfully", domain);
56 }
57 }
58
59 Ok(())
60 }
61
62 fn get_real_ip() -> Result<Ipv4Addr> {
63 let url = "https://api.ipify.org";
64 let client = reqwest::blocking::Client::new();
65
66 match client.get(url).send() {
67 Ok(resp) =>
68 if resp.status().is_success() {
69 let content = resp.text().unwrap();
70 match content.parse() {
71 Ok(IpAddr::V4(ip_v4)) => Ok(ip_v4),
72 _ => Err(Box::new(Error { message: String::from("Can't parse IPv4 from ipify") }))
73 }
74 } else {
75 Err(Box::new(Error { message: format!("Request unsuccessful to {}: {:#?}", url, resp) }))
76 },
77
78 Err(error) => {
79 Err(Box::new(Error { message: format!("Error during request to {}: {:?}", url, error) }))
80 }
81 }
82 }
83
84 fn get_current_record_ip(api_key: &str, name: &str, fqdn: &str) -> Result<Ipv4Addr> {
85 let json_value = request_livedns_gandi(api_key, &format!("domains/{}/records/{}/A", fqdn, name), Method::Get)?;
86
87 match &json_value["rrset_values"][0] {
88 Value::String(ip_str) =>
89 Ok(ip_str.parse()?),
90 _ =>
91 Result::Err(Box::new(Error { message: format!("Unable to extract the IP from the JSON answer: {}", json_value) }))
92 }
93 }
94
95 fn update_record_ip(api_key: &str, name: &str, fqdn: &str, ip: Ipv4Addr, ttl: i32) -> Result<()> {
96 let json_body =
97 json!(
98 {
99 "rrset_values": [ format!("{}", ip) ],
100 "rrset_ttl": ttl
101 }
102 );
103
104 let json_value = request_livedns_gandi(api_key, &format!("domains/{}/records/{}/A", fqdn, name), Method::Put(json_body.to_string()))?;
105
106 println!("Update response: {}", json_value);
107
108 Ok(())
109 }
110
111 enum Method {
112 Put(String),
113 Get
114 }
115
116 fn request_livedns_gandi(api_key: &str, url_fragment: &str, method: Method) -> Result<Value> {
117 let url = format!("https://api.gandi.net/v5/livedns/{}", url_fragment);
118 let client = reqwest::blocking::Client::new();
119
120 let request_builder =
121 match method {
122 Method::Put(body) => client.put(&url).body(body),
123 Method::Get => client.get(&url)
124 };
125
126 match request_builder.header("Authorization", format!("Apikey {}", api_key)).send() {
127 Ok(resp) =>
128 if resp.status().is_success() {
129 let content = resp.text().unwrap();
130 Ok(serde_json::from_str(&content).unwrap())
131 } else {
132 Err(Box::new(Error { message: format!("Request unsuccessful to {}: {:#?}", &url, resp) }))
133 },
134 Err(error) =>
135 Err(Box::new(Error { message: format!("Error during request to {}: {:?}", &url, error) }))
136 }
137 }