Skip to content

feat: implement fastn-p2p daemon MVP #9

@amitu

Description

@amitu

🚀 MVP Proposal: fastn-p2p Daemon

This issue tracks implementation of a centralized P2P daemon that addresses security, performance, and resource management issues with the current direct P2P approach.

Requirements Summary

  • Security: Eliminate secret key sharing with clients
  • Performance: Reduce P2P connection overhead through reuse
  • Resource Management: Avoid port conflicts from multiple CLI instances
  • Architecture: Simple Unix domain socket communication (no WASM for MVP)

Architecture Overview

┌─────────────────┐    Unix Domain Socket    ┌──────────────────┐
│   CLI Client    │◄─────────────────────────►│   fastn-daemon   │
│   (no secrets)  │                           │   (has secrets)  │
└─────────────────┘                           └──────────────────┘
                                                       │
                                                       ▼
                                              ┌──────────────────┐
                                              │   P2P Network    │
                                              │ (persistent)     │
                                              └──────────────────┘

Core Components

1. fastn-daemon Binary

  • Single background process per user
  • Exclusive secret key access
  • Persistent P2P connection management
  • Unix socket listener (~/.fastn/daemon.sock)
  • Built-in protocol handlers (compiled Rust, not WASM)

2. Client CLI

  • Zero secret key access
  • Unix socket communication only
  • JSON + streaming protocol
  • Backward compatible commands

3. Unix Socket Protocol

Request/Response (JSON):

// Request
{
  "id": "uuid",
  "type": "call",
  "target": "peer_id52",  
  "protocol": "Echo",
  "data": {"message": "hello"}
}

// Response  
{
  "id": "uuid",
  "status": "ok|error",
  "data": {...} | "error_msg"
}

Bidirectional Streaming:

// Stream request
{
  "id": "uuid",
  "type": "stream",
  "target": "peer_id52",
  "protocol": "Shell",
  "data": {"command": "interactive_shell"}
}

// Stream established response
{"id": "uuid", "status": "streaming"}

// Then raw bidirectional data flows over the Unix socket

MVP Scope

✅ Included

  • Basic daemon process with socket listener
  • Request/response pattern support
  • Simple bidirectional streaming (shell, file transfer)
  • Built-in protocols from examples/ (Echo, Shell, FileTransfer)
  • P2P connection pooling and reuse
  • Simple authentication via Unix socket permissions
  • Daemon management commands

❌ Not in MVP

  • WASM plugin system
  • iptables integration
  • Complex security sandboxing
  • Auto-installation features
  • Complex streaming protocols (focus on simple patterns)

Implementation Phases

Phase 1: Basic Daemon

  • Create fastn-daemon binary with Unix socket listener
  • Implement JSON protocol parser
  • Add built-in Echo protocol handler
  • Basic daemon lifecycle management

Phase 2: Streaming Support

  • Add bidirectional streaming over Unix socket
  • Implement simple Shell protocol handler
  • Handle stream lifecycle (setup, data flow, cleanup)

Phase 3: Client Integration

  • Modify CLI to detect and communicate with daemon
  • Support both request/response and streaming modes
  • Graceful fallback to direct P2P when daemon unavailable
  • Add daemon control commands (fastn daemon start/stop/status)

Phase 4: Protocol Expansion

  • Port remaining examples/ protocols to daemon handlers
  • Implement connection pooling for target peers
  • Error handling and basic logging

Benefits

  • Security: Secret keys isolated in daemon process
  • Performance: Connection reuse eliminates repeated handshakes
  • Flexibility: Both request/response and streaming patterns
  • Simplicity: Unix sockets + simple protocols
  • Compatibility: Existing CLI commands continue working

This MVP delivers secure, performant P2P communication through centralized connection management with support for both simple request/response and bidirectional streaming patterns.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions