rrdnsd/
api_clients.rs

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// RRDNSD - DNS API clients
// Copyright 2024 Federico Ceratto <federico@debian.org>
// Released under AGPLv3

use log::{debug, info};
use reqwest::Client as Reqw;
use std::net::IpAddr;

pub struct Dynu {
    token: String,
    client: Reqw,
}

use serde::{Deserialize, Serialize};

// // Dynu // //

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct DynuRecord {
    id: u32,
    domain_id: u32,
    //domain_name: String,
    //node_name: String,
    //record_type: String,
    ipv4_address: IpAddr,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct DynuRecordsResp {
    //status_code: u32,
    dns_records: Vec<DynuRecord>,
}

#[derive(Deserialize, Debug)]
struct DynuGetRoot {
    //statusCode: u32,
    id: u32,
    //hostname: String,
    //domain_name: String,
    //node: String,
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
struct DynuAddPayload {
    group: String,
    ipv4_address: IpAddr,
    node_name: String,
    record_type: String,
    state: bool,
    ttl: u32,
}

fn extract_record_name(fqdn: &str, zone: &str) -> String {
    fqdn[..(fqdn.len() - zone.len() - 1)].to_owned()
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_extract_record_name() {
        assert_eq!(extract_record_name("a.b.c.d", "b.c.d"), "a");
        assert_eq!(extract_record_name("aaa.b.c.d", "b.c.d"), "aaa");
    }
}

impl Dynu {
    pub fn new(token: String) -> Self {
        let client = Reqw::builder()
            .user_agent("rrdnsd")
            .pool_max_idle_per_host(0)
            .build()
            .unwrap();
        Dynu { token, client }
    }

    pub async fn add_record(&self, fqdn: &str, zone: &str, ipaddr: IpAddr) {
        debug!("[dynu] Fetching domain ID for {fqdn}");
        let domain_id = {
            let url = format!("https://api.dynu.com/v2/dns/getroot/{fqdn}");
            let res = self
                .client
                .get(url)
                .header("API-Key", self.token.clone())
                .send()
                .await
                .unwrap();
            let r = res.json::<DynuGetRoot>().await.unwrap();
            r.id
        };
        debug!("[dynu] Received domain ID {domain_id}");

        let url = format!("https://api.dynu.com/v2/dns/{domain_id}/record");
        let payload = DynuAddPayload {
            group: String::new(),
            ipv4_address: ipaddr,
            node_name: extract_record_name(fqdn, zone),
            record_type: "A".to_owned(),
            state: true,
            ttl: 30,
        };
        debug!("[dynu] Adding record domain ID {domain_id}");
        let res = self
            .client
            .post(url)
            .header("API-Key", self.token.clone())
            .json(&payload)
            .send()
            .await
            .unwrap();
        let qq = res.text().await.unwrap();
        let v: serde_json::Value = serde_json::from_str(&qq).unwrap();
        info!("{:?}", v);
    }

    pub async fn delete_record(&self, fqdn: &str, _zone: &str, ipaddr: IpAddr) {
        let existing_record: DynuRecord = 'found: {
            let url = format!("https://api.dynu.com/v2/dns/record/{fqdn}?recordType=A");
            let res = self
                .client
                .get(url)
                .header("API-Key", self.token.clone())
                .send()
                .await
                .unwrap();
            let j = res.text().await.unwrap();
            let r: DynuRecordsResp = serde_json::from_str(&j).unwrap();
            for rdata in r.dns_records {
                if rdata.ipv4_address == ipaddr {
                    break 'found rdata;
                }
            }
            info!("not found");
            return;
        };
        debug!("Found record {:?}", existing_record);
        let url = format!(
            "https://api.dynu.com/v2/dns/{}/record/{}",
            existing_record.domain_id, existing_record.id
        );
        let res = self
            .client
            .delete(url.clone())
            .header("API-Key", self.token.clone())
            .header("accept", "application/json")
            .header("Content-Type", "application/json")
            .send()
            .await
            .unwrap();
        // FIXME unwrap: retry?
        let qq = res.text().await.unwrap();
        let v: serde_json::Value = serde_json::from_str(&qq).unwrap();
        info!("{:?}", v);
    }
}