diff --git a/network/src/network.rs b/network/src/network.rs index 6134e7cb12..269d67e66d 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -1163,7 +1163,38 @@ impl NetworkService { let p2p_control: ServiceAsyncControl = p2p_control.clone().into(); handle.spawn_task(async move { #[cfg(not(target_family = "wasm"))] - for addr in &config.listen_addresses { + let listen_addresses = { + let mut addresses = config + .listen_addresses + .clone() + .into_iter() + .collect::>() + .into_iter() + .collect::>(); + if config.reuse_tcp_with_ws { + let has_ws = addresses + .iter() + .any(|a| matches!(find_type(a), TransportType::Ws)); + if !has_ws { + let ws_listen = { + let mut addr = addresses + .iter() + .find(|a| matches!(find_type(a), TransportType::Tcp)) + .expect("Tcp listen must exsit") + .clone(); + addr.push(Protocol::Ws); + addr + }; + addresses.push(ws_listen); + } + + addresses + } else { + addresses + } + }; + #[cfg(not(target_family = "wasm"))] + for addr in &listen_addresses { match p2p_service.listen(addr.to_owned()).await { Ok(listen_address) => { info!("Listen on address: {}", listen_address); diff --git a/resource/ckb.toml b/resource/ckb.toml index 3df8e09dde..dba35b9757 100644 --- a/resource/ckb.toml +++ b/resource/ckb.toml @@ -84,6 +84,8 @@ bootnodes = [] # {{ # whitelist_peers = [] ### Enable `SO_REUSEPORT` feature to reuse port on Linux, not supported on other OS yet # reuse_port_on_linux = true +### Allow ckb to upgrade tcp listening to tcp + ws listening when only tcp listening is found +# reuse_tcp_with_ws = true max_peers = 125 max_outbound_peers = 8 diff --git a/util/app-config/src/configs/network.rs b/util/app-config/src/configs/network.rs index 70d8cb3550..0a19523fb2 100644 --- a/util/app-config/src/configs/network.rs +++ b/util/app-config/src/configs/network.rs @@ -83,6 +83,9 @@ pub struct Config { /// Network use reuse port or not #[serde(default = "default_reuse")] pub reuse_port_on_linux: bool, + /// Allow ckb to upgrade tcp listening to tcp + ws listening when only tcp listening is found + #[serde(default = "default_reuse")] + pub reuse_tcp_with_ws: bool, /// Chain synchronization config options. #[serde(default)] pub sync: SyncConfig,