dnsync 0.2.2

DNS Sync and Control with MCP
Documentation
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use serde_json::Value;

use crate::control_plane::config::{self, DnsServerConfig, VendorKind};
use crate::core::dns::capabilities::VendorCapabilities;
use crate::core::dns::logs::{LogLine, LogsOptions, LogsRead};
use crate::core::dns::records::RecordData;
use crate::core::dns::responses::ListRecordsResponse;
use crate::core::dns::service::{
    AccessListRead, AccessListWrite, CacheRead, CacheWrite, DnsVendor, ListRecordsOptions,
    RecordWrite, SettingsRead, StatsRead, ZoneExport, ZoneImport, ZoneRead, ZoneWrite,
};
use crate::core::error::{Error, Result};

#[derive(Debug, Clone, Copy, Default)]
pub struct ClientOverrides<'a> {
    pub selected_server: Option<&'a str>,
    pub base_url: Option<&'a str>,
    pub token: Option<&'a str>,
}

#[derive(Clone, Debug)]
pub enum VendorClient {
    #[cfg(feature = "technitium")]
    Technitium(crate::vendors::technitium::client::TechnitiumClient),
    #[cfg(feature = "pangolin")]
    Pangolin(crate::vendors::pangolin::client::PangolinClient),
    #[cfg(feature = "cloudflare")]
    Cloudflare(crate::vendors::cloudflare::client::CloudflareClient),
    #[cfg(feature = "unifi")]
    Unifi(crate::vendors::unifi::client::UnifiClient),
    #[cfg(feature = "pihole")]
    Pihole(crate::vendors::pihole::client::PiholeClient),
}

impl VendorClient {
    pub fn from_cli_options(
        app_config: Option<&config::AppConfig>,
        overrides: ClientOverrides<'_>,
    ) -> Result<Self> {
        let Some(app_config) = app_config else {
            return Self::client_without_config(overrides);
        };

        let server = app_config.selected_server(overrides.selected_server)?;
        Self::from_selected_server(server, overrides)
    }

    pub fn from_server(server: &DnsServerConfig) -> Result<Self> {
        match server.vendor {
            #[cfg(feature = "technitium")]
            VendorKind::Technitium => Ok(Self::Technitium(
                crate::vendors::technitium::client_from_server(server, ClientOverrides::default())?,
            )),
            #[cfg(feature = "pangolin")]
            VendorKind::Pangolin => Ok(Self::Pangolin(
                crate::vendors::pangolin::client_from_server(server, ClientOverrides::default())?,
            )),
            #[cfg(feature = "cloudflare")]
            VendorKind::Cloudflare => Ok(Self::Cloudflare(
                crate::vendors::cloudflare::client_from_server(server, ClientOverrides::default())?,
            )),
            #[cfg(feature = "unifi")]
            VendorKind::Unifi => Ok(Self::Unifi(crate::vendors::unifi::client_from_server(
                server,
                ClientOverrides::default(),
            )?)),
            #[cfg(feature = "pihole")]
            VendorKind::Pihole => Ok(Self::Pihole(crate::vendors::pihole::client_from_server(
                server,
                ClientOverrides::default(),
            )?)),
            #[allow(unreachable_patterns)]
            _ => Err(Error::parse(format!(
                "server '{}' has unsupported vendor in this build",
                server.id
            ))),
        }
    }

    pub async fn export_zone_for_server(server: &DnsServerConfig, zone: &str) -> Result<String> {
        let _ = zone;
        // Keep unsupported vendors from resolving credentials before reporting
        // capability errors; zone transfer should fail on support, not auth.
        match server.vendor {
            #[cfg(feature = "technitium")]
            VendorKind::Technitium => {
                let client = crate::vendors::technitium::client_from_server(
                    server,
                    ClientOverrides::default(),
                )?;
                client.export_zone_file(zone).await
            }
            #[cfg(feature = "cloudflare")]
            VendorKind::Cloudflare => {
                let client = crate::vendors::cloudflare::client_from_server(
                    server,
                    ClientOverrides::default(),
                )?;
                client.export_zone_file(zone).await
            }
            #[cfg(feature = "pangolin")]
            VendorKind::Pangolin => Err(Error::unsupported("Pangolin", "zone export")),
            #[cfg(feature = "unifi")]
            VendorKind::Unifi => Err(Error::unsupported("UniFi", "zone export")),
            #[cfg(feature = "pihole")]
            VendorKind::Pihole => Err(Error::unsupported("Pi-hole", "zone export")),
            #[allow(unreachable_patterns)]
            _ => Err(Error::parse(format!(
                "server '{}' has unsupported vendor in this build",
                server.id
            ))),
        }
    }

    pub async fn import_zone_for_server(
        server: &DnsServerConfig,
        zone: &str,
        file_name: String,
        file_bytes: Vec<u8>,
        overwrite: bool,
        overwrite_zone: bool,
    ) -> Result<Value> {
        let _ = (zone, &file_name, &file_bytes, overwrite, overwrite_zone);
        // Keep unsupported vendors from resolving credentials before reporting
        // capability errors; zone transfer should fail on support, not auth.
        match server.vendor {
            #[cfg(feature = "technitium")]
            VendorKind::Technitium => {
                let client = crate::vendors::technitium::client_from_server(
                    server,
                    ClientOverrides::default(),
                )?;
                client
                    .import_zone_file(
                        zone,
                        file_name,
                        file_bytes,
                        overwrite,
                        overwrite_zone,
                        false,
                    )
                    .await
            }
            #[cfg(feature = "cloudflare")]
            VendorKind::Cloudflare => {
                let client = crate::vendors::cloudflare::client_from_server(
                    server,
                    ClientOverrides::default(),
                )?;
                client
                    .import_zone_file(
                        zone,
                        file_name,
                        file_bytes,
                        overwrite,
                        overwrite_zone,
                        false,
                    )
                    .await
            }
            #[cfg(feature = "pangolin")]
            VendorKind::Pangolin => Err(Error::unsupported("Pangolin", "zone import")),
            #[cfg(feature = "unifi")]
            VendorKind::Unifi => Err(Error::unsupported("UniFi", "zone import")),
            #[cfg(feature = "pihole")]
            VendorKind::Pihole => Err(Error::unsupported("Pi-hole", "zone import")),
            #[allow(unreachable_patterns)]
            _ => Err(Error::parse(format!(
                "server '{}' has unsupported vendor in this build",
                server.id
            ))),
        }
    }

    fn from_selected_server(
        server: &DnsServerConfig,
        overrides: ClientOverrides<'_>,
    ) -> Result<Self> {
        match server.vendor {
            #[cfg(feature = "technitium")]
            VendorKind::Technitium => Ok(Self::Technitium(
                crate::vendors::technitium::client_from_server(server, overrides)?,
            )),
            #[cfg(feature = "pangolin")]
            VendorKind::Pangolin => Ok(Self::Pangolin(
                crate::vendors::pangolin::client_from_server(server, overrides)?,
            )),
            #[cfg(feature = "cloudflare")]
            VendorKind::Cloudflare => Ok(Self::Cloudflare(
                crate::vendors::cloudflare::client_from_server(server, overrides)?,
            )),
            #[cfg(feature = "unifi")]
            VendorKind::Unifi => Ok(Self::Unifi(crate::vendors::unifi::client_from_server(
                server, overrides,
            )?)),
            #[cfg(feature = "pihole")]
            VendorKind::Pihole => Ok(Self::Pihole(crate::vendors::pihole::client_from_server(
                server, overrides,
            )?)),
            #[allow(unreachable_patterns)]
            _ => Err(Error::parse(format!(
                "server '{}' has unsupported vendor in this build",
                server.id
            ))),
        }
    }

    #[cfg(feature = "technitium")]
    fn client_without_config(overrides: ClientOverrides<'_>) -> Result<Self> {
        Ok(Self::Technitium(
            crate::vendors::technitium::client_from_cli_without_config(overrides)?,
        ))
    }

    #[cfg(not(feature = "technitium"))]
    fn client_without_config(_overrides: ClientOverrides<'_>) -> Result<Self> {
        Err(Error::parse(
            "Technitium vendor is not supported in this build",
        ))
    }
}

macro_rules! delegate_vendor {
    ($self:expr, $client:ident => $body:expr) => {
        match $self {
            #[cfg(feature = "technitium")]
            Self::Technitium($client) => $body,
            #[cfg(feature = "pangolin")]
            Self::Pangolin($client) => $body,
            #[cfg(feature = "cloudflare")]
            Self::Cloudflare($client) => $body,
            #[cfg(feature = "unifi")]
            Self::Unifi($client) => $body,
            #[cfg(feature = "pihole")]
            Self::Pihole($client) => $body,
        }
    };
}

impl DnsVendor for VendorClient {
    fn kind(&self) -> VendorKind {
        delegate_vendor!(self, client => client.kind())
    }

    fn capabilities(&self) -> VendorCapabilities {
        delegate_vendor!(self, client => client.capabilities())
    }
}

impl ZoneRead for VendorClient {
    async fn list_zones(&self, page: u32, per_page: u32) -> Result<Value> {
        delegate_vendor!(self, client => client.list_zones(page, per_page).await)
    }

    async fn list_records(
        &self,
        domain: &str,
        zone: Option<&str>,
        options: ListRecordsOptions,
    ) -> Result<ListRecordsResponse> {
        delegate_vendor!(self, client => client.list_records(domain, zone, options).await)
    }
}

impl ZoneWrite for VendorClient {
    async fn create_zone(&self, zone: &str, zone_type: &str) -> Result<Value> {
        delegate_vendor!(self, client => client.create_zone(zone, zone_type).await)
    }

    async fn delete_zone(&self, zone: &str) -> Result<Value> {
        delegate_vendor!(self, client => client.delete_zone(zone).await)
    }

    async fn enable_zone(&self, zone: &str) -> Result<Value> {
        delegate_vendor!(self, client => client.enable_zone(zone).await)
    }

    async fn disable_zone(&self, zone: &str) -> Result<Value> {
        delegate_vendor!(self, client => client.disable_zone(zone).await)
    }
}

impl RecordWrite for VendorClient {
    async fn add_record(
        &self,
        zone: &str,
        domain: &str,
        ttl: u32,
        record: &RecordData,
    ) -> Result<Value> {
        delegate_vendor!(self, client => client.add_record(zone, domain, ttl, record).await)
    }

    async fn delete_record(
        &self,
        zone: &str,
        domain: &str,
        type_params: &[(&str, String)],
    ) -> Result<Value> {
        delegate_vendor!(self, client => client.delete_record(zone, domain, type_params).await)
    }
}

impl CacheRead for VendorClient {
    async fn list_cache(&self, domain: &str) -> Result<Value> {
        delegate_vendor!(self, client => client.list_cache(domain).await)
    }
}

impl CacheWrite for VendorClient {
    async fn delete_cache_zone(&self, domain: &str) -> Result<Value> {
        delegate_vendor!(self, client => client.delete_cache_zone(domain).await)
    }

    async fn flush_cache(&self) -> Result<Value> {
        delegate_vendor!(self, client => client.flush_cache().await)
    }
}

impl StatsRead for VendorClient {
    async fn get_stats(&self, stats_type: &str) -> Result<Value> {
        delegate_vendor!(self, client => client.get_stats(stats_type).await)
    }
}

impl AccessListRead for VendorClient {
    async fn list_blocked(&self) -> Result<Value> {
        delegate_vendor!(self, client => client.list_blocked().await)
    }

    async fn list_allowed(&self) -> Result<Value> {
        delegate_vendor!(self, client => client.list_allowed().await)
    }
}

impl AccessListWrite for VendorClient {
    async fn add_blocked(&self, domain: &str) -> Result<Value> {
        delegate_vendor!(self, client => client.add_blocked(domain).await)
    }

    async fn delete_blocked(&self, domain: &str) -> Result<Value> {
        delegate_vendor!(self, client => client.delete_blocked(domain).await)
    }

    async fn add_allowed(&self, domain: &str) -> Result<Value> {
        delegate_vendor!(self, client => client.add_allowed(domain).await)
    }

    async fn delete_allowed(&self, domain: &str) -> Result<Value> {
        delegate_vendor!(self, client => client.delete_allowed(domain).await)
    }
}

impl ZoneImport for VendorClient {
    async fn import_zone_file(
        &self,
        zone: &str,
        file_name: String,
        file_bytes: Vec<u8>,
        overwrite: bool,
        overwrite_zone: bool,
        overwrite_soa_serial: bool,
    ) -> Result<Value> {
        delegate_vendor!(self, client => {
            client
                .import_zone_file(
                    zone,
                    file_name,
                    file_bytes,
                    overwrite,
                    overwrite_zone,
                    overwrite_soa_serial,
                )
                .await
        })
    }
}

impl ZoneExport for VendorClient {
    async fn export_zone_file(&self, zone: &str) -> Result<String> {
        delegate_vendor!(self, client => client.export_zone_file(zone).await)
    }
}

impl SettingsRead for VendorClient {
    async fn get_settings(&self) -> Result<Value> {
        delegate_vendor!(self, client => client.get_settings().await)
    }
}

impl LogsRead for VendorClient {
    async fn get_logs(&self, options: LogsOptions) -> Result<Vec<LogLine>> {
        delegate_vendor!(self, client => client.get_logs(options).await)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(feature = "technitium")]
    #[test]
    fn default_without_config_builds_technitium_client() {
        let client = VendorClient::from_cli_options(
            None,
            ClientOverrides {
                token: Some("token"),
                ..ClientOverrides::default()
            },
        )
        .unwrap();

        assert_eq!(client.kind(), VendorKind::Technitium);
    }
}