2024-03-31 10:09:32 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.net.client.handlers ;
import engine.exception.MsgSendException ;
import engine.gameManager.ConfigManager ;
import engine.gameManager.DbManager ;
import engine.gameManager.SessionManager ;
2024-04-05 07:59:44 -04:00
import engine.mbEnums ;
2024-03-31 10:09:32 -04:00
import engine.net.client.ClientConnection ;
import engine.net.client.msg.ClientNetMsg ;
import engine.net.client.msg.login.ClientLoginInfoMsg ;
import engine.objects.Account ;
import engine.objects.PlayerCharacter ;
import engine.server.MBServerStatics ;
import engine.server.login.LoginServer ;
import engine.session.Session ;
import org.pmw.tinylog.Logger ;
public class ClientLoginInfoMsgHandler extends AbstractClientMsgHandler {
public ClientLoginInfoMsgHandler ( ) {
super ( ClientLoginInfoMsg . class ) ;
}
@Override
protected boolean _handleNetMsg ( ClientNetMsg baseMsg , ClientConnection origin ) throws MsgSendException {
PlayerCharacter playerCharacter = origin . getPlayerCharacter ( ) ;
// Member variable declaration
ClientLoginInfoMsg msg ;
// Member variable assignment
msg = ( ClientLoginInfoMsg ) baseMsg ;
// Add zero length strings to eliminate the need for null checking.
String uname = msg . getUname ( ) ;
String pass = msg . getPword ( ) ;
// Check to see if there is actually any data in uname.pass
if ( uname . length ( ) = = 0 ) {
2024-03-31 10:37:03 -04:00
LoginServer . KickToLogin ( MBServerStatics . LOGINERROR_UNABLE_TO_LOGIN , " The username provided was zero length. " , origin ) ;
2024-03-31 10:09:32 -04:00
return true ;
}
if ( pass . length ( ) = = 0 ) {
2024-03-31 10:37:03 -04:00
LoginServer . KickToLogin ( MBServerStatics . LOGINERROR_UNABLE_TO_LOGIN , " The password provided was zero length. " , origin ) ;
2024-03-31 10:09:32 -04:00
return true ;
}
if ( LoginServer . loginServerRunning = = false ) {
2024-03-31 10:37:03 -04:00
LoginServer . KickToLogin ( MBServerStatics . LOGINERROR_LOGINSERVER_BUSY , " " , origin ) ;
2024-03-31 10:09:32 -04:00
return true ;
}
Account account ;
account = DbManager . AccountQueries . GET_ACCOUNT ( uname ) ;
// Create the account if it doesn't exist and MB_LOGIN_AUTOREG is TRUE;
// This is to support MagicBox users without a web hosting skillset.
if ( account = = null ) {
if ( ConfigManager . MB_LOGIN_AUTOREG . getValue ( ) . equalsIgnoreCase ( " false " ) ) {
2024-03-31 10:37:03 -04:00
LoginServer . KickToLogin ( MBServerStatics . LOGINERROR_INVALID_USERNAME_PASSWORD , " Could not find account ( " + uname + ')' , origin ) ;
2024-03-31 10:09:32 -04:00
Logger . info ( " Could not find account ( " + uname + ')' ) ;
return true ;
}
Logger . info ( " AutoRegister: " + uname + " / " + pass ) ;
DbManager . AccountQueries . CREATE_SINGLE ( uname , pass ) ;
account = DbManager . AccountQueries . GET_ACCOUNT ( uname ) ;
if ( account = = null ) {
2024-03-31 10:37:03 -04:00
LoginServer . KickToLogin ( MBServerStatics . LOGINERROR_INVALID_USERNAME_PASSWORD , " Could not find account ( " + uname + ')' , origin ) ;
2024-03-31 10:09:32 -04:00
Logger . info ( " Could not auto-create ( " + uname + ')' ) ;
return true ;
}
}
if ( account . getLastLoginFailure ( ) + MBServerStatics . RESET_LOGIN_ATTEMPTS_AFTER < System . currentTimeMillis ( ) )
account . resetLoginAttempts ( ) ;
// TODO: Log the login attempts IP, name, password and timestamp
// Check number invalid login attempts. If 5 or greater, kick to login.
if ( account . getLoginAttempts ( ) > = MBServerStatics . MAX_LOGIN_ATTEMPTS ) {
2024-03-31 10:37:03 -04:00
LoginServer . KickToLogin ( MBServerStatics . LOGINERROR_UNABLE_TO_LOGIN , " Too many login in attempts for ' " + uname + '\'' , origin ) ;
2024-03-31 10:09:32 -04:00
Logger . info ( " Too many login in attempts for ' " + uname + '\'' ) ;
return true ;
}
if ( account . lastPasswordCheck < System . currentTimeMillis ( ) ) {
account . lastPasswordCheck = System . currentTimeMillis ( ) + MBServerStatics . ONE_MINUTE ;
}
// Attempt to validate login
try {
if ( ! account . passIsValid ( pass , origin . getClientIpAddress ( ) , origin . machineID ) ) {
account . incrementLoginAttempts ( ) ;
2024-03-31 10:37:03 -04:00
LoginServer . KickToLogin ( MBServerStatics . LOGINERROR_INVALID_USERNAME_PASSWORD , " " , origin ) ;
2024-03-31 10:09:32 -04:00
Logger . info ( " Incorrect password( " + uname + ')' ) ;
return true ;
}
} catch ( IllegalArgumentException e1 ) {
2024-03-31 10:37:03 -04:00
LoginServer . KickToLogin ( MBServerStatics . LOGINERROR_UNABLE_TO_LOGIN , " " , origin ) ;
2024-03-31 10:09:32 -04:00
Logger . info ( " Failed forum account validation( " + uname + ')' ) ;
}
// Account deactivated
2024-04-05 07:59:44 -04:00
if ( account . status . equals ( mbEnums . AccountStatus . BANNED ) ) {
2024-03-31 10:37:03 -04:00
LoginServer . KickToLogin ( MBServerStatics . LOGINERROR_NO_MORE_PLAYTIME_ON_ACCOUNT , " " , origin ) ;
2024-03-31 10:09:32 -04:00
return true ;
}
// Check to see if we have a Session mapped with this Account:
Session session = SessionManager . getSession ( account ) ;
// If there is, then the account is in use and must be handled:
// kick the 'other connection'
if ( session ! = null )
2024-03-31 10:37:03 -04:00
LoginServer . KickToLogin ( MBServerStatics . LOGINERROR_UNABLE_TO_LOGIN , " Your account has been accessed from a different IP & Port. " , session . getConn ( ) ) ; // Logout the character
2024-03-31 10:09:32 -04:00
// TODO implement character logout
// Get a new session
session = SessionManager . getNewSession ( account , origin ) ;
// Set Invalid Login Attempts to 0
account . resetLoginAttempts ( ) ;
// Send Login Response
ClientLoginInfoMsg loginResponse = new ClientLoginInfoMsg ( msg ) ;
loginResponse . setUnknown06 ( 8323072 ) ;
loginResponse . setUnknown07 ( 3276800 ) ;
loginResponse . setUnknown08 ( 196608 ) ;
loginResponse . setUnknown09 ( ( short ) 15 ) ;
origin . sendMsg ( loginResponse ) ;
// send character select screen
try {
2024-03-31 10:37:03 -04:00
LoginServer . sendCharacterSelectScreen ( session ) ;
2024-03-31 10:09:32 -04:00
} catch ( Exception e ) {
Logger . error ( " Unable to Send Character Select Screen to client " ) ;
2024-03-31 10:37:03 -04:00
LoginServer . KickToLogin ( MBServerStatics . LOGINERROR_UNABLE_TO_LOGIN , " Unable to send Character Select Screen to client. " , origin ) ;
2024-03-31 10:09:32 -04:00
return true ;
}
// Logging
String addyPort = origin . getRemoteAddressAndPortAsString ( ) ;
int id = account . getObjectUUID ( ) ;
Logger . info ( uname + '(' + id + " ) has successfully logged in from " + addyPort ) ;
return true ;
}
}