-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathdns01_client.rs
More file actions
83 lines (73 loc) · 2.46 KB
/
Copy pathdns01_client.rs
File metadata and controls
83 lines (73 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// SPDX-FileCopyrightText: © 2024 Phala Network <dstack@phala.network>
//
// SPDX-License-Identifier: Apache-2.0
use anyhow::Result;
use cloudflare::CloudflareClient;
use enum_dispatch::enum_dispatch;
use serde::{Deserialize, Serialize};
use tracing::debug;
mod cloudflare;
#[derive(Debug, Deserialize, Serialize)]
/// Represents a DNS record
pub(crate) struct Record {
/// Unique identifier for the record
pub id: String,
/// The name of the DNS record (e.g., "_acme-challenge.example.com")
pub name: String,
/// The content of the DNS record (e.g., the TXT value for ACME challenges)
pub content: String,
/// The type of DNS record (e.g., "TXT" for ACME challenges)
pub r#type: String,
}
#[enum_dispatch]
pub(crate) trait Dns01Api {
/// Creates a TXT DNS record with the given domain and content.
///
/// Returns the ID of the created record.
/// The `ttl` parameter specifies the time-to-live in seconds (1 = auto, min 60 for Cloudflare).
async fn add_txt_record(&self, domain: &str, content: &str, ttl: u32) -> Result<String>;
/// Add a CAA record for the given domain.
async fn add_caa_record(
&self,
domain: &str,
flags: u8,
tag: &str,
value: &str,
) -> Result<String>;
/// Remove a DNS record.
///
/// Deletes a DNS record using its unique identifier.
async fn remove_record(&self, record_id: &str) -> Result<()>;
/// Get all records for a domain.
async fn get_records(&self, domain: &str) -> Result<Vec<Record>>;
/// Remove TXT DNS records by domain.
///
/// Deletes all TXT DNS records matching the given domain.
async fn remove_txt_records(&self, domain: &str) -> Result<()> {
for record in self.get_records(domain).await? {
if record.r#type != "TXT" {
continue;
}
debug!(domain = %domain, id = %record.id, "removing txt record");
self.remove_record(&record.id).await?;
}
Ok(())
}
}
/// A DNS-01 client.
#[derive(Debug, Serialize, Deserialize)]
#[enum_dispatch(Dns01Api)]
#[serde(rename_all = "lowercase")]
pub enum Dns01Client {
Cloudflare(CloudflareClient),
}
impl Dns01Client {
pub async fn new_cloudflare(
base_domain: String,
api_token: String,
api_url: Option<String>,
) -> Result<Self> {
let client = CloudflareClient::new(base_domain, api_token, api_url).await?;
Ok(Self::Cloudflare(client))
}
}