diff --git a/server/Cargo.toml b/server/Cargo.toml index 538e0df..9a8a330 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -12,7 +12,6 @@ hyper-tungstenite = "0.14.0" rustls = "0.23.5" rustls-pemfile = "2.1.2" webpki-roots = "0.26" -opaque-ke = "2.0.0" hyper = { version = "1.4.1", features = ["full"] } hyper-util = { version = "0.1.7", features = ["tokio"] } http-body-util = "0.1.2" diff --git a/server/src/main.rs b/server/src/main.rs index 509db5c..0cdea50 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,8 +1,6 @@ use std::net::SocketAddr; use std::path::Path; -use bus::Bus; - mod config; mod util; mod app; @@ -24,19 +22,11 @@ use system::{ }; use protocol::QRPacket; +#[derive(Clone)] struct HttpServiceArgs { tx:Sender, cache:WebCache, } -impl HttpServiceArgs { - pub async fn clone(&mut self) -> Self - { - Self { - tx:self.tx.clone(), - cache:self.cache.clone(), - } - } -} async fn service_http(mut request:hyper::Request, args:HttpServiceArgs) -> Result>, std::convert::Infallible> // Serve cached files and upgrade websocket connections. @@ -120,7 +110,7 @@ async fn service_http(mut request:hyper::Request, args:Ht } } -async fn handle_http(stream:S, addr:SocketAddr, mut args:HttpServiceArgs) -> Result<(),()> +async fn handle_http(stream:S, addr:SocketAddr, args:HttpServiceArgs) -> Result<(),()> where S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static // Hand off socket connection to Hyper server. // @@ -132,10 +122,9 @@ where S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static let io = TokioIo::new(stream); - let args = args.clone().await; let conn = http1::Builder::new() .serve_connection(io, service_fn(move |req| { - service_http(req, args) + service_http(req, args.clone()) })); conn.with_upgrades().await.ok(); @@ -207,123 +196,111 @@ async fn main() } js_asset_data += "}};"; - match b_main.connect().await { - Ok(mut bus) => { + /* + ** Cache source files. + */ + let cache = WebCache::new(); + cache.cache("text/html", "/.html", &[ + WebCache::file("www/.html"), + ]).ok(); + cache.cache_whitespace_minimize("/.html").ok(); + cache.cache("text/css", "/.css", &[ + WebCache::file("www/css/main.css"), + WebCache::file("www/css/ui.css"), + WebCache::file("www/css/form.css"), + WebCache::file("www/css/game.css"), + WebCache::file("www/css/text.css"), + WebCache::file("www/css/profile.css"), + WebCache::file("www/css/util.css"), + ]).ok(); + cache.cache("text/javascript", "/.js", &[ + WebCache::file("www/js/const.js"), + WebCache::file("www/js/language.js"), + WebCache::file("www/js/util.js"), + WebCache::file("www/js/badge.js"), + WebCache::file("www/js/game_asset.js"), + WebCache::string(&js_asset_data), + WebCache::file("www/js/game.js"), + WebCache::file("www/js/interface.js"), + WebCache::file("www/js/ui.js"), + WebCache::file("www/js/scene.js"), + WebCache::file("www/js/system.js"), + WebCache::file("www/js/main.js"), + ]).ok(); + cache.cache("image/png", "/favicon.png", &[ + WebCache::file("www/asset/favicon.png"), + ]).ok(); + cache.cache("image/png", "/favicon_notify.png", &[ + WebCache::file("www/asset/favicon_notify.png"), + ]).ok(); - /* - ** Cache source files. - */ - let cache = WebCache::new(); - cache.cache("text/html", "/.html", &[ - WebCache::file("www/.html"), - ]).ok(); - cache.cache_whitespace_minimize("/.html").ok(); - cache.cache("text/css", "/.css", &[ - WebCache::file("www/css/main.css"), - WebCache::file("www/css/ui.css"), - WebCache::file("www/css/form.css"), - WebCache::file("www/css/game.css"), - WebCache::file("www/css/text.css"), - WebCache::file("www/css/profile.css"), - WebCache::file("www/css/util.css"), - ]).ok(); - cache.cache("text/javascript", "/.js", &[ - WebCache::file("www/js/const.js"), - WebCache::file("www/js/language.js"), - WebCache::file("www/js/util.js"), - WebCache::file("www/js/badge.js"), - WebCache::file("www/js/game_asset.js"), - WebCache::string(&js_asset_data), - WebCache::file("www/js/game.js"), - WebCache::file("www/js/interface.js"), - WebCache::file("www/js/ui.js"), - WebCache::file("www/js/scene.js"), - WebCache::file("www/js/system.js"), - WebCache::file("www/js/main.js"), - ]).ok(); - cache.cache("image/png", "/favicon.png", &[ - WebCache::file("www/asset/favicon.png"), - ]).ok(); - cache.cache("image/png", "/favicon_notify.png", &[ - WebCache::file("www/asset/favicon_notify.png"), - ]).ok(); + let about_path = std::path::Path::new("www/pages/about"); + for doc in [ + "main", + ] { + if cache.cache("text/html", &format!("/about/{}.html", doc), &[ + WebCache::markdown(about_path.join(format!("{}.md", doc))) + ]).is_err() { + println!("error: failed to load: {}", doc); + } + } - let about_path = std::path::Path::new("www/pages/about"); - for doc in [ - "main", - ] { - if cache.cache("text/html", &format!("/about/{}.html", doc), &[ - WebCache::markdown(about_path.join(format!("{}.md", doc))) - ]).is_err() { - println!("error: failed to load: {}", doc); - } - } + let guide_path = std::path::Path::new("www/pages/guide"); + for doc in [ + "game", + "pieces", + "interface", + ] { + if cache.cache("text/html", &format!("/guide/{}.html", doc), &[ + WebCache::markdown(guide_path.join(format!("{}.md", doc))), + ]).is_err() { + println!("error: failed to load: {}", doc); + } + } + - let guide_path = std::path::Path::new("www/pages/guide"); - for doc in [ - "game", - "pieces", - "interface", - ] { - if cache.cache("text/html", &format!("/guide/{}.html", doc), &[ - WebCache::markdown(guide_path.join(format!("{}.md", doc))), - ]).is_err() { - println!("error: failed to load: {}", doc); - } - } - - - /* - ** Initialize network services. - */ - let mut tcp_server = TcpServer::new(); - match tcp_server.bind("127.0.0.1:38611").await { - Ok(_) => { - let mut b = bus.connect().await.unwrap(); - let c = cache.clone(); - let data_tx = data_tx.clone(); - tokio::spawn(async move { - while tcp_server.accept(handle_tcp, HttpServiceArgs { - tx:data_tx, - cache:c.clone(), - }).await.is_ok() { } - }); - } - Err(_) => { - println!("error: failed to bind TCP port 38611."); - } - } - - let mut tls_server = TlsServer::new(); - for domain in [ - "omen.kirisame.com", - "dzura.com", - ] { - if tls_server.add_cert(domain, &format!("cert/{}/fullchain.pem", domain), &format!("cert/{}/privkey.pem", domain)).await.is_err() { - println!("error: failed to load TLS certificates for {}.", domain); - } - } - match tls_server.bind("0.0.0.0:38612").await { - Ok(_) => { - let (tx, rx) = tokio::sync::mpsc::channel::<>(24); - let mut b = bus.connect().await.unwrap(); - let c = cache.clone(); - - let data_tx = data_tx.clone(); - tokio::spawn(async move { - while tls_server.accept(handle_tls, HttpServiceArgs { - tx:data_tx, - cache:c.clone(), - }).await.is_ok() { } - }); - } - Err(_) => { - println!("error: failed to bind TLS port 38612."); - } - } + /* + ** Initialize network services. + */ + let mut tcp_server = TcpServer::new(); + match tcp_server.bind("127.0.0.1:38611").await { + Ok(_) => { + let c = cache.clone(); + let data_tx = data_tx.clone(); + tokio::spawn(async move { + while tcp_server.accept(handle_tcp, HttpServiceArgs { + tx:data_tx.clone(), + cache:c.clone(), + }).await.is_ok() { } + }); } Err(_) => { - println!("error: failed to initialize HTTPS service."); + println!("error: failed to bind TCP port 38611."); + } + } + + let mut tls_server = TlsServer::new(); + for domain in [ + "omen.kirisame.com", + "dzura.com", + ] { + if tls_server.add_cert(domain, &format!("cert/{}/fullchain.pem", domain), &format!("cert/{}/privkey.pem", domain)).await.is_err() { + println!("error: failed to load TLS certificates for {}.", domain); + } + } + match tls_server.bind("0.0.0.0:38612").await { + Ok(_) => { + let c = cache.clone(); + let data_tx = data_tx.clone(); + tokio::spawn(async move { + while tls_server.accept(handle_tls, HttpServiceArgs { + tx:data_tx.clone(), + cache:c.clone(), + }).await.is_ok() { } + }); + } + Err(_) => { + println!("error: failed to bind TLS port 38612."); } } diff --git a/server/src/manager/data.rs b/server/src/manager/data.rs index 4f44216..1789aa8 100644 --- a/server/src/manager/data.rs +++ b/server/src/manager/data.rs @@ -23,8 +23,7 @@ pub async fn thread_system(mut app:App, mut rx:Receiver) let mut send_user_status = Vec::::new(); - while let Some(packet) = rx.recv().await { - let qr = packet.data; + while let Some(qr) = rx.recv().await { let mut user_id = None; let mut context = Context::None; @@ -44,7 +43,7 @@ pub async fn thread_system(mut app:App, mut rx:Receiver) stream, } => { let id = app.connections.add(Connection { - stream: request.stream, + stream, auth: None, context:Context::None, @@ -1053,10 +1052,8 @@ pub async fn thread_system(mut app:App, mut rx:Receiver) } } -fn generate_game_state(app:&App, session:&Session) -> protocol::PacketGameStateResponse +fn generate_game_state(app:&App, session:&Session) -> PacketGameStateResponse { - use protocol::PacketGameStateResponse; - let mut response = PacketGameStateResponse::new(); response.token = session.token; diff --git a/server/src/manager/ws.rs b/server/src/manager/ws.rs index fb88bab..6c2dadd 100644 --- a/server/src/manager/ws.rs +++ b/server/src/manager/ws.rs @@ -11,7 +11,7 @@ use crate::{ HttpServiceArgs, }; -pub async fn handle_ws(ws:WebSocketStream>, mut args:HttpServiceArgs) -> Result<(),()> +pub async fn handle_ws(ws:WebSocketStream>, args:HttpServiceArgs) -> Result<(),()> // Handle websocket connection. // { @@ -21,7 +21,7 @@ pub async fn handle_ws(ws:WebSocketStream>, mut args:HttpServi let conn_id :u32; let (sink, mut stream) = ws.split(); - let (tx, rx) = mpsc::channel::(1); + let (tx, mut rx) = mpsc::channel::(1); // Perform connection handshake with data system. // - Provide system with connection/bus pairing. @@ -30,11 +30,10 @@ pub async fn handle_ws(ws:WebSocketStream>, mut args:HttpServi args.tx.send(QRPacket::new(0, QRPacketData::QConn { tx:tx.clone(), stream:Arc::new(RwLock::new(sink)), - })).await?; + })).await.map_err(|_| ())?; - if let Some(resp) = rx.blocking_recv() { - let qr = &resp.data; - match qr.data { + if let Some(qr) = rx.recv().await { + match &qr.data { QRPacketData::RConn => { conn_id = qr.id; } _ => { return Err(()); } }