Mist/Resolver.rs
1#![allow(non_camel_case_types, non_upper_case_globals)]
2//! # DNS Resolver
3//!
4//! Provides DNS resolution for the CodeEditorLand private network.
5//! Routes `*.editor.land` queries to loopback; other domains fall back to
6//! system DNS.
7
8use std::net::{IpAddr, Ipv4Addr, SocketAddr};
9
10/// Stub DNS resolver type.
11///
12/// In production this would wrap a real hickory-client resolver connected
13/// to the local DNS server.
14pub struct TokioResolver;
15
16/// Creates a `TokioResolver` stub that queries the local DNS server.
17pub fn LandResolver(_DNSPort:u16) -> TokioResolver { TokioResolver }
18
19/// Secured DNS resolver for use with `reqwest`'s DNS override.
20///
21/// Routes `*.editor.land` queries to `127.0.0.1` and lets other domains
22/// fall back to system resolution.
23pub struct LandDnsResolver;
24
25impl LandDnsResolver {
26 /// Creates a new `LandDnsResolver` connected to the given DNS port.
27 pub fn New(_Port:u16) -> Self { Self }
28
29 // Keep snake_case alias for reqwest compatibility (external crate pattern)
30 pub fn new(_Port:u16) -> Self { Self }
31}
32
33impl reqwest::dns::Resolve for LandDnsResolver {
34 fn resolve(&self, Name:reqwest::dns::Name) -> reqwest::dns::Resolving {
35 let NameString = Name.as_str().to_string();
36
37 Box::pin(async move {
38 let IsEditorLand = NameString.ends_with(".editor.land") || NameString == "editor.land";
39 if IsEditorLand {
40 let Addresses = vec![SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0)];
41 Ok(Box::new(Addresses.into_iter()) as Box<dyn Iterator<Item = SocketAddr> + Send>)
42 } else {
43 Ok(Box::new(std::iter::empty()) as Box<dyn Iterator<Item = SocketAddr> + Send>)
44 }
45 })
46 }
47}
48
49#[cfg(test)]
50mod tests {
51
52 use super::*;
53
54 #[test]
55 fn TestResolverCreation() { let _Resolver = LandResolver(15353); }
56
57 #[test]
58 fn TestLandDnsResolverCreation() { let _Resolver = LandDnsResolver::New(15354); }
59
60 #[test]
61 fn TestEditorLandDomainDetection() {
62 assert!("example.editor.land".ends_with(".editor.land"));
63
64 assert!(!"example.com".ends_with(".editor.land"));
65 }
66}