Expose wireguard details on self described endpoint (#4825)

* Expose wireguard details on self described endpoint

* Fill placeholder string
This commit is contained in:
Bogdan-Ștefan Neacşu
2024-09-02 11:51:48 +02:00
committed by GitHub
parent a7910c1049
commit bdf45cafb5
7 changed files with 63 additions and 13 deletions
+9
View File
@@ -566,6 +566,9 @@ pub struct NymNodeDescription {
#[serde(default)]
pub authenticator: Option<AuthenticatorDetails>,
#[serde(default)]
pub wireguard: Option<WireguardDetails>,
// for now we only care about their ws/wss situation, nothing more
pub mixnet_websockets: WebSockets,
@@ -629,6 +632,12 @@ pub struct AuthenticatorDetails {
pub address: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
pub struct WireguardDetails {
pub port: u16,
pub public_key: String,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
pub struct ApiHealthResponse {
pub status: ApiStatus,
+11
View File
@@ -9,6 +9,7 @@ use crate::support::config::DEFAULT_NODE_DESCRIBE_BATCH_SIZE;
use futures::{stream, StreamExt};
use nym_api_requests::models::{
AuthenticatorDetails, IpPacketRouterDetails, NetworkRequesterDetails, NymNodeDescription,
WireguardDetails,
};
use nym_api_requests::nym_nodes::NodeRole;
use nym_config::defaults::{mainnet, DEFAULT_NYM_NODE_HTTP_PORT};
@@ -197,6 +198,15 @@ async fn try_get_description(
None
};
let wireguard = if let Ok(wg) = client.get_wireguard().await {
Some(WireguardDetails {
port: wg.port,
public_key: wg.public_key,
})
} else {
None
};
let description = NymNodeDescription {
host_information: host_info.data.into(),
last_polled: OffsetDateTime::now_utc().into(),
@@ -204,6 +214,7 @@ async fn try_get_description(
network_requester,
ip_packet_router,
authenticator,
wireguard,
mixnet_websockets: websockets.into(),
auxiliary_details,
role: data.role(),
@@ -6,7 +6,7 @@ use axum::extract::Query;
use axum::http::StatusCode;
use axum::routing::get;
use axum::Router;
use nym_node_requests::api::v1::gateway::models::{ClientInterfaces, WebSockets};
use nym_node_requests::api::v1::gateway::models::{ClientInterfaces, WebSockets, Wireguard};
use nym_node_requests::routes::api::v1::gateway::client_interfaces;
pub(crate) fn routes<S: Send + Sync + 'static + Clone>(
@@ -27,6 +27,13 @@ pub(crate) fn routes<S: Send + Sync + 'static + Clone>(
move |query| mixnet_websockets(websockets, query)
}),
)
.route(
client_interfaces::WIREGUARD,
get({
let wireguard = interfaces.as_ref().and_then(|i| i.wireguard.clone());
move |query| wireguard_details(wireguard, query)
}),
)
}
/// Returns client interfaces supported by this gateway.
@@ -80,3 +87,29 @@ pub(crate) async fn mixnet_websockets(
}
pub type MixnetWebSocketsResponse = FormattedResponse<WebSockets>;
/// Returns wireguard information on this gateway.
#[utoipa::path(
get,
path = "/wireguard",
context_path = "/api/v1/gateway/client-interfaces",
tag = "Gateway",
responses(
(status = 501, description = "the endpoint hasn't been implemented yet"),
(status = 200, content(
("application/json" = Wireguard),
("application/yaml" = Wireguard)
))
),
params(OutputParams)
)]
pub(crate) async fn wireguard_details(
wireguard: Option<Wireguard>,
Query(output): Query<OutputParams>,
) -> Result<WireguardResponse, StatusCode> {
let wireguard = wireguard.ok_or(StatusCode::NOT_IMPLEMENTED)?;
let output = output.output.unwrap_or_default();
Ok(output.to_response(wireguard))
}
pub type WireguardResponse = FormattedResponse<Wireguard>;
@@ -16,6 +16,8 @@ use crate::api::v1::network_requester::exit_policy::models::UsedExitPolicy;
use crate::api::v1::network_requester::models::NetworkRequester;
pub use nym_http_api_client::Client;
use super::v1::gateway::models::Wireguard;
pub type NymNodeApiClientError = HttpClientError<ErrorResponse>;
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
@@ -69,6 +71,11 @@ pub trait NymNodeApiClientExt: ApiClient {
self.get_json_from(routes::api::v1::authenticator_absolute())
.await
}
async fn get_wireguard(&self) -> Result<Wireguard, NymNodeApiClientError> {
self.get_json_from(routes::api::v1::gateway::client_interfaces::wireguard_absolute())
.await
}
}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
@@ -16,7 +16,7 @@ pub struct Gateway {
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct Wireguard {
#[cfg_attr(feature = "openapi", schema(example = 51820, default = 51820))]
#[cfg_attr(feature = "openapi", schema(example = 51822, default = 51822))]
pub port: u16,
pub public_key: String,
-10
View File
@@ -96,16 +96,6 @@ pub mod routes {
client_interfaces_absolute(),
WEBSOCKETS
);
pub mod wireguard {
use super::*;
pub const CLIENT: &str = "/client";
pub const CLIENTS: &str = "/clients";
absolute_route!(client_absolute, wireguard_absolute(), CLIENT);
absolute_route!(clients_absolute, wireguard_absolute(), CLIENTS);
}
}
}
+1 -1
View File
@@ -639,7 +639,7 @@ impl NymNode {
let wireguard = if self.config.wireguard.enabled {
Some(api_requests::v1::gateway::models::Wireguard {
port: self.config.wireguard.announced_port,
public_key: "placeholder key value".to_string(),
public_key: self.wireguard.inner.keypair().public_key().to_string(),
})
} else {
None