mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2025-04-02 10:35:39 +02:00
114 lines
2.7 KiB
Rust
114 lines
2.7 KiB
Rust
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)]
|
|
#[serde(default)]
|
|
pub struct DnsConfig {
|
|
pub allow_forward_lookup: bool,
|
|
pub allow_reverse_lookup: bool,
|
|
pub hidden_suffixes: Vec<String>,
|
|
pub search: Vec<String>,
|
|
pub resolver: HashMap<String,DnsResolverConfig>,
|
|
|
|
pub enable_system_resolver: bool,
|
|
pub system_resolver_name: String,
|
|
pub system_resolver_weight: i32,
|
|
pub system_resolver_id: String,
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize, Clone)]
|
|
#[serde(rename_all="lowercase")]
|
|
pub enum DnsProtocol {
|
|
Udp,
|
|
Tcp,
|
|
Tls,
|
|
Https,
|
|
Quic,
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize, Clone)]
|
|
pub struct DnsResolverConfig {
|
|
pub display_name: String,
|
|
#[serde(default)]
|
|
pub info_url: Option<String>,
|
|
#[serde(default)]
|
|
pub aliases: Vec<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>,
|
|
#[serde(skip_serializing)] //Don't leak our bind address to the outside
|
|
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(),
|
|
resolver: Default::default(),
|
|
search: Vec::new(),
|
|
|
|
enable_system_resolver: true,
|
|
system_resolver_name: "System".to_string(),
|
|
system_resolver_weight: 1000,
|
|
system_resolver_id: "system".to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
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,
|
|
additional_search: &Vec<String>,
|
|
) -> 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,
|
|
});
|
|
}
|
|
// Not configuring domain search here because searching
|
|
// on the resolver level is a bad idea unless we are
|
|
// taling about the system resolver which we
|
|
// can't tell what to do (which is good!)
|
|
return resolver;
|
|
}
|
|
}
|
|
|