mirror of
https://github.com/nestriness/nestri.git
synced 2026-09-26 20:42:25 +03:00
fix: wrong neshub version, formatting
This commit is contained in:
@@ -1,18 +1,7 @@
|
||||
//! The ticket: how a client finds this box.
|
||||
//!
|
||||
//! neshub makes one per boot and serves it on a Unix socket. It listens and
|
||||
//! nesinit dials, the same way every other socket here works -- so there is no
|
||||
//! race against a process that has not started yet, and no file left on disk
|
||||
//! holding a live address after the box is gone.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use anyhow::Result;
|
||||
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
|
||||
use iroh::EndpointAddr;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::net::UnixListener;
|
||||
|
||||
/// A Nestri connection ticket.
|
||||
///
|
||||
@@ -64,39 +53,6 @@ pub fn generate_stream_name() -> String {
|
||||
format!("stream-{}", uuid::Uuid::now_v7().as_simple())
|
||||
}
|
||||
|
||||
/// Serve the ticket to anything that dials, forever.
|
||||
///
|
||||
/// Returns once the socket is bound, not when someone connects: the caller has
|
||||
/// an endpoint to run and a reader that never arrives is not a reason to refuse
|
||||
/// clients. Every connection gets the ticket and a newline, then is dropped --
|
||||
/// there is nothing to say afterwards, and a reader blocked on more would hang.
|
||||
pub fn serve(path: &Path, ticket: String) -> Result<()> {
|
||||
// A stale socket file makes bind fail with EADDRINUSE, which reads as
|
||||
// "something is already listening" when nothing is.
|
||||
let _ = std::fs::remove_file(path);
|
||||
let listener = UnixListener::bind(path)
|
||||
.with_context(|| format!("could not listen on {}", path.display()))?;
|
||||
tracing::info!("serving the ticket on {}", path.display());
|
||||
|
||||
tokio::spawn(async move {
|
||||
let line = format!("{ticket}\n");
|
||||
loop {
|
||||
match listener.accept().await {
|
||||
Ok((mut stream, _)) => {
|
||||
if let Err(e) = stream.write_all(line.as_bytes()).await {
|
||||
tracing::warn!("could not hand over the ticket: {e}");
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!("accepting a ticket connection failed: {e}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user