projects
/
gandi_dns_update.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Display the hostname when renewing.
[gandi_dns_update.git]
/
src
/
main.rs
diff --git
a/src/main.rs
b/src/main.rs
index
79b2262
..
4b0d196
100644
(file)
--- a/
src/main.rs
+++ b/
src/main.rs
@@
-1,12
+1,10
@@
/*
* API Reference: https://api.gandi.net/docs/livedns/
/*
* API Reference: https://api.gandi.net/docs/livedns/
+ *
* Some similar implementations:
* - https://github.com/rmarchant/gandi-ddns/blob/master/gandi_ddns.py
* - https://github.com/brianhp2/gandi-automatic-dns
*
* Some similar implementations:
* - https://github.com/rmarchant/gandi-ddns/blob/master/gandi_ddns.py
* - https://github.com/brianhp2/gandi-automatic-dns
*
- * TODO:
- * - Log to stdout with (at least) timestamps.
- * - Renew function.
*/
#![cfg_attr(debug_assertions, allow(unused_variables, unused_imports, dead_code))]
*/
#![cfg_attr(debug_assertions, allow(unused_variables, unused_imports, dead_code))]
@@
-23,8
+21,7
@@
use crate::config::Config;
const FILE_CONF: &str = "config.ron";
fn main() -> Result<()> {
const FILE_CONF: &str = "config.ron";
fn main() -> Result<()> {
-
- println!("GANDI DynDNS");
+ println!("=== GANDI LiveDNS updater ===");
let config = Config::read(FILE_CONF)?;
let config = Config::read(FILE_CONF)?;
@@
-33,8
+30,8
@@
fn main() -> Result<()> {
loop {
let time_beginning_loop = time::Instant::now();
loop {
let time_beginning_loop = time::Instant::now();
- if let Err(err) = check_and_update_dns(&config.api_key, &config.
fqdn, &config.
domains, config.ttl) {
- println!("!!
Error:
{}", err);
+ if let Err(err) = check_and_update_dns(&config.api_key, &config.domains, config.ttl) {
+ println!("!! {}", err);
}
let elapsed = time::Instant::now() - time_beginning_loop;
}
let elapsed = time::Instant::now() - time_beginning_loop;
@@
-46,16
+43,16
@@
fn main() -> Result<()> {
}
}
}
}
-fn check_and_update_dns(api_key: &str,
fqdn: &str, domains: &Vec<String
>, ttl: i32) -> Result<()> {
+fn check_and_update_dns(api_key: &str,
domains: &Vec<(String, String)
>, ttl: i32) -> Result<()> {
let real_ip = get_real_ip()?;
let real_ip = get_real_ip()?;
- for
domain
in domains {
- let current_ip = get_current_record_ip(api_key,
domain, fqd
n)?;
+ for
(hostname, domain)
in domains {
+ let current_ip = get_current_record_ip(api_key,
hostname, domai
n)?;
if real_ip != current_ip {
println!("IP addresses don't match for domain {}: real = {}, dns = {}. Renewing DNS...", domain, real_ip, current_ip);
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, fqd
n, real_ip, ttl)?;
- println!("Renewing of {}
successfully"
, domain);
+ update_record_ip(api_key,
hostname, domai
n, real_ip, ttl)?;
+ println!("Renewing of {}
.{} successfully", hostname
, domain);
}
}
}
}
@@
-75,11
+72,11
@@
fn get_real_ip() -> Result<Ipv4Addr> {
_ => Err(Box::new(Error { message: String::from("Can't parse IPv4 from ipify") }))
}
} else {
_ => Err(Box::new(Error { message: String::from("Can't parse IPv4 from ipify") }))
}
} else {
- Err(Box::new(Error { message: format!("Request unsuccessful
: {:#?}"
, resp) }))
+ Err(Box::new(Error { message: format!("Request unsuccessful
to {}: {:#?}", url
, resp) }))
},
Err(error) => {
},
Err(error) => {
- Err(Box::new(Error { message: format!("Error during request
: {:?}"
, error) }))
+ Err(Box::new(Error { message: format!("Error during request
to {}: {:?}", url
, error) }))
}
}
}
}
}
}
@@
-122,8
+119,8
@@
fn request_livedns_gandi(api_key: &str, url_fragment: &str, method: Method) -> R
let request_builder =
match method {
let request_builder =
match method {
- Method::Put(body) => client.put(url).body(body),
- Method::Get => client.get(url)
+ Method::Put(body) => client.put(
&
url).body(body),
+ Method::Get => client.get(
&
url)
};
match request_builder.header("Authorization", format!("Apikey {}", api_key)).send() {
};
match request_builder.header("Authorization", format!("Apikey {}", api_key)).send() {
@@
-132,9
+129,9
@@
fn request_livedns_gandi(api_key: &str, url_fragment: &str, method: Method) -> R
let content = resp.text().unwrap();
Ok(serde_json::from_str(&content).unwrap())
} else {
let content = resp.text().unwrap();
Ok(serde_json::from_str(&content).unwrap())
} else {
- Err(Box::new(Error { message: format!("Request unsuccessful
: {:#?}"
, resp) }))
+ Err(Box::new(Error { message: format!("Request unsuccessful
to {}: {:#?}", &url
, resp) }))
},
Err(error) =>
},
Err(error) =>
- Err(Box::new(Error { message: format!("Error during request
: {:?}"
, error) }))
+ Err(Box::new(Error { message: format!("Error during request
to {}: {:?}", &url
, error) }))
}
}
}
}