From b7ff0d1ffe707d593584db7fb32283d494f40255 Mon Sep 17 00:00:00 2001 From: Mark Lodato Date: Fri, 19 Jun 2020 15:38:11 -0400 Subject: [PATCH] Add bare-bones WS server for autobahn testing (#331) --- Cargo.toml | 1 + websocket-autobahn/Cargo.toml | 16 ++++++ websocket-autobahn/README.md | 33 ++++++++++++ websocket-autobahn/config/fuzzingclient.json | 15 ++++++ websocket-autobahn/reports/.gitignore | 2 + websocket-autobahn/src/main.rs | 55 ++++++++++++++++++++ 6 files changed, 122 insertions(+) create mode 100644 websocket-autobahn/Cargo.toml create mode 100644 websocket-autobahn/README.md create mode 100644 websocket-autobahn/config/fuzzingclient.json create mode 100644 websocket-autobahn/reports/.gitignore create mode 100644 websocket-autobahn/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index b669c145..cb65c0fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ members = [ "unix-socket", "web-cors/backend", "websocket", + "websocket-autobahn", "websocket-chat", "websocket-chat-broker", "websocket-tcp-chat", diff --git a/websocket-autobahn/Cargo.toml b/websocket-autobahn/Cargo.toml new file mode 100644 index 00000000..04111d71 --- /dev/null +++ b/websocket-autobahn/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "websocket-autobahn" +version = "2.0.0" +authors = ["Mark Lodato Result { + ws::start(WebSocket::new(), &r, stream) +} + +struct WebSocket {} + +impl Actor for WebSocket { + type Context = ws::WebsocketContext; + + fn started(&mut self, _ctx: &mut Self::Context) {} +} + +impl StreamHandler> for WebSocket { + fn handle( + &mut self, + msg: Result, + ctx: &mut Self::Context, + ) { + if let Ok(msg) = msg { + match msg { + ws::Message::Text(text) => ctx.text(text), + ws::Message::Binary(bin) => ctx.binary(bin), + ws::Message::Ping(bytes) => ctx.pong(&bytes), + _ => {} + } + } else { + ctx.stop(); + } + } +} + +impl WebSocket { + fn new() -> Self { + Self {} + } +} + +#[actix_rt::main] +async fn main() -> std::io::Result<()> { + std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info"); + env_logger::init(); + + HttpServer::new(|| { + App::new() + .wrap(middleware::Logger::default()) + .service(web::resource("/").route(web::get().to(ws_index))) + }) + .bind("127.0.0.1:9001")? + .run() + .await +}