dnscrypt 0.1.1

A pure-Rust DNSCrypt v2 client library — sync and async support.
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
416
417
418
419
420
421
422
//! Live-network integration tests. These exercise real `DNSCrypt` resolvers
//! over the network and are not run as part of unit test coverage in
//! network-restricted environments.
#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::uninlined_format_args
)]

use dnscrypt::{
    HARDCODED_RESOLVERS, HardcodedResolver, establish_dnscrypt_session, resolve,
    resolve_domain_via_dnscrypt_session,
};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

// Define a custom, user-specified compile-time ready resolver list.
static CUSTOM_RESOLVERS: &[HardcodedResolver] = &[
    HardcodedResolver {
        ip: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(9, 9, 9, 9)), 8443),
        provider_name: "2.dnscrypt-cert.quad9.net",
        provider_pk: [
            0x67, 0xc8, 0x47, 0xb8, 0xc8, 0x75, 0x8c, 0xd1, 0x20, 0x24, 0x55, 0x43, 0xbe, 0x75,
            0x67, 0x46, 0xdf, 0x34, 0xdf, 0x1d, 0x84, 0xc0, 0x0b, 0x8c, 0x47, 0x03, 0x68, 0xdf,
            0x82, 0x1d, 0x86, 0x3e,
        ],
    },
    HardcodedResolver {
        ip: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(102, 214, 10, 82)), 8443),
        provider_name: "2.dnscrypt-cert.jb1.cipherdns.co.za",
        provider_pk: [
            0x29, 0xfd, 0x92, 0xbc, 0x01, 0xbe, 0xfb, 0xc8, 0x85, 0xc5, 0x23, 0xb0, 0x08, 0x79,
            0x2a, 0xe0, 0x8d, 0x98, 0xd4, 0x27, 0x6c, 0xc6, 0xf5, 0xa7, 0x0a, 0x0b, 0x45, 0xc0,
            0x44, 0x96, 0x69, 0xad,
        ],
    },
];

#[test]
fn test_sync_resolve_github() {
    let res = resolve(HARDCODED_RESOLVERS, "github.com");
    match res {
        Ok(ips) => {
            println!("Sync resolved github.com => {:?}", ips);
            assert!(!ips.is_empty() && (ips[0].is_ipv4() || ips[0].is_ipv6()));
        }
        Err(e) => {
            panic!("Sync resolve failed: {e:?}");
        }
    }
}

#[test]
fn test_sync_resolve_custom_resolvers() {
    let res = resolve(CUSTOM_RESOLVERS, "github.com");
    match res {
        Ok(ips) => {
            println!("Sync resolved via custom list github.com => {:?}", ips);
            assert!(!ips.is_empty() && (ips[0].is_ipv4() || ips[0].is_ipv6()));
        }
        Err(e) => {
            panic!("Sync custom resolve failed: {e:?}");
        }
    }
}

#[test]
fn test_sync_session_reuse() {
    let sessions =
        establish_dnscrypt_session(HARDCODED_RESOLVERS).expect("Failed to establish sync session");
    let session = &sessions[0];

    // Reuse the same session to resolve two different domains.
    let ips1 = resolve_domain_via_dnscrypt_session(session, "github.com")
        .expect("Failed to resolve github.com with session");
    let ips2 = resolve_domain_via_dnscrypt_session(session, "quad9.net")
        .expect("Failed to resolve quad9.net with session");

    println!(
        "Session reused: github.com => {:?}, quad9.net => {:?}",
        ips1, ips2
    );
    assert!(!ips1.is_empty() && (ips1[0].is_ipv4() || ips1[0].is_ipv6()));
    assert!(!ips2.is_empty() && (ips2[0].is_ipv4() || ips2[0].is_ipv6()));
}

#[test]
fn test_manual_udp_and_tcp_resolution() {
    let mut udp_success = false;
    let mut tcp_success = false;

    for i in 0..CUSTOM_RESOLVERS.len() {
        let Ok(sessions) = establish_dnscrypt_session(&CUSTOM_RESOLVERS[i..=i]) else {
            continue;
        };
        let session = &sessions[0];

        // Build the payload dynamically to avoid Replay Attacks which close TCP!
        let build_query = || {
            let mut txid = [0u8; 2];
            getrandom::fill(&mut txid).expect("getrandom failed");
            let client_query = dnscrypt::packet::build_a_record_query("github.com", txid);
            let padded_query = dnscrypt::packet::pad_query(&client_query, 1024);

            let mut client_nonce = [0u8; 12];
            getrandom::fill(&mut client_nonce).expect("getrandom failed");
            let mut query_nonce = [0u8; 24];
            query_nonce[..12].copy_from_slice(&client_nonce);

            let encrypted_query = dnscrypt::crypto::xchacha20_djb_poly1305_encrypt(
                &session.shared_key,
                &query_nonce,
                &padded_query,
            );

            let mut request_packet = Vec::with_capacity(8 + 32 + 12 + encrypted_query.len());
            request_packet.extend_from_slice(&session.client_magic);
            request_packet.extend_from_slice(&session.client_pk_bytes);
            request_packet.extend_from_slice(&client_nonce);
            request_packet.extend_from_slice(&encrypted_query);
            request_packet
        };

        // Send UDP explicit
        let request_packet_udp = build_query();
        if let Ok(resp_udp) =
            dnscrypt::net::send_dns_query_udp(session.resolver_ip, &request_packet_udp)
        {
            if resp_udp.len() > 32 {
                // Briefly test decrypting the UDP response to prove it's a valid DNSCrypt response
                let mut resp_nonce = [0u8; 24];
                resp_nonce.copy_from_slice(&resp_udp[8..32]);
                if let Ok(decrypted) = dnscrypt::crypto::xchacha20_djb_poly1305_decrypt(
                    &session.shared_key,
                    &resp_nonce,
                    &resp_udp[32..],
                ) {
                    if let Ok(unpadded) = dnscrypt::packet::unpad_response(&decrypted) {
                        let ips_parsed = dnscrypt::packet::parse_dns_response(&unpadded);
                        if !ips_parsed.is_empty() && ips_parsed[0].is_ipv4() {
                            udp_success = true;
                        }
                    }
                }
            }
        }

        // Send TCP explicit - using a brand new query to prevent replay drops!
        let request_packet_tcp = build_query();
        if let Ok(resp_tcp) =
            dnscrypt::net::send_dns_query_tcp(session.resolver_ip, &request_packet_tcp)
        {
            if resp_tcp.len() > 32 {
                tcp_success = true;
            }
        }

        if udp_success && tcp_success {
            break;
        }
    }

    assert!(udp_success, "UDP query failed across all custom resolvers");
    assert!(tcp_success, "TCP query failed across all custom resolvers");
}

#[test]
fn test_all_hardcoded_providers_individually() {
    let test_domains = ["github.com", "google.com", "proton.me", "mullvad.net"];

    for domain in test_domains {
        println!("Testing domain: {}", domain);
        let mut results: Vec<(String, std::net::IpAddr)> = Vec::new();

        for resolver in HARDCODED_RESOLVERS {
            println!(
                "  -> Querying provider {} at IP {}",
                resolver.provider_name, resolver.ip
            );

            // Pass a slice of exactly one resolver so we test this specific IP.
            let single_resolver_slice = std::slice::from_ref(resolver);

            let sessions =
                match dnscrypt::resolver::establish_dnscrypt_session(single_resolver_slice) {
                    Ok(s) => s,
                    Err(e) => {
                        let err_str = e.to_string();
                        if err_str.contains("os error 111")
                            || err_str.contains("timeout")
                            || err_str.contains("timed out")
                        {
                            println!(
                                "    Skipping {} ({}) due to network block/timeout: {}",
                                resolver.provider_name, resolver.ip, err_str
                            );
                            continue;
                        }
                        panic!(
                            "Failed to establish session with {} ({}): {}",
                            resolver.provider_name, resolver.ip, e
                        );
                    }
                };

            assert_eq!(sessions.len(), 1, "Should have exactly 1 session");

            let session = &sessions[0];
            let ips = match dnscrypt::resolver::resolve_domain_via_dnscrypt_session(session, domain)
            {
                Ok(ips) => ips,
                Err(e) => {
                    let err_str = e.to_string();
                    if err_str.contains("os error 111")
                        || err_str.contains("timeout")
                        || err_str.contains("timed out")
                    {
                        println!(
                            "    Skipping resolution for {} ({}) due to network block/timeout: {}",
                            resolver.provider_name, resolver.ip, err_str
                        );
                        continue;
                    }
                    println!(
                        "    WARNING: Failed to resolve {} via {} ({}): {}",
                        domain, resolver.provider_name, resolver.ip, e
                    );
                    continue;
                }
            };

            assert!(!ips.is_empty() && (ips[0].is_ipv4() || ips[0].is_ipv6()));
            println!(
                "    Successfully resolved {} to {:?} via {}",
                domain, ips, resolver.ip
            );
            results.push((
                format!("{} ({})", resolver.provider_name, resolver.ip),
                ips[0],
            ));
        }

        if results.is_empty() {
            continue;
        }

        // Voting mechanism
        let mut frequency: std::collections::HashMap<std::net::IpAddr, usize> =
            std::collections::HashMap::new();
        for (_, ip) in &results {
            *frequency.entry(*ip).or_insert(0) += 1;
        }

        let mut majority_ip = results[0].1;
        let mut max_count = 0;
        for (ip, count) in &frequency {
            if *count > max_count {
                max_count = *count;
                majority_ip = *ip;
            }
        }

        // If a provider returned a different IP than the majority, log a warning instead of panicking
        // because global CDNs and Geo-DNS naturally return different localized IPs.
        for (provider_id, ip) in &results {
            if *ip != majority_ip {
                println!(
                    "WARNING (Geo-DNS / CDN): Provider {} returned IP {} for {}. Majority IP is {}.",
                    provider_id, ip, domain, majority_ip
                );
            }
        }
    }
}

// Only compiled/run when the `max-security` feature is active (e.g. `cargo
// test --all-features`), since it exercises `resolve()`'s majority-vote
// aggregation path specifically.
#[cfg(feature = "max-security")]
#[test]
fn test_resolve_max_security_all_providers_fail_is_reported() {
    // A label over 63 bytes fails `validate_domain` before any network I/O,
    // so every established session fails the domain query deterministically
    // and `resolve()` must report the "all providers failed" error rather
    // than panicking or returning an empty success.
    let invalid_domain = format!("{}.com", "a".repeat(64));
    let result = resolve(HARDCODED_RESOLVERS, &invalid_domain);
    match result {
        Err(e) => {
            let msg = e.to_string();
            assert!(
                msg.contains("all providers failed"),
                "unexpected error message: {msg}"
            );
        }
        Ok(ips) => panic!("expected resolution to fail for an invalid domain, got {ips:?}"),
    }
}

#[cfg(feature = "tokio")]
mod async_tests {
    use super::*;
    #[cfg(feature = "reqwest")]
    use dnscrypt::DnscryptResolver;
    use dnscrypt::{
        establish_dnscrypt_session_async, resolve_async, resolve_domain_via_dnscrypt_session_async,
    };
    #[cfg(feature = "reqwest")]
    use std::sync::Arc;

    #[tokio::test]
    async fn test_async_resolve_github() {
        let res = resolve_async(HARDCODED_RESOLVERS, "github.com").await;
        match res {
            Ok(ips) => {
                println!("Async resolved github.com => {:?}", ips);
                assert!(!ips.is_empty() && (ips[0].is_ipv4() || ips[0].is_ipv6()));
            }
            Err(e) => {
                panic!("Async resolve failed: {e:?}");
            }
        }
    }

    #[tokio::test]
    async fn test_async_resolve_custom_resolvers() {
        let res = resolve_async(CUSTOM_RESOLVERS, "github.com").await;
        match res {
            Ok(ips) => {
                println!("Async resolved via custom list github.com => {:?}", ips);
                assert!(!ips.is_empty() && (ips[0].is_ipv4() || ips[0].is_ipv6()));
            }
            Err(e) => {
                panic!("Async custom resolve failed: {e:?}");
            }
        }
    }

    #[tokio::test]
    async fn test_async_session_reuse() {
        let sessions = establish_dnscrypt_session_async(HARDCODED_RESOLVERS)
            .await
            .expect("Failed to establish async session");
        let session = &sessions[0];

        let ips1 = resolve_domain_via_dnscrypt_session_async(session, "github.com")
            .await
            .expect("Failed to resolve github.com with async session");
        let ips2 = resolve_domain_via_dnscrypt_session_async(session, "quad9.net")
            .await
            .expect("Failed to resolve quad9.net with async session");

        println!(
            "Async session reused: github.com => {:?}, quad9.net => {:?}",
            ips1, ips2
        );
        assert!(!ips1.is_empty() && (ips1[0].is_ipv4() || ips1[0].is_ipv6()));
        assert!(!ips2.is_empty() && (ips2[0].is_ipv4() || ips2[0].is_ipv6()));
    }

    #[cfg(feature = "max-security")]
    #[tokio::test]
    async fn test_resolve_async_max_security_all_providers_fail_is_reported() {
        // Async counterpart of test_resolve_max_security_all_providers_fail_is_reported.
        let invalid_domain = format!("{}.com", "a".repeat(64));
        let result = resolve_async(HARDCODED_RESOLVERS, &invalid_domain).await;
        match result {
            Err(e) => {
                let msg = e.to_string();
                assert!(
                    msg.contains("all providers failed"),
                    "unexpected error message: {msg}"
                );
            }
            Ok(ips) => panic!("expected resolution to fail for an invalid domain, got {ips:?}"),
        }
    }

    #[cfg(feature = "reqwest")]
    #[tokio::test]
    async fn test_reqwest_resolver_integration() {
        let resolver = Arc::new(DnscryptResolver::new());
        let client = reqwest::Client::builder()
            .dns_resolver(resolver)
            .build()
            .expect("Failed to build reqwest client");

        let resp = client.get("http://www.github.com").send().await;
        match resp {
            Ok(r) => {
                let status = r.status();
                println!("Reqwest request succeeded with status: {status}");
                assert!(status.is_success() || status.is_redirection());
            }
            Err(e) => {
                println!("Reqwest request failed (expected if network outbound is limited): {e:?}");
            }
        }
    }

    #[cfg(feature = "reqwest")]
    #[tokio::test]
    async fn test_reqwest_resolver_custom_resolvers_integration() {
        let resolver = Arc::new(DnscryptResolver::new_with_resolvers(CUSTOM_RESOLVERS));
        let client = reqwest::Client::builder()
            .dns_resolver(resolver)
            .build()
            .expect("Failed to build reqwest client");

        let resp = client.get("http://www.github.com").send().await;
        match resp {
            Ok(r) => {
                let status = r.status();
                println!("Reqwest request with custom resolvers succeeded with status: {status}");
                assert!(status.is_success() || status.is_redirection());
            }
            Err(e) => {
                println!("Reqwest request with custom resolvers failed: {e:?}");
            }
        }
    }
}