From d5ce78649b7b7124c762c030875edbb94eb5ba2e Mon Sep 17 00:00:00 2001 From: zephyr Date: Sat, 23 Nov 2024 02:06:10 +0900 Subject: [PATCH] new argument -e for listen interface binding --- src/cmd/flag.rs | 12 +++++++++--- src/cmd/mod.rs | 4 +--- src/conf/endpoint.rs | 15 ++++++++++----- src/conf/legacy/mod.rs | 1 + src/conf/mod.rs | 2 +- src/conf/net.rs | 5 ++++- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/cmd/flag.rs b/src/cmd/flag.rs index 7a22e6c0..19dc613b 100644 --- a/src/cmd/flag.rs +++ b/src/cmd/flag.rs @@ -90,21 +90,27 @@ pub fn add_options(app: Command) -> Command { Arg::new("interface") .short('i') .long("interface") - .help("bind to interface") + .help("send through interface") .value_name("device") .display_order(4), + Arg::new("listen_interface") + .short('e') + .long("listen-interface") + .help("listen interface") + .value_name("device") + .display_order(5), Arg::new("listen_transport") .short('a') .long("listen-transport") .help("listen transport") .value_name("options") - .display_order(5), + .display_order(6), Arg::new("remote_transport") .short('b') .long("remote-transport") .help("remote transport") .value_name("options") - .display_order(6), + .display_order(7), ]) } diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 67fcf3cd..a69ccaa6 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -22,9 +22,7 @@ pub enum CmdInput { pub fn scan() -> CmdInput { let ver = format!("{} {}", VERSION, FEATURES); - let app = Command::new("Realm") - .about("A high efficiency relay tool") - .version(ver); + let app = Command::new("Realm").about("A high efficiency relay tool").version(ver); let app = app .disable_help_flag(true) diff --git a/src/conf/endpoint.rs b/src/conf/endpoint.rs index d0a9300e..2a8744b2 100644 --- a/src/conf/endpoint.rs +++ b/src/conf/endpoint.rs @@ -33,6 +33,10 @@ pub struct EndpointConf { #[serde(skip_serializing_if = "Option::is_none")] pub interface: Option, + #[serde(default)] + #[serde(skip_serializing_if = "Option::is_none")] + pub listen_interface: Option, + #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] pub listen_transport: Option, @@ -155,16 +159,12 @@ impl Config for EndpointConf { // build partial conn_opts from netconf let NetInfo { - bind_opts, + mut bind_opts, mut conn_opts, no_tcp, use_udp, } = self.network.build(); - // build left fields of conn_opts - - conn_opts.bind_address = self.build_send_through(); - #[cfg(feature = "balance")] { conn_opts.balancer = self.build_balancer(); @@ -175,7 +175,10 @@ impl Config for EndpointConf { conn_opts.transport = self.build_transport(); } + // build left fields of bind_opts and conn_opts + conn_opts.bind_address = self.build_send_through(); conn_opts.bind_interface = self.interface; + bind_opts.bind_interface = self.listen_interface; EndpointInfo { no_tcp, @@ -203,6 +206,7 @@ impl Config for EndpointConf { let remote = matches.get_one("remote").cloned().unwrap(); let through = matches.get_one("through").cloned(); let interface = matches.get_one("interface").cloned(); + let listen_interface = matches.get_one("listen_interface").cloned(); let listen_transport = matches.get_one("listen_transport").cloned(); let remote_transport = matches.get_one("remote_transport").cloned(); @@ -211,6 +215,7 @@ impl Config for EndpointConf { remote, through, interface, + listen_interface, listen_transport, remote_transport, network: Default::default(), diff --git a/src/conf/legacy/mod.rs b/src/conf/legacy/mod.rs index 548493ae..e4dd9686 100644 --- a/src/conf/legacy/mod.rs +++ b/src/conf/legacy/mod.rs @@ -72,6 +72,7 @@ impl From for FullConf { remote, through: None, interface: None, + listen_interface: None, listen_transport: None, remote_transport: None, network: Default::default(), diff --git a/src/conf/mod.rs b/src/conf/mod.rs index 8a1268fe..ada6b695 100644 --- a/src/conf/mod.rs +++ b/src/conf/mod.rs @@ -98,7 +98,7 @@ impl FullConf { .filter_entry(|e| e.file_name().to_str().is_some_and(|x| !x.starts_with('.'))) .filter_map(|e| e.ok()) .filter(|e| e.file_type().is_file()) - .filter(|e| e.path().extension().map_or(false, |s| s == "toml" || s == "json")) + .filter(|e| e.path().extension().is_some_and(|s| s == "toml" || s == "json")) { let conf_part = fs::read_to_string(entry.path()) .unwrap_or_else(|e| panic!("failed to open {}: {}", entry.path().to_string_lossy(), e)); diff --git a/src/conf/net.rs b/src/conf/net.rs index 08a680cf..dd73d050 100644 --- a/src/conf/net.rs +++ b/src/conf/net.rs @@ -90,7 +90,10 @@ impl Config for NetConf { let tcp_timeout = unbox!(tcp_timeout, TCP_TIMEOUT); let udp_timeout = unbox!(udp_timeout, UDP_TIMEOUT); - let bind_opts = BindOpts { ipv6_only }; + let bind_opts = BindOpts { + ipv6_only, + bind_interface: None, + }; let conn_opts = ConnectOpts { tcp_keepalive: tcp_kpa, tcp_keepalive_probe: tcp_kpa_probe,