pub fn get_remote_id52(conn: &iroh::endpoint::Connection) -> String {
let remote_id = conn.remote_id();
let bytes = remote_id.as_bytes();
data_encoding::BASE32_DNSSEC.encode(bytes)
}
async fn ack(send: &mut iroh::endpoint::SendStream) -> eyre::Result<()> {
tracing::trace!("sending ack");
send.write_all(format!("{}\n", crate::ACK).as_bytes())
.await?;
tracing::trace!("sent ack");
Ok(())
}
pub async fn accept_bi(
conn: &iroh::endpoint::Connection,
expected: crate::Protocol,
) -> eyre::Result<(iroh::endpoint::SendStream, iroh::endpoint::RecvStream)> {
loop {
tracing::trace!("accepting bidirectional stream");
match accept_bi_(conn).await? {
(mut send, _recv, crate::Protocol::Ping) => {
tracing::trace!("got ping");
tracing::trace!("sending PONG");
send.write_all(crate::PONG)
.await
.inspect_err(|e| tracing::error!("failed to write PONG: {e:?}"))?;
tracing::trace!("sent PONG");
}
(s, r, found) => {
tracing::trace!("got bidirectional stream: {found:?}");
if found != expected {
return Err(eyre::anyhow!("expected: {expected:?}, got {found:?}"));
}
return Ok((s, r));
}
}
}
}
pub async fn accept_bi_any(
conn: &iroh::endpoint::Connection,
expected: &[crate::Protocol],
) -> eyre::Result<(
iroh::endpoint::SendStream,
iroh::endpoint::RecvStream,
crate::Protocol,
)> {
loop {
tracing::trace!("accepting bidirectional stream (any)");
match accept_bi_(conn).await? {
(mut send, _recv, crate::Protocol::Ping) => {
tracing::trace!("got ping");
send.write_all(crate::PONG)
.await
.inspect_err(|e| tracing::error!("failed to write PONG: {e:?}"))?;
tracing::trace!("sent PONG");
}
(s, r, found) => {
tracing::trace!("got bidirectional stream: {found:?}");
if !expected.contains(&found) {
return Err(eyre::anyhow!("expected one of {expected:?}, got {found:?}"));
}
return Ok((s, r, found));
}
}
}
}
pub async fn accept_bi_with<T: serde::de::DeserializeOwned>(
conn: &iroh::endpoint::Connection,
expected: crate::Protocol,
) -> eyre::Result<(T, iroh::endpoint::SendStream, iroh::endpoint::RecvStream)> {
let (send, mut recv) = accept_bi(conn, expected).await?;
let next = next_json(&mut recv)
.await
.inspect_err(|e| tracing::error!("failed to read next message: {e}"))?;
Ok((next, send, recv))
}
async fn accept_bi_(
conn: &iroh::endpoint::Connection,
) -> eyre::Result<(
iroh::endpoint::SendStream,
iroh::endpoint::RecvStream,
crate::Protocol,
)> {
tracing::trace!("accept_bi_ called");
let (mut send, mut recv) = conn.accept_bi().await?;
tracing::trace!("accept_bi_ got send and recv");
let msg: crate::Protocol = next_json(&mut recv)
.await
.inspect_err(|e| tracing::error!("failed to read next message: {e}"))?;
tracing::trace!("msg: {msg:?}");
ack(&mut send).await?;
tracing::trace!("ack sent");
Ok((send, recv, msg))
}
pub async fn next_json<T: serde::de::DeserializeOwned>(
recv: &mut iroh::endpoint::RecvStream,
) -> eyre::Result<T> {
let mut buffer = Vec::with_capacity(1024);
loop {
let mut byte = [0u8];
let n = recv.read(&mut byte).await?;
if n == Some(0) || n.is_none() {
return Err(eyre::anyhow!(
"connection closed while reading response header"
));
}
if byte[0] == b'\n' {
break;
} else {
buffer.push(byte[0]);
}
}
Ok(serde_json::from_slice(&buffer)?)
}
pub async fn next_string(recv: &mut iroh::endpoint::RecvStream) -> eyre::Result<String> {
let mut buffer = Vec::with_capacity(1024);
loop {
let mut byte = [0u8];
let n = recv.read(&mut byte).await?;
if n == Some(0) || n.is_none() {
return Err(eyre::anyhow!(
"connection closed while reading response header"
));
}
if byte[0] == b'\n' {
break;
} else {
buffer.push(byte[0]);
}
}
String::from_utf8(buffer).map_err(|e| eyre::anyhow!("failed to convert bytes to string: {e}"))
}
pub async fn global_iroh_endpoint() -> iroh::Endpoint {
fn new_iroh_endpoint() -> iroh::Endpoint {
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
iroh::Endpoint::builder()
.discovery(iroh::discovery::pkarr::PkarrPublisher::n0_dns())
.discovery(iroh::discovery::dns::DnsDiscovery::n0_dns())
.discovery(iroh::discovery::mdns::MdnsDiscovery::builder())
.alpns(vec![crate::APNS_IDENTITY.into()])
.bind()
.await
.expect("failed to create iroh Endpoint")
})
})
}
static IROH_ENDPOINT: std::sync::Mutex<Option<(iroh::Endpoint, tokio::task::JoinHandle<()>)>> =
std::sync::Mutex::new(None);
{
let guard = IROH_ENDPOINT.lock().unwrap();
if let Some((ep, sentinel)) = guard.as_ref()
&& !sentinel.is_finished()
{
return ep.clone();
}
}
let ep = new_iroh_endpoint();
let sentinel = tokio::spawn(std::future::pending::<()>());
let mut guard = IROH_ENDPOINT.lock().unwrap();
*guard = Some((ep.clone(), sentinel));
ep
}