diff --git a/server/src/app/mod.rs b/server/src/app/mod.rs index acad229..1c47a1e 100644 --- a/server/src/app/mod.rs +++ b/server/src/app/mod.rs @@ -4,7 +4,12 @@ use sparse::Sparse; use pool::Pool; use trie::Trie; use crate::{ - protocol::QRPacket, system::filesystem::FileSystem, util::Chain + protocol::QRPacket, + system::{ + filesystem::FileSystem, + log::Log, + }, + util::Chain, }; pub mod connection; use connection::Connection; @@ -15,6 +20,7 @@ pub mod context; pub struct App { pub filesystem:FileSystem, + pub log:Log, pub connections:Pool, @@ -106,6 +112,7 @@ impl App { println!("App data ready."); Ok(Self { filesystem:filesystem, + log:Log::new(), connections:Pool::new(), diff --git a/server/src/manager/data.rs b/server/src/manager/data.rs index 2ddb4fa..47be853 100644 --- a/server/src/manager/data.rs +++ b/server/src/manager/data.rs @@ -54,7 +54,7 @@ pub async fn thread_system(mut app:App, bus:Bus) conn.next = id as u32; } - println!("Connect: {}", id); + app.log.log(&format!("Connect: {}", id)); bus.send( packet.from, @@ -65,7 +65,7 @@ pub async fn thread_system(mut app:App, bus:Bus) } QRPacketData::QDisconn => { - println!("Disconnect: {}", qr.id); + app.log.log(&format!("Disconnect: {}", qr.id)); // Uninitialize connection if if let Some(conn) = app.connections.get(qr.id as usize).cloned() { @@ -139,7 +139,7 @@ pub async fn thread_system(mut app:App, bus:Bus) let mut response = PacketRegisterResponse::new(); response.status = STATUS_SERVER_ERROR; - println!("Request: Register"); + app.log.log("Request: Register"); let mut is_valid = true; if request.code != crate::config::REGISTER_CODE.as_bytes() { response.status = STATUS_BAD_CODE; is_valid = false; } @@ -185,7 +185,7 @@ pub async fn thread_system(mut app:App, bus:Bus) app.user_id.set(user_id as isize, user_pos); - println!("Registered user '{}' @ {} with id {}", request.handle, user_pos, user_id); + app.log.log(&format!("Registered user '{}' @ {} with id {}", request.handle, user_pos, user_id)); // Generate authentication token and secret response.status = STATUS_OK; @@ -209,12 +209,12 @@ pub async fn thread_system(mut app:App, bus:Bus) } } } - Err(_) => { println!("error: failed to generate salt.") } + Err(_) => { app.log.log("error: failed to generate salt.") } } } Some(_) => { response.status = STATUS_BAD_HANDLE; - println!("notice: attempt to register existing handle: '{}'", request.handle); + app.log.log(&format!("notice: attempt to register existing handle: '{}'", request.handle)); } } } @@ -226,7 +226,7 @@ pub async fn thread_system(mut app:App, bus:Bus) let mut response = PacketAuthResponse::new(); response.status = STATUS_ERROR; - println!("Request: Auth"); + app.log.log("Request: Auth"); let mut is_valid = true; if is_valid && request.handle.len() == 0 { response.status = STATUS_BAD_HANDLE; is_valid = false; } @@ -244,7 +244,7 @@ pub async fn thread_system(mut app:App, bus:Bus) // [TEMPORARY] WORKAROUND FOR PASSWORD RESET if user.secret.is_empty() { - println!("Password reset: {}", user.handle); + app.log.log(&format!("Password reset: {}", user.handle)); if let Ok(secret) = argon2::hash_raw(&request.secret.as_bytes(), &salt, &argon_config) { user.secret = secret; if if let Some(app_user) = app.users.get_mut(tuid) { @@ -257,7 +257,7 @@ pub async fn thread_system(mut app:App, bus:Bus) } else { // Verify salted secret against user data if argon2::verify_raw(&request.secret.as_bytes(), &salt, &user.secret, &argon_config).unwrap_or(false) { - println!("Authenticated user '{}' id {}", user.handle, uid); + app.log.log(&format!("Authenticated user '{}' id {}", user.handle, uid)); // Generate authentication token and secret response.status = STATUS_OK; @@ -300,17 +300,17 @@ pub async fn thread_system(mut app:App, bus:Bus) } } } else { - println!("notice: password verification failed."); + app.log.log("notice: password verification failed."); } } } else { - println!("error: user salt id '{}' not found.", user.na_key); + app.log.log(&format!("error: user salt id '{}' not found.", user.na_key)); } } else { - println!("error: user with id '{}' not found.", uid); + app.log.log(&format!("error: user with id '{}' not found.", uid)); } } else { - println!("error: user with id '{}' not found.", uid); + app.log.log(&format!("error: user with id '{}' not found.", uid)); } } None => { } @@ -321,7 +321,7 @@ pub async fn thread_system(mut app:App, bus:Bus) } QRPacketData::QAuthResume(request) => { - println!("Request: Auth Resume"); + app.log.log("Request: Auth Resume"); let mut response = PacketAuthResumeResponse::new(); response.status = STATUS_ERROR; @@ -346,7 +346,7 @@ pub async fn thread_system(mut app:App, bus:Bus) // Add connection to chain. if let Some(user) = app.get_user_by_id(auth.user).cloned() { response.handle = user.handle.clone(); - + if let Some(user_cid) = user.connection { if let Some(existing) = app.connections.get(user_cid as usize).cloned() { if let Some(conn) = app.connections.get_mut(qr.id as usize) { @@ -370,7 +370,7 @@ pub async fn thread_system(mut app:App, bus:Bus) } QRPacketData::QAuthRevoke => { - println!("Request: Auth Revoke"); + app.log.log("Request: Auth Revoke"); // Remove connection from chain. if let Some(conn) = app.connections.get(qr.id as usize).cloned() { @@ -408,7 +408,7 @@ pub async fn thread_system(mut app:App, bus:Bus) if let Some(conn) = app.connections.get_mut(qr.id as usize) { match conn.auth { Some(auth) => { - println!("Deauthenticated connection: {}", qr.id); + app.log.log(&format!("Deauthenticated connection: {}", qr.id)); app.auths.unset(&auth); } None => { } @@ -420,7 +420,7 @@ pub async fn thread_system(mut app:App, bus:Bus) } QRPacketData::QSessionList(request) => { - println!("Request: Session List"); + app.log.log("Request: Session List"); let mut response = PacketSessionListResponse::new(); @@ -484,7 +484,7 @@ pub async fn thread_system(mut app:App, bus:Bus) } QRPacketData::QSessionView(request) => { - println!("Request: Session Join"); + app.log.log("Request: Session Join"); let mut response = PacketSessionViewResponse::new(); response.status = STATUS_ERROR; @@ -508,7 +508,7 @@ pub async fn thread_system(mut app:App, bus:Bus) if user_id.is_some() { // Resume session if user is player if Some(session.p_dawn.user) == user_id || Some(session.p_dusk.user) == user_id { - println!("User resumes session."); + app.log.log("User resumes session."); response.status = STATUS_OK; true } else { false } @@ -517,7 +517,7 @@ pub async fn thread_system(mut app:App, bus:Bus) // Join game as spectator. else { - println!("User spectates session."); + app.log.log("User spectates session."); response.status = STATUS_OK; true } { @@ -543,7 +543,7 @@ pub async fn thread_system(mut app:App, bus:Bus) // SessionResign QRPacketData::QSessionResign(request) => { use game::history::Play; - println!("Request: Session Resign"); + app.log.log("Request: Session Resign"); let mut packets = Vec::::new(); @@ -587,7 +587,7 @@ pub async fn thread_system(mut app:App, bus:Bus) // SessionLeave QRPacketData::QSessionLeave => { - println!("Request: Session Leave"); + app.log.log("Request: Session Leave"); // Verify that session exists. if let Some(session_token) = session_id { @@ -613,7 +613,7 @@ pub async fn thread_system(mut app:App, bus:Bus) QRPacketData::QGameState(request) => { let mut response = PacketGameStateResponse::new(); - println!("Request: Game State"); + app.log.log("Request: Game State"); if let Some(session) = app.sessions.get(&request.token) { response.status = STATUS_OK; @@ -630,7 +630,7 @@ pub async fn thread_system(mut app:App, bus:Bus) // GameMessage QRPacketData::GameMessage(request) => { - println!("Request: Game Message"); + app.log.log("Request: Game Message"); let mut packets = Vec::::new(); let mut response = QRPacketData::None; @@ -669,9 +669,9 @@ pub async fn thread_system(mut app:App, bus:Bus) !request.expected }; if result { - println!("OK {} {}", request.expected, text); + app.log.log(&format!("OK {} {}", request.expected, text)); } else { - println!("NO {} {}", request.expected, text); + app.log.log(&format!("NO {} {}", request.expected, text)); } } else if !session.game.is_complete() { if (user_id == Some(session.p_dawn.user) && session.game.turn & 1 == 0) @@ -823,7 +823,7 @@ pub async fn thread_system(mut app:App, bus:Bus) // Challenge QRPacketData::QChallenge(request) => { - println!("Request: Challenge"); + app.log.log("Request: Challenge"); if let Some(user_id) = user_id { if let Some(chal_id) = app.user_handle.get(request.handle.to_lowercase().as_bytes()).cloned() { @@ -836,7 +836,7 @@ pub async fn thread_system(mut app:App, bus:Bus) chal_user.challenges.push(user_id); send_user_status.push(chal_id); } else { - println!("notice: duplicate challenge."); + app.log.log("notice: duplicate challenge."); } } } @@ -847,7 +847,7 @@ pub async fn thread_system(mut app:App, bus:Bus) // ChallengeAnswer QRPacketData::QChallengeAnswer(request) => { - println!("Request: Challenge Answer"); + app.log.log("Request: Challenge Answer"); use crate::app::session::{SessionToken, SessionSecret, Player}; @@ -885,7 +885,6 @@ pub async fn thread_system(mut app:App, bus:Bus) // Choose player seats. let time = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_millis() as u64; - println!("Time {}", time); // Build session. let mut session = Session { @@ -932,7 +931,7 @@ pub async fn thread_system(mut app:App, bus:Bus) // ChallengeList QRPacketData::QChallengeList => { - println!("Request: Challenge List"); + app.log.log("Request: Challenge List"); let mut response = PacketChallengeListResponse::new(); response.status = STATUS_NOAUTH; @@ -956,7 +955,7 @@ pub async fn thread_system(mut app:App, bus:Bus) // UserList QRPacketData::QUserList => { - println!("Request: User List"); + app.log.log("Request: User List"); let mut response = PacketUserListResponse::new(); response.status = STATUS_NOAUTH; diff --git a/server/src/manager/ws.rs b/server/src/manager/ws.rs index 5ac94c3..012b53d 100644 --- a/server/src/manager/ws.rs +++ b/server/src/manager/ws.rs @@ -53,8 +53,6 @@ pub async fn handle_ws(ws:WebSocketStream>, args:HttpServiceAr let mut index :usize = 0; let code: u16 = unpack_u16(&data, &mut index); - println!("MESSAGE {:x}", code); - match code { CODE_HELLO => { args.bus.send( diff --git a/server/src/system/log/mod.rs b/server/src/system/log/mod.rs new file mode 100644 index 0000000..2ca4114 --- /dev/null +++ b/server/src/system/log/mod.rs @@ -0,0 +1,18 @@ +const DEBUG_PRINT :bool = false; + +pub struct Log { + +} +impl Log { + pub fn new() -> Self + { + Self { } + } + + pub fn log(&self, text:&str) + { + if DEBUG_PRINT { + println!("{}", text); + } + } +} diff --git a/server/src/system/mod.rs b/server/src/system/mod.rs index 717244e..1fcacb8 100644 --- a/server/src/system/mod.rs +++ b/server/src/system/mod.rs @@ -1,5 +1,6 @@ #![allow(dead_code)] +pub mod log; pub mod net; pub mod cache; pub mod filesystem;