Skip to content

Commit 49ca7c2

Browse files
authored
feat(cli): forest-cli net reachability (#4052)
1 parent ccdd023 commit 49ca7c2

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
`forest-tool shed key-pair-from-private-key` commands. These facilate moving
3535
between Forest and Lotus without losing the peer-to-peer identity.
3636

37+
- [#4052](https://github.com/ChainSafe/forest/pull/4052) Add
38+
`forest-cli net reachability` command that prints information about
39+
reachability from the internet.
40+
3741
### Changed
3842

3943
### Removed

src/cli/subcommands/net_cmd.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub enum NetCommands {
3333
/// Peer ID to disconnect from
3434
id: String,
3535
},
36+
/// Print information about reachability from the internet
37+
Reachability,
3638
}
3739

3840
impl NetCommands {
@@ -156,6 +158,18 @@ impl NetCommands {
156158
println!("disconnect {id}: success");
157159
Ok(())
158160
}
161+
Self::Reachability => {
162+
let nat_status = api.net_auto_nat_status().await?;
163+
println!("AutoNAT status: {}", nat_status.reachability_as_str());
164+
if let Some(public_addrs) = nat_status.public_addrs {
165+
if !public_addrs.is_empty() {
166+
// Format is compatible with Go code:
167+
// `fmt.Println("Public address:", []string{"foo", "bar"})`
168+
println!("Public address: [{}]", public_addrs.join(" "));
169+
}
170+
}
171+
Ok(())
172+
}
159173
}
160174
}
161175
}

src/rpc_api/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,18 @@ pub mod net_api {
467467
}
468468
lotus_json_with_self!(NatStatusResult);
469469

470+
impl NatStatusResult {
471+
// See <https://github.com/libp2p/go-libp2p/blob/164adb40fef9c19774eb5fe6d92afb95c67ba83c/core/network/network.go#L93>
472+
pub fn reachability_as_str(&self) -> &'static str {
473+
match self.reachability {
474+
0 => "Unknown",
475+
1 => "Public",
476+
2 => "Private",
477+
_ => "(unrecognized)",
478+
}
479+
}
480+
}
481+
470482
impl From<libp2p::autonat::NatStatus> for NatStatusResult {
471483
fn from(nat: libp2p::autonat::NatStatus) -> Self {
472484
use libp2p::autonat::NatStatus;

src/rpc_client/net_ops.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ impl ApiInfo {
5858
RpcRequest::new(NET_AGENT_VERSION, (peer,))
5959
}
6060

61+
pub async fn net_auto_nat_status(&self) -> Result<NatStatusResult, JsonRpcError> {
62+
self.call(Self::net_auto_nat_status_req()).await
63+
}
64+
6165
pub fn net_auto_nat_status_req() -> RpcRequest<NatStatusResult> {
62-
RpcRequest::new(NET_AUTO_NAT_STATUS, ())
66+
RpcRequest::new_v1(NET_AUTO_NAT_STATUS, ())
6367
}
6468
}

0 commit comments

Comments
 (0)