From 58b45a85a9bdbffdefee436daae3455f75a28cf0 Mon Sep 17 00:00:00 2001 From: Fate-JH Date: Fri, 10 Jan 2020 19:40:15 -0500 Subject: [PATCH] Better handle database login errors (#321) * logging errors for database login ops * clarified types of messages to expect * TeardownConnection handled --- .../src/main/scala/LoginSessionActor.scala | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/pslogin/src/main/scala/LoginSessionActor.scala b/pslogin/src/main/scala/LoginSessionActor.scala index ff7e0ea7a..4f1a939a7 100644 --- a/pslogin/src/main/scala/LoginSessionActor.scala +++ b/pslogin/src/main/scala/LoginSessionActor.scala @@ -46,7 +46,7 @@ class LoginSessionActor extends Actor with MDCContextAware { var canonicalHostName : String = "" var port : Int = 0 - val serverName = WorldConfig.Get[String]("worldserver.ServerName"); + val serverName = WorldConfig.Get[String]("worldserver.ServerName") // This MUST be an IP address. The client DOES NOT do name resolution var serverHost : String = if (WorldConfig.Get[String]("worldserver.Hostname") != "") @@ -110,6 +110,9 @@ class LoginSessionActor extends Actor with MDCContextAware { val serverTick = Math.abs(System.nanoTime().toInt) // limit the size to prevent encoding error sendResponse(PacketCoding.CreateControlPacket(ControlSyncResp(diff, serverTick, fa, fb, fb, fa))) + case TeardownConnection(_) => + sendResponse(DropSession(sessionId, "client requested session termination")) + case default => log.error(s"Unhandled ControlPacket $default") } @@ -142,7 +145,7 @@ class LoginSessionActor extends Actor with MDCContextAware { log.debug(s"Unhandled GamePacket $pkt") } - def startAccountLogin(username: String, password: String) = { + def startAccountLogin(username: String, password: String) : Unit = { val newToken = this.generateToken() Database.getConnection.connect.onComplete { case Success(connection) => @@ -165,7 +168,7 @@ class LoginSessionActor extends Actor with MDCContextAware { } } - def startAccountAuthentication() : Receive = { + def startAccountAuthentication : Receive = { case StartAccountAuthentication(connection, username, password, newToken, queryResult) => queryResult match { @@ -202,10 +205,11 @@ class LoginSessionActor extends Actor with MDCContextAware { self ! FinishAccountLogin(connection, username, newToken, false) } } - case default => failWithError(s"Invalid message '$default' received in startAccountAuthentication") + case default => + failWithError(s"Invalid message '$default' received in startAccountAuthentication") } - def createNewAccount() : Receive = { + def createNewAccount : Receive = { case CreateNewAccount(connection, username, password, newToken) => log.info(s"Account $username does not exist, creating new account...") val bcryptPassword : String = password.bcrypt(numBcryptPasses) @@ -230,18 +234,30 @@ class LoginSessionActor extends Actor with MDCContextAware { context.become(finishAccountLogin) self ! FinishAccountLogin(connection, username, newToken, false) } - case _ => - log.error(s"Error creating new account for $username") + case default => + log.error(s"Error creating new account for $username - $default") context.become(finishAccountLogin) self ! FinishAccountLogin(connection, username, newToken, false) } - case _ => failWithError("Something to do ?") + case Failure(e : com.github.mauricio.async.db.postgresql.exceptions.GenericDatabaseException) => + log.error(s"Error creating new account - ${e.errorMessage.message}") + context.become(finishAccountLogin) + self ! FinishAccountLogin(connection, username, newToken, false) + + case Failure(e : java.sql.SQLException) => + log.error(s"Error creating new account - ${e.getMessage}") + context.become(finishAccountLogin) + self ! FinishAccountLogin(connection, username, newToken, false) + + case _ => + failWithError(s"Something to do?") } - case default => failWithError(s"Invalid message '$default' received in createNewAccount") + case default => + failWithError(s"Invalid message '$default' received in createNewAccount") } // Essentially keeps a record of this individual login occurrence - def logTheLoginOccurrence() : Receive = { + def logTheLoginOccurrence : Receive = { case LogTheLoginOccurrence(connection, username, newToken, isSuccessfulLogin, accountId) => connection.get.inTransaction { c => c.sendPreparedStatement( @@ -249,14 +265,14 @@ class LoginSessionActor extends Actor with MDCContextAware { Array(accountId, new java.sql.Timestamp(System.currentTimeMillis), ipAddress, canonicalHostName, hostName, port) ) }.onComplete { - case _ => + _ => context.become(finishAccountLogin) self ! FinishAccountLogin(connection, username, newToken, isSuccessfulLogin) } case default => failWithError(s"Invalid message '$default' received in logTheLoginOccurrence") } - def finishAccountLogin() : Receive = { + def finishAccountLogin : Receive = { case FinishAccountLogin(connection, username, newToken, isSuccessfulLogin, isInactive) => if(isSuccessfulLogin) { // Login OK loginSuccessfulResponse(username, newToken) @@ -336,7 +352,7 @@ class LoginSessionActor extends Actor with MDCContextAware { def generateToken() = { val r = new scala.util.Random val sb = new StringBuilder - for (i <- 1 to 31) { + for (_ <- 1 to 31) { sb.append(r.nextPrintableChar) } sb.toString