Reduce server-side prints.

This commit is contained in:
yukirij 2024-10-13 18:27:01 -07:00
parent 981aef3c1e
commit 7dae905288
5 changed files with 59 additions and 36 deletions

View File

@ -4,7 +4,12 @@ use sparse::Sparse;
use pool::Pool; use pool::Pool;
use trie::Trie; use trie::Trie;
use crate::{ use crate::{
protocol::QRPacket, system::filesystem::FileSystem, util::Chain protocol::QRPacket,
system::{
filesystem::FileSystem,
log::Log,
},
util::Chain,
}; };
pub mod connection; use connection::Connection; pub mod connection; use connection::Connection;
@ -15,6 +20,7 @@ pub mod context;
pub struct App { pub struct App {
pub filesystem:FileSystem, pub filesystem:FileSystem,
pub log:Log,
pub connections:Pool<Connection>, pub connections:Pool<Connection>,
@ -106,6 +112,7 @@ impl App {
println!("App data ready."); println!("App data ready.");
Ok(Self { Ok(Self {
filesystem:filesystem, filesystem:filesystem,
log:Log::new(),
connections:Pool::new(), connections:Pool::new(),

View File

@ -54,7 +54,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
conn.next = id as u32; conn.next = id as u32;
} }
println!("Connect: {}", id); app.log.log(&format!("Connect: {}", id));
bus.send( bus.send(
packet.from, packet.from,
@ -65,7 +65,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
} }
QRPacketData::QDisconn => { QRPacketData::QDisconn => {
println!("Disconnect: {}", qr.id); app.log.log(&format!("Disconnect: {}", qr.id));
// Uninitialize connection // Uninitialize connection
if if let Some(conn) = app.connections.get(qr.id as usize).cloned() { 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<protocol::QRPacket>)
let mut response = PacketRegisterResponse::new(); let mut response = PacketRegisterResponse::new();
response.status = STATUS_SERVER_ERROR; response.status = STATUS_SERVER_ERROR;
println!("Request: Register"); app.log.log("Request: Register");
let mut is_valid = true; let mut is_valid = true;
if request.code != crate::config::REGISTER_CODE.as_bytes() { response.status = STATUS_BAD_CODE; is_valid = false; } 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<protocol::QRPacket>)
app.user_id.set(user_id as isize, user_pos); 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 // Generate authentication token and secret
response.status = STATUS_OK; response.status = STATUS_OK;
@ -209,12 +209,12 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
} }
} }
} }
Err(_) => { println!("error: failed to generate salt.") } Err(_) => { app.log.log("error: failed to generate salt.") }
} }
} }
Some(_) => { Some(_) => {
response.status = STATUS_BAD_HANDLE; 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<protocol::QRPacket>)
let mut response = PacketAuthResponse::new(); let mut response = PacketAuthResponse::new();
response.status = STATUS_ERROR; response.status = STATUS_ERROR;
println!("Request: Auth"); app.log.log("Request: Auth");
let mut is_valid = true; let mut is_valid = true;
if is_valid && request.handle.len() == 0 { response.status = STATUS_BAD_HANDLE; is_valid = false; } 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<protocol::QRPacket>)
// [TEMPORARY] WORKAROUND FOR PASSWORD RESET // [TEMPORARY] WORKAROUND FOR PASSWORD RESET
if user.secret.is_empty() { 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) { if let Ok(secret) = argon2::hash_raw(&request.secret.as_bytes(), &salt, &argon_config) {
user.secret = secret; user.secret = secret;
if if let Some(app_user) = app.users.get_mut(tuid) { 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<protocol::QRPacket>)
} else { } else {
// Verify salted secret against user data // Verify salted secret against user data
if argon2::verify_raw(&request.secret.as_bytes(), &salt, &user.secret, &argon_config).unwrap_or(false) { 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 // Generate authentication token and secret
response.status = STATUS_OK; response.status = STATUS_OK;
@ -300,17 +300,17 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
} }
} }
} else { } else {
println!("notice: password verification failed."); app.log.log("notice: password verification failed.");
} }
} }
} else { } 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 { } else {
println!("error: user with id '{}' not found.", uid); app.log.log(&format!("error: user with id '{}' not found.", uid));
} }
} else { } else {
println!("error: user with id '{}' not found.", uid); app.log.log(&format!("error: user with id '{}' not found.", uid));
} }
} }
None => { } None => { }
@ -321,7 +321,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
} }
QRPacketData::QAuthResume(request) => { QRPacketData::QAuthResume(request) => {
println!("Request: Auth Resume"); app.log.log("Request: Auth Resume");
let mut response = PacketAuthResumeResponse::new(); let mut response = PacketAuthResumeResponse::new();
response.status = STATUS_ERROR; response.status = STATUS_ERROR;
@ -346,7 +346,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
// Add connection to chain. // Add connection to chain.
if let Some(user) = app.get_user_by_id(auth.user).cloned() { if let Some(user) = app.get_user_by_id(auth.user).cloned() {
response.handle = user.handle.clone(); response.handle = user.handle.clone();
if let Some(user_cid) = user.connection { if let Some(user_cid) = user.connection {
if let Some(existing) = app.connections.get(user_cid as usize).cloned() { if let Some(existing) = app.connections.get(user_cid as usize).cloned() {
if let Some(conn) = app.connections.get_mut(qr.id as usize) { 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<protocol::QRPacket>)
} }
QRPacketData::QAuthRevoke => { QRPacketData::QAuthRevoke => {
println!("Request: Auth Revoke"); app.log.log("Request: Auth Revoke");
// Remove connection from chain. // Remove connection from chain.
if let Some(conn) = app.connections.get(qr.id as usize).cloned() { 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<protocol::QRPacket>)
if let Some(conn) = app.connections.get_mut(qr.id as usize) { if let Some(conn) = app.connections.get_mut(qr.id as usize) {
match conn.auth { match conn.auth {
Some(auth) => { Some(auth) => {
println!("Deauthenticated connection: {}", qr.id); app.log.log(&format!("Deauthenticated connection: {}", qr.id));
app.auths.unset(&auth); app.auths.unset(&auth);
} }
None => { } None => { }
@ -420,7 +420,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
} }
QRPacketData::QSessionList(request) => { QRPacketData::QSessionList(request) => {
println!("Request: Session List"); app.log.log("Request: Session List");
let mut response = PacketSessionListResponse::new(); let mut response = PacketSessionListResponse::new();
@ -484,7 +484,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
} }
QRPacketData::QSessionView(request) => { QRPacketData::QSessionView(request) => {
println!("Request: Session Join"); app.log.log("Request: Session Join");
let mut response = PacketSessionViewResponse::new(); let mut response = PacketSessionViewResponse::new();
response.status = STATUS_ERROR; response.status = STATUS_ERROR;
@ -508,7 +508,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
if user_id.is_some() { if user_id.is_some() {
// Resume session if user is player // Resume session if user is player
if Some(session.p_dawn.user) == user_id || Some(session.p_dusk.user) == user_id { 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; response.status = STATUS_OK;
true true
} else { false } } else { false }
@ -517,7 +517,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
// Join game as spectator. // Join game as spectator.
else { else {
println!("User spectates session."); app.log.log("User spectates session.");
response.status = STATUS_OK; response.status = STATUS_OK;
true true
} { } {
@ -543,7 +543,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
// SessionResign // SessionResign
QRPacketData::QSessionResign(request) => { QRPacketData::QSessionResign(request) => {
use game::history::Play; use game::history::Play;
println!("Request: Session Resign"); app.log.log("Request: Session Resign");
let mut packets = Vec::<QRPacket>::new(); let mut packets = Vec::<QRPacket>::new();
@ -587,7 +587,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
// SessionLeave // SessionLeave
QRPacketData::QSessionLeave => { QRPacketData::QSessionLeave => {
println!("Request: Session Leave"); app.log.log("Request: Session Leave");
// Verify that session exists. // Verify that session exists.
if let Some(session_token) = session_id { if let Some(session_token) = session_id {
@ -613,7 +613,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
QRPacketData::QGameState(request) => { QRPacketData::QGameState(request) => {
let mut response = PacketGameStateResponse::new(); let mut response = PacketGameStateResponse::new();
println!("Request: Game State"); app.log.log("Request: Game State");
if let Some(session) = app.sessions.get(&request.token) { if let Some(session) = app.sessions.get(&request.token) {
response.status = STATUS_OK; response.status = STATUS_OK;
@ -630,7 +630,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
// GameMessage // GameMessage
QRPacketData::GameMessage(request) => { QRPacketData::GameMessage(request) => {
println!("Request: Game Message"); app.log.log("Request: Game Message");
let mut packets = Vec::<QRPacket>::new(); let mut packets = Vec::<QRPacket>::new();
let mut response = QRPacketData::None; let mut response = QRPacketData::None;
@ -669,9 +669,9 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
!request.expected !request.expected
}; };
if result { if result {
println!("OK {} {}", request.expected, text); app.log.log(&format!("OK {} {}", request.expected, text));
} else { } else {
println!("NO {} {}", request.expected, text); app.log.log(&format!("NO {} {}", request.expected, text));
} }
} else if !session.game.is_complete() { } else if !session.game.is_complete() {
if (user_id == Some(session.p_dawn.user) && session.game.turn & 1 == 0) 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<protocol::QRPacket>)
// Challenge // Challenge
QRPacketData::QChallenge(request) => { QRPacketData::QChallenge(request) => {
println!("Request: Challenge"); app.log.log("Request: Challenge");
if let Some(user_id) = user_id { if let Some(user_id) = user_id {
if let Some(chal_id) = app.user_handle.get(request.handle.to_lowercase().as_bytes()).cloned() { 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<protocol::QRPacket>)
chal_user.challenges.push(user_id); chal_user.challenges.push(user_id);
send_user_status.push(chal_id); send_user_status.push(chal_id);
} else { } 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<protocol::QRPacket>)
// ChallengeAnswer // ChallengeAnswer
QRPacketData::QChallengeAnswer(request) => { QRPacketData::QChallengeAnswer(request) => {
println!("Request: Challenge Answer"); app.log.log("Request: Challenge Answer");
use crate::app::session::{SessionToken, SessionSecret, Player}; use crate::app::session::{SessionToken, SessionSecret, Player};
@ -885,7 +885,6 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
// Choose player seats. // Choose player seats.
let time = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_millis() as u64; let time = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_millis() as u64;
println!("Time {}", time);
// Build session. // Build session.
let mut session = Session { let mut session = Session {
@ -932,7 +931,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
// ChallengeList // ChallengeList
QRPacketData::QChallengeList => { QRPacketData::QChallengeList => {
println!("Request: Challenge List"); app.log.log("Request: Challenge List");
let mut response = PacketChallengeListResponse::new(); let mut response = PacketChallengeListResponse::new();
response.status = STATUS_NOAUTH; response.status = STATUS_NOAUTH;
@ -956,7 +955,7 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
// UserList // UserList
QRPacketData::QUserList => { QRPacketData::QUserList => {
println!("Request: User List"); app.log.log("Request: User List");
let mut response = PacketUserListResponse::new(); let mut response = PacketUserListResponse::new();
response.status = STATUS_NOAUTH; response.status = STATUS_NOAUTH;

View File

@ -53,8 +53,6 @@ pub async fn handle_ws(ws:WebSocketStream<TokioIo<Upgraded>>, args:HttpServiceAr
let mut index :usize = 0; let mut index :usize = 0;
let code: u16 = unpack_u16(&data, &mut index); let code: u16 = unpack_u16(&data, &mut index);
println!("MESSAGE {:x}", code);
match code { match code {
CODE_HELLO => { CODE_HELLO => {
args.bus.send( args.bus.send(

View File

@ -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);
}
}
}

View File

@ -1,5 +1,6 @@
#![allow(dead_code)] #![allow(dead_code)]
pub mod log;
pub mod net; pub mod net;
pub mod cache; pub mod cache;
pub mod filesystem; pub mod filesystem;