Skip to main content

iroh_services/
protocol.rs

1use anyhow::Result;
2use irpc::{channel::oneshot, rpc_requests};
3use rcan::Rcan;
4use serde::{Deserialize, Serialize};
5use uuid::Uuid;
6
7use crate::{caps::Caps, net_diagnostics::DiagnosticsReport};
8
9/// The main ALPN for connecting from the client to the cloud node.
10pub const ALPN: &[u8] = b"/iroh/n0des/1";
11
12pub type IrohServicesClient = irpc::Client<IrohServicesProtocol>;
13
14#[rpc_requests(message = ServicesMessage)]
15#[derive(Debug, Serialize, Deserialize)]
16#[allow(clippy::large_enum_variant)]
17pub enum IrohServicesProtocol {
18    #[rpc(tx=oneshot::Sender<()>)]
19    Auth(Auth),
20    #[rpc(tx=oneshot::Sender<RemoteResult<()>>)]
21    PutMetrics(PutMetrics),
22    #[rpc(tx=oneshot::Sender<Pong>)]
23    Ping(Ping),
24
25    #[rpc(tx=oneshot::Sender<RemoteResult<()>>)]
26    PutNetworkDiagnostics(PutNetworkDiagnostics),
27
28    #[rpc(tx=oneshot::Sender<RemoteResult<()>>)]
29    GrantCap(GrantCap),
30}
31
32/// Dedicated protocol for cloud-to-endpoint net diagnostics connections.
33#[rpc_requests(message = NetDiagnosticsMessage)]
34#[derive(Debug, Serialize, Deserialize)]
35#[allow(clippy::large_enum_variant)]
36pub enum ClientHostProtocol {
37    #[rpc(tx=oneshot::Sender<()>)]
38    Auth(Auth),
39    #[rpc(tx=oneshot::Sender<RemoteResult<DiagnosticsReport>>)]
40    RunNetworkDiagnostics(RunNetworkDiagnostics),
41}
42
43pub type RemoteResult<T> = Result<T, RemoteError>;
44
45#[derive(Clone, Serialize, Deserialize, thiserror::Error, Debug)]
46pub enum RemoteError {
47    #[error("Missing capability: {}", _0.to_strings().join(", "))]
48    MissingCapability(Caps),
49    #[error("Unauthorized: {}", _0)]
50    AuthError(String),
51    #[error("Internal server error")]
52    InternalServerError,
53}
54
55/// Authentication on first request
56#[derive(Debug, Serialize, Deserialize)]
57pub struct Auth {
58    pub caps: Rcan<Caps>,
59}
60
61/// Request to store the given metrics data
62#[derive(Debug, Serialize, Deserialize)]
63pub struct PutMetrics {
64    pub session_id: Uuid,
65    pub update: iroh_metrics::encoding::Update,
66}
67
68/// Simple ping requests
69#[derive(Debug, Serialize, Deserialize)]
70pub struct Ping {
71    pub req_id: [u8; 16],
72}
73
74/// Simple ping response
75#[derive(Debug, Serialize, Deserialize)]
76pub struct Pong {
77    pub req_id: [u8; 16],
78}
79
80/// Publishing network diagnostics
81#[derive(Debug, Serialize, Deserialize)]
82pub struct PutNetworkDiagnostics {
83    pub report: crate::net_diagnostics::DiagnosticsReport,
84}
85
86/// ask this node to run diagnostics & return the result.
87/// present even without the net_diagnostics feature flag because the request
88/// struct is empty in both cases
89#[derive(Debug, Serialize, Deserialize)]
90pub struct RunNetworkDiagnostics;
91
92/// Grant a capability token to the remote endpoint. The remote should store
93/// the RCAN and use it when dialing back to authorize its requests.
94#[derive(Debug, Serialize, Deserialize)]
95pub struct GrantCap {
96    pub cap: Rcan<Caps>,
97}