diff --git a/CHANGELOG.md b/CHANGELOG.md index c36f67bd45a4..99cfc53883c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,10 @@ `forest-tool shed key-pair-from-private-key` commands. These facilate moving between Forest and Lotus without losing the peer-to-peer identity. +- [#4052](https://github.com/ChainSafe/forest/pull/4052) Add + `forest-cli net reachability` command that prints information about + reachability from the internet. + ### Changed ### Removed diff --git a/src/cli/subcommands/net_cmd.rs b/src/cli/subcommands/net_cmd.rs index e13daad77840..80e2159c484c 100644 --- a/src/cli/subcommands/net_cmd.rs +++ b/src/cli/subcommands/net_cmd.rs @@ -33,6 +33,8 @@ pub enum NetCommands { /// Peer ID to disconnect from id: String, }, + /// Print information about reachability from the internet + Reachability, } impl NetCommands { @@ -156,6 +158,18 @@ impl NetCommands { println!("disconnect {id}: success"); Ok(()) } + Self::Reachability => { + let nat_status = api.net_auto_nat_status().await?; + println!("AutoNAT status: {}", nat_status.reachability_as_str()); + if let Some(public_addrs) = nat_status.public_addrs { + if !public_addrs.is_empty() { + // Format is compatible with Go code: + // `fmt.Println("Public address:", []string{"foo", "bar"})` + println!("Public address: [{}]", public_addrs.join(" ")); + } + } + Ok(()) + } } } } diff --git a/src/rpc_api/mod.rs b/src/rpc_api/mod.rs index 42a50e31134b..27dd2d7fb03c 100644 --- a/src/rpc_api/mod.rs +++ b/src/rpc_api/mod.rs @@ -467,6 +467,18 @@ pub mod net_api { } lotus_json_with_self!(NatStatusResult); + impl NatStatusResult { + // See + pub fn reachability_as_str(&self) -> &'static str { + match self.reachability { + 0 => "Unknown", + 1 => "Public", + 2 => "Private", + _ => "(unrecognized)", + } + } + } + impl From for NatStatusResult { fn from(nat: libp2p::autonat::NatStatus) -> Self { use libp2p::autonat::NatStatus; diff --git a/src/rpc_client/net_ops.rs b/src/rpc_client/net_ops.rs index bd03d4aa3e7b..5f4396cdad54 100644 --- a/src/rpc_client/net_ops.rs +++ b/src/rpc_client/net_ops.rs @@ -58,7 +58,11 @@ impl ApiInfo { RpcRequest::new(NET_AGENT_VERSION, (peer,)) } + pub async fn net_auto_nat_status(&self) -> Result { + self.call(Self::net_auto_nat_status_req()).await + } + pub fn net_auto_nat_status_req() -> RpcRequest { - RpcRequest::new(NET_AUTO_NAT_STATUS, ()) + RpcRequest::new_v1(NET_AUTO_NAT_STATUS, ()) } }