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