//! 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> { // 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 { 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(()) }