Skip to main content

mist/
Resolver.rs

1#![allow(non_snake_case, 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		Box::pin(async move {
37			let IsEditorLand = NameString.ends_with(".editor.land") || NameString == "editor.land";
38			if IsEditorLand {
39				let Addresses = vec![SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0)];
40				Ok(Box::new(Addresses.into_iter()) as Box<dyn Iterator<Item = SocketAddr> + Send>)
41			} else {
42				Ok(Box::new(std::iter::empty()) as Box<dyn Iterator<Item = SocketAddr> + Send>)
43			}
44		})
45	}
46}
47
48#[cfg(test)]
49mod tests {
50	use super::*;
51
52	#[test]
53	fn TestResolverCreation() { let _Resolver = LandResolver(15353); }
54
55	#[test]
56	fn TestLandDnsResolverCreation() { let _Resolver = LandDnsResolver::New(15354); }
57
58	#[test]
59	fn TestEditorLandDomainDetection() {
60		assert!("example.editor.land".ends_with(".editor.land"));
61		assert!(!"example.com".ends_with(".editor.land"));
62	}
63}