Init
This commit is contained in:
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/target
|
||||
|
||||
.vscode
|
||||
|
||||
.env
|
||||
1085
Cargo.lock
generated
Normal file
1085
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
Cargo.toml
Normal file
12
Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "ionos-ddns"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
once_cell = "1.18.0"
|
||||
tokio = { version = "1.33.0", features = ["rt-multi-thread", "macros", "time"] }
|
||||
reqwest = { version = "0.11.22", features = ["json"] }
|
||||
serde = { version = "1.0.188", features = ["derive"] }
|
||||
129
src/api.rs
Normal file
129
src/api.rs
Normal file
@@ -0,0 +1,129 @@
|
||||
use crate::config::CONFIG;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
||||
pub fn get_api_key() -> String {
|
||||
format!("{}.{}", CONFIG.ionos_public_prefix, CONFIG.ionos_secret)
|
||||
}
|
||||
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Zone {
|
||||
pub name: String,
|
||||
pub id: String
|
||||
}
|
||||
|
||||
|
||||
pub async fn get_zones() -> Result<Vec<Zone>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let response = reqwest::Client::new()
|
||||
.get("https://api.hosting.ionos.com/dns/v1/zones")
|
||||
.header("User-Agent", "Reqwest")
|
||||
.header("X-API-KEY", get_api_key())
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
match response.json::<Vec<Zone>>().await {
|
||||
Ok(v) => Ok(v),
|
||||
Err(err) => Err(Box::new(err))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Record {
|
||||
pub name: String,
|
||||
pub content: String,
|
||||
pub id: String
|
||||
}
|
||||
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ZoneDetail {
|
||||
pub name: String,
|
||||
pub id: String,
|
||||
pub records: Vec<Record>
|
||||
}
|
||||
|
||||
|
||||
pub async fn get_zone_info(
|
||||
zone_id: String,
|
||||
record_name: String
|
||||
) -> Result<ZoneDetail, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let response = reqwest::Client::new()
|
||||
.get(format!("https://api.hosting.ionos.com/dns/v1/zones/{}", zone_id))
|
||||
.query(&[("recordName", record_name)])
|
||||
.header("User-Agent", "Reqwest")
|
||||
.header("X-API-KEY", get_api_key())
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
match response.json::<ZoneDetail>().await {
|
||||
Ok(v) => Ok(v),
|
||||
Err(err) => Err(Box::new(err))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct CreateRecord {
|
||||
pub name: String,
|
||||
#[serde(rename = "type")]
|
||||
pub type_: String,
|
||||
pub content: String,
|
||||
pub ttl: u32,
|
||||
pub prio: u32,
|
||||
pub disabled: bool
|
||||
}
|
||||
|
||||
|
||||
pub async fn create_record(
|
||||
zone_id: String,
|
||||
create_data: CreateRecord
|
||||
) -> Result<Vec<Record>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let response = reqwest::Client::new()
|
||||
.post(format!("https://api.hosting.ionos.com/dns/v1/zones/{}/records", zone_id))
|
||||
.header("User-Agent", "Reqwest")
|
||||
.header("X-API-KEY", get_api_key())
|
||||
.json(&vec![create_data])
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
match response.json::<Vec<Record>>().await {
|
||||
Ok(v) => Ok(v),
|
||||
Err(err) => Err(Box::new(err))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct UpdateRecord {
|
||||
pub content: String,
|
||||
pub ttl: u32,
|
||||
pub prio: u32,
|
||||
pub disabled: bool
|
||||
}
|
||||
|
||||
|
||||
pub async fn update_record(
|
||||
zone_id: String,
|
||||
record_id: String,
|
||||
update_data: UpdateRecord
|
||||
) -> Result<Record, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let response = reqwest::Client::new()
|
||||
.put(format!("https://api.hosting.ionos.com/dns/v1/zones/{}/records/{}", zone_id, record_id))
|
||||
.header("User-Agent", "Reqwest")
|
||||
.header("X-API-KEY", get_api_key())
|
||||
.json(&update_data)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
match response.json::<Record>().await {
|
||||
Ok(v) => Ok(v),
|
||||
Err(err) => Err(Box::new(err))
|
||||
}
|
||||
}
|
||||
36
src/config.rs
Normal file
36
src/config.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
|
||||
fn get_env(env: &'static str) -> String {
|
||||
std::env::var(env).unwrap_or_else(|_| panic!("Cannot get the {} env variable", env))
|
||||
}
|
||||
|
||||
|
||||
pub struct Config {
|
||||
// https://developer.hosting.ionos.com/?source=IonosControlPanel
|
||||
pub ionos_public_prefix: String,
|
||||
pub ionos_secret: String,
|
||||
|
||||
pub zone_name: String,
|
||||
pub record_name: Vec<String>,
|
||||
|
||||
pub check_interval_seconds: u64,
|
||||
}
|
||||
|
||||
|
||||
impl Config {
|
||||
pub fn load() -> Config {
|
||||
Config {
|
||||
ionos_public_prefix: get_env("IONOS_PUBLIC_PREFIX"),
|
||||
ionos_secret: get_env("IONOS_SECRET"),
|
||||
|
||||
zone_name: get_env("ZONE_NAME"),
|
||||
record_name: get_env("RECORD_NAME").split(",").map(|x| x.to_string()).collect(),
|
||||
|
||||
check_interval_seconds: get_env("CHECK_INTERVAL_SECONDS").parse().unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub static CONFIG: Lazy<Config> = Lazy::new(Config::load);
|
||||
21
src/ip_getter.rs
Normal file
21
src/ip_getter.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct IpData {
|
||||
pub ip: String,
|
||||
}
|
||||
|
||||
|
||||
pub async fn get_ip_string() -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let response = reqwest::Client::new()
|
||||
.get("https://api.ipify.org?format=json")
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
match response.json::<IpData>().await {
|
||||
Ok(v) => Ok(v.ip),
|
||||
Err(err) => Err(Box::new(err)),
|
||||
}
|
||||
}
|
||||
91
src/main.rs
Normal file
91
src/main.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
mod config;
|
||||
mod api;
|
||||
mod ip_getter;
|
||||
|
||||
use tokio::time::{self, Duration};
|
||||
|
||||
use api::{get_zones, Zone, get_zone_info, Record, create_record, CreateRecord, update_record, UpdateRecord};
|
||||
use config::CONFIG;
|
||||
use ip_getter::get_ip_string;
|
||||
|
||||
|
||||
async fn update() {
|
||||
let current_ip = get_ip_string()
|
||||
.await
|
||||
.unwrap_or_else(|err| panic!("Can't get current ip! Err: {}", err));
|
||||
|
||||
let zones = get_zones()
|
||||
.await
|
||||
.unwrap_or_else(
|
||||
|err| panic!("Can't get zones! Err: {}", err)
|
||||
);
|
||||
|
||||
let zone_id = zones
|
||||
.iter()
|
||||
.filter(|zone| zone.name.eq(&CONFIG.zone_name))
|
||||
.collect::<Vec<&Zone>>()
|
||||
.get(0)
|
||||
.unwrap_or_else(|| panic!("Can't find zone with name `{}`", CONFIG.zone_name))
|
||||
.id
|
||||
.clone();
|
||||
|
||||
let zone_detail = get_zone_info(zone_id.clone(), CONFIG.record_name.join(","))
|
||||
.await
|
||||
.unwrap_or_else(|err| panic!("Can't get zone info! Err: {}", err));
|
||||
|
||||
for record_name in CONFIG.record_name.iter() {
|
||||
let record = zone_detail.records
|
||||
.iter()
|
||||
.filter(|record| record.name.eq(record_name))
|
||||
.collect::<Vec<&Record>>()
|
||||
.get(0)
|
||||
.cloned();
|
||||
|
||||
match record {
|
||||
Some(v) => {
|
||||
if !v.content.eq(¤t_ip.clone()) {
|
||||
update_record(
|
||||
zone_id.clone(),
|
||||
v.id.clone(),
|
||||
UpdateRecord {
|
||||
content: current_ip.clone(),
|
||||
ttl: CONFIG.check_interval_seconds.try_into().unwrap(),
|
||||
prio: 0,
|
||||
disabled: false,
|
||||
}
|
||||
)
|
||||
.await
|
||||
.unwrap_or_else(|err| panic!("Can't update record! Err: {}", err));
|
||||
}
|
||||
},
|
||||
None => {
|
||||
create_record(
|
||||
zone_id.clone(),
|
||||
CreateRecord {
|
||||
name: record_name.to_string(),
|
||||
type_: "A".to_string(),
|
||||
content: current_ip.clone(),
|
||||
ttl: CONFIG.check_interval_seconds.try_into().unwrap(),
|
||||
prio: 0,
|
||||
disabled: false,
|
||||
}
|
||||
)
|
||||
.await
|
||||
.unwrap_or_else(|err| panic!("Can't create record! Err: {}", err));
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let mut interval = time::interval(Duration::from_secs(CONFIG.check_interval_seconds));
|
||||
|
||||
loop {
|
||||
update().await;
|
||||
println!("Updated!");
|
||||
|
||||
interval.tick().await;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user