gandi-live-dns-rust/src/main.rs

102 lines
3.6 KiB
Rust
Raw Normal View History

2021-12-24 19:20:31 -06:00
use crate::config::Config;
2022-01-23 02:22:41 -06:00
use anyhow;
2021-12-24 19:20:31 -06:00
use futures;
2022-01-23 02:22:41 -06:00
use reqwest::{header, Client, ClientBuilder, StatusCode};
2022-01-23 21:26:57 -06:00
use std::collections::HashMap;
2022-01-23 02:22:41 -06:00
use structopt::StructOpt;
use tokio::{self, task::JoinHandle};
2021-12-12 03:32:05 -06:00
mod config;
2021-12-14 21:30:36 -06:00
mod opts;
2022-01-23 15:12:27 -06:00
use die_exit::*;
2021-12-12 00:36:40 -06:00
2021-12-24 19:20:31 -06:00
fn gandi_api_url(fqdn: &str, rrset_name: &str, rrset_type: &str) -> String {
2022-01-23 02:22:41 -06:00
return format!(
" https://api.gandi.net/v5/livedns/domains/{}/records/{}/{}",
fqdn, rrset_name, rrset_type
);
2021-12-24 19:20:31 -06:00
}
2022-01-23 02:22:41 -06:00
fn api_client(api_key: &str) -> anyhow::Result<Client> {
2021-12-14 21:30:36 -06:00
let client_builder = ClientBuilder::new();
let key = format!("Apikey {}", api_key);
let mut auth_value = header::HeaderValue::from_str(&key)?;
let mut headers = header::HeaderMap::new();
auth_value.set_sensitive(true);
headers.insert(header::AUTHORIZATION, auth_value);
let accept_value = header::HeaderValue::from_static("application/json");
headers.insert(header::ACCEPT, accept_value);
let client = client_builder.default_headers(headers).build()?;
return Ok(client);
}
2022-01-23 15:12:27 -06:00
async fn get_ip(api_url: &str) -> anyhow::Result<String> {
let response = reqwest::get(api_url).await?;
let text = response.text().await?;
Ok(text)
}
2021-12-14 21:30:36 -06:00
#[tokio::main]
2022-01-23 02:22:41 -06:00
async fn main() -> anyhow::Result<()> {
2021-12-12 03:32:05 -06:00
let opts = opts::Opts::from_args();
2022-01-23 15:12:27 -06:00
let conf = config::load_config(&opts)
.die_with(|error| format!("Failed to read config file: {}", error));
config::validate_config(&conf).die_with(|error| format!("Invalid config: {}", error));
2022-01-23 15:38:16 -06:00
println!("Finding out the IP address...");
let ipv4_result = get_ip("https://api.ipify.org").await;
let ipv6_result = get_ip("https://api6.ipify.org").await;
let ipv4 = ipv4_result.as_ref();
let ipv6 = ipv6_result.as_ref();
2022-01-23 15:12:27 -06:00
println!("Found these:");
2022-01-23 15:38:16 -06:00
match ipv4 {
Ok(ip) => println!("\tIPv4: {}", ip),
Err(err) => eprintln!("\tIPv4 failed: {}", err),
}
2022-01-23 21:26:57 -06:00
match ipv6 {
2022-01-23 15:38:16 -06:00
Ok(ip) => println!("\tIPv6: {}", ip),
2022-01-23 21:26:57 -06:00
Err(err) => eprintln!("\tIPv6 failed: {}", err),
2022-01-23 15:38:16 -06:00
}
2021-12-14 21:30:36 -06:00
let client = api_client(&conf.api_key)?;
2022-01-23 15:12:27 -06:00
let mut tasks: Vec<JoinHandle<(StatusCode, String)>> = Vec::new();
println!("Attempting to update DNS entries now");
2021-12-24 19:20:31 -06:00
for entry in &conf.entry {
for entry_type in Config::types(entry) {
let fqdn = Config::fqdn(&entry, &conf);
let url = gandi_api_url(fqdn, entry.name.as_str(), entry_type);
2022-01-23 15:12:27 -06:00
let ip = match entry_type {
2022-01-23 21:26:57 -06:00
"A" => {
ipv4.die_with(|error| format!("Needed IPv4 for {}: {}", fqdn, error))
},
"AAAA" => ipv6.die_with(|error| format!("Needed IPv6 for {}: {}", fqdn, error)),
bad_entry_type => die!("Unexpected type in config: {}", bad_entry_type),
2022-01-23 02:22:41 -06:00
};
2021-12-24 19:20:31 -06:00
let mut map = HashMap::new();
2022-01-23 21:29:32 -06:00
map.insert("rrset_values", vec![ip]);
2021-12-24 19:20:31 -06:00
let req = client.put(url).json(&map);
let task = tokio::task::spawn(async move {
2022-01-23 02:22:41 -06:00
match req.send().await {
Ok(response) => (
response.status(),
response
.text()
.await
.unwrap_or_else(|error| error.to_string()),
),
2022-01-23 15:12:27 -06:00
Err(error) => (StatusCode::IM_A_TEAPOT, error.to_string()),
2022-01-23 02:22:41 -06:00
}
2021-12-24 19:20:31 -06:00
});
2022-01-23 15:12:27 -06:00
tasks.push(task);
2021-12-24 19:20:31 -06:00
}
}
2022-01-23 02:22:41 -06:00
2022-01-23 15:12:27 -06:00
let results = futures::future::try_join_all(tasks).await?;
println!("Updates done for {} entries", results.len());
2021-12-24 19:20:31 -06:00
for (status, body) in results {
println!("{} - {}", status, body);
}
2021-12-12 02:49:21 -06:00
return Ok(());
2021-12-12 00:36:40 -06:00
}