diff --git a/CHANGELOG.md b/CHANGELOG.md index 535c53482e..9eaa9db428 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,10 @@ Post 1.0.0 release, the changelog format is based on [Keep a Changelog](https:// ### Added - remove coconut feature and unify builds ([#2890]) +- native-client: is now capable of listening for requests on sockets different than `127.0.0.1` ([#2939]). This can be specified via `--host` flag during `init` or `run`. Alternatively a custom `host` can be set in `config.toml` file under `socket` section. [#2890]: https://github.com/nymtech/nym/pull/2890 +[#2939]: https://github.com/nymtech/nym/pull/2939 # [v1.1.8] (2023-01-31) diff --git a/clients/native/src/client/config/mod.rs b/clients/native/src/client/config/mod.rs index e49d2e7569..f7f08b2008 100644 --- a/clients/native/src/client/config/mod.rs +++ b/clients/native/src/client/config/mod.rs @@ -9,6 +9,7 @@ use config::defaults::DEFAULT_WEBSOCKET_LISTENING_PORT; use config::{NymConfig, OptionalSet}; use serde::{Deserialize, Serialize}; use std::fmt::Debug; +use std::net::{IpAddr, Ipv4Addr}; use std::path::PathBuf; use std::str::FromStr; @@ -104,6 +105,11 @@ impl Config { self } + pub fn with_host(mut self, host: IpAddr) -> Self { + self.socket.host = host; + self + } + pub fn with_port(mut self, port: u16) -> Self { self.socket.listening_port = port; self @@ -130,6 +136,10 @@ impl Config { self.socket.socket_type } + pub fn get_listening_ip(&self) -> IpAddr { + self.socket.host + } + pub fn get_listening_port(&self) -> u16 { self.socket.listening_port } @@ -180,9 +190,10 @@ impl Config { } #[derive(Debug, Deserialize, PartialEq, Eq, Serialize)] -#[serde(deny_unknown_fields)] +#[serde(default, deny_unknown_fields)] pub struct Socket { socket_type: SocketType, + host: IpAddr, listening_port: u16, } @@ -190,6 +201,7 @@ impl Default for Socket { fn default() -> Self { Socket { socket_type: SocketType::WebSocket, + host: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), listening_port: DEFAULT_WEBSOCKET_LISTENING_PORT, } } diff --git a/clients/native/src/client/config/template.rs b/clients/native/src/client/config/template.rs index 15a0763ed9..df32829b22 100644 --- a/clients/native/src/client/config/template.rs +++ b/clients/native/src/client/config/template.rs @@ -93,6 +93,9 @@ socket_type = '{{ socket.socket_type }}' # will be listening for incoming requests listening_port = {{ socket.listening_port }} +# if applicable (for the case of 'WebSocket'), the ip address on which the client +# will be listening for incoming requests +host = '{{ socket.host }}' ##### logging configuration options ##### diff --git a/clients/native/src/client/mod.rs b/clients/native/src/client/mod.rs index 549f52dbe1..9c58b614e8 100644 --- a/clients/native/src/client/mod.rs +++ b/clients/native/src/client/mod.rs @@ -102,7 +102,8 @@ impl SocketClient { reply_controller_sender, ); - websocket::Listener::new(config.get_listening_port()).start(websocket_handler, shutdown); + websocket::Listener::new(config.get_listening_ip(), config.get_listening_port()) + .start(websocket_handler, shutdown); } /// blocking version of `start_socket` method. Will run forever (or until SIGINT is sent) diff --git a/clients/native/src/commands/init.rs b/clients/native/src/commands/init.rs index c2273c020f..b56e570ce7 100644 --- a/clients/native/src/commands/init.rs +++ b/clients/native/src/commands/init.rs @@ -12,6 +12,7 @@ use crypto::asymmetric::identity; use nymsphinx::addressing::clients::Recipient; use serde::Serialize; use std::fmt::Display; +use std::net::IpAddr; use tap::TapFallible; #[derive(Args, Clone)] @@ -46,6 +47,10 @@ pub(crate) struct Init { #[clap(short, long)] port: Option, + /// Ip for the socket (if applicable) to listen for requests. + #[clap(long)] + host: Option, + /// Mostly debug-related option to increase default traffic rate so that you would not need to /// modify config post init #[clap(long, hide = true)] @@ -71,6 +76,7 @@ impl From for OverrideConfig { nym_apis: init_config.nym_apis, disable_socket: init_config.disable_socket, port: init_config.port, + host: init_config.host, fastmode: init_config.fastmode, no_cover: init_config.no_cover, diff --git a/clients/native/src/commands/mod.rs b/clients/native/src/commands/mod.rs index e11f3eb7f9..5e651a5f0b 100644 --- a/clients/native/src/commands/mod.rs +++ b/clients/native/src/commands/mod.rs @@ -9,6 +9,7 @@ use completions::{fig_generate, ArgShell}; use config::OptionalSet; use lazy_static::lazy_static; use std::error::Error; +use std::net::IpAddr; pub(crate) mod init; pub(crate) mod run; @@ -56,6 +57,7 @@ pub(crate) struct OverrideConfig { nym_apis: Option>, disable_socket: Option, port: Option, + host: Option, fastmode: bool, no_cover: bool, nyxd_urls: Option>, @@ -81,6 +83,7 @@ pub(crate) fn override_config(config: Config, args: OverrideConfig) -> Config { .with_base(BaseConfig::with_high_default_traffic_volume, args.fastmode) .with_base(BaseConfig::with_disabled_cover_traffic, args.no_cover) .with_optional(Config::with_port, args.port) + .with_optional(Config::with_host, args.host) .with_optional_custom_env_ext( BaseConfig::with_custom_nym_apis, args.nym_apis, diff --git a/clients/native/src/commands/run.rs b/clients/native/src/commands/run.rs index f0537be3c9..ab9a04427e 100644 --- a/clients/native/src/commands/run.rs +++ b/clients/native/src/commands/run.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 use std::error::Error; +use std::net::IpAddr; use crate::{ client::{config::Config, SocketClient}, @@ -43,6 +44,10 @@ pub(crate) struct Run { #[clap(short, long)] port: Option, + /// Ip for the socket (if applicable) to listen for requests. + #[clap(long)] + host: Option, + /// Mostly debug-related option to increase default traffic rate so that you would not need to /// modify config post init #[clap(long, hide = true)] @@ -64,6 +69,7 @@ impl From for OverrideConfig { nym_apis: run_config.nym_apis, disable_socket: run_config.disable_socket, port: run_config.port, + host: run_config.host, fastmode: run_config.fastmode, no_cover: run_config.no_cover, nyxd_urls: run_config.nyxd_urls, diff --git a/clients/native/src/websocket/listener.rs b/clients/native/src/websocket/listener.rs index 459eb1bc17..534fd2dafc 100644 --- a/clients/native/src/websocket/listener.rs +++ b/clients/native/src/websocket/listener.rs @@ -3,6 +3,7 @@ use super::handler::HandlerBuilder; use log::*; +use std::net::IpAddr; use std::{net::SocketAddr, process, sync::Arc}; use tokio::io::AsyncWriteExt; use tokio::{sync::Notify, task::JoinHandle}; @@ -24,10 +25,9 @@ pub(crate) struct Listener { } impl Listener { - pub(crate) fn new(port: u16) -> Self { + pub(crate) fn new(host: IpAddr, port: u16) -> Self { Listener { - // unless we find compelling reason not to, just listen on local only - address: SocketAddr::new("127.0.0.1".parse().unwrap(), port), + address: SocketAddr::new(host, port), state: State::AwaitingConnection, } }