add kaminari entry point from cmd/env/file

This commit is contained in:
zephyr
2022-04-13 17:25:13 +09:00
parent 7b7529a5e7
commit effcdc423d
6 changed files with 107 additions and 6 deletions
+14
View File
@@ -94,6 +94,20 @@ pub fn add_options(app: Command) -> Command {
.value_name("device")
.takes_value(true)
.display_order(6),
Arg::new("listen_transport")
.short('a')
.long("listen-transport")
.help("listen transport")
.value_name("options")
.takes_value(true)
.display_order(7),
Arg::new("remote_transport")
.short('b')
.long("remote-transport")
.help("remote transport")
.value_name("options")
.takes_value(true)
.display_order(8),
])
}
+70 -4
View File
@@ -3,6 +3,9 @@ use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
use super::{NetConf, Config};
use crate::utils::{Endpoint, RemoteAddr};
#[cfg(feature = "transport")]
use kaminari::mix::{MixAccept, MixConnect};
#[derive(Debug, Serialize, Deserialize)]
pub struct EndpointConf {
pub listen: String,
@@ -17,6 +20,14 @@ pub struct EndpointConf {
#[serde(skip_serializing_if = "Option::is_none")]
pub interface: Option<String>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub listen_transport: Option<String>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub remote_transport: Option<String>,
#[serde(default)]
#[serde(skip_serializing_if = "Config::is_empty")]
pub network: NetConf,
@@ -62,6 +73,47 @@ impl EndpointConf {
}
}
}
#[cfg(feature = "transport")]
fn build_transport(&self) -> Option<(MixAccept, MixConnect)> {
use kaminari::mix::{MixClientConf, MixServerConf};
use kaminari::opt::get_ws_conf;
use kaminari::opt::get_tls_client_conf;
use kaminari::opt::get_tls_server_conf;
let Self {
listen_transport,
remote_transport,
..
} = self;
let listen_ws = listen_transport.as_ref().and_then(|s| get_ws_conf(s));
let listen_tls = listen_transport
.as_ref()
.and_then(|s| get_tls_server_conf(s));
let remote_ws = remote_transport.as_ref().and_then(|s| get_ws_conf(s));
let remote_tls = remote_transport
.as_ref()
.and_then(|s| get_tls_client_conf(s));
if matches!(
(&listen_ws, &listen_tls, &remote_ws, &remote_tls),
(None, None, None, None)
) {
None
} else {
let ac = MixAccept::new(MixServerConf {
ws: listen_ws,
tls: listen_tls,
});
let cc = MixConnect::new(MixClientConf {
ws: remote_ws,
tls: remote_tls,
});
Some((ac, cc))
}
}
}
impl Config for EndpointConf {
@@ -75,12 +127,20 @@ impl Config for EndpointConf {
let local = self.build_local();
let remote = self.build_remote();
let through = self.build_send_through();
// iface is untested
// build partial conn_opts from netconf
let mut conn_opts = self.network.build();
conn_opts.send_through = through;
// build left fields of conn_opts
conn_opts.send_through = self.build_send_through();
#[cfg(feature = "transport")]
{
conn_opts.transport = self.build_transport();
}
conn_opts.bind_interface = self.interface;
Endpoint::new(local, remote, conn_opts)
}
@@ -97,12 +157,18 @@ impl Config for EndpointConf {
let remote = matches.value_of("remote").unwrap().to_string();
let through = matches.value_of("through").map(String::from);
let interface = matches.value_of("interface").map(String::from);
let listen_transport =
matches.value_of("listen_transport").map(String::from);
let remote_transport =
matches.value_of("remote_transport").map(String::from);
EndpointConf {
listen,
remote,
through,
interface,
listen_transport,
remote_transport,
network: Default::default(),
}
}
+2
View File
@@ -84,6 +84,8 @@ impl From<LegacyConf> for FullConf {
remote,
through: None,
interface: None,
listen_transport: None,
remote_transport: None,
network: Default::default(),
})
.collect();
+7
View File
@@ -19,6 +19,13 @@ pub use endpoint::EndpointConf;
mod legacy;
pub use legacy::LegacyConf;
/// Conig Architecture
/// cmd | file => LogConf => { level, output }
/// cmd | file => DnsConf => { resolve cinfig, opts }
/// cmd | file => NetConf
/// \
/// cmd | file => EndpointConf => { [local, remote, conn_opts] }
pub trait Config {
type Output;
+1
View File
@@ -86,6 +86,7 @@ impl Config for NetConf {
// from endpoint
send_through: None,
bind_interface: None,
transport: None,
haproxy_opts: HaproxyOpts {
send_proxy,
+13 -2
View File
@@ -3,6 +3,8 @@ use std::fmt::{Formatter, Display};
use std::net::SocketAddr;
use crate::dns;
#[cfg(feature = "transport")]
use kaminari::mix::{MixAccept, MixConnect};
#[derive(Clone)]
@@ -141,9 +143,18 @@ impl Display for ConnectOpts {
write!(
f,
"tcp-timeout={}s, udp-timeout={}s",
"tcp-timeout={}s, udp-timeout={}s; ",
self.tcp_timeout, self.udp_timeout
)
)?;
#[cfg(feature = "transport")]
if self.transport.is_some() {
write!(f, "transport=kaminari")?;
} else {
write!(f, "transport=none")?;
}
Ok(())
}
}