use anyhow::Result;
use iroh::{Endpoint, protocol::Router};
use iroh_n0des::{
ApiSecret, Client, ClientHost, CLIENT_HOST_ALPN, API_SECRET_ENV_VAR_NAME,
caps::NetDiagnosticsCap,
};
async fn setup_net_diagnostics(endpoint: &Endpoint) -> Result<Router> {
// Get your secret somehow, either from an environment variable or config file
let secret = ApiSecret::from_env_var(API_SECRET_ENV_VAR_NAME)?;
// The remote_id is the id of the endpoint we'll be sending the network
// report to, derived from the secret's address
let remote_id = secret.addr().id;
// Build the client
let client = Client::builder(endpoint)
.api_secret(secret)?
.build()
.await?;
// Grant the GetAny capability so the platform can request diagnostics
// from this endpoint on demand
let client2 = client.clone();
tokio::spawn(async move {
client2
.grant_capability(remote_id, vec![NetDiagnosticsCap::GetAny])
.await
.unwrap();
});
// Set up a ClientHost so the platform can dial back into this endpoint
let host = ClientHost::new(endpoint);
let router = Router::builder(endpoint.clone())
.accept(CLIENT_HOST_ALPN, host)
.spawn();
Ok(router)
}