Implement protobuf for iroh peer info

This commit is contained in:
fluo10 2025-09-04 08:25:22 +09:00
parent b53c7170eb
commit b461dc39a7
12 changed files with 143 additions and 34 deletions

View file

@ -1,4 +1,6 @@
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_prost_build::compile_protos("proto/caretta_sync.proto")?; tonic_prost_build::compile_protos("proto/caretta_sync.proto")?;
tonic_prost_build::compile_protos("proto/iroh.proto")?;
Ok(()) Ok(())
} }

43
core/proto/iroh.proto Normal file
View file

@ -0,0 +1,43 @@
syntax = "proto3";
package iroh;
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";
service Iroh {
rpc RemoteInfo(RemoteInfoRequest) returns (RemoteInfoMessage);
rpc RemoteInfoIter(RemoteInfoIterRequest) returns (stream RemoteInfoMessage);
}
message RemoteInfoRequest {
string node_id = 1;
}
message RemoteInfoIterRequest {}
message RemoteInfoMessage {
string node_id = 1;
string relay_url = 2;
repeated DirectAddrInfoMessage addrs = 3;
string conn_type = 4;
google.protobuf.Duration latency = 5;
google.protobuf.Duration last_used = 6;
}
message DirectAddrInfoMessage {
string addr = 1;
google.protobuf.Duration latency = 2;
LastControlMessage last_control = 3;
google.protobuf.Duration last_payload = 4;
google.protobuf.Duration last_alive = 5;
repeated SourceMessage sources = 6;
}
message LastControlMessage {
google.protobuf.Duration duration = 1;
string control_msg = 2;
}
message SourceMessage {
string source = 1;
google.protobuf.Duration duration = 2;
}

View file

@ -1,16 +0,0 @@
use libp2p::Multiaddr;
use crate::cache::entity::CachedAddressModel;
use crate::utils::utc_to_timestamp;
use crate::proto::CachedAddressMessage;
impl From<&CachedAddressModel> for CachedAddressMessage {
fn from(a: &CachedAddressModel) -> Self {
Self {
number: a.id,
created_at: Some(utc_to_timestamp(&a.created_at)),
updated_at: Some(utc_to_timestamp(&a.updated_at)),
multiaddress: Multiaddr::from(a.multiaddress.clone()).to_string(),
}
}
}

View file

@ -1,14 +0,0 @@
use crate::{cache::entity::{CachedAddressModel, CachedPeerModel}, proto::{CachedAddressMessage, CachedPeerMessage}, utils::utc_to_timestamp};
impl From<(&CachedPeerModel, &Vec<CachedAddressModel>)> for CachedPeerMessage {
fn from(source: (&CachedPeerModel, &Vec<CachedAddressModel>)) -> Self {
let (peer, addresses) = source;
Self {
number: peer.id,
peer_id: peer.peer_id.to_string(),
created_at: Some(utc_to_timestamp(&peer.created_at)),
addresses: addresses.iter().map(|x| CachedAddressMessage::from(x)).collect(),
}
}
}

View file

@ -0,0 +1,18 @@
use iroh::endpoint::DirectAddrInfo;
use prost_types::DurationError;
use crate::proto::iroh::{DirectAddrInfoMessage, SourceMessage};
impl TryFrom<DirectAddrInfo> for DirectAddrInfoMessage {
type Error = DurationError;
fn try_from(value: DirectAddrInfo) -> Result<Self, Self::Error> {
Ok(DirectAddrInfoMessage {
addr: value.addr.to_string(),
latency: value.latency.map(|x| x.try_into()).transpose()?,
last_control: value.last_control.map(|x| super::LastControlMessage::try_from(x)).transpose()?,
last_payload: value.last_payload.map(|x| x.try_into()).transpose()?,
last_alive: value.last_alive.map(|x| x.try_into()).transpose()?,
sources: value.sources.into_iter().map(|x| SourceMessage::try_from(x)).collect::<Result<Vec<SourceMessage>, DurationError>>()?
})
}
}

View file

@ -0,0 +1,16 @@
use std::time::Duration;
use iroh::endpoint::ControlMsg;
use prost_types::DurationError;
use crate::proto::iroh::LastControlMessage;
impl TryFrom<(Duration, ControlMsg)> for LastControlMessage {
type Error = DurationError;
fn try_from(value: (Duration, ControlMsg)) -> Result<Self, Self::Error> {
Ok(LastControlMessage {
duration: Some(value.0.try_into()?),
control_msg: value.1.to_string()
})
}
}

View file

@ -0,0 +1,8 @@
mod direct_addr_info_message;
mod last_control_message;
mod remote_info_iter_request;
mod remote_info_message;
mod remote_info_request;
mod source_message;
tonic::include_proto!("iroh");

View file

@ -0,0 +1,7 @@
use crate::proto::iroh::RemoteInfoIterRequest;
impl RemoteInfoIterRequest {
pub fn new() -> Self {
Self{}
}
}

View file

@ -0,0 +1,20 @@
use iroh::endpoint::RemoteInfo;
use prost_types::DurationError;
use crate::proto::iroh::{DirectAddrInfoMessage, RemoteInfoMessage};
impl TryFrom<RemoteInfo> for RemoteInfoMessage {
type Error = DurationError;
fn try_from(value: RemoteInfo) -> Result<Self, Self::Error> {
Ok(Self {
node_id: value.node_id.to_string(),
relay_url: value.relay_url.map_or(String::from(""), |x| x.relay_url.to_string()),
addrs: value.addrs.into_iter()
.map(|x| DirectAddrInfoMessage::try_from(x))
.collect::<Result<Vec<DirectAddrInfoMessage>,DurationError>>()?,
conn_type: value.conn_type.to_string(),
latency: value.latency.map(|x| x.try_into()).transpose()?,
last_used: value.last_used.map(|x| x.try_into()).transpose()?,
})
}
}

View file

@ -0,0 +1,11 @@
use iroh::NodeId;
use crate::proto::iroh::RemoteInfoRequest;
impl From<NodeId> for RemoteInfoRequest {
fn from(value: NodeId) -> Self {
Self {
node_id : value.to_string()
}
}
}

View file

@ -0,0 +1,16 @@
use std::time::Duration;
use iroh::endpoint::Source;
use crate::proto::iroh::SourceMessage;
impl TryFrom<(Source, Duration)> for SourceMessage {
type Error = prost_types::DurationError;
fn try_from(src: (Source, Duration)) -> Result<Self, Self::Error> {
let (source, duration )= src;
Ok(Self {
source: source.to_string(),
duration: Some(duration.try_into()?),
})
}
}

View file

@ -1,5 +1,3 @@
mod cached_address; pub mod iroh;
mod cached_peer;
tonic::include_proto!("caretta_sync"); tonic::include_proto!("caretta_sync");