|
| 1 | +use std::net::TcpListener; |
| 2 | +use std::path::Path; |
| 3 | +use std::sync::Arc; |
| 4 | +use std::thread::spawn; |
| 5 | + |
| 6 | +use rustls::pki_types::pem::PemObject; |
| 7 | +use rustls::pki_types::{CertificateDer, PrivateKeyDer}; |
| 8 | +use rustls::{ServerConfig, ServerConnection, StreamOwned}; |
| 9 | +use tungstenite::accept; |
| 10 | + |
| 11 | +/// A WebSocket echo server over TLS (wss://) |
| 12 | +fn main() { |
| 13 | + // Use fixed relative paths from this crate to the repo certs |
| 14 | + let cert_path = Path::new("../../certs/localhost.pem"); |
| 15 | + let key_path = Path::new("../../certs/localhost-key.pem"); |
| 16 | + |
| 17 | + eprintln!("Using certificate: {}", cert_path.display()); |
| 18 | + eprintln!("Using private key: {}", key_path.display()); |
| 19 | + |
| 20 | + let cert = CertificateDer::from_pem_file(cert_path).expect("load certs"); |
| 21 | + let key = PrivateKeyDer::from_pem_file(key_path).expect("load private key"); |
| 22 | + |
| 23 | + // Build rustls server config (no client auth) |
| 24 | + let config = ServerConfig::builder() |
| 25 | + .with_no_client_auth() |
| 26 | + .with_single_cert(vec![cert], key) |
| 27 | + .expect("invalid cert/key"); |
| 28 | + let config = Arc::new(config); |
| 29 | + |
| 30 | + let listener = TcpListener::bind("127.0.0.1:8000").expect("bind 127.0.0.1:8000"); |
| 31 | + eprintln!("tungstenite server listening on wss://127.0.0.1:8000"); |
| 32 | + |
| 33 | + for stream in listener.incoming() { |
| 34 | + match stream { |
| 35 | + Ok(stream) => { |
| 36 | + eprintln!( |
| 37 | + "incoming TCP connection from {}", |
| 38 | + stream.peer_addr().unwrap() |
| 39 | + ); |
| 40 | + let cfg = Arc::clone(&config); |
| 41 | + spawn(move || { |
| 42 | + // Wrap TCP in a rustls TLS stream. |
| 43 | + let conn = match ServerConnection::new(cfg) { |
| 44 | + Ok(c) => c, |
| 45 | + Err(e) => { |
| 46 | + eprintln!("TLS ServerConnection error: {e}"); |
| 47 | + return; |
| 48 | + } |
| 49 | + }; |
| 50 | + let tls_stream = StreamOwned::new(conn, stream); |
| 51 | + |
| 52 | + match accept(tls_stream) { |
| 53 | + Ok(mut websocket) => loop { |
| 54 | + // Send message so we can test the server performance |
| 55 | + for i in (10..=510).step_by(10) { |
| 56 | + for j in (10..=510).step_by(10) { |
| 57 | + let message = format!("{j},{i}"); |
| 58 | + if let Err(e) = websocket.send(message.into()) { |
| 59 | + eprintln!("send error: {e}"); |
| 60 | + break; |
| 61 | + } |
| 62 | + std::thread::sleep(std::time::Duration::from_millis(1)); |
| 63 | + } |
| 64 | + } |
| 65 | + // break after one batch for demo purposes |
| 66 | + eprintln!("Completed one batch of messages, closing connection."); |
| 67 | + break; |
| 68 | + }, |
| 69 | + Err(e) => { |
| 70 | + eprintln!("websocket handshake failed: {e}"); |
| 71 | + eprintln!("Hint: Ensure your client connects with wss://localhost:8000 and trusts the local certificate in certs/."); |
| 72 | + } |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + Err(e) => eprintln!("incoming connection error: {e}"), |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments