circles/examples/wss_demo/wss_server.rs
2025-07-08 22:49:47 +02:00

91 lines
3.5 KiB
Rust

//! WSS Server Demo
//!
//! This example demonstrates a complete WSS (WebSocket Secure) server with:
//! - TLS encryption using self-signed certificates
//! - secp256k1 authentication
//! - JSON-RPC protocol support
//! - Comprehensive logging and error handling
//!
//! Usage: cargo run --example wss_server --features auth
use circle_ws_lib::{ServerConfig, spawn_circle_server};
use log::{info, warn, error};
use std::time::Duration;
use tokio::time::sleep;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logging
env_logger::init();
info!("🚀 Starting WSS Server Demo");
info!("🔐 This demo includes TLS encryption and secp256k1 authentication");
info!("");
// Create server configuration with TLS and authentication enabled
let config = ServerConfig::new(
"127.0.0.1".to_string(),
8080, // Regular WebSocket port
"redis://127.0.0.1:6379".to_string(),
)
.with_tls(
"../../src/server/examples/wss_demo/cert.pem".to_string(),
"../../src/server/examples/wss_demo/key.pem".to_string()
)
.with_tls_port(8443) // Secure WebSocket port
.with_auth(); // Enable secp256k1 authentication
info!("📋 Server Configuration:");
info!(" Host: {}", config.host);
info!(" Regular WS Port: {}", config.port);
info!(" WSS Port: {}", config.get_tls_port());
info!(" TLS Enabled: {}", config.enable_tls);
info!(" Auth Enabled: {}", config.enable_auth);
info!(" Certificate: {:?}", config.cert_path);
info!(" Private Key: {:?}", config.key_path);
info!("");
// Start the server
let (join_handle, _server_handle) = spawn_circle_server(config)
.map_err(|e| -> Box<dyn std::error::Error> {
error!("❌ Failed to start WSS server: {}", e);
Box::new(e)
})?;
info!("✅ WSS Server started successfully!");
info!("");
info!("🔗 Connection URLs:");
info!(" 🔒 Secure WebSocket: wss://127.0.0.1:8443/ws");
info!(" 🔓 Regular WebSocket: ws://127.0.0.1:8080/ws");
info!("");
info!("🛡️ Authentication: secp256k1 signatures required for 'play' commands");
info!("🔓 Public methods: 'fetch_nonce' (no auth required)");
info!("");
info!("📝 Example JSON-RPC requests:");
info!(" 1. Fetch nonce (no auth):");
info!(" {{\"jsonrpc\":\"2.0\",\"method\":\"fetch_nonce\",\"params\":{{\"pubkey\":\"your_pubkey\"}},\"id\":1}}");
info!("");
info!(" 2. Authenticate:");
info!(" {{\"jsonrpc\":\"2.0\",\"method\":\"authenticate\",\"params\":{{\"pubkey\":\"your_pubkey\",\"signature\":\"signed_nonce\"}},\"id\":2}}");
info!("");
info!(" 3. Execute script (requires auth):");
info!(" {{\"jsonrpc\":\"2.0\",\"method\":\"play\",\"params\":{{\"script\":\"40 + 2\"}},\"id\":3}}");
info!("");
info!("🧪 Test with the WSS client:");
info!(" cargo run --example wss_client");
info!("");
info!("🌐 Test with browser (open console):");
info!(" const ws = new WebSocket('wss://127.0.0.1:8443/ws');");
info!(" ws.onopen = () => ws.send(JSON.stringify({{jsonrpc:'2.0',method:'fetch_nonce',params:{{pubkey:'test'}},id:1}}));");
info!(" ws.onmessage = (e) => console.log(JSON.parse(e.data));");
info!("");
info!("⚠️ Note: Browser may show certificate warning (self-signed cert)");
info!("");
info!("🔄 Server running... Press Ctrl+C to stop");
// Keep server running until interrupted
let _ = join_handle.await;
info!("🛑 WSS Server stopped");
Ok(())
}