diff --git a/network/src/peers/peers.rs b/network/src/peers/peers.rs index 78ec4abb93..d163d049e8 100644 --- a/network/src/peers/peers.rs +++ b/network/src/peers/peers.rs @@ -460,7 +460,7 @@ impl Node { /// as disconnected from this node server. /// #[inline] - pub(crate) fn disconnect_from_peer(&self, remote_address: SocketAddr) -> Result<(), NetworkError> { + pub fn disconnect_from_peer(&self, remote_address: SocketAddr) -> Result<(), NetworkError> { // Set the peer as disconnected in the peer book. let result = self.peer_book.set_disconnected(remote_address); diff --git a/rpc/documentation/private_endpoints/disconnect.md b/rpc/documentation/private_endpoints/disconnect.md new file mode 100644 index 0000000000..924a78a487 --- /dev/null +++ b/rpc/documentation/private_endpoints/disconnect.md @@ -0,0 +1,20 @@ +Disconnects the node from the given address. + +### Protected Endpoint + +Yes + +### Arguments + +| Parameter | Type | Required | Description | +|:-------------------:|:------:|:--------:|:---------------------------------------------- | +| `address` | string | Yes | The address to disconnect in an IP:port format | + +### Response + +null + +### Example +```ignore +curl --user username:password --data-binary '{"jsonrpc": "2.0", "id":"1", "method": "disconnect", "params": ["127.0.0.1:4141"] }' -H 'content-type: application/json' http://127.0.0.1:3030/ +``` diff --git a/rpc/src/rpc_impl_protected.rs b/rpc/src/rpc_impl_protected.rs index 238c210228..8ea4369bd5 100644 --- a/rpc/src/rpc_impl_protected.rs +++ b/rpc/src/rpc_impl_protected.rs @@ -52,7 +52,7 @@ use snarkvm_utilities::{ use itertools::Itertools; use jsonrpc_http_server::jsonrpc_core::{IoDelegate, MetaIoHandler, Params, Value}; use rand::{thread_rng, Rng}; -use std::{str::FromStr, sync::Arc}; +use std::{net::SocketAddr, str::FromStr, sync::Arc}; type JsonRPCError = jsonrpc_core::Error; @@ -245,6 +245,23 @@ impl RpcImpl { } } + /// Disconnects from the given address + pub async fn disconnect_protected(self, params: Params, meta: Meta) -> Result { + self.validate_auth(meta)?; + + let value = match params { + Params::Array(arr) => arr, + _ => return Err(JsonRPCError::invalid_request()), + }; + + let address: SocketAddr = serde_json::from_value(value[0].clone()) + .map_err(|e| JsonRPCError::invalid_params(format!("Invalid params: {}.", e)))?; + + let _ = self.node.disconnect_from_peer(address); + + Ok(Value::Null) + } + /// Expose the protected functions as RPC enpoints pub fn add_protected(&self, io: &mut MetaIoHandler) { let mut d = IoDelegate::::new(Arc::new(self.clone())); @@ -285,6 +302,10 @@ impl RpcImpl { let rpc = rpc.clone(); rpc.create_account_protected(params, meta) }); + d.add_method_with_meta("disconnect", |rpc, params, meta| { + let rpc = rpc.clone(); + rpc.disconnect_protected(params, meta) + }); io.extend_with(d) }