2021-12-24 19:20:31 -06:00
|
|
|
use crate::config::Config;
|
2022-08-22 21:20:10 -05:00
|
|
|
use crate::gandi::GandiAPI;
|
|
|
|
use crate::ip_source::{ip_source::IPSource, ipify::IPSourceIpify};
|
2022-01-23 02:22:41 -06:00
|
|
|
use anyhow;
|
2022-04-03 20:27:45 -05:00
|
|
|
use clap::Parser;
|
2022-08-23 00:17:17 -05:00
|
|
|
use config::IPSourceName;
|
2021-12-24 19:20:31 -06:00
|
|
|
use futures;
|
2022-08-23 00:17:17 -05:00
|
|
|
use ip_source::icanhazip::IPSourceIcanhazip;
|
2022-01-23 02:22:41 -06:00
|
|
|
use reqwest::{header, Client, ClientBuilder, StatusCode};
|
2022-06-09 20:29:02 -05:00
|
|
|
use serde::Serialize;
|
|
|
|
use std::{num::NonZeroU32, sync::Arc, time::Duration};
|
2022-01-23 02:22:41 -06:00
|
|
|
use tokio::{self, task::JoinHandle};
|
2021-12-12 03:32:05 -06:00
|
|
|
mod config;
|
2022-08-22 21:20:10 -05:00
|
|
|
mod gandi;
|
|
|
|
mod ip_source;
|
2021-12-14 21:30:36 -06:00
|
|
|
mod opts;
|
2022-11-13 15:16:48 -06:00
|
|
|
use die_exit_2::*;
|
2022-03-10 20:18:41 -06:00
|
|
|
use governor;
|
|
|
|
|
|
|
|
/// 30 requests per minute, see https://api.gandi.net/docs/reference/
|
|
|
|
const GANDI_RATE_LIMIT: u32 = 30;
|
|
|
|
/// If we hit the rate limit, wait up to this many seconds before next attempt
|
|
|
|
const GANDI_DELAY_JITTER: u64 = 20;
|
|
|
|
|
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-06-09 20:29:02 -05:00
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct APIPayload {
|
|
|
|
pub rrset_values: Vec<String>,
|
|
|
|
pub rrset_ttl: u32,
|
|
|
|
}
|
|
|
|
|
2022-08-23 00:17:17 -05:00
|
|
|
async fn run<IP: IPSource>(base_url: &str, conf: Config) -> anyhow::Result<()> {
|
2022-01-23 15:12:27 -06:00
|
|
|
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...");
|
2022-08-22 21:20:10 -05:00
|
|
|
let ipv4_result = IP::get_ipv4().await;
|
|
|
|
let ipv6_result = IP::get_ipv6().await;
|
2022-01-23 15:38:16 -06:00
|
|
|
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
|
|
|
}
|
2022-03-10 20:18:41 -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
|
|
|
|
2022-03-10 20:18:41 -06:00
|
|
|
let governor = Arc::new(governor::RateLimiter::direct(governor::Quota::per_minute(
|
|
|
|
NonZeroU32::new(GANDI_RATE_LIMIT).die("Governor rate is 0"),
|
|
|
|
)));
|
|
|
|
let retry_jitter =
|
|
|
|
governor::Jitter::new(Duration::ZERO, Duration::from_secs(GANDI_DELAY_JITTER));
|
|
|
|
|
2021-12-24 19:20:31 -06:00
|
|
|
for entry in &conf.entry {
|
|
|
|
for entry_type in Config::types(entry) {
|
2022-03-10 20:18:41 -06:00
|
|
|
let fqdn = Config::fqdn(&entry, &conf).to_string();
|
2022-08-22 21:20:10 -05:00
|
|
|
let url = GandiAPI {
|
|
|
|
fqdn: &fqdn,
|
|
|
|
rrset_name: &entry.name,
|
|
|
|
rrset_type: &entry_type,
|
|
|
|
base_url,
|
|
|
|
}
|
|
|
|
.url();
|
2022-01-23 15:12:27 -06:00
|
|
|
let ip = match entry_type {
|
2022-08-22 21:20:10 -05:00
|
|
|
"A" => ipv4.die_with(|error| format!("Needed IPv4 for {fqdn}: {error}")),
|
|
|
|
"AAAA" => ipv6.die_with(|error| format!("Needed IPv6 for {fqdn}: {error}")),
|
2022-01-23 21:26:57 -06:00
|
|
|
bad_entry_type => die!("Unexpected type in config: {}", bad_entry_type),
|
2022-01-23 02:22:41 -06:00
|
|
|
};
|
2022-06-09 20:29:02 -05:00
|
|
|
let payload = APIPayload {
|
|
|
|
rrset_values: vec![ip.to_string()],
|
|
|
|
rrset_ttl: Config::ttl(&entry, &conf),
|
|
|
|
};
|
|
|
|
let req = client.put(url).json(&payload);
|
2022-03-10 20:18:41 -06:00
|
|
|
let task_governor = governor.clone();
|
2022-07-20 22:18:37 -05:00
|
|
|
let entry_type = entry_type.to_string();
|
2022-06-09 21:11:42 -05:00
|
|
|
let task = tokio::task::spawn(async move {
|
2022-03-10 20:18:41 -06:00
|
|
|
task_governor.until_ready_with_jitter(retry_jitter).await;
|
2022-07-20 22:18:37 -05:00
|
|
|
println!("Updating {} record for {}", entry_type, &fqdn);
|
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
|
|
|
|
2022-08-22 21:20:10 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
|
|
|
let opts = opts::Opts::parse();
|
2022-08-23 00:17:17 -05:00
|
|
|
let conf = config::load_config(&opts)
|
|
|
|
.die_with(|error| format!("Failed to read config file: {}", error));
|
|
|
|
|
|
|
|
match conf.ip_source {
|
|
|
|
IPSourceName::Ipify => run::<IPSourceIpify>("https://api.gandi.net", conf).await,
|
|
|
|
IPSourceName::Icanhazip => run::<IPSourceIcanhazip>("https://api.gandi.net", conf).await,
|
|
|
|
}
|
2022-08-22 21:20:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::env::temp_dir;
|
|
|
|
|
2022-08-23 00:17:17 -05:00
|
|
|
use crate::{config, ip_source::ip_source::IPSource, opts::Opts, run};
|
2022-08-22 21:20:10 -05:00
|
|
|
use async_trait::async_trait;
|
|
|
|
use httpmock::MockServer;
|
|
|
|
use tokio::fs;
|
|
|
|
|
|
|
|
struct IPSourceMock {}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl IPSource for IPSourceMock {
|
|
|
|
async fn get_ipv4() -> anyhow::Result<String> {
|
|
|
|
Ok("192.168.0.0".to_string())
|
|
|
|
}
|
|
|
|
async fn get_ipv6() -> anyhow::Result<String> {
|
|
|
|
Ok("fe80:0000:0208:74ff:feda:625c".to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn create_repo_success_test() {
|
|
|
|
let mut temp = temp_dir().join("gandi-live-dns-test");
|
|
|
|
fs::create_dir_all(&temp)
|
|
|
|
.await
|
|
|
|
.expect("Failed to create test dir");
|
|
|
|
temp.push("test.toml");
|
|
|
|
fs::write(
|
|
|
|
&temp,
|
|
|
|
"fqdn = \"example.com\"\napi_key = \"xxx\"\nttl = 300\n[[entry]]\nname =\"@\"\n",
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.expect("Failed to write test config file");
|
|
|
|
let fqdn = "example.com";
|
|
|
|
let rname = "@";
|
|
|
|
let rtype = "A";
|
|
|
|
let server = MockServer::start();
|
|
|
|
let mock = server.mock(|when, then| {
|
|
|
|
when.method("PUT")
|
|
|
|
.path(format!(
|
|
|
|
"/v5/livedns/domains/{fqdn}/records/{rname}/{rtype}"
|
|
|
|
))
|
|
|
|
.body_contains("192.168.0.0");
|
|
|
|
then.status(200);
|
|
|
|
});
|
|
|
|
|
2022-08-23 00:17:17 -05:00
|
|
|
let opts = Opts {
|
|
|
|
config: Some(temp.to_string_lossy().to_string()),
|
2022-11-13 14:36:21 -06:00
|
|
|
..Opts::default()
|
2022-08-23 00:17:17 -05:00
|
|
|
};
|
|
|
|
let conf = config::load_config(&opts).expect("Failed to load config");
|
|
|
|
run::<IPSourceMock>(server.base_url().as_str(), conf)
|
|
|
|
.await
|
|
|
|
.expect("Failed when running the update");
|
2022-08-22 21:20:10 -05:00
|
|
|
|
|
|
|
// Assert
|
|
|
|
mock.assert();
|
|
|
|
}
|
2021-12-12 00:36:40 -06:00
|
|
|
}
|