2010-10-07 15:35:36 +02:00
/*
2014-01-01 00:07:53 +01:00
* Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/>
2010-10-07 15:35:36 +02:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2010-10-07 14:50:05 +02:00
*/
2009-10-17 15:51:44 -07:00
2014-07-06 12:17:27 -05:00
#include <memory>
2008-11-02 16:53:46 -06:00
#include "WorldSocket.h"
2010-06-07 16:21:52 -06:00
#include "BigNumber.h"
2014-07-06 01:26:29 +02:00
#include "Opcodes.h"
2010-08-06 19:23:43 +02:00
#include "ScriptMgr.h"
2014-07-06 01:26:29 +02:00
#include "SHA1.h"
2014-07-19 13:30:51 +02:00
#include "PacketLog.h"
2014-06-13 17:56:44 +02:00
#include "BattlenetAccountMgr.h"
2009-10-17 15:51:44 -07:00
2014-07-06 01:26:29 +02:00
using boost :: asio :: ip :: tcp ;
2012-07-18 20:14:51 +02:00
2014-07-10 13:36:54 -05:00
WorldSocket :: WorldSocket ( tcp :: socket && socket )
2014-07-26 23:26:01 +02:00
: Socket ( std :: move ( socket ), sizeof ( ClientPktHeader )), _authSeed ( rand32 ()), _OverSpeedPings ( 0 ), _worldSession ( nullptr )
2008-10-02 16:23:55 -05:00
{
2008-10-04 06:17:19 -05:00
}
2009-10-17 15:51:44 -07:00
2014-07-07 22:03:41 +02:00
void WorldSocket :: Start ()
2008-10-04 06:17:19 -05:00
{
2014-08-11 17:28:10 +02:00
sScriptMgr -> OnSocketOpen ( shared_from_this ());
2014-07-06 01:26:29 +02:00
AsyncReadHeader ();
2012-01-30 15:28:38 +01:00
// not an opcode. this packet sends raw string WORLD OF WARCRAFT CONNECTION - SERVER TO CLIENT"
2014-07-19 03:38:57 +02:00
// because of our implementation of WorldPacket sending, bytes "WO" become the opcode
2012-01-30 15:28:38 +01:00
WorldPacket packet ( MSG_VERIFY_CONNECTIVITY );
2011-11-20 18:28:18 +01:00
packet << "RLD OF WARCRAFT CONNECTION - SERVER TO CLIENT" ;
2014-07-19 03:38:57 +02:00
AsyncWrite ( packet );
2008-10-04 06:17:19 -05:00
}
2009-10-17 15:51:44 -07:00
2014-07-07 22:03:41 +02:00
void WorldSocket :: HandleSendAuthSession ()
2008-10-04 06:17:19 -05:00
{
2014-07-06 01:26:29 +02:00
WorldPacket packet ( SMSG_AUTH_CHALLENGE , 37 );
BigNumber seed1 ;
seed1 . SetRand ( 16 * 8 );
packet . append ( seed1 . AsByteArray ( 16 ). get (), 16 ); // new encryption seeds
2009-10-17 15:51:44 -07:00
2014-07-06 01:26:29 +02:00
BigNumber seed2 ;
seed2 . SetRand ( 16 * 8 );
packet . append ( seed2 . AsByteArray ( 16 ). get (), 16 ); // new encryption seeds
2009-10-17 15:51:44 -07:00
2014-07-19 03:38:57 +02:00
packet << uint32 ( _authSeed );
packet << uint8 ( 1 );
2014-07-06 01:26:29 +02:00
AsyncWrite ( packet );
2008-10-04 06:17:19 -05:00
}
2009-10-17 15:51:44 -07:00
2014-08-07 19:02:08 +02:00
void WorldSocket :: ReadHeaderHandler ()
2008-10-04 06:17:19 -05:00
{
2014-08-07 19:02:08 +02:00
_authCrypt . DecryptRecv ( GetHeaderBuffer (), sizeof ( ClientPktHeader ));
2009-10-17 15:51:44 -07:00
2014-08-07 19:02:08 +02:00
ClientPktHeader * header = reinterpret_cast < ClientPktHeader *> ( GetHeaderBuffer ());
EndianConvertReverse ( header -> size );
EndianConvert ( header -> cmd );
2009-10-17 15:51:44 -07:00
2014-08-07 19:02:08 +02:00
AsyncReadData ( header -> size - sizeof ( header -> cmd ));
2008-10-04 06:17:19 -05:00
}
2009-10-17 15:51:44 -07:00
2014-08-07 19:02:08 +02:00
void WorldSocket :: ReadDataHandler ()
2008-10-04 06:17:19 -05:00
{
2014-08-07 19:02:08 +02:00
ClientPktHeader * header = reinterpret_cast < ClientPktHeader *> ( GetHeaderBuffer ());
2009-10-17 15:51:44 -07:00
2014-08-07 19:02:08 +02:00
header -> size -= sizeof ( header -> cmd );
2009-10-17 15:51:44 -07:00
2014-07-27 12:09:32 +02:00
Opcodes opcode = PacketFilter :: DropHighBytes ( Opcodes ( header -> cmd ));
2009-10-17 15:51:44 -07:00
2014-08-07 19:02:08 +02:00
std :: string opcodeName = GetOpcodeNameForLogging ( opcode );
2009-10-17 15:51:44 -07:00
2014-08-07 19:02:08 +02:00
WorldPacket packet ( opcode , MoveData ());
2009-10-17 15:51:44 -07:00
2014-08-07 19:02:08 +02:00
if ( sPacketLog -> CanLogPacket ())
sPacketLog -> LogPacket ( packet , CLIENT_TO_SERVER , GetRemoteIpAddress (), GetRemotePort ());
2014-07-19 13:30:51 +02:00
2014-08-07 19:02:08 +02:00
TC_LOG_TRACE ( "network.opcode" , "C->S: %s %s" , ( _worldSession ? _worldSession -> GetPlayerInfo () : GetRemoteIpAddress (). to_string ()). c_str (), opcodeName . c_str ());
2014-07-19 13:30:51 +02:00
2014-08-07 19:02:08 +02:00
switch ( opcode )
{
case CMSG_PING :
HandlePing ( packet );
break ;
case CMSG_AUTH_SESSION :
if ( _worldSession )
2014-07-27 12:09:32 +02:00
{
2014-08-07 19:02:08 +02:00
TC_LOG_ERROR ( "network" , "WorldSocket::ProcessIncoming: received duplicate CMSG_AUTH_SESSION from %s" , _worldSession -> GetPlayerInfo (). c_str ());
2014-07-27 12:09:32 +02:00
break ;
}
2014-07-19 13:30:51 +02:00
2014-08-07 19:02:08 +02:00
HandleAuthSession ( packet );
break ;
case CMSG_KEEP_ALIVE :
TC_LOG_DEBUG ( "network" , "%s" , opcodeName . c_str ());
2014-08-11 17:28:10 +02:00
sScriptMgr -> OnPacketReceive ( _worldSession , packet );
2014-08-07 19:02:08 +02:00
break ;
2014-08-12 00:44:26 +02:00
case CMSG_LOG_DISCONNECT :
packet . rfinish (); // contains uint32 disconnectReason;
TC_LOG_DEBUG ( "network" , "%s" , opcodeName . c_str ());
sScriptMgr -> OnPacketReceive ( _worldSession , packet );
return ;
// not an opcode, client sends string "WORLD OF WARCRAFT CONNECTION - CLIENT TO SERVER" without opcode
// first 4 bytes become the opcode (2 dropped)
case MSG_VERIFY_CONNECTIVITY :
{
TC_LOG_DEBUG ( "network" , "%s" , opcodeName . c_str ());
sScriptMgr -> OnPacketReceive ( _worldSession , packet );
std :: string str ;
packet >> str ;
if ( str != "D OF WARCRAFT CONNECTION - CLIENT TO SERVER" )
2014-07-27 12:09:32 +02:00
{
2014-08-12 00:44:26 +02:00
CloseSocket ();
2014-07-27 12:09:32 +02:00
break ;
}
2014-07-26 23:26:01 +02:00
2014-08-12 00:44:26 +02:00
HandleSendAuthSession ();
break ;
}
case CMSG_ENABLE_NAGLE :
{
TC_LOG_DEBUG ( "network" , "%s" , opcodeName . c_str ());
sScriptMgr -> OnPacketReceive ( _worldSession , packet );
if ( _worldSession )
_worldSession -> HandleEnableNagleAlgorithm ();
break ;
}
2014-08-07 19:02:08 +02:00
default :
2014-07-26 23:26:01 +02:00
{
2014-08-07 19:02:08 +02:00
if ( ! _worldSession )
2014-07-06 01:26:29 +02:00
{
2014-08-07 19:02:08 +02:00
TC_LOG_ERROR ( "network.opcode" , "ProcessIncoming: Client not authed opcode = %u" , uint32 ( opcode ));
2014-07-26 23:26:01 +02:00
break ;
}
2014-08-16 11:23:26 +02:00
// Our Idle timer will reset on any non PING opcodes.
// Catches people idling on the login screen and any lingering ingame connections.
_worldSession -> ResetTimeOutTime ();
2014-08-07 19:02:08 +02:00
// Copy the packet to the heap before enqueuing
_worldSession -> QueuePacket ( new WorldPacket ( std :: move ( packet )));
break ;
}
2014-07-26 23:26:01 +02:00
}
2014-08-07 19:02:08 +02:00
AsyncReadHeader ();
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2014-07-27 17:46:46 +02:00
void WorldSocket :: AsyncWrite ( WorldPacket & packet )
2008-10-02 16:23:55 -05:00
{
2014-08-11 20:43:07 +02:00
if ( ! IsOpen ())
return ;
2014-07-19 13:30:51 +02:00
if ( sPacketLog -> CanLogPacket ())
2014-07-30 03:44:30 +01:00
sPacketLog -> LogPacket ( packet , SERVER_TO_CLIENT , GetRemoteIpAddress (), GetRemotePort ());
2014-07-19 13:30:51 +02:00
2014-08-12 00:44:26 +02:00
if ( _worldSession && packet . size () > 0x400 && ! packet . IsCompressed ())
2014-07-27 20:59:22 +02:00
packet . Compress ( _worldSession -> GetCompressionStream ());
2009-10-17 15:51:44 -07:00
2014-07-26 23:26:01 +02:00
TC_LOG_TRACE ( "network.opcode" , "S->C: %s %s" , ( _worldSession ? _worldSession -> GetPlayerInfo () : GetRemoteIpAddress (). to_string ()). c_str (), GetOpcodeNameForLogging ( packet . GetOpcode ()). c_str ());
2009-10-17 15:51:44 -07:00
2014-07-06 01:26:29 +02:00
ServerPktHeader header ( packet . size () + 2 , packet . GetOpcode ());
2009-10-17 15:51:44 -07:00
2014-07-24 17:08:53 +02:00
std :: lock_guard < std :: mutex > guard ( _writeLock );
2009-10-17 15:51:44 -07:00
2014-07-24 17:08:53 +02:00
bool needsWriteStart = _writeQueue . empty ();
2014-07-27 17:46:46 +02:00
_authCrypt . EncryptSend ( header . header , header . getHeaderLength ());
2014-07-24 17:08:53 +02:00
2014-07-28 16:37:42 +02:00
_writeQueue . emplace ( header , packet );
2011-11-20 18:28:18 +01:00
2014-07-24 17:08:53 +02:00
if ( needsWriteStart )
AsyncWrite ( _writeQueue . front ());
}
2014-07-07 22:03:41 +02:00
void WorldSocket :: HandleAuthSession ( WorldPacket & recvPacket )
2008-10-02 16:23:55 -05:00
{
2008-11-06 16:10:28 -06:00
uint8 digest [ 20 ];
2012-02-23 14:07:58 +01:00
uint32 clientSeed ;
2013-01-16 11:17:14 +01:00
uint8 security ;
2013-01-16 15:29:41 +01:00
uint16 clientBuild ;
2011-11-20 18:28:18 +01:00
uint32 id ;
2012-07-04 18:24:05 +02:00
uint32 addonSize ;
2011-11-20 18:28:18 +01:00
LocaleConstant locale ;
2012-02-23 14:07:58 +01:00
std :: string account ;
SHA1Hash sha ;
2012-07-05 14:16:44 +02:00
BigNumber k ;
2014-07-11 10:22:27 +02:00
bool wardenActive = sWorld -> getBoolConfig ( CONFIG_WARDEN_ENABLED );
2012-07-04 18:24:05 +02:00
WorldPacket addonsData ;
2011-11-20 18:28:18 +01:00
2012-07-04 18:24:05 +02:00
recvPacket . read_skip < uint32 > ();
recvPacket . read_skip < uint32 > ();
2011-11-20 18:28:18 +01:00
recvPacket . read_skip < uint8 > ();
2012-07-04 18:24:05 +02:00
recvPacket >> digest [ 10 ];
recvPacket >> digest [ 18 ];
recvPacket >> digest [ 12 ];
recvPacket >> digest [ 5 ];
recvPacket . read_skip < uint64 > ();
recvPacket >> digest [ 15 ];
recvPacket >> digest [ 9 ];
recvPacket >> digest [ 19 ];
recvPacket >> digest [ 4 ];
recvPacket >> digest [ 7 ];
recvPacket >> digest [ 16 ];
recvPacket >> digest [ 3 ];
2011-11-20 18:28:18 +01:00
recvPacket >> clientBuild ;
2012-07-04 18:24:05 +02:00
recvPacket >> digest [ 8 ];
2011-11-20 18:28:18 +01:00
recvPacket . read_skip < uint32 > ();
recvPacket . read_skip < uint8 > ();
2012-07-04 18:24:05 +02:00
recvPacket >> digest [ 17 ];
recvPacket >> digest [ 6 ];
recvPacket >> digest [ 0 ];
recvPacket >> digest [ 1 ];
recvPacket >> digest [ 11 ];
2011-11-20 18:28:18 +01:00
recvPacket >> clientSeed ;
2012-07-04 18:24:05 +02:00
recvPacket >> digest [ 2 ];
2011-11-20 18:28:18 +01:00
recvPacket . read_skip < uint32 > ();
2012-07-04 18:24:05 +02:00
recvPacket >> digest [ 14 ];
recvPacket >> digest [ 13 ];
2009-10-17 15:51:44 -07:00
2012-07-04 18:24:05 +02:00
recvPacket >> addonSize ;
2014-04-25 22:59:22 +02:00
if ( addonSize )
{
addonsData . resize ( addonSize );
recvPacket . read (( uint8 * ) addonsData . contents (), addonSize );
}
2012-07-04 18:24:05 +02:00
2012-08-10 18:48:34 +02:00
recvPacket . ReadBit ();
uint32 accountNameLength = recvPacket . ReadBits ( 12 );
account = recvPacket . ReadString ( accountNameLength );
2010-12-23 23:25:44 +01:00
if ( sWorld -> IsClosed ())
2009-03-21 13:51:28 +01:00
{
2013-01-16 11:17:14 +01:00
SendAuthResponseError ( AUTH_REJECT );
2014-07-26 23:26:01 +02:00
TC_LOG_ERROR ( "network" , "WorldSocket::HandleAuthSession: World closed, denying client (%s)." , GetRemoteIpAddress (). to_string (). c_str ());
2014-07-05 19:41:18 -05:00
return ;
2009-03-21 13:51:28 +01:00
}
2009-10-17 15:51:44 -07:00
2014-07-29 23:45:34 +02:00
// Get the account information from the auth database
2013-02-07 16:15:23 +01:00
// 0 1 2 3 4 5 6 7 8
// SELECT id, sessionkey, last_ip, locked, expansion, mutetime, locale, recruiter, os FROM account WHERE username = ?
2014-06-08 14:54:37 +02:00
PreparedStatement * stmt ;
2014-06-08 17:58:28 +02:00
uint32 battlenetAccountId = 0 ;
2014-06-13 17:56:44 +02:00
uint8 battlenetAccountIndex = 0 ;
if ( Battlenet :: AccountMgr :: GetAccountIdAndIndex ( account , & battlenetAccountId , & battlenetAccountIndex ))
2014-06-08 14:54:37 +02:00
{
stmt = LoginDatabase . GetPreparedStatement ( LOGIN_SEL_ACCOUNT_INFO_BY_BNET );
2014-06-08 17:58:28 +02:00
stmt -> setUInt32 ( 0 , battlenetAccountId );
2014-06-13 17:56:44 +02:00
stmt -> setUInt8 ( 1 , battlenetAccountIndex );
2014-06-08 14:54:37 +02:00
}
else
{
stmt = LoginDatabase . GetPreparedStatement ( LOGIN_SEL_ACCOUNT_INFO_BY_NAME );
stmt -> setString ( 0 , account );
}
2012-05-30 08:01:02 +02:00
PreparedQueryResult result = LoginDatabase . Query ( stmt );
2009-10-17 15:51:44 -07:00
2008-11-06 16:10:28 -06:00
// Stop if the account is not found
if ( ! result )
2008-10-04 06:17:19 -05:00
{
2014-05-02 03:44:21 +02:00
// We can not log here, as we do not know the account. Thus, no accountId.
2013-01-16 11:17:14 +01:00
SendAuthResponseError ( AUTH_UNKNOWN_ACCOUNT );
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "WorldSocket::HandleAuthSession: Sent Auth Response (unknown account)." );
2014-07-05 19:41:18 -05:00
return ;
2008-10-04 06:17:19 -05:00
}
2009-10-17 15:51:44 -07:00
2010-05-12 22:03:07 +02:00
Field * fields = result -> Fetch ();
2009-10-17 15:51:44 -07:00
2013-02-07 16:15:23 +01:00
uint8 expansion = fields [ 4 ]. GetUInt8 ();
2010-12-23 23:25:44 +01:00
uint32 world_expansion = sWorld -> getIntConfig ( CONFIG_EXPANSION );
2010-04-07 19:14:10 +02:00
if ( expansion > world_expansion )
2008-10-20 12:23:56 -05:00
expansion = world_expansion ;
2009-10-17 15:51:44 -07:00
2014-05-02 03:44:21 +02:00
// For hook purposes, we get Remoteaddress at this point.
2014-07-26 23:26:01 +02:00
std :: string address = GetRemoteIpAddress (). to_string ();
2014-05-02 03:44:21 +02:00
// As we don't know if attempted login process by ip works, we update last_attempt_ip right away
stmt = LoginDatabase . GetPreparedStatement ( LOGIN_UPD_LAST_ATTEMPT_IP );
stmt -> setString ( 0 , address );
stmt -> setString ( 1 , account );
LoginDatabase . Execute ( stmt );
// This also allows to check for possible "hack" attempts on account
// id has to be fetched at this point, so that first actual account response that fails can be logged
id = fields [ 0 ]. GetUInt32 ();
2014-07-29 23:45:34 +02:00
///- Re-check ip locking (same check as in auth).
2010-05-12 22:03:07 +02:00
if ( fields [ 3 ]. GetUInt8 () == 1 ) // if ip is locked
2008-10-04 06:17:19 -05:00
{
2014-07-06 01:26:29 +02:00
if ( strcmp ( fields [ 2 ]. GetCString (), address . c_str ()))
2008-10-04 06:17:19 -05:00
{
2013-01-16 11:17:14 +01:00
SendAuthResponseError ( AUTH_FAILED );
2014-05-02 03:44:21 +02:00
TC_LOG_DEBUG ( "network" , "WorldSocket::HandleAuthSession: Sent Auth Response (Account IP differs. Original IP: %s, new IP: %s)." , fields [ 2 ]. GetCString (), address . c_str ());
// We could log on hook only instead of an additional db log, however action logger is config based. Better keep DB logging as well
sScriptMgr -> OnFailedAccountLogin ( id );
2014-07-05 19:41:18 -05:00
return ;
2008-10-04 06:17:19 -05:00
}
}
2009-10-17 15:51:44 -07:00
2012-07-04 18:24:05 +02:00
k . SetHexStr ( fields [ 1 ]. GetCString ());
2009-10-17 15:51:44 -07:00
2013-02-07 16:15:23 +01:00
int64 mutetime = fields [ 5 ]. GetInt64 ();
2011-07-27 17:51:57 +02:00
//! Negative mutetime indicates amount of seconds to be muted effective on next login - which is now.
if ( mutetime < 0 )
{
2011-07-28 23:44:39 +02:00
mutetime = time ( NULL ) + llabs ( mutetime );
2011-12-27 00:29:17 +01:00
2013-01-30 13:09:41 +02:00
PreparedStatement * stmt = LoginDatabase . GetPreparedStatement ( LOGIN_UPD_MUTE_TIME_LOGIN );
2011-12-27 00:29:17 +01:00
stmt -> setInt64 ( 0 , mutetime );
stmt -> setUInt32 ( 1 , id );
LoginDatabase . Execute ( stmt );
2011-07-27 17:51:57 +02:00
}
2009-10-17 15:51:44 -07:00
2014-07-06 01:26:29 +02:00
locale = LocaleConstant ( fields [ 6 ]. GetUInt8 ());
2010-09-28 08:21:51 +03:00
if ( locale >= TOTAL_LOCALES )
2008-11-06 16:10:28 -06:00
locale = LOCALE_enUS ;
2009-10-17 15:51:44 -07:00
2013-02-07 16:15:23 +01:00
uint32 recruiter = fields [ 7 ]. GetUInt32 ();
std :: string os = fields [ 8 ]. GetString ();
2010-08-17 19:18:09 -07:00
2012-11-12 00:31:08 +01:00
// Must be done before WorldSession is created
2014-05-02 03:44:21 +02:00
if ( wardenActive && os != "Win" && os != "OSX" )
2012-11-12 00:31:08 +01:00
{
2013-01-16 11:17:14 +01:00
SendAuthResponseError ( AUTH_REJECT );
2014-05-02 03:44:21 +02:00
TC_LOG_ERROR ( "network" , "WorldSocket::HandleAuthSession: Client %s attempted to log in using invalid client OS (%s)." , address . c_str (), os . c_str ());
2014-07-05 19:41:18 -05:00
return ;
2012-11-12 00:31:08 +01:00
}
2009-12-21 21:21:39 -06:00
// Checks gmlevel per Realm
2012-05-30 08:01:02 +02:00
stmt = LoginDatabase . GetPreparedStatement ( LOGIN_GET_GMLEVEL_BY_REALMID );
stmt -> setUInt32 ( 0 , id );
stmt -> setInt32 ( 1 , int32 ( realmID ));
result = LoginDatabase . Query ( stmt );
2010-04-07 19:14:10 +02:00
if ( ! result )
2010-01-10 02:12:02 +01:00
security = 0 ;
else
{
2010-05-12 22:03:07 +02:00
fields = result -> Fetch ();
2012-05-30 08:01:02 +02:00
security = fields [ 0 ]. GetUInt8 ();
2010-01-10 02:12:02 +01:00
}
2009-12-21 21:08:29 -06:00
2014-07-29 23:45:34 +02:00
// Re-check account ban (same check as in auth)
2012-05-30 08:01:02 +02:00
stmt = LoginDatabase . GetPreparedStatement ( LOGIN_SEL_BANS );
stmt -> setUInt32 ( 0 , id );
2014-05-02 03:44:21 +02:00
stmt -> setString ( 1 , address );
2012-05-30 08:01:02 +02:00
PreparedQueryResult banresult = LoginDatabase . Query ( stmt );
2009-10-17 15:51:44 -07:00
2008-11-06 16:10:28 -06:00
if ( banresult ) // if account banned
2008-10-04 06:17:19 -05:00
{
2013-01-16 11:17:14 +01:00
SendAuthResponseError ( AUTH_BANNED );
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "WorldSocket::HandleAuthSession: Sent Auth Response (Account banned)." );
2014-05-02 03:44:21 +02:00
sScriptMgr -> OnFailedAccountLogin ( id );
2014-07-05 19:41:18 -05:00
return ;
2008-10-06 04:14:44 -05:00
}
2009-10-17 15:51:44 -07:00
2008-11-06 16:10:28 -06:00
// Check locked state for server
2010-12-23 23:25:44 +01:00
AccountTypes allowedAccountType = sWorld -> GetPlayerSecurityLimit ();
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "network" , "Allowed Level: %u Player Level %u" , allowedAccountType , AccountTypes ( security ));
2011-11-20 18:28:18 +01:00
if ( allowedAccountType > SEC_PLAYER && AccountTypes ( security ) < allowedAccountType )
2008-10-06 04:14:44 -05:00
{
2013-01-16 11:17:14 +01:00
SendAuthResponseError ( AUTH_UNAVAILABLE );
2013-11-08 10:50:51 +01:00
TC_LOG_INFO ( "network" , "WorldSocket::HandleAuthSession: User tries to login but his security level is not enough" );
2014-05-02 03:44:21 +02:00
sScriptMgr -> OnFailedAccountLogin ( id );
2014-07-05 19:41:18 -05:00
return ;
2008-10-06 04:14:44 -05:00
}
2009-10-17 15:51:44 -07:00
2008-11-06 16:10:28 -06:00
// Check that Key and account name are the same on client and server
uint32 t = 0 ;
2009-10-17 15:51:44 -07:00
2012-07-04 18:24:05 +02:00
sha . UpdateData ( account );
sha . UpdateData (( uint8 * ) & t , 4 );
sha . UpdateData (( uint8 * ) & clientSeed , 4 );
2014-07-06 01:26:29 +02:00
sha . UpdateData (( uint8 * ) & _authSeed , 4 );
2012-07-04 18:24:05 +02:00
sha . UpdateBigNumbers ( & k , NULL );
2010-05-12 22:03:07 +02:00
sha . Finalize ();
2009-10-17 15:51:44 -07:00
2012-07-04 18:24:05 +02:00
if ( memcmp ( sha . GetDigest (), digest , 20 ))
{
2013-01-16 11:17:14 +01:00
SendAuthResponseError ( AUTH_FAILED );
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "WorldSocket::HandleAuthSession: Authentication failed for account: %u ('%s') address: %s" , id , account . c_str (), address . c_str ());
2014-07-05 19:41:18 -05:00
return ;
2012-07-04 18:24:05 +02:00
}
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "network" , "WorldSocket::HandleAuthSession: Client '%s' authenticated successfully from %s." ,
2011-11-20 18:28:18 +01:00
account . c_str (),
address . c_str ());
2009-10-17 15:51:44 -07:00
2011-08-03 09:28:12 -07:00
// Check if this user is by any chance a recruiter
2012-05-30 08:01:02 +02:00
stmt = LoginDatabase . GetPreparedStatement ( LOGIN_SEL_ACCOUNT_RECRUITER );
stmt -> setUInt32 ( 0 , id );
result = LoginDatabase . Query ( stmt );
2011-08-03 09:28:12 -07:00
bool isRecruiter = false ;
if ( result )
isRecruiter = true ;
2014-05-02 03:44:21 +02:00
// Update the last_ip in the database as it was successful for login
2012-05-30 08:01:02 +02:00
stmt = LoginDatabase . GetPreparedStatement ( LOGIN_UPD_LAST_IP );
2011-12-27 00:29:17 +01:00
stmt -> setString ( 0 , address );
stmt -> setString ( 1 , account );
2011-12-27 16:31:10 +01:00
LoginDatabase . Execute ( stmt );
2009-10-17 15:51:44 -07:00
2009-04-29 00:31:03 -05:00
// NOTE ATM the socket is single-threaded, have this in mind ...
2014-07-19 03:38:57 +02:00
_worldSession = new WorldSession ( id , battlenetAccountId , shared_from_this (), AccountTypes ( security ), expansion , mutetime , locale , recruiter , isRecruiter );
2009-10-17 15:51:44 -07:00
2014-07-06 01:26:29 +02:00
_authCrypt . Init ( & k );
2009-10-17 15:51:44 -07:00
2014-07-06 01:26:29 +02:00
_worldSession -> LoadGlobalAccountData ();
_worldSession -> LoadTutorialsData ();
2014-07-19 13:40:14 +02:00
_worldSession -> ReadAddonsInfo ( addonsData );
2014-07-06 01:26:29 +02:00
_worldSession -> LoadPermissions ();
2009-10-17 15:51:44 -07:00
2014-05-02 03:44:21 +02:00
// At this point, we can safely hook a successful login
sScriptMgr -> OnAccountLogin ( id );
2012-02-19 13:51:16 +01:00
// Initialize Warden system only if it is enabled by config
2014-05-02 03:44:21 +02:00
if ( wardenActive )
2014-07-06 01:26:29 +02:00
_worldSession -> InitWarden ( & k , os );
2012-02-19 13:51:16 +01:00
2014-07-06 01:26:29 +02:00
sWorld -> AddSession ( _worldSession );
}
2009-10-17 15:51:44 -07:00
2014-07-07 22:03:41 +02:00
void WorldSocket :: SendAuthResponseError ( uint8 code )
2014-07-06 01:26:29 +02:00
{
WorldPacket packet ( SMSG_AUTH_RESPONSE , 1 );
2014-07-19 03:38:57 +02:00
packet . WriteBit ( 0 ); // has queue info
packet . WriteBit ( 0 ); // has account info
2014-07-06 01:26:29 +02:00
packet << uint8 ( code );
AsyncWrite ( packet );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2014-07-07 22:03:41 +02:00
void WorldSocket :: HandlePing ( WorldPacket & recvPacket )
2008-10-02 16:23:55 -05:00
{
2008-11-06 16:10:28 -06:00
uint32 ping ;
uint32 latency ;
2009-10-17 15:51:44 -07:00
2008-11-06 16:10:28 -06:00
// Get the ping packet content
recvPacket >> latency ;
2012-07-26 18:36:26 +02:00
recvPacket >> ping ;
2009-10-17 15:51:44 -07:00
2014-07-06 18:35:04 +02:00
if ( _LastPingTime == steady_clock :: time_point ())
{
_LastPingTime = steady_clock :: now ();
}
2008-11-06 16:10:28 -06:00
else
2008-10-04 06:17:19 -05:00
{
2014-07-06 18:35:04 +02:00
steady_clock :: time_point now = steady_clock :: now ();
steady_clock :: duration diff = now - _LastPingTime ;
_LastPingTime = now ;
2009-10-17 15:51:44 -07:00
2014-07-06 18:35:04 +02:00
if ( diff < seconds ( 27 ))
2008-10-04 06:17:19 -05:00
{
2014-07-06 18:35:04 +02:00
++ _OverSpeedPings ;
2009-10-17 15:51:44 -07:00
2014-07-06 18:35:04 +02:00
uint32 maxAllowed = sWorld -> getIntConfig ( CONFIG_MAX_OVERSPEED_PINGS );
2009-10-17 15:51:44 -07:00
2014-07-06 18:35:04 +02:00
if ( maxAllowed && _OverSpeedPings > maxAllowed )
2008-10-04 06:17:19 -05:00
{
2014-07-06 18:35:04 +02:00
if ( _worldSession && ! _worldSession -> HasPermission ( rbac :: RBAC_PERM_SKIP_CHECK_OVERSPEED_PING ))
2008-10-04 06:17:19 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "WorldSocket::HandlePing: %s kicked for over-speed pings (address: %s)" ,
2014-07-26 23:26:01 +02:00
_worldSession -> GetPlayerInfo (). c_str (), GetRemoteIpAddress (). to_string (). c_str ());
2009-10-17 15:51:44 -07:00
2014-07-25 18:01:27 +01:00
CloseSocket ();
2014-07-06 18:35:04 +02:00
return ;
2008-10-04 06:17:19 -05:00
}
}
}
2008-11-06 16:10:28 -06:00
else
2014-07-06 18:35:04 +02:00
_OverSpeedPings = 0 ;
2008-10-04 06:17:19 -05:00
}
2009-10-17 15:51:44 -07:00
2014-07-06 18:35:04 +02:00
if ( _worldSession )
{
_worldSession -> SetLatency ( latency );
_worldSession -> ResetClientTimeDelay ();
}
else
2008-11-06 16:10:28 -06:00
{
2014-07-26 23:26:01 +02:00
TC_LOG_ERROR ( "network" , "WorldSocket::HandlePing: peer sent CMSG_PING, but is not authenticated or got recently kicked, address = %s" , GetRemoteIpAddress (). to_string (). c_str ());
2009-10-17 15:51:44 -07:00
2014-07-25 18:01:27 +01:00
CloseSocket ();
2014-07-06 18:35:04 +02:00
return ;
2008-11-06 16:10:28 -06:00
}
2009-10-17 15:51:44 -07:00
2012-07-12 14:16:20 +02:00
WorldPacket packet ( SMSG_PONG , 4 );
2008-11-06 16:10:28 -06:00
packet << ping ;
2014-07-06 18:35:04 +02:00
return AsyncWrite ( packet );
2013-01-16 15:29:41 +01:00
}
2014-08-11 17:28:10 +02:00
void WorldSocket :: CloseSocket ()
{
sScriptMgr -> OnSocketClose ( shared_from_this ());
Socket :: CloseSocket ();
}