mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2025-08-12 11:01:43 +02:00
Configurable multiple dns resolvers
This commit is contained in:
99
src/config/dns.rs
Normal file
99
src/config/dns.rs
Normal file
@@ -0,0 +1,99 @@
|
||||
use serde::{Deserialize,Serialize};
|
||||
use trust_dns_resolver::config::Protocol;
|
||||
use trust_dns_resolver::Name;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
#[derive(Deserialize, Clone)]
|
||||
pub struct DnsConfig {
|
||||
pub allow_forward_lookup: bool,
|
||||
pub allow_reverse_lookup: bool,
|
||||
pub hidden_suffixes: Vec<String>,
|
||||
#[serde(default="default_dns_resolver_name")]
|
||||
pub default_resolver: String,
|
||||
pub resolver: HashMap<String,DnsResolverConfig>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone)]
|
||||
pub enum DnsProtocol {
|
||||
Udp,
|
||||
Tcp,
|
||||
Tls,
|
||||
Https,
|
||||
Quic,
|
||||
}
|
||||
|
||||
pub fn default_dns_resolver_name() -> String {
|
||||
"default".to_string()
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone)]
|
||||
pub struct DnsResolverConfig {
|
||||
pub display_name: String,
|
||||
#[serde(default="zero")]
|
||||
pub weight: i32,
|
||||
pub servers: Vec<SocketAddr>,
|
||||
#[serde(default)]
|
||||
pub search: Vec<String>,
|
||||
pub protocol: DnsProtocol,
|
||||
pub tls_dns_name: Option<String>,
|
||||
pub bind_address: Option<SocketAddr>,
|
||||
#[serde(default="default_true")]
|
||||
pub trust_nx_responses: bool,
|
||||
}
|
||||
|
||||
fn zero() -> i32 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
fn default_true() -> bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
impl Default for DnsConfig {
|
||||
fn default() -> Self {
|
||||
DnsConfig {
|
||||
allow_forward_lookup: true,
|
||||
allow_reverse_lookup: false,
|
||||
hidden_suffixes: Vec::new(),
|
||||
default_resolver: "default".to_string(),
|
||||
resolver: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<Protocol> for DnsProtocol {
|
||||
fn into(self) -> Protocol {
|
||||
match self {
|
||||
Self::Udp => Protocol::Udp,
|
||||
Self::Tcp => Protocol::Tcp,
|
||||
Self::Tls => Protocol::Tls,
|
||||
Self::Https => Protocol::Https,
|
||||
Self::Quic => Protocol::Quic,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DnsResolverConfig {
|
||||
pub fn to_trust_resolver_config(&self) -> trust_dns_resolver::config::ResolverConfig {
|
||||
let mut resolver = trust_dns_resolver::config::ResolverConfig::new();
|
||||
for server in &self.servers {
|
||||
resolver.add_name_server(trust_dns_resolver::config::NameServerConfig{
|
||||
socket_addr: *server,
|
||||
protocol: self.protocol.clone().into(),
|
||||
tls_dns_name: self.tls_dns_name.clone(),
|
||||
trust_nx_responses: self.trust_nx_responses,
|
||||
tls_config: None,
|
||||
bind_addr: self.bind_address,
|
||||
});
|
||||
}
|
||||
for search in &self.search {
|
||||
if let Ok(name) = Name::from_str_relaxed(search) {
|
||||
resolver.add_search(name);
|
||||
}
|
||||
}
|
||||
return resolver;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user