mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-12 08:45:38 +02:00
## 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>
174 lines
5.3 KiB
Go
174 lines
5.3 KiB
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log/slog"
|
|
"relay/internal/shared"
|
|
"time"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
)
|
|
|
|
// --- PubSub Message Handlers ---
|
|
|
|
// handleRoomStateMessages processes incoming room state updates from peers.
|
|
func (r *Relay) handleRoomStateMessages(ctx context.Context, sub *pubsub.Subscription) {
|
|
slog.Debug("Starting room state message handler...")
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
slog.Info("Stopping room state message handler")
|
|
return
|
|
default:
|
|
msg, err := sub.Next(ctx)
|
|
if err != nil {
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, pubsub.ErrSubscriptionCancelled) || errors.Is(err, context.DeadlineExceeded) {
|
|
slog.Info("Room state subscription ended", "err", err)
|
|
return
|
|
}
|
|
slog.Error("Error receiving room state message", "err", err)
|
|
time.Sleep(1 * time.Second)
|
|
continue
|
|
}
|
|
if msg.GetFrom() == r.Host.ID() {
|
|
continue
|
|
}
|
|
|
|
var states []shared.RoomInfo
|
|
if err := json.Unmarshal(msg.Data, &states); err != nil {
|
|
slog.Error("Failed to unmarshal room states", "from", msg.GetFrom(), "data_len", len(msg.Data), "err", err)
|
|
continue
|
|
}
|
|
|
|
r.updateMeshRoomStates(msg.GetFrom(), states)
|
|
}
|
|
}
|
|
}
|
|
|
|
// handleRelayMetricsMessages processes incoming status updates from peers.
|
|
func (r *Relay) handleRelayMetricsMessages(ctx context.Context, sub *pubsub.Subscription) {
|
|
slog.Debug("Starting relay metrics message handler...")
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
slog.Info("Stopping relay metrics message handler")
|
|
return
|
|
default:
|
|
msg, err := sub.Next(ctx)
|
|
if err != nil {
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, pubsub.ErrSubscriptionCancelled) || errors.Is(err, context.DeadlineExceeded) {
|
|
slog.Info("Relay metrics subscription ended", "err", err)
|
|
return
|
|
}
|
|
slog.Error("Error receiving relay metrics message", "err", err)
|
|
time.Sleep(1 * time.Second)
|
|
continue
|
|
}
|
|
if msg.GetFrom() == r.Host.ID() {
|
|
continue
|
|
}
|
|
|
|
var info RelayInfo
|
|
if err := json.Unmarshal(msg.Data, &info); err != nil {
|
|
slog.Error("Failed to unmarshal relay status", "from", msg.GetFrom(), "data_len", len(msg.Data), "err", err)
|
|
continue
|
|
}
|
|
if info.ID != msg.GetFrom() {
|
|
slog.Error("Peer ID mismatch in relay status", "expected", info.ID, "actual", msg.GetFrom())
|
|
continue
|
|
}
|
|
r.onPeerStatus(info)
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- State Check Functions ---
|
|
// hasConnectedPeer checks if peer is in map and has a valid connection
|
|
func (r *Relay) hasConnectedPeer(peerID peer.ID) bool {
|
|
if _, ok := r.LocalMeshPeers.Get(peerID); !ok {
|
|
return false
|
|
}
|
|
if r.Host.Network().Connectedness(peerID) != network.Connected {
|
|
slog.Debug("Peer not connected", "peer", peerID)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// --- State Change Functions ---
|
|
|
|
// onPeerStatus updates the status of a peer based on received metrics, adding local perspective
|
|
func (r *Relay) onPeerStatus(recvInfo RelayInfo) {
|
|
r.LocalMeshPeers.Set(recvInfo.ID, &recvInfo)
|
|
}
|
|
|
|
// onPeerConnected is called when a new peer connects to the relay
|
|
func (r *Relay) onPeerConnected(peerID peer.ID) {
|
|
// Add to local peer map
|
|
r.LocalMeshPeers.Set(peerID, &RelayInfo{
|
|
ID: peerID,
|
|
})
|
|
|
|
slog.Info("Peer connected", "peer", peerID)
|
|
|
|
// Trigger immediate state exchange
|
|
go func() {
|
|
if err := r.publishRelayMetrics(context.Background()); err != nil {
|
|
slog.Error("Failed to publish relay metrics on connect", "err", err)
|
|
} else {
|
|
if err = r.publishRoomStates(context.Background()); err != nil {
|
|
slog.Error("Failed to publish room states on connect", "err", err)
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
// onPeerDisconnected marks a peer as disconnected in our status view and removes latency info
|
|
func (r *Relay) onPeerDisconnected(peerID peer.ID) {
|
|
slog.Info("Mesh peer disconnected, deleting from local peer map", "peer", peerID)
|
|
// Remove peer from local mesh peers
|
|
if r.LocalMeshPeers.Has(peerID) {
|
|
r.LocalMeshPeers.Delete(peerID)
|
|
}
|
|
// Remove any rooms associated with this peer
|
|
if r.MeshRooms.Has(peerID.String()) {
|
|
r.MeshRooms.Delete(peerID.String())
|
|
}
|
|
// Remove any latencies associated with this peer
|
|
if r.LocalMeshPeers.Has(peerID) {
|
|
r.LocalMeshPeers.Delete(peerID)
|
|
}
|
|
|
|
// TODO: If any rooms were routed through this peer, handle that case
|
|
}
|
|
|
|
// updateMeshRoomStates merges received room states into the MeshRooms map
|
|
// TODO: Wrap in another type with timestamp or another mechanism to avoid conflicts
|
|
func (r *Relay) updateMeshRoomStates(peerID peer.ID, states []shared.RoomInfo) {
|
|
for _, state := range states {
|
|
if state.OwnerID == r.ID {
|
|
continue
|
|
}
|
|
|
|
// If previously did not exist, but does now, request a connection if participants exist for our room
|
|
existed := r.MeshRooms.Has(state.ID.String())
|
|
if !existed {
|
|
// Request connection to this peer if we have participants in our local room
|
|
if room, ok := r.LocalRooms.Get(state.ID); ok {
|
|
if room.Participants.Len() > 0 {
|
|
slog.Debug("Got new remote room state, we locally have participants for, requesting stream", "room_name", room.Name, "peer", peerID)
|
|
if err := r.StreamProtocol.RequestStream(context.Background(), room, peerID); err != nil {
|
|
slog.Error("Failed to request stream for new remote room state", "room_name", room.Name, "peer", peerID, "err", err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
r.MeshRooms.Set(state.ID.String(), state)
|
|
}
|
|
}
|