base for dns manager

This commit is contained in:
2025-10-10 01:04:32 +02:00
commit adab0517ac
9 changed files with 2144 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/target
config.toml

1970
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

9
Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "dns_manager"
version = "0.1.0"
edition = "2024"
[dependencies]
dns-comunications = {path = "./dns-comunications"}
dns-config = {path = "./dns-config"}
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }

7
config_template.toml Normal file
View File

@@ -0,0 +1,7 @@
[key]
name = "key name"
value = "key in base64"
[zone]
root_domain = "root.domain.example"
conection_str = "tcp://127.0.0.1:53"

View File

@@ -0,0 +1,8 @@
[package]
name = "dns-comunications"
version = "0.1.0"
edition = "2024"
[dependencies]
dns-update = "0.1"
base64 = "*"

View File

@@ -0,0 +1,73 @@
use std::net::Ipv4Addr;
use std::str::FromStr;
use dns_update::{DnsRecord, DnsRecordType, DnsUpdater, TsigAlgorithm};
use base64::engine::general_purpose::STANDARD;
use base64::Engine;
pub struct DnsManager {
pub dns_updater: DnsUpdater,
pub zone: String
}
impl DnsManager {
pub fn new(conection_str: String, key_name: String, key: String, zone: String) -> Self {
Self {
dns_updater: DnsUpdater::new_rfc2136_tsig(
conection_str,
key_name,
STANDARD.decode(key).unwrap(),
TsigAlgorithm::HmacSha256.into()
).unwrap(),
zone
}
}
pub async fn add_domain(&self, domain_name: &String, domain_ip: Ipv4Addr) {
let domain = format!("{}.{}", domain_name, self.zone);
self.dns_updater.create(
domain,
DnsRecord::A { content: domain_ip },
300,
self.zone.clone()
).await.unwrap();
}
pub async fn del_domain(&self, domain_name: &String) {
let domain = format!("{}.{}", domain_name, self.zone);
self.dns_updater.delete(domain, self.zone.clone(), DnsRecordType::A).await.unwrap();
}
}
pub async fn add_domain(key_name: &String, key: &String, domain_name: &String, domain_ip: Ipv4Addr) {
// Create a new RFC2136 client
let client = DnsUpdater::new_rfc2136_tsig(
"tcp://10.0.0.1:53",
key_name,
STANDARD.decode(key).unwrap(),
TsigAlgorithm::HmacSha256.into()
).unwrap();
// Create a new TXT record
client.create(
"test000.minecraft.rochegmr.com",
DnsRecord::A { content: Ipv4Addr::from_str("1.1.1.1").unwrap() },
300,
"minecraft.rochegmr.com",
)
.await
.unwrap();
}
pub async fn del_domain(key_name: &String, key: &String, domain_name: &String) {
// Create a new RFC2136 client
let client = DnsUpdater::new_rfc2136_tsig(
"tcp://10.0.0.1:53",
key_name,
STANDARD.decode(key).unwrap(),
TsigAlgorithm::HmacSha256.into()
).unwrap();
// Delete the record
client.delete("test000.minecraft.rochegmr.com", "minecraft.rochegmr.com", DnsRecordType::A).await.unwrap();
}

7
dns-config/Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[package]
name = "dns-config"
version = "0.1.0"
edition = "2024"
[dependencies]
toml = "0.9"

36
dns-config/src/lib.rs Normal file
View File

@@ -0,0 +1,36 @@
use std::fs::read_to_string;
use toml::Table;
pub struct KeyData {
pub key_name: String,
pub key: String,
}
pub struct ZoneData {
pub root_domain: String,
pub conection_str: String,
}
impl KeyData {
pub fn get_config(file_name: &String) -> Self {
let key_toml = read_to_string(file_name).unwrap().parse::<Table>().unwrap();
let name: String = key_toml["key"]["name"].as_str().unwrap().to_string();
let value: String = key_toml["key"]["value"].as_str().unwrap().to_string();
Self {
key_name: name,
key: value
}
}
}
impl ZoneData {
pub fn get_config(file_name: &String) -> Self {
let key_toml = read_to_string(file_name).unwrap().parse::<Table>().unwrap();
let root_domain: String = key_toml["zone"]["root_domain"].as_str().unwrap().to_string();
let conection_str: String = key_toml["zone"]["conection_str"].as_str().unwrap().to_string();
Self {
root_domain,
conection_str
}
}
}

32
src/main.rs Normal file
View File

@@ -0,0 +1,32 @@
use std::{net::Ipv4Addr, str::FromStr};
use dns_comunications;
use dns_config;
#[tokio::main]
async fn main() {
//dns_comunications::do_something();
//dns_config::do_something();
//dns_comunications::add_domain().await;
let key = dns_config::KeyData::get_config(&"config.toml".to_string());
let zone = dns_config::ZoneData::get_config(&"config.toml".to_string());
let dns_manager = dns_comunications::DnsManager::new(
zone.conection_str,
key.key_name,
key.key,
zone.root_domain);
dns_manager.add_domain(&"test001".to_string(), Ipv4Addr::from_str("1.1.1.1").unwrap()).await;
dns_manager.del_domain(&"test000".to_string()).await;
/*dns_comunications::add_domain(
&key.key_name,
&key.key,
&"test000".to_string(),
Ipv4Addr::from_str("1.1.1.1").unwrap()
).await;
dns_comunications::del_domain(
&key.key_name,
&key.key,
&"test000".to_string(),
).await;*/
}