Use a single threaded setup & allow configurable TTL (#21)

* use a single threaded executor

* add configurable TTL support
This commit is contained in:
Kaan Barmore-Genç 2022-06-09 18:29:02 -07:00 committed by GitHub
parent e4938a3f95
commit 661dfc55fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 10 deletions

View File

@ -9,6 +9,12 @@ fqdn = "example.com"
# otherwise do things that will cause you to be charged money. # otherwise do things that will cause you to be charged money.
api_key = "xxxxxxxxxxxxxxxxxxxxxxxx" api_key = "xxxxxxxxxxxxxxxxxxxxxxxx"
# The Time To Live value to be used by entries. This can be an integer between
# 300 and 2592000. It is 300 by default. This is roughly how quickly DNS changes
# will propagate when updated, you should keep this the minimum so changes to
# your IP address propagate quickly.
ttl = 300
# For every domain or subdomain you want to update, create an entry below. # For every domain or subdomain you want to update, create an entry below.
[[entry]] [[entry]]
@ -24,3 +30,5 @@ types = ["A", "AAAA"]
# Updates A for some.example.net # Updates A for some.example.net
name = "some" name = "some"
fqdn = "example.net" # Overrides top level setting fqdn = "example.net" # Overrides top level setting
# Individual entries can override the global TTL
ttl = 600

View File

@ -10,28 +10,37 @@ pub struct Entry {
pub name: String, pub name: String,
types: Option<Vec<String>>, types: Option<Vec<String>>,
fqdn: Option<String>, fqdn: Option<String>,
ttl: Option<u32>,
} }
fn default_ttl() -> u32 { return 300; }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct Config { pub struct Config {
fqdn: String, fqdn: String,
pub api_key: String, pub api_key: String,
pub entry: Vec<Entry>, pub entry: Vec<Entry>,
#[serde(default = "default_ttl")]
pub ttl: u32,
} }
const DEFAULT_TYPES: &'static [&'static str] = &["A"]; const DEFAULT_TYPES: &'static [&'static str] = &["A"];
impl Config { impl Config {
pub fn fqdn<'c>(entry: &'c Entry, config: &'c Config) -> &'c str { pub fn fqdn<'c>(entry: &'c Entry, config: &'c Config) -> &'c str {
return entry.fqdn.as_ref().unwrap_or(&config.fqdn).as_str(); entry.fqdn.as_ref().unwrap_or(&config.fqdn).as_str()
}
pub fn ttl(entry: &Entry, config: &Config) -> u32 {
entry.ttl.unwrap_or(config.ttl)
} }
pub fn types<'e>(entry: &'e Entry) -> Vec<&'e str> { pub fn types<'e>(entry: &'e Entry) -> Vec<&'e str> {
return entry entry
.types .types
.as_ref() .as_ref()
.and_then(|ts| Some(ts.iter().map(|t| t.as_str()).collect())) .and_then(|ts| Some(ts.iter().map(|t| t.as_str()).collect()))
.unwrap_or_else(|| DEFAULT_TYPES.to_vec()); .unwrap_or_else(|| DEFAULT_TYPES.to_vec())
} }
} }

View File

@ -3,7 +3,8 @@ use anyhow;
use clap::Parser; use clap::Parser;
use futures; use futures;
use reqwest::{header, Client, ClientBuilder, StatusCode}; use reqwest::{header, Client, ClientBuilder, StatusCode};
use std::{collections::HashMap, num::NonZeroU32, sync::Arc, time::Duration}; use serde::Serialize;
use std::{num::NonZeroU32, sync::Arc, time::Duration};
use tokio::{self, task::JoinHandle}; use tokio::{self, task::JoinHandle};
mod config; mod config;
mod opts; mod opts;
@ -43,7 +44,13 @@ async fn get_ip(api_url: &str) -> anyhow::Result<String> {
Ok(text) Ok(text)
} }
#[tokio::main] #[derive(Serialize)]
pub struct APIPayload {
pub rrset_values: Vec<String>,
pub rrset_ttl: u32,
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
let opts = opts::Opts::parse(); let opts = opts::Opts::parse();
let conf = config::load_config(&opts) let conf = config::load_config(&opts)
@ -83,11 +90,13 @@ async fn main() -> anyhow::Result<()> {
"AAAA" => ipv6.die_with(|error| format!("Needed IPv6 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), bad_entry_type => die!("Unexpected type in config: {}", bad_entry_type),
}; };
let mut map = HashMap::new(); let payload = APIPayload {
map.insert("rrset_values", vec![ip]); rrset_values: vec![ip.to_string()],
let req = client.put(url).json(&map); rrset_ttl: Config::ttl(&entry, &conf),
};
let req = client.put(url).json(&payload);
let task_governor = governor.clone(); let task_governor = governor.clone();
let task = tokio::task::spawn(async move { let task = tokio::task::spawn_local(async move {
task_governor.until_ready_with_jitter(retry_jitter).await; task_governor.until_ready_with_jitter(retry_jitter).await;
println!("Updating {}", &fqdn); println!("Updating {}", &fqdn);
match req.send().await { match req.send().await {

View File

@ -1,4 +1,4 @@
use clap::{Parser, ArgEnum}; use clap::Parser;
/// A tool to automatically update DNS entries on Gandi, using it as a dynamic DNS system. /// A tool to automatically update DNS entries on Gandi, using it as a dynamic DNS system.
#[derive(Parser, Debug)] #[derive(Parser, Debug)]