From 11c8aa00226df340c35af0226674c3bc0e4fb0f8 Mon Sep 17 00:00:00 2001 From: Timo Ley Date: Tue, 14 Sep 2021 14:27:15 +0200 Subject: [PATCH] Init --- .gitignore | 4 ++++ Cargo.toml | 15 ++++++++++++ config.toml | 4 ++++ src/config.rs | 10 ++++++++ src/main.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 config.toml create mode 100644 src/config.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f279076 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/target +.idea +Cargo.lock +*.iml diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d723bbf --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "dnsupdate" +version = "0.1.0" +authors = ["Timo Ley "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tokio = { version = "1.0", features = ["full"] } +reqwest = { version = "0.11", features = ["json"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0.51" +toml = "0.5.8" +structopt = "0.3.22" diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..f71a840 --- /dev/null +++ b/config.toml @@ -0,0 +1,4 @@ +token = "cloudflare_token" +zone = "cloudflare_zone_id" +entry = "cloudflare_entry_id" +domain = "subdomain" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..4a91645 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,10 @@ +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct Config { + pub token: String, + pub zone: String, + pub entry: String, + pub domain: String, +} + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..492b5ea --- /dev/null +++ b/src/main.rs @@ -0,0 +1,65 @@ +use std::path::PathBuf; +use structopt::StructOpt; +use serde::{Deserialize, Serialize}; +use reqwest::Result; +use crate::config::Config; + + +mod config; + +#[derive(StructOpt)] +struct Opt { + #[structopt( + short, + long, + help = "config file to use", + default_value = "./config.toml" + )] + config: PathBuf, +} + +#[tokio::main] +async fn main() -> Result<()> { + let opt = Opt::from_args(); + let config = std::fs::read(&opt.config).expect("Config file reading error"); + let config = toml::from_slice::(&config).expect("Config file parsing error"); + + let client = reqwest::ClientBuilder::new().user_agent("curl").build()?; + let res: IPInfo = client.get("https://api.myip.com/").send().await?.json().await?; + let req = DNSRequest::new(config.domain, res.ip); + let url = format!("https://api.cloudflare.com/client/v4/zones/{}/dns_records/{}", config.zone, config.entry); + client.put(url).bearer_auth(config.token).json(&req).send().await?; + + Ok(()) + +} + +#[derive(Deserialize)] +struct IPInfo { + ip: String, +} + +#[derive(Serialize)] +struct DNSRequest { + #[serde(rename = "type")] + dtype: String, + name: String, + content: String, + ttl: u32, + proxied: bool, +} + +impl DNSRequest { + + fn new(name: String, content: String) -> Self { + DNSRequest { + dtype: "A".to_string(), + name, + content, + ttl: 1, + proxied: true + } + } + +} +