mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-22 21:40:05 +00:00
refactored and cleaned up login function
This commit is contained in:
parent
5c6d7d6055
commit
eee8aab888
@ -78,6 +78,16 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#pragma region packets
|
#pragma region packets
|
||||||
|
|
||||||
|
void loginFail(LoginError errorCode, std::string userLogin, CNSocket* sock) {
|
||||||
|
INITSTRUCT(sP_LS2CL_REP_LOGIN_FAIL, resp);
|
||||||
|
U8toU16(userLogin, resp.szID, sizeof(resp.szID));
|
||||||
|
resp.iErrorCode = (int)errorCode;
|
||||||
|
sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_FAIL, sizeof(sP_LS2CL_REP_LOGIN_FAIL));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void CNLoginServer::login(CNSocket* sock, CNPacketData* data) {
|
void CNLoginServer::login(CNSocket* sock, CNPacketData* data) {
|
||||||
if (data->size != sizeof(sP_CL2LS_REQ_LOGIN))
|
if (data->size != sizeof(sP_CL2LS_REQ_LOGIN))
|
||||||
return; // ignore the malformed packet
|
return; // ignore the malformed packet
|
||||||
@ -100,106 +110,102 @@ void CNLoginServer::login(CNSocket* sock, CNPacketData* data) {
|
|||||||
userPassword = std::string(U16toU8(login->szPassword).c_str());
|
userPassword = std::string(U16toU8(login->szPassword).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool success = false;
|
// check regex
|
||||||
int errorCode = 0;
|
if (!CNLoginServer::isLoginDataGood(userLogin, userPassword))
|
||||||
|
return loginFail(LoginError::LOGIN_ERROR, userLogin, sock);
|
||||||
|
|
||||||
// checking regex
|
std::unique_ptr<Database::Account> findUser = Database::findAccount(userLogin);
|
||||||
if (!CNLoginServer::isLoginDataGood(userLogin, userPassword)) {
|
if (findUser == nullptr)
|
||||||
errorCode = (int)LoginError::LOGIN_ERROR;
|
return newAccount(sock, userLogin, userPassword, login->iClientVerC);
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::unique_ptr<Database::Account> findUser = Database::findAccount(userLogin);
|
|
||||||
// if account not found, make new one
|
|
||||||
if (findUser == nullptr) {
|
|
||||||
loginSessions[sock] = CNLoginData();
|
|
||||||
loginSessions[sock].userID = Database::addAccount(userLogin, userPassword);
|
|
||||||
loginSessions[sock].slot = 1;
|
|
||||||
loginSessions[sock].lastHeartbeat = getTime();
|
|
||||||
success = true;
|
|
||||||
|
|
||||||
// if user exists, check if password is correct
|
if (!CNLoginServer::isPasswordCorrect(findUser->Password, userPassword))
|
||||||
}
|
return loginFail(LoginError::ID_AND_PASSWORD_DO_NOT_MATCH, userLogin, sock);
|
||||||
else if (CNLoginServer::isPasswordCorrect(findUser->Password, userPassword)) {
|
|
||||||
/*calling this here to timestamp login attempt,
|
/* calling this here to timestamp login attempt,
|
||||||
* in order to make duplicate exit sanity check work*/
|
* in order to make duplicate exit sanity check work*/
|
||||||
Database::updateSelected(findUser->AccountID, findUser->Selected);
|
Database::updateSelected(findUser->AccountID, findUser->Selected);
|
||||||
// check if account isn't currently in use
|
|
||||||
if (CNLoginServer::isAccountInUse(findUser->AccountID)) {
|
if (CNLoginServer::isAccountInUse(findUser->AccountID))
|
||||||
errorCode = (int)LoginError::ID_ALREADY_IN_USE;
|
return loginFail(LoginError::ID_ALREADY_IN_USE, userLogin, sock);
|
||||||
}
|
|
||||||
else { // if not, login success
|
loginSessions[sock] = CNLoginData();
|
||||||
loginSessions[sock] = CNLoginData();
|
loginSessions[sock].userID = findUser->AccountID;
|
||||||
loginSessions[sock].userID = findUser->AccountID;
|
loginSessions[sock].slot = findUser->Selected;
|
||||||
loginSessions[sock].slot = findUser->Selected;
|
loginSessions[sock].lastHeartbeat = getTime();
|
||||||
loginSessions[sock].lastHeartbeat = getTime();
|
|
||||||
success = true;
|
std::vector<Player> characters = Database::getCharacters(loginSessions[sock].userID);
|
||||||
}
|
int charCount = characters.size();
|
||||||
}
|
|
||||||
else {
|
INITSTRUCT(sP_LS2CL_REP_LOGIN_SUCC, resp);
|
||||||
errorCode = (int)LoginError::ID_AND_PASSWORD_DO_NOT_MATCH;
|
memcpy(resp.szID, login->szID, sizeof(char16_t) * 33);
|
||||||
|
|
||||||
|
resp.iCharCount = charCount;
|
||||||
|
resp.iSlotNum = loginSessions[sock].slot;
|
||||||
|
resp.iPaymentFlag = 1;
|
||||||
|
resp.iOpenBetaFlag = 0;
|
||||||
|
resp.uiSvrTime = getTime();
|
||||||
|
|
||||||
|
// send the resp in with original key
|
||||||
|
sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_SUCC, sizeof(sP_LS2CL_REP_LOGIN_SUCC));
|
||||||
|
|
||||||
|
// update keys
|
||||||
|
sock->setEKey(CNSocketEncryption::createNewKey(resp.uiSvrTime, resp.iCharCount + 1, resp.iSlotNum + 1));
|
||||||
|
sock->setFEKey(CNSocketEncryption::createNewKey((uint64_t)(*(uint64_t*)&CNSocketEncryption::defaultKey[0]), login->iClientVerC, 1));
|
||||||
|
|
||||||
|
// now send the characters :)
|
||||||
|
std::vector<Player>::iterator it;
|
||||||
|
for (it = characters.begin(); it != characters.end(); it++) {
|
||||||
|
sP_LS2CL_REP_CHAR_INFO charInfo = sP_LS2CL_REP_CHAR_INFO();
|
||||||
|
|
||||||
|
charInfo.iSlot = (int8_t)it->slot;
|
||||||
|
charInfo.iLevel = (int16_t)it->level;
|
||||||
|
charInfo.sPC_Style = it->PCStyle;
|
||||||
|
charInfo.sPC_Style2 = it->PCStyle2;
|
||||||
|
|
||||||
|
// position
|
||||||
|
charInfo.iX = it->x;
|
||||||
|
charInfo.iY = it->y;
|
||||||
|
charInfo.iZ = it->z;
|
||||||
|
|
||||||
|
// save character in session (for char select)
|
||||||
|
int UID = it->iID;
|
||||||
|
loginSessions[sock].characters[UID] = Player(*it);
|
||||||
|
loginSessions[sock].characters[UID].FEKey = sock->getFEKey();
|
||||||
|
|
||||||
|
// Equip info
|
||||||
|
for (int i = 0; i < AEQUIP_COUNT; i++) {
|
||||||
|
charInfo.aEquip[i] = it->Equip[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set default to the first character
|
||||||
|
if (it == characters.begin())
|
||||||
|
loginSessions[sock].selectedChar = UID;
|
||||||
|
|
||||||
|
sock->sendPacket((void*)&charInfo, P_LS2CL_REP_CHAR_INFO, sizeof(sP_LS2CL_REP_CHAR_INFO));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CNLoginServer::newAccount(CNSocket* sock, std::string userLogin, std::string userPassword, int32_t clientVerC) {
|
||||||
|
loginSessions[sock] = CNLoginData();
|
||||||
|
loginSessions[sock].userID = Database::addAccount(userLogin, userPassword);
|
||||||
|
loginSessions[sock].slot = 1;
|
||||||
|
loginSessions[sock].lastHeartbeat = getTime();
|
||||||
|
|
||||||
if (success) {
|
INITSTRUCT(sP_LS2CL_REP_LOGIN_SUCC, resp);
|
||||||
std::vector<Player> characters = Database::getCharacters(loginSessions[sock].userID);
|
U8toU16(userLogin, resp.szID, sizeof(resp.szID));
|
||||||
int charCount = characters.size();
|
|
||||||
|
|
||||||
INITSTRUCT(sP_LS2CL_REP_LOGIN_SUCC, resp);
|
resp.iCharCount = 0;
|
||||||
// set username in resp packet
|
resp.iSlotNum = loginSessions[sock].slot;
|
||||||
memcpy(resp.szID, login->szID, sizeof(char16_t) * 33);
|
resp.iPaymentFlag = 1;
|
||||||
|
resp.iOpenBetaFlag = 0;
|
||||||
|
resp.uiSvrTime = getTime();
|
||||||
|
|
||||||
resp.iCharCount = charCount;
|
// send the resp in with original key
|
||||||
resp.iSlotNum = loginSessions[sock].slot;
|
sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_SUCC, sizeof(sP_LS2CL_REP_LOGIN_SUCC));
|
||||||
resp.iPaymentFlag = 1;
|
|
||||||
resp.iOpenBetaFlag = 0;
|
|
||||||
resp.uiSvrTime = getTime();
|
|
||||||
|
|
||||||
// send the resp in with original key
|
// update keys
|
||||||
sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_SUCC, sizeof(sP_LS2CL_REP_LOGIN_SUCC));
|
sock->setEKey(CNSocketEncryption::createNewKey(resp.uiSvrTime, resp.iCharCount + 1, resp.iSlotNum + 1));
|
||||||
|
sock->setFEKey(CNSocketEncryption::createNewKey((uint64_t)(*(uint64_t*)&CNSocketEncryption::defaultKey[0]), clientVerC, 1));
|
||||||
// update keys
|
|
||||||
sock->setEKey(CNSocketEncryption::createNewKey(resp.uiSvrTime, resp.iCharCount + 1, resp.iSlotNum + 1));
|
|
||||||
sock->setFEKey(CNSocketEncryption::createNewKey((uint64_t)(*(uint64_t*)&CNSocketEncryption::defaultKey[0]), login->iClientVerC, 1));
|
|
||||||
|
|
||||||
// now send the characters :)
|
|
||||||
std::vector<Player>::iterator it;
|
|
||||||
for (it = characters.begin(); it != characters.end(); it++) {
|
|
||||||
sP_LS2CL_REP_CHAR_INFO charInfo = sP_LS2CL_REP_CHAR_INFO();
|
|
||||||
|
|
||||||
charInfo.iSlot = (int8_t)it->slot;
|
|
||||||
charInfo.iLevel = (int16_t)it->level;
|
|
||||||
charInfo.sPC_Style = it->PCStyle;
|
|
||||||
charInfo.sPC_Style2 = it->PCStyle2;
|
|
||||||
|
|
||||||
// position
|
|
||||||
charInfo.iX = it->x;
|
|
||||||
charInfo.iY = it->y;
|
|
||||||
charInfo.iZ = it->z;
|
|
||||||
|
|
||||||
// save character in session (for char select)
|
|
||||||
int UID = it->iID;
|
|
||||||
loginSessions[sock].characters[UID] = Player(*it);
|
|
||||||
loginSessions[sock].characters[UID].FEKey = sock->getFEKey();
|
|
||||||
|
|
||||||
// Equip info
|
|
||||||
for (int i = 0; i < AEQUIP_COUNT; i++) {
|
|
||||||
charInfo.aEquip[i] = it->Equip[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// set default to the first character
|
|
||||||
if (it == characters.begin())
|
|
||||||
loginSessions[sock].selectedChar = UID;
|
|
||||||
|
|
||||||
sock->sendPacket((void*)&charInfo, P_LS2CL_REP_CHAR_INFO, sizeof(sP_LS2CL_REP_CHAR_INFO));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
INITSTRUCT(sP_LS2CL_REP_LOGIN_FAIL, resp);
|
|
||||||
U8toU16(userLogin, resp.szID, sizeof(resp.szID));
|
|
||||||
resp.iErrorCode = errorCode;
|
|
||||||
sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_FAIL, sizeof(sP_LS2CL_REP_LOGIN_FAIL));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CNLoginServer::nameCheck(CNSocket* sock, CNPacketData* data) {
|
void CNLoginServer::nameCheck(CNSocket* sock, CNPacketData* data) {
|
||||||
@ -371,20 +377,20 @@ void CNLoginServer::shardSelect(CNSocket* sock, CNPacketData* data) {
|
|||||||
// tell client to connect to the shard server
|
// tell client to connect to the shard server
|
||||||
sP_CL2LS_REQ_SHARD_SELECT* shard = (sP_CL2LS_REQ_SHARD_SELECT*)data->buf;
|
sP_CL2LS_REQ_SHARD_SELECT* shard = (sP_CL2LS_REQ_SHARD_SELECT*)data->buf;
|
||||||
INITSTRUCT(sP_LS2CL_REP_SHARD_SELECT_SUCC, resp);
|
INITSTRUCT(sP_LS2CL_REP_SHARD_SELECT_SUCC, resp);
|
||||||
|
|
||||||
DEBUGLOG(
|
DEBUGLOG(
|
||||||
std::cout << "P_CL2LS_REQ_SHARD_SELECT:" << std::endl;
|
std::cout << "P_CL2LS_REQ_SHARD_SELECT:" << std::endl;
|
||||||
std::cout << "\tShard: " << (int)shard->ShardNum << std::endl;
|
std::cout << "\tShard: " << (int)shard->ShardNum << std::endl;
|
||||||
)
|
)
|
||||||
|
|
||||||
const char* SHARD_IP = settings::SHARDSERVERIP.c_str();
|
const char* SHARD_IP = settings::SHARDSERVERIP.c_str();
|
||||||
resp.iEnterSerialKey = rand();
|
resp.iEnterSerialKey = rand();
|
||||||
resp.g_FE_ServerPort = settings::SHARDPORT;
|
resp.g_FE_ServerPort = settings::SHARDPORT;
|
||||||
|
|
||||||
// copy IP to resp (this struct uses ASCII encoding so we don't have to goof around converting encodings)
|
// copy IP to resp (this struct uses ASCII encoding so we don't have to goof around converting encodings)
|
||||||
memcpy(resp.g_FE_ServerIP, SHARD_IP, strlen(SHARD_IP));
|
memcpy(resp.g_FE_ServerIP, SHARD_IP, strlen(SHARD_IP));
|
||||||
resp.g_FE_ServerIP[strlen(SHARD_IP)] = '\0';
|
resp.g_FE_ServerIP[strlen(SHARD_IP)] = '\0';
|
||||||
|
|
||||||
// pass player to CNSharedData
|
// pass player to CNSharedData
|
||||||
CNSharedData::setPlayer(resp.iEnterSerialKey, loginSessions[sock].characters[loginSessions[sock].selectedChar]);
|
CNSharedData::setPlayer(resp.iEnterSerialKey, loginSessions[sock].characters[loginSessions[sock].selectedChar]);
|
||||||
|
|
||||||
|
@ -46,6 +46,7 @@ private:
|
|||||||
static bool isPasswordCorrect(std::string actualPassword, std::string tryPassword);
|
static bool isPasswordCorrect(std::string actualPassword, std::string tryPassword);
|
||||||
static bool isAccountInUse(int accountId);
|
static bool isAccountInUse(int accountId);
|
||||||
static bool isCharacterNameGood(std::string Firstname, std::string Lastname);
|
static bool isCharacterNameGood(std::string Firstname, std::string Lastname);
|
||||||
|
static void newAccount(CNSocket* sock, std::string userLogin, std::string userPassword, int32_t clientVerC);
|
||||||
// returns true if success
|
// returns true if success
|
||||||
static bool exitDuplicate(int accountId);
|
static bool exitDuplicate(int accountId);
|
||||||
public:
|
public:
|
||||||
|
@ -166,6 +166,7 @@ int Database::addAccount(std::string login, std::string password) {
|
|||||||
account.Password = password;
|
account.Password = password;
|
||||||
account.Selected = 1;
|
account.Selected = 1;
|
||||||
account.Created = getTimestamp();
|
account.Created = getTimestamp();
|
||||||
|
account.LastLogin = account.Created;
|
||||||
return db.insert(account);
|
return db.insert(account);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user