mirror of
https://github.com/zhboner/realm.git
synced 2026-11-17 16:17:22 +00:00
extend dns options
This commit is contained in:
+4
-3
@@ -15,7 +15,7 @@ serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
|
||||
tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "net", "time"] }
|
||||
trust-dns-resolver = "0.20"
|
||||
trust-dns-resolver = { version = "0.20", optional = true }
|
||||
|
||||
lazy_static = "1"
|
||||
|
||||
@@ -33,7 +33,8 @@ lto = true
|
||||
opt-level = 0
|
||||
|
||||
[features]
|
||||
default = ["udp", "zero-copy"]
|
||||
default = ["udp", "zero-copy", "trust-dns"]
|
||||
udp = []
|
||||
tfo = ["tokio-tfo"]
|
||||
zero-copy = ["libc"]
|
||||
zero-copy = ["libc"]
|
||||
trust-dns = ["trust-dns-resolver"]
|
||||
+70
-9
@@ -1,18 +1,19 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
use trust_dns_resolver::config::LookupIpStrategy;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
use std::net::ToSocketAddrs;
|
||||
|
||||
use trust_dns_resolver as resolver;
|
||||
use resolver::config::{LookupIpStrategy, NameServerConfig, Protocol};
|
||||
use resolver::config::{ResolverConfig, ResolverOpts};
|
||||
|
||||
// dns mode
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum DnsMode {
|
||||
/// Only query for A (Ipv4) records
|
||||
Ipv4Only,
|
||||
/// Only query for AAAA (Ipv6) records
|
||||
Ipv6Only,
|
||||
/// Query for A and AAAA in parallel
|
||||
Ipv4AndIpv6,
|
||||
/// Query for Ipv4 if that fails, query for Ipv6 (default)
|
||||
Ipv4ThenIpv6,
|
||||
/// Query for Ipv6 if that fails, query for Ipv4
|
||||
Ipv6ThenIpv4,
|
||||
}
|
||||
|
||||
@@ -22,14 +23,74 @@ impl Default for DnsMode {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DnsMode> for LookupIpStrategy {
|
||||
impl From<DnsMode> for ResolverOpts {
|
||||
fn from(mode: DnsMode) -> Self {
|
||||
match mode {
|
||||
let ip_strategy = match mode {
|
||||
DnsMode::Ipv4Only => LookupIpStrategy::Ipv4Only,
|
||||
DnsMode::Ipv6Only => LookupIpStrategy::Ipv6Only,
|
||||
DnsMode::Ipv4AndIpv6 => LookupIpStrategy::Ipv4AndIpv6,
|
||||
DnsMode::Ipv4ThenIpv6 => LookupIpStrategy::Ipv4thenIpv6,
|
||||
DnsMode::Ipv6ThenIpv4 => LookupIpStrategy::Ipv6thenIpv4,
|
||||
};
|
||||
ResolverOpts {
|
||||
ip_strategy,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// dns config
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct DnsConf {
|
||||
#[serde(default)]
|
||||
pub mode: DnsMode,
|
||||
#[serde(default)]
|
||||
pub protocol: String,
|
||||
pub nameservers: Vec<String>,
|
||||
}
|
||||
|
||||
fn read_protocol(net: &str) -> Vec<Protocol> {
|
||||
match net.to_ascii_lowercase().as_str() {
|
||||
"tcp" => vec![Protocol::Tcp],
|
||||
"udp" => vec![Protocol::Udp],
|
||||
_ => vec![Protocol::Tcp, Protocol::Udp],
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DnsConf> for (ResolverConfig, ResolverOpts) {
|
||||
fn from(config: DnsConf) -> Self {
|
||||
if config.nameservers.is_empty() {
|
||||
panic!("no nameserver provided");
|
||||
}
|
||||
let opts = config.mode.into();
|
||||
let mut conf = ResolverConfig::new();
|
||||
let protocols = read_protocol(&config.protocol);
|
||||
for addr in config.nameservers {
|
||||
let socket_addr = addr.to_socket_addrs().unwrap().next().unwrap();
|
||||
for protocol in protocols.clone() {
|
||||
conf.add_name_server(NameServerConfig {
|
||||
socket_addr,
|
||||
protocol,
|
||||
tls_dns_name: None,
|
||||
trust_nx_responses: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
(conf, opts)
|
||||
}
|
||||
}
|
||||
|
||||
// compatible dns config
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(untagged, rename_all = "snake_case")]
|
||||
pub enum CompatibeDnsConf {
|
||||
Dns(DnsConf),
|
||||
DnsMode(DnsMode),
|
||||
None,
|
||||
}
|
||||
|
||||
impl Default for CompatibeDnsConf {
|
||||
fn default() -> Self {
|
||||
Self::None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use serde::{Serialize, Deserialize};
|
||||
use crate::utils::Endpoint;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct EndpointConfig {
|
||||
pub struct EndpointConf {
|
||||
#[serde(default)]
|
||||
udp: bool,
|
||||
local: String,
|
||||
@@ -11,7 +11,7 @@ pub struct EndpointConfig {
|
||||
through: String,
|
||||
}
|
||||
|
||||
impl EndpointConfig {
|
||||
impl EndpointConf {
|
||||
pub fn build(&self) -> Endpoint {
|
||||
Endpoint::new(&self.local, &self.remote, &self.through, self.udp)
|
||||
}
|
||||
|
||||
+14
-9
@@ -2,23 +2,28 @@ use std::fs;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
mod dns;
|
||||
mod endpoint;
|
||||
pub use dns::DnsMode;
|
||||
pub use endpoint::EndpointConfig;
|
||||
pub use endpoint::EndpointConf;
|
||||
|
||||
#[cfg(feature = "trust-dns")]
|
||||
mod dns;
|
||||
#[cfg(feature = "trust-dns")]
|
||||
pub use dns::CompatibeDnsConf;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct FullConfig {
|
||||
#[serde(default)]
|
||||
pub dns_mode: DnsMode,
|
||||
pub endpoints: Vec<EndpointConfig>,
|
||||
pub struct FullConf {
|
||||
#[cfg(feature = "trust-dns")]
|
||||
#[serde(default, rename = "dns_mode")]
|
||||
pub dns: CompatibeDnsConf,
|
||||
|
||||
pub endpoints: Vec<EndpointConf>,
|
||||
}
|
||||
|
||||
impl FullConfig {
|
||||
impl FullConf {
|
||||
pub fn from_config_file(file: &str) -> Self {
|
||||
let config = fs::read_to_string(file)
|
||||
.unwrap_or_else(|_| panic!("unable to open {}", file));
|
||||
serde_json::from_str(&config)
|
||||
.unwrap_or_else(|_| panic!("unable to open {}", file))
|
||||
.unwrap_or_else(|_| panic!("unable to parse {}", file))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "trust-dns")] {
|
||||
mod trust_dns;
|
||||
pub use trust_dns::*;
|
||||
} else {
|
||||
mod sys_dns;
|
||||
pub use sys_dns::*;
|
||||
pub use resolve as resolve_sync;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
use std::io::Result;
|
||||
use std::net::{SocketAddr, ToSocketAddrs};
|
||||
|
||||
pub fn resolve(addr: &str, port: u16) -> Result<SocketAddr> {
|
||||
(addr, port)
|
||||
.to_socket_addrs()
|
||||
.map(|mut x| x.next().unwrap())
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
use std::io::{Result, Error, ErrorKind};
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
use trust_dns_resolver as resolver;
|
||||
use resolver::TokioAsyncResolver;
|
||||
use resolver::config::{ResolverConfig, ResolverOpts};
|
||||
use resolver::system_conf::read_system_conf;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DnsConf {
|
||||
conf: ResolverConfig,
|
||||
opts: ResolverOpts,
|
||||
}
|
||||
|
||||
impl Default for DnsConf {
|
||||
fn default() -> Self {
|
||||
#[cfg(any(unix, windows))]
|
||||
let (conf, opts) = read_system_conf().unwrap();
|
||||
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
let (conf, opts) = Default::default();
|
||||
|
||||
Self { conf, opts }
|
||||
}
|
||||
}
|
||||
|
||||
impl DnsConf {
|
||||
pub fn set_conf(&mut self, conf: ResolverConfig) {
|
||||
self.conf = conf;
|
||||
}
|
||||
|
||||
pub fn set_opts(&mut self, opts: ResolverOpts) {
|
||||
self.opts = opts;
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref DNS_CONF: Mutex<DnsConf> = Mutex::new(DnsConf::default());
|
||||
static ref DNS: TokioAsyncResolver = {
|
||||
let DnsConf { conf, opts } = DNS_CONF.lock().unwrap().clone();
|
||||
TokioAsyncResolver::tokio(conf, opts).unwrap()
|
||||
};
|
||||
}
|
||||
|
||||
pub fn configure(conf: Option<ResolverConfig>, opts: Option<ResolverOpts>) {
|
||||
lazy_static::initialize(&DNS_CONF);
|
||||
|
||||
if let Some(conf) = conf {
|
||||
DNS_CONF.lock().unwrap().set_conf(conf);
|
||||
}
|
||||
if let Some(opts) = opts {
|
||||
DNS_CONF.lock().unwrap().set_opts(opts);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build() {
|
||||
lazy_static::initialize(&DNS);
|
||||
}
|
||||
|
||||
pub async fn resolve(addr: &str, port: u16) -> Result<SocketAddr> {
|
||||
DNS.lookup_ip(addr).await.map_or_else(
|
||||
|e| Err(Error::new(ErrorKind::Other, e)),
|
||||
|ip| Ok(SocketAddr::new(ip.into_iter().next().unwrap(), port)),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn resolve_sync(addr: &str, port: u16) -> Result<SocketAddr> {
|
||||
let rt = Runtime::new().unwrap();
|
||||
rt.block_on(resolve(addr, port))
|
||||
}
|
||||
+29
-12
@@ -1,38 +1,55 @@
|
||||
mod cmd;
|
||||
mod dns;
|
||||
mod conf;
|
||||
mod utils;
|
||||
mod relay;
|
||||
|
||||
use cmd::CmdInput;
|
||||
use conf::FullConfig;
|
||||
use conf::FullConf;
|
||||
use utils::Endpoint;
|
||||
|
||||
const VERSION: &str = "1.5.0-rc2";
|
||||
|
||||
fn main() {
|
||||
match cmd::scan() {
|
||||
CmdInput::Config(c) => start_from_config(c),
|
||||
CmdInput::Endpoint(ep) => start_from_cmd(ep),
|
||||
CmdInput::Config(conf) => start_from_conf(conf),
|
||||
CmdInput::Navigate => cmd::run_navigator(),
|
||||
CmdInput::None => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn start_from_cmd(c: Endpoint) {
|
||||
run_relay(vec![c])
|
||||
fn start_from_cmd(ep: Endpoint) {
|
||||
run_relay(vec![ep])
|
||||
}
|
||||
|
||||
fn start_from_config(c: String) {
|
||||
let config = FullConfig::from_config_file(&c);
|
||||
utils::init_resolver(config.dns_mode.into());
|
||||
let eps: Vec<Endpoint> = config
|
||||
.endpoints
|
||||
.into_iter()
|
||||
.map(|epc| epc.build())
|
||||
.collect();
|
||||
fn start_from_conf(conf: String) {
|
||||
let conf = FullConf::from_config_file(&conf);
|
||||
#[cfg(feature = "trust-dns")]
|
||||
{
|
||||
let FullConf { dns, .. } = conf;
|
||||
setup_dns(dns);
|
||||
}
|
||||
|
||||
let eps: Vec<Endpoint> =
|
||||
conf.endpoints.into_iter().map(|epc| epc.build()).collect();
|
||||
run_relay(eps);
|
||||
}
|
||||
|
||||
#[cfg(feature = "trust-dns")]
|
||||
fn setup_dns(dns: conf::CompatibeDnsConf) {
|
||||
use conf::CompatibeDnsConf::*;
|
||||
match dns {
|
||||
Dns(conf) => {
|
||||
let (conf, opts) = conf.into();
|
||||
dns::configure(Some(conf), Some(opts));
|
||||
}
|
||||
DnsMode(mode) => dns::configure(Option::None, Some(mode.into())),
|
||||
None => (),
|
||||
}
|
||||
dns::build();
|
||||
}
|
||||
|
||||
fn run_relay(eps: Vec<Endpoint>) {
|
||||
tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
use std::io::{Result, Error, ErrorKind};
|
||||
use std::net::IpAddr;
|
||||
|
||||
use tokio::runtime::Runtime;
|
||||
use trust_dns_resolver::TokioAsyncResolver;
|
||||
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts, LookupIpStrategy};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
static mut RESOLVE_STRATEGY: LookupIpStrategy = LookupIpStrategy::Ipv4thenIpv6;
|
||||
|
||||
lazy_static! {
|
||||
static ref DNS: TokioAsyncResolver = TokioAsyncResolver::tokio(
|
||||
ResolverConfig::default(),
|
||||
ResolverOpts {
|
||||
ip_strategy: unsafe { RESOLVE_STRATEGY },
|
||||
..Default::default()
|
||||
}
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn init_resolver(strategy: LookupIpStrategy) {
|
||||
unsafe { RESOLVE_STRATEGY = strategy };
|
||||
lazy_static::initialize(&DNS);
|
||||
}
|
||||
|
||||
pub fn resolve_sync(addr: &str) -> Result<IpAddr> {
|
||||
let rt = Runtime::new().unwrap();
|
||||
rt.block_on(resolve_async(addr))
|
||||
}
|
||||
|
||||
pub async fn resolve_async(addr: &str) -> Result<IpAddr> {
|
||||
let res = DNS
|
||||
.lookup_ip(addr)
|
||||
.await
|
||||
.map_err(|e| Error::new(ErrorKind::Other, e))?
|
||||
.into_iter()
|
||||
.next()
|
||||
.unwrap();
|
||||
Ok(res)
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
mod dns;
|
||||
mod types;
|
||||
|
||||
pub use dns::*;
|
||||
pub use types::*;
|
||||
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
|
||||
+21
-6
@@ -1,6 +1,7 @@
|
||||
use std::io::Result;
|
||||
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
|
||||
use super::dns;
|
||||
|
||||
use crate::dns;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum RemoteAddr {
|
||||
@@ -21,8 +22,15 @@ impl RemoteAddr {
|
||||
match self {
|
||||
Self::SocketAddr(sockaddr) => Ok(sockaddr),
|
||||
Self::DomainName(addr, port) => {
|
||||
let ip = dns::resolve_async(&addr).await?;
|
||||
Ok(SocketAddr::new(ip, port))
|
||||
#[cfg(feature = "trust-dns")]
|
||||
{
|
||||
dns::resolve(&addr, port).await
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "trust-dns"))]
|
||||
{
|
||||
dns::resolve(&addr, port)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,8 +39,15 @@ impl RemoteAddr {
|
||||
match self {
|
||||
Self::SocketAddr(sockaddr) => Ok(*sockaddr),
|
||||
Self::DomainName(addr, port) => {
|
||||
let ip = dns::resolve_async(addr).await?;
|
||||
Ok(SocketAddr::new(ip, *port))
|
||||
#[cfg(feature = "trust-dns")]
|
||||
{
|
||||
dns::resolve(addr, *port).await
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "trust-dns"))]
|
||||
{
|
||||
dns::resolve(addr, *port)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,7 +70,7 @@ impl Endpoint {
|
||||
let port = iter.next().unwrap().parse::<u16>().unwrap();
|
||||
let addr = iter.next().unwrap().to_string();
|
||||
// test addr
|
||||
let _ = dns::resolve_sync(&addr).unwrap();
|
||||
let _ = dns::resolve_sync(&addr, 0).unwrap();
|
||||
RemoteAddr::DomainName(addr, port)
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user