mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-15 02:05:37 +02:00
⭐ feat: Migrate from WebSocket to libp2p for peer-to-peer connectivity (#286)
## Description Whew, some stuff is still not re-implemented, but it's working! Rabbit's gonna explode with the amount of changes I reckon 😅 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a peer-to-peer relay system using libp2p with enhanced stream forwarding, room state synchronization, and mDNS peer discovery. - Added decentralized room and participant management, metrics publishing, and safe, size-limited, concurrent message streaming with robust framing and callback dispatching. - Implemented asynchronous, callback-driven message handling over custom libp2p streams replacing WebSocket signaling. - **Improvements** - Migrated signaling and stream protocols from WebSocket to libp2p, improving reliability and scalability. - Simplified configuration and environment variables, removing deprecated flags and adding persistent data support. - Enhanced logging, error handling, and connection management for better observability and robustness. - Refined RTP header extension registration and NAT IP handling for improved WebRTC performance. - **Bug Fixes** - Improved ICE candidate buffering and SDP negotiation in WebRTC connections. - Fixed NAT IP and UDP port range configuration issues. - **Refactor** - Modularized codebase, reorganized relay and server logic, and removed deprecated WebSocket-based components. - Streamlined message structures, removed obsolete enums and message types, and simplified SafeMap concurrency. - Replaced WebSocket signaling with libp2p stream protocols in server and relay components. - **Chores** - Updated and cleaned dependencies across Go, Rust, and JavaScript packages. - Added `.gitignore` for persistent data directory in relay package. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: DatCaptainHorse <DatCaptainHorse@users.noreply.github.com> Co-authored-by: Philipp Neumann <3daquawolf@gmail.com>
This commit is contained in:
committed by
GitHub
parent
e67a8d2b32
commit
6e82eff9e2
131
packages/server/src/p2p/p2p.rs
Normal file
131
packages/server/src/p2p/p2p.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
use futures_util::StreamExt;
|
||||
use libp2p::multiaddr::Protocol;
|
||||
use libp2p::{
|
||||
Multiaddr, PeerId, Swarm, identify, noise, ping,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux,
|
||||
};
|
||||
use std::error::Error;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct NestriConnection {
|
||||
pub peer_id: PeerId,
|
||||
pub control: libp2p_stream::Control,
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
struct NestriBehaviour {
|
||||
identify: identify::Behaviour,
|
||||
ping: ping::Behaviour,
|
||||
stream: libp2p_stream::Behaviour,
|
||||
}
|
||||
|
||||
pub struct NestriP2P {
|
||||
swarm: Arc<Mutex<Swarm<NestriBehaviour>>>,
|
||||
}
|
||||
impl NestriP2P {
|
||||
pub async fn new() -> Result<Self, Box<dyn Error>> {
|
||||
let swarm = Arc::new(Mutex::new(
|
||||
libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_dns()?
|
||||
.with_behaviour(|key| {
|
||||
let identify_behaviour = identify::Behaviour::new(identify::Config::new(
|
||||
"/ipfs/id/1.0.0".to_string(),
|
||||
key.public(),
|
||||
));
|
||||
let ping_behaviour = ping::Behaviour::default();
|
||||
let stream_behaviour = libp2p_stream::Behaviour::default();
|
||||
|
||||
Ok(NestriBehaviour {
|
||||
identify: identify_behaviour,
|
||||
ping: ping_behaviour,
|
||||
stream: stream_behaviour,
|
||||
})
|
||||
})?
|
||||
.build(),
|
||||
));
|
||||
|
||||
// Spawn the swarm event loop
|
||||
let swarm_clone = swarm.clone();
|
||||
tokio::spawn(swarm_loop(swarm_clone));
|
||||
|
||||
{
|
||||
let mut swarm_lock = swarm.lock().await;
|
||||
swarm_lock.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?; // IPv4 - TCP Raw
|
||||
swarm_lock.listen_on("/ip6/::/tcp/0".parse()?)?; // IPv6 - TCP Raw
|
||||
}
|
||||
|
||||
Ok(NestriP2P { swarm })
|
||||
}
|
||||
|
||||
pub async fn connect(&self, conn_url: &str) -> Result<NestriConnection, Box<dyn Error>> {
|
||||
let conn_addr: Multiaddr = conn_url.parse()?;
|
||||
|
||||
let mut swarm_lock = self.swarm.lock().await;
|
||||
swarm_lock.dial(conn_addr.clone())?;
|
||||
|
||||
let Some(Protocol::P2p(peer_id)) = conn_addr.clone().iter().last() else {
|
||||
return Err("Invalid connection URL: missing peer ID".into());
|
||||
};
|
||||
|
||||
Ok(NestriConnection {
|
||||
peer_id,
|
||||
control: swarm_lock.behaviour().stream.new_control(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async fn swarm_loop(swarm: Arc<Mutex<Swarm<NestriBehaviour>>>) {
|
||||
loop {
|
||||
let event = {
|
||||
let mut swarm_lock = swarm.lock().await;
|
||||
swarm_lock.select_next_some().await
|
||||
};
|
||||
match event {
|
||||
SwarmEvent::NewListenAddr { address, .. } => {
|
||||
tracing::info!("Listening on: '{}'", address);
|
||||
}
|
||||
SwarmEvent::ConnectionEstablished { peer_id, .. } => {
|
||||
tracing::info!("Connection established with peer: {}", peer_id);
|
||||
}
|
||||
SwarmEvent::ConnectionClosed { peer_id, cause, .. } => {
|
||||
if let Some(err) = cause {
|
||||
tracing::error!(
|
||||
"Connection with peer {} closed due to error: {}",
|
||||
peer_id,
|
||||
err
|
||||
);
|
||||
} else {
|
||||
tracing::info!("Connection with peer {} closed", peer_id);
|
||||
}
|
||||
}
|
||||
SwarmEvent::IncomingConnection {
|
||||
local_addr,
|
||||
send_back_addr,
|
||||
..
|
||||
} => {
|
||||
tracing::info!(
|
||||
"Incoming connection from: {} (send back to: {})",
|
||||
local_addr,
|
||||
send_back_addr
|
||||
);
|
||||
}
|
||||
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
|
||||
if let Some(peer_id) = peer_id {
|
||||
tracing::error!("Failed to connect to peer {}: {}", peer_id, error);
|
||||
} else {
|
||||
tracing::error!("Failed to connect: {}", error);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
149
packages/server/src/p2p/p2p_protocol_stream.rs
Normal file
149
packages/server/src/p2p/p2p_protocol_stream.rs
Normal file
@@ -0,0 +1,149 @@
|
||||
use crate::p2p::p2p::NestriConnection;
|
||||
use crate::p2p::p2p_safestream::SafeStream;
|
||||
use libp2p::StreamProtocol;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
// Cloneable callback type
|
||||
pub type CallbackInner = dyn Fn(Vec<u8>) + Send + Sync + 'static;
|
||||
pub struct Callback(Arc<CallbackInner>);
|
||||
impl Callback {
|
||||
pub fn new<F>(f: F) -> Self
|
||||
where
|
||||
F: Fn(Vec<u8>) + Send + Sync + 'static,
|
||||
{
|
||||
Callback(Arc::new(f))
|
||||
}
|
||||
|
||||
pub fn call(&self, data: Vec<u8>) {
|
||||
self.0(data)
|
||||
}
|
||||
}
|
||||
impl Clone for Callback {
|
||||
fn clone(&self) -> Self {
|
||||
Callback(Arc::clone(&self.0))
|
||||
}
|
||||
}
|
||||
impl From<Box<CallbackInner>> for Callback {
|
||||
fn from(boxed: Box<CallbackInner>) -> Self {
|
||||
Callback(Arc::from(boxed))
|
||||
}
|
||||
}
|
||||
|
||||
/// NestriStreamProtocol manages the stream protocol for Nestri connections.
|
||||
pub struct NestriStreamProtocol {
|
||||
tx: mpsc::Sender<Vec<u8>>,
|
||||
safe_stream: Arc<SafeStream>,
|
||||
callbacks: Arc<RwLock<HashMap<String, Callback>>>,
|
||||
}
|
||||
impl NestriStreamProtocol {
|
||||
const NESTRI_PROTOCOL_STREAM_PUSH: StreamProtocol =
|
||||
StreamProtocol::new("/nestri-relay/stream-push/1.0.0");
|
||||
|
||||
pub async fn new(
|
||||
nestri_connection: NestriConnection,
|
||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let mut nestri_connection = nestri_connection.clone();
|
||||
let push_stream = match nestri_connection
|
||||
.control
|
||||
.open_stream(nestri_connection.peer_id, Self::NESTRI_PROTOCOL_STREAM_PUSH)
|
||||
.await
|
||||
{
|
||||
Ok(stream) => stream,
|
||||
Err(e) => {
|
||||
return Err(Box::new(e));
|
||||
}
|
||||
};
|
||||
|
||||
let (tx, rx) = mpsc::channel(1000);
|
||||
|
||||
let sp = NestriStreamProtocol {
|
||||
tx,
|
||||
safe_stream: Arc::new(SafeStream::new(push_stream)),
|
||||
callbacks: Arc::new(RwLock::new(HashMap::new())),
|
||||
};
|
||||
|
||||
// Spawn the loops
|
||||
sp.spawn_read_loop();
|
||||
sp.spawn_write_loop(rx);
|
||||
|
||||
Ok(sp)
|
||||
}
|
||||
|
||||
fn spawn_read_loop(&self) -> tokio::task::JoinHandle<()> {
|
||||
let safe_stream = self.safe_stream.clone();
|
||||
let callbacks = self.callbacks.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
let data = {
|
||||
match safe_stream.receive_raw().await {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
tracing::error!("Error receiving data: {}", e);
|
||||
break; // Exit the loop on error
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
match serde_json::from_slice::<crate::messages::MessageBase>(&data) {
|
||||
Ok(base_message) => {
|
||||
let response_type = base_message.payload_type;
|
||||
let callback = {
|
||||
let callbacks_lock = callbacks.read().unwrap();
|
||||
callbacks_lock.get(&response_type).cloned()
|
||||
};
|
||||
|
||||
if let Some(callback) = callback {
|
||||
// Call the registered callback with the raw data
|
||||
callback.call(data);
|
||||
} else {
|
||||
tracing::warn!(
|
||||
"No callback registered for response type: {}",
|
||||
response_type
|
||||
);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to decode message: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn spawn_write_loop(&self, mut rx: mpsc::Receiver<Vec<u8>>) -> tokio::task::JoinHandle<()> {
|
||||
let safe_stream = self.safe_stream.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
// Wait for a message from the channel
|
||||
if let Some(tx_data) = rx.recv().await {
|
||||
if let Err(e) = safe_stream.send_raw(&tx_data).await {
|
||||
tracing::error!("Error sending data: {:?}", e);
|
||||
}
|
||||
} else {
|
||||
tracing::info!("Receiver closed, exiting write loop");
|
||||
break;
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn send_message<M: serde::Serialize>(
|
||||
&self,
|
||||
message: &M,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let json_data = serde_json::to_vec(message)?;
|
||||
self.tx.try_send(json_data)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Register a callback for a specific response type
|
||||
pub fn register_callback<F>(&self, response_type: &str, callback: F)
|
||||
where
|
||||
F: Fn(Vec<u8>) + Send + Sync + 'static,
|
||||
{
|
||||
let mut callbacks_lock = self.callbacks.write().unwrap();
|
||||
callbacks_lock.insert(response_type.to_string(), Callback::new(callback));
|
||||
}
|
||||
}
|
||||
105
packages/server/src/p2p/p2p_safestream.rs
Normal file
105
packages/server/src/p2p/p2p_safestream.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
use byteorder::{BigEndian, ByteOrder};
|
||||
use futures_util::io::{ReadHalf, WriteHalf};
|
||||
use futures_util::{AsyncReadExt, AsyncWriteExt};
|
||||
use prost::Message;
|
||||
use serde::Serialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
const MAX_SIZE: usize = 1024 * 1024; // 1MB
|
||||
|
||||
pub struct SafeStream {
|
||||
stream_read: Arc<Mutex<ReadHalf<libp2p::Stream>>>,
|
||||
stream_write: Arc<Mutex<WriteHalf<libp2p::Stream>>>,
|
||||
}
|
||||
impl SafeStream {
|
||||
pub fn new(stream: libp2p::Stream) -> Self {
|
||||
let (read, write) = stream.split();
|
||||
SafeStream {
|
||||
stream_read: Arc::new(Mutex::new(read)),
|
||||
stream_write: Arc::new(Mutex::new(write)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_json<T: Serialize>(
|
||||
&self,
|
||||
data: &T,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let json_data = serde_json::to_vec(data)?;
|
||||
tracing::info!("Sending JSON");
|
||||
let e = self.send_with_length_prefix(&json_data).await;
|
||||
tracing::info!("Sent JSON");
|
||||
e
|
||||
}
|
||||
|
||||
pub async fn receive_json<T: DeserializeOwned>(&self) -> Result<T, Box<dyn std::error::Error>> {
|
||||
let data = self.receive_with_length_prefix().await?;
|
||||
let msg = serde_json::from_slice(&data)?;
|
||||
Ok(msg)
|
||||
}
|
||||
|
||||
pub async fn send_proto<M: Message>(&self, msg: &M) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut proto_data = Vec::new();
|
||||
msg.encode(&mut proto_data)?;
|
||||
self.send_with_length_prefix(&proto_data).await
|
||||
}
|
||||
|
||||
pub async fn receive_proto<M: Message + Default>(
|
||||
&self,
|
||||
) -> Result<M, Box<dyn std::error::Error>> {
|
||||
let data = self.receive_with_length_prefix().await?;
|
||||
let msg = M::decode(&*data)?;
|
||||
Ok(msg)
|
||||
}
|
||||
|
||||
pub async fn send_raw(&self, data: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
self.send_with_length_prefix(data).await
|
||||
}
|
||||
|
||||
pub async fn receive_raw(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
|
||||
self.receive_with_length_prefix().await
|
||||
}
|
||||
|
||||
async fn send_with_length_prefix(&self, data: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if data.len() > MAX_SIZE {
|
||||
return Err(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"Data exceeds maximum size",
|
||||
)));
|
||||
}
|
||||
|
||||
let mut stream_write = self.stream_write.lock().await;
|
||||
|
||||
// Write the 4-byte length prefix
|
||||
let mut length_prefix = [0u8; 4];
|
||||
BigEndian::write_u32(&mut length_prefix, data.len() as u32);
|
||||
stream_write.write_all(&length_prefix).await?;
|
||||
|
||||
// Write the actual data
|
||||
stream_write.write_all(data).await?;
|
||||
stream_write.flush().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn receive_with_length_prefix(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
|
||||
let mut stream_read = self.stream_read.lock().await;
|
||||
|
||||
// Read the 4-byte length prefix
|
||||
let mut length_prefix = [0u8; 4];
|
||||
stream_read.read_exact(&mut length_prefix).await?;
|
||||
let length = BigEndian::read_u32(&length_prefix) as usize;
|
||||
|
||||
if length > MAX_SIZE {
|
||||
return Err(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"Data exceeds maximum size",
|
||||
)));
|
||||
}
|
||||
|
||||
// Read the actual data
|
||||
let mut buffer = vec![0; length];
|
||||
stream_read.read_exact(&mut buffer).await?;
|
||||
Ok(buffer)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user