This commit is contained in:
Timo Ley 2021-09-14 14:27:15 +02:00
commit 11c8aa0022
5 changed files with 98 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
/target
.idea
Cargo.lock
*.iml

15
Cargo.toml Normal file
View file

@ -0,0 +1,15 @@
[package]
name = "dnsupdate"
version = "0.1.0"
authors = ["Timo Ley <timo@leyt.xyz>"]
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"

4
config.toml Normal file
View file

@ -0,0 +1,4 @@
token = "cloudflare_token"
zone = "cloudflare_zone_id"
entry = "cloudflare_entry_id"
domain = "subdomain"

10
src/config.rs Normal file
View file

@ -0,0 +1,10 @@
use serde::Deserialize;
#[derive(Deserialize)]
pub struct Config {
pub token: String,
pub zone: String,
pub entry: String,
pub domain: String,
}

65
src/main.rs Normal file
View file

@ -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>(&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
}
}
}