Moved loading account info from rest session to session

This commit is contained in:
hondacrx
2017-10-02 12:17:54 -04:00
parent 780899e1d1
commit 340aac5659
4 changed files with 137 additions and 151 deletions
+76 -97
View File
@@ -92,112 +92,91 @@ namespace BNetServer.Networking
}
}
PreparedStatement stmt = DB.Login.GetPreparedStatement(LoginStatements.SEL_BNET_ACCOUNT_INFO);
PreparedStatement stmt = DB.Login.GetPreparedStatement(LoginStatements.SEL_BNET_AUTHENTICATION);
stmt.AddValue(0, login);
SQLResult result = DB.Login.Query(stmt);
if (result.IsEmpty())
if (!result.IsEmpty())
{
uint accountId = result.Read<uint>(0);
string pass_hash = result.Read<string>(1);
uint failedLogins = result.Read<uint>(2);
string loginTicket = result.Read<string>(3);
uint loginTicketExpiry = result.Read<uint>(4);
bool isBanned = result.Read<ulong>(5) != 0;
if (CalculateShaPassHash(login, password) == pass_hash)
{
if (loginTicket.IsEmpty() || loginTicketExpiry < Time.UnixTime)
{
byte[] ticket = new byte[0].GenerateRandomKey(20);
loginTicket = "TC-" + ticket.ToHexString();
}
stmt = DB.Login.GetPreparedStatement(LoginStatements.UPD_BNET_AUTHENTICATION);
stmt.AddValue(0, loginTicket);
stmt.AddValue(1, Time.UnixTime + 3600);
stmt.AddValue(2, accountId);
DB.Login.Execute(stmt);
loginResult.LoginTicket = loginTicket;
}
else if (!isBanned)
{
uint maxWrongPassword = ConfigMgr.GetDefaultValue("WrongPass.MaxCount", 0u);
if (ConfigMgr.GetDefaultValue("WrongPass.Logging", false))
Log.outDebug(LogFilter.Network, "[{0}, Account {1}, Id {2}] Attempted to connect with wrong password!", request.Host, login, accountId);
if (maxWrongPassword != 0)
{
SQLTransaction trans = new SQLTransaction();
stmt = DB.Login.GetPreparedStatement(LoginStatements.UPD_BNET_FAILED_LOGINS);
stmt.AddValue(0, accountId);
trans.Append(stmt);
++failedLogins;
Log.outDebug(LogFilter.Network, "MaxWrongPass : {0}, failed_login : {1}", maxWrongPassword, accountId);
if (failedLogins >= maxWrongPassword)
{
BanMode banType = ConfigMgr.GetDefaultValue("WrongPass.BanType", BanMode.Ip);
int banTime = ConfigMgr.GetDefaultValue("WrongPass.BanTime", 600);
if (banType == BanMode.Account)
{
stmt = DB.Login.GetPreparedStatement(LoginStatements.INS_BNET_ACCOUNT_AUTO_BANNED);
stmt.AddValue(0, accountId);
}
else
{
stmt = DB.Login.GetPreparedStatement(LoginStatements.INS_IP_AUTO_BANNED);
stmt.AddValue(0, request.Host);
}
stmt.AddValue(1, banTime);
trans.Append(stmt);
stmt = DB.Login.GetPreparedStatement(LoginStatements.UPD_BNET_RESET_FAILED_LOGINS);
stmt.AddValue(0, accountId);
trans.Append(stmt);
}
DB.Login.CommitTransaction(trans);
}
}
loginResult.AuthenticationState = "DONE";
SendResponse(HttpCode.Ok, loginResult);
}
else
{
loginResult.AuthenticationState = "LOGIN";
loginResult.ErrorCode = "UNABLE_TO_DECODE";
loginResult.ErrorMessage = "There was an internal error while connecting to Battle.net. Please try again later.";
SendResponse(HttpCode.BadRequest, loginResult);
return;
}
string pass_hash = result.Read<string>(13);
var accountInfo = new AccountInfo();
accountInfo.LoadResult(result);
if (CalculateShaPassHash(login, password) == pass_hash)
{
stmt = DB.Login.GetPreparedStatement(LoginStatements.SEL_BNET_CHARACTER_COUNTS_BY_BNET_ID);
stmt.AddValue(0, accountInfo.Id);
SQLResult characterCountsResult = DB.Login.Query(stmt);
if (!characterCountsResult.IsEmpty())
{
do
{
accountInfo.GameAccounts[characterCountsResult.Read<uint>(0)]
.CharacterCounts[new RealmHandle(characterCountsResult.Read<byte>(3), characterCountsResult.Read<byte>(4), characterCountsResult.Read<uint>(2)).GetAddress()] = characterCountsResult.Read<byte>(1);
} while (characterCountsResult.NextRow());
}
stmt = DB.Login.GetPreparedStatement(LoginStatements.SEL_BNET_LAST_PLAYER_CHARACTERS);
stmt.AddValue(0, accountInfo.Id);
SQLResult lastPlayerCharactersResult = DB.Login.Query(stmt);
if (!lastPlayerCharactersResult.IsEmpty())
{
RealmHandle realmId = new RealmHandle(lastPlayerCharactersResult.Read<byte>(1), lastPlayerCharactersResult.Read<byte>(2), lastPlayerCharactersResult.Read<uint>(3));
LastPlayedCharacterInfo lastPlayedCharacter = new LastPlayedCharacterInfo();
lastPlayedCharacter.RealmId = realmId;
lastPlayedCharacter.CharacterName = lastPlayerCharactersResult.Read<string>(4);
lastPlayedCharacter.CharacterGUID = lastPlayerCharactersResult.Read<ulong>(5);
lastPlayedCharacter.LastPlayedTime = lastPlayerCharactersResult.Read<uint>(6);
accountInfo.GameAccounts[lastPlayerCharactersResult.Read<uint>(0)].LastPlayedCharacters[realmId.GetSubRegionAddress()] = lastPlayedCharacter;
}
byte[] ticket = new byte[0].GenerateRandomKey(20);
loginResult.LoginTicket = "TC-" + ticket.ToHexString();
Global.SessionMgr.AddLoginTicket(loginResult.LoginTicket, accountInfo);
}
else if (!accountInfo.IsBanned)
{
uint maxWrongPassword = ConfigMgr.GetDefaultValue("WrongPass.MaxCount", 0u);
if (ConfigMgr.GetDefaultValue("WrongPass.Logging", false))
Log.outDebug(LogFilter.Network, "[{0}, Account {1}, Id {2}] Attempted to connect with wrong password!", request.Host, login, accountInfo.Id);
if (maxWrongPassword != 0)
{
SQLTransaction trans = new SQLTransaction();
stmt = DB.Login.GetPreparedStatement(LoginStatements.UPD_BNET_FAILED_LOGINS);
stmt.AddValue(0, accountInfo.Id);
trans.Append(stmt);
++accountInfo.FailedLogins;
Log.outDebug(LogFilter.Network, "MaxWrongPass : {0}, failed_login : {1}", maxWrongPassword, accountInfo.Id);
if (accountInfo.FailedLogins >= maxWrongPassword)
{
BanMode banType = ConfigMgr.GetDefaultValue("WrongPass.BanType", BanMode.Ip);
int banTime = ConfigMgr.GetDefaultValue("WrongPass.BanTime", 600);
if (banType == BanMode.Account)
{
stmt = DB.Login.GetPreparedStatement(LoginStatements.INS_BNET_ACCOUNT_AUTO_BANNED);
stmt.AddValue(0, accountInfo.Id);
}
else
{
stmt = DB.Login.GetPreparedStatement(LoginStatements.INS_IP_AUTO_BANNED);
stmt.AddValue(0, request.Host);
}
stmt.AddValue(1, banTime);
trans.Append(stmt);
stmt = DB.Login.GetPreparedStatement(LoginStatements.UPD_BNET_RESET_FAILED_LOGINS);
stmt.AddValue(0, accountInfo.Id);
trans.Append(stmt);
}
DB.Login.CommitTransaction(trans);
}
}
loginResult.AuthenticationState = "DONE";
SendResponse(HttpCode.Ok, loginResult);
}
void SendResponse<T>(HttpCode code, T response)
+54 -8
View File
@@ -214,6 +214,7 @@ namespace BNetServer.Networking
_locale = logonRequest.Locale;
_os = logonRequest.Platform;
_build = (uint)logonRequest.ApplicationVersion;
var endpoint = Global.SessionMgr.GetAddressForClient(GetRemoteIpAddress());
@@ -227,12 +228,54 @@ namespace BNetServer.Networking
public BattlenetRpcErrorCode HandleVerifyWebCredentials(Bgs.Protocol.Authentication.V1.VerifyWebCredentialsRequest verifyWebCredentialsRequest)
{
Bgs.Protocol.Authentication.V1.LogonResult logonResult = new Bgs.Protocol.Authentication.V1.LogonResult();
logonResult.ErrorCode = 0;
_accountInfo = Global.SessionMgr.VerifyLoginTicket(verifyWebCredentialsRequest.WebCredentials.ToStringUtf8());
if (_accountInfo == null)
PreparedStatement stmt = DB.Login.GetPreparedStatement(LoginStatements.SEL_BNET_ACCOUNT_INFO);
stmt.AddValue(0, verifyWebCredentialsRequest.WebCredentials.ToStringUtf8());
SQLResult result = DB.Login.Query(stmt);
if (result.IsEmpty())
return BattlenetRpcErrorCode.Denied;
_accountInfo = new AccountInfo();
_accountInfo.LoadResult(result);
if (_accountInfo.LoginTicketExpiry < Time.UnixTime)
return BattlenetRpcErrorCode.TimedOut;
stmt = DB.Login.GetPreparedStatement(LoginStatements.SEL_BNET_CHARACTER_COUNTS_BY_BNET_ID);
stmt.AddValue(0, _accountInfo.Id);
SQLResult characterCountsResult = DB.Login.Query(stmt);
if (!characterCountsResult.IsEmpty())
{
do
{
RealmHandle realmId = new RealmHandle(characterCountsResult.Read<byte>(3), characterCountsResult.Read<byte>(4), characterCountsResult.Read<uint>(2));
_accountInfo.GameAccounts[characterCountsResult.Read<uint>(0)].CharacterCounts[realmId.GetAddress()] = characterCountsResult.Read<byte>(1);
} while (characterCountsResult.NextRow());
}
stmt = DB.Login.GetPreparedStatement(LoginStatements.SEL_BNET_LAST_PLAYER_CHARACTERS);
stmt.AddValue(0, _accountInfo.Id);
SQLResult lastPlayerCharactersResult = DB.Login.Query(stmt);
if (!lastPlayerCharactersResult.IsEmpty())
{
do
{
RealmHandle realmId = new RealmHandle(lastPlayerCharactersResult.Read<byte>(1), lastPlayerCharactersResult.Read<byte>(2), lastPlayerCharactersResult.Read<uint>(3));
LastPlayedCharacterInfo lastPlayedCharacter = new LastPlayedCharacterInfo();
lastPlayedCharacter.RealmId = realmId;
lastPlayedCharacter.CharacterName = lastPlayerCharactersResult.Read<string>(4);
lastPlayedCharacter.CharacterGUID = lastPlayerCharactersResult.Read<ulong>(5);
lastPlayedCharacter.LastPlayedTime = lastPlayerCharactersResult.Read<uint>(6);
_accountInfo.GameAccounts[lastPlayerCharactersResult.Read<uint>(0)].LastPlayedCharacters[realmId.GetSubRegionAddress()] = lastPlayedCharacter;
} while (lastPlayerCharactersResult.NextRow());
}
string ip_address = GetRemoteIpAddress().ToString();
// If the IP is 'locked', check that the player comes indeed from the correct IP address
@@ -274,6 +317,8 @@ namespace BNetServer.Networking
}
}
Bgs.Protocol.Authentication.V1.LogonResult logonResult = new Bgs.Protocol.Authentication.V1.LogonResult();
logonResult.ErrorCode = 0;
logonResult.AccountId = new EntityId();
logonResult.AccountId.Low = _accountInfo.Id;
logonResult.AccountId.High = 0x100000000000000;
@@ -412,15 +457,16 @@ namespace BNetServer.Networking
if (_gameAccountInfo == null)
return BattlenetRpcErrorCode.UtilServerInvalidIdentityArgs;
bool clientInfoOk = false;
Variant clientInfo = GetParam(Params, "Param_ClientInfo");
if (clientInfo != null)
{
var realmListTicketClientInformation = Json.CreateObject<RealmListTicketClientInformation>(clientInfo.BlobValue.ToStringUtf8(), true);
_build = (uint)realmListTicketClientInformation.Info.ClientVersion.Build;
clientInfoOk = true;
_clientSecret.AddRange(realmListTicketClientInformation.Info.Secret.Select(x => Convert.ToByte(x)).ToArray());
}
if (_build == 0)
if (!clientInfoOk)
return BattlenetRpcErrorCode.WowServicesDeniedRealmListTicket;
PreparedStatement stmt = DB.Login.GetPreparedStatement(LoginStatements.UPD_BNET_LAST_LOGIN_INFO);
@@ -592,7 +638,7 @@ namespace BNetServer.Networking
IsLockedToIP = result.Read<bool>(2);
LockCountry = result.Read<string>(3);
LastIP = result.Read<string>(4);
FailedLogins = result.Read<uint>(5);
LoginTicketExpiry = result.Read<uint>(5);
IsBanned = result.Read<ulong>(6) != 0;
IsPermanenetlyBanned = result.Read<ulong>(7) != 0;
PasswordVerifier = result.Read<string>(9);
@@ -614,7 +660,7 @@ namespace BNetServer.Networking
public bool IsLockedToIP;
public string LockCountry;
public string LastIP;
public uint FailedLogins;
public uint LoginTicketExpiry;
public bool IsBanned;
public bool IsPermanenetlyBanned;
public string PasswordVerifier;