use std::time::{Duration, Instant};
use anyhow::{Context, bail};
use iroh::EndpointId;
use tokio::io::{AsyncBufReadExt, BufReader};
use crate::iroh_pool::ConnPool;
use crate::stream::{StreamHeader, TUNNEL_STREAM_ALPN};
pub const PING_HOST: &str = "__tunnet_ping__";
pub struct PingResult {
pub latency_ms: f64,
pub path: String,
}
pub async fn ping_peer(pool: &ConnPool, peer: EndpointId, seq: u32) -> anyhow::Result<PingResult> {
let conn = pool
.get_alpn(peer, TUNNEL_STREAM_ALPN)
.await
.context("dial ping")?;
let (mut send, recv) = conn.open_bi().await.context("open_bi ping")?;
let header = StreamHeader {
dst_port: 0,
host: PING_HOST.into(),
};
header.write_to(&mut send).await?;
let start = Instant::now();
let msg = format!("PING {seq}\n");
send.write_all(msg.as_bytes()).await?;
send.finish()?;
let mut reader = BufReader::new(recv);
let mut line = String::new();
tokio::time::timeout(Duration::from_secs(5), reader.read_line(&mut line))
.await
.context("ping timeout")??;
let expected = format!("PONG {seq}");
if line.trim() != expected {
bail!("unexpected ping reply: {}", line.trim());
}
let latency_ms = start.elapsed().as_secs_f64() * 1000.0;
Ok(PingResult {
latency_ms,
path: "unknown".into(),
})
}
pub async fn handle_inbound_ping(
header: &StreamHeader,
mut send: iroh::endpoint::SendStream,
recv: iroh::endpoint::RecvStream,
) -> bool {
if header.host != PING_HOST {
return false;
}
let mut reader = BufReader::new(recv);
let mut line = String::new();
if reader.read_line(&mut line).await.is_err() {
return true;
}
let trimmed = line.trim();
if let Some(seq) = trimmed.strip_prefix("PING ") {
let reply = format!("PONG {seq}\n");
let _ = send.write_all(reply.as_bytes()).await;
let _ = send.finish();
}
true
}