Skip to content

Commit

Permalink
chore: update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
single9 committed Jul 11, 2024
1 parent 6d6f716 commit 67cba3a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/configs_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, RoutingCountryConfig>,
/// A list of domains and their respective geo routing
pub domain: Vec<DomainConfig>,
}

#[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<String>,
}

/// A struct to hold the subnets for a country
#[derive(Default, Debug, Clone, Deserialize)]
pub struct RoutingCountryConfig {
/// A list of subnets
pub subnets: Vec<String>,
}

#[derive(Clone)]
pub struct ConfigParser<T: for<'a> Deserialize<'a>> {
/// The parsed config
config: T,
}

impl<C: for<'a> Deserialize<'a>> ConfigParser<C> {
/// Parse the contents
pub fn parse(contents: String) -> C {
toml::from_str(&contents).unwrap()
}
}

impl ConfigParser<Config> {
/// Create a new ConfigParser with the contents of a file
pub fn new_with_path<T: ToString>(path: T) -> ConfigParser<Config> {
let contents =
fs::read_to_string(&path.to_string()).expect("Should have been able to read the file");
Expand All @@ -41,6 +51,7 @@ impl ConfigParser<Config> {
}
}

/// Get the parsed config
pub fn config(&self) -> &Config {
&self.config
}
Expand Down
12 changes: 12 additions & 0 deletions src/dns_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -33,10 +40,12 @@ impl DnsServerAddr {

#[derive(Clone)]
pub struct DnsClient {
/// The DNS client
client: Arc<AsyncClient>,
}

impl DnsClient {
/// Create a new DNS client
pub async fn new(resolver: DnsServerAddr) -> Self {
let addr = resolver.addr();
let stream = UdpClientStream::<UdpSocket>::new(addr);
Expand All @@ -50,6 +59,7 @@ impl DnsClient {
}
}

/// Resolve a domain with a subnet
pub async fn resolve_with_subnet(
&self,
domain: &str,
Expand Down Expand Up @@ -90,6 +100,7 @@ impl DnsClient {
}
}

/// A DNS resolver
#[derive(Clone)]
pub enum DnsResolver {
Google,
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions src/ip_geo_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<String>,
}

Expand Down

0 comments on commit 67cba3a

Please sign in to comment.