Skip to main content

Mist/
Resolver.rs

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