From 67cba3a57f1ba0344085b3e546ef8cff93522900 Mon Sep 17 00:00:00 2001 From: Duye Chen Date: Thu, 11 Jul 2024 17:55:24 +0800 Subject: [PATCH] chore: update comments --- src/configs_parser.rs | 11 +++++++++++ src/dns_client.rs | 12 ++++++++++++ src/ip_geo_checker.rs | 9 +++++++++ 3 files changed, 32 insertions(+) diff --git a/src/configs_parser.rs b/src/configs_parser.rs index 40ed701..6d1834b 100644 --- a/src/configs_parser.rs +++ b/src/configs_parser.rs @@ -3,35 +3,45 @@ use serde::Deserialize; use std::{collections::HashMap, fs}; +/// A struct to hold the parsed config #[derive(Default, Debug, Clone, Deserialize)] pub struct Config { + /// A map of country codes to their respective subnets pub test_subnets: HashMap, + /// A list of domains and their respective geo routing pub domain: Vec, } #[derive(Default, Debug, Clone, Deserialize)] pub struct DomainConfig { + /// The host of the domain pub host: String, + /// A list of country codes to route to pub geo_routing: Vec, } +/// A struct to hold the subnets for a country #[derive(Default, Debug, Clone, Deserialize)] pub struct RoutingCountryConfig { + /// A list of subnets pub subnets: Vec, } #[derive(Clone)] pub struct ConfigParser Deserialize<'a>> { + /// The parsed config config: T, } impl Deserialize<'a>> ConfigParser { + /// Parse the contents pub fn parse(contents: String) -> C { toml::from_str(&contents).unwrap() } } impl ConfigParser { + /// Create a new ConfigParser with the contents of a file pub fn new_with_path(path: T) -> ConfigParser { let contents = fs::read_to_string(&path.to_string()).expect("Should have been able to read the file"); @@ -41,6 +51,7 @@ impl ConfigParser { } } + /// Get the parsed config pub fn config(&self) -> &Config { &self.config } diff --git a/src/dns_client.rs b/src/dns_client.rs index 75ceaf5..6cad972 100644 --- a/src/dns_client.rs +++ b/src/dns_client.rs @@ -15,13 +15,20 @@ use hickory_proto::{ use hickory_resolver::Name; use tokio::net::UdpSocket; +/// The address of a DNS server +/// +/// This can be either a predefined server or a custom one pub enum DnsServerAddr { + /// Google's public DNS server Google, + /// CloudFlare's public DNS server CloudFlare, + /// Custom DNS server Custom(SocketAddr), } impl DnsServerAddr { + /// Get the address of the DNS server pub fn addr(&self) -> SocketAddr { match *self { DnsServerAddr::Google => ("8.8.8.8", 53).to_socket_addrs().unwrap().next().unwrap(), @@ -33,10 +40,12 @@ impl DnsServerAddr { #[derive(Clone)] pub struct DnsClient { + /// The DNS client client: Arc, } impl DnsClient { + /// Create a new DNS client pub async fn new(resolver: DnsServerAddr) -> Self { let addr = resolver.addr(); let stream = UdpClientStream::::new(addr); @@ -50,6 +59,7 @@ impl DnsClient { } } + /// Resolve a domain with a subnet pub async fn resolve_with_subnet( &self, domain: &str, @@ -90,6 +100,7 @@ impl DnsClient { } } +/// A DNS resolver #[derive(Clone)] pub enum DnsResolver { Google, @@ -98,6 +109,7 @@ pub enum DnsResolver { } impl DnsResolver { + /// Connect to the DNS server pub async fn connect(&self) -> DnsClient { match self { DnsResolver::Google => DnsClient::new(DnsServerAddr::Google).await, diff --git a/src/ip_geo_checker.rs b/src/ip_geo_checker.rs index a8f81ee..122da68 100644 --- a/src/ip_geo_checker.rs +++ b/src/ip_geo_checker.rs @@ -9,6 +9,7 @@ use crate::dns_client::DnsResolver; const IP_API_BATCH: &'static str = "http://ip-api.com/batch"; +/// A struct to hold the response from the ip-api.com API #[derive(Default, Debug, Clone, Deserialize)] pub struct IpApiResponse { pub query: String, @@ -25,14 +26,22 @@ pub struct IpApiResponse { pub lon: f64, } +/// A struct to hold the tested data #[derive(Debug, Clone)] pub struct IpGeoCheckerTestedData { + /// The host of the domain pub host: String, + /// The IP address pub ip: IpAddr, + /// The response from the ip-api.com API pub geoip: IpApiResponse, + /// The subnet pub subnet: String, + /// The expected country code pub expected: String, + /// The actual country code pub actual: String, + /// The error message pub error: Option, }