/
protocollogin.cpp
171 lines (145 loc) · 5.29 KB
/
protocollogin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// 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, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//You need also accept Additional conditions.
//Additional conditions:
//1. Software's author gives no guarantee and takes no responsibility for working and any possible uncommon effects or/and faults caused.
//2. Any individuals using, modyfing and sharing this software cannot remove any information like authorship, software's sources, properties of DevLand.exe file and information attached in programe icon i.e. software's title.
//
//Terms and conditions apply.
//////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "protocollogin.h"
#include "outputmessage.h"
#include "connection.h"
#include "rsa.h"
#include "configmanager.h"
#include "tools.h"
#include "ioaccount.h"
#include "ban.h"
#include <iomanip>
#include "game.h"
extern RSA* g_otservRSA;
extern ConfigManager g_config;
extern IPList serverIPs;
extern Ban g_bans;
extern Game g_game;
#ifdef __DEBUG_NET_DETAIL__
void ProtocolLogin::deleteProtocolTask()
{
std::cout << "Deleting ProtocolLogin" << std::endl;
Protocol::deleteProtocolTask();
}
#endif
void ProtocolLogin::disconnectClient(uint8_t error, const char* message)
{
OutputMessage* output = OutputMessagePool::getInstance()->getOutputMessage(this, false);
output->AddByte(error);
output->AddString(message);
OutputMessagePool::getInstance()->send(output);
getConnection()->closeConnection();
}
bool ProtocolLogin::parseFirstPacket(NetworkMessage& msg)
{
if(g_game.getGameState() == GAME_STATE_SHUTDOWN){
getConnection()->closeConnection();
return false;
}
uint32_t clientip = getConnection()->getIP();
/*uint16_t clientos =*/ msg.GetU16();
uint16_t version = msg.GetU16();
msg.SkipBytes(12);
if(version <= 760){
disconnectClient(0x0A, STRING_CLIENT_VERSION);
}
if(!RSA_decrypt(g_otservRSA, msg)){
getConnection()->closeConnection();
return false;
}
uint32_t key[4];
key[0] = msg.GetU32();
key[1] = msg.GetU32();
key[2] = msg.GetU32();
key[3] = msg.GetU32();
enableXTEAEncryption();
setXTEAKey(key);
uint32_t accnumber = msg.GetU32();
std::string password = msg.GetString();
if(!accnumber){
disconnectClient(0x0A, "You must enter your account number.");
return false;
}
if(version < CLIENT_VERSION_MIN || version > CLIENT_VERSION_MAX){
disconnectClient(0x0A, STRING_CLIENT_VERSION);
return false;
}
if(g_game.getGameState() == GAME_STATE_STARTUP){
disconnectClient(0x0A, "Gameworld is starting up. Please wait.");
return false;
}
if(g_bans.isIpDisabled(clientip)){
disconnectClient(0x0A, "Too many connections attempts from this IP. Try again later.");
return false;
}
if(g_bans.isIpBanished(clientip)){
disconnectClient(0x0A, "Your IP is banished!");
return false;
}
uint32_t serverip = serverIPs[0].first;
for(uint32_t i = 0; i < serverIPs.size(); i++){
if((serverIPs[i].first & serverIPs[i].second) == (clientip & serverIPs[i].second)){
serverip = serverIPs[i].first;
break;
}
}
Account account = IOAccount::instance()->loadAccount(accnumber);
if(!(accnumber != 0 && account.accnumber == accnumber &&
passwordTest(password, account.password))){
g_bans.addLoginAttempt(clientip, false);
disconnectClient(0x0A, "Please enter a valid account number and password.");
return false;
}
g_bans.addLoginAttempt(clientip, true);
OutputMessage* output = OutputMessagePool::getInstance()->getOutputMessage(this, false);
//Add MOTD
std::stringstream motd;
output->AddByte(0x14);
motd << g_config.getNumber(ConfigManager::MOTD_NUM) << "\n";
motd << g_config.getString(ConfigManager::MOTD);
output->AddString(motd.str());
//Add char list
output->AddByte(0x64);
output->AddByte((uint8_t)account.charList.size());
std::list<std::string>::iterator it;
for(it = account.charList.begin(); it != account.charList.end(); it++){
output->AddString((*it));
output->AddString(g_config.getString(ConfigManager::WORLD_NAME));
output->AddU32(serverip);
output->AddU16(g_config.getNumber(ConfigManager::PORT));
}
//Add premium days
output->AddU16(account.premiumDays);//output->AddU16(0);
OutputMessagePool::getInstance()->send(output);
getConnection()->closeConnection();
return true;
}
void ProtocolLogin::onRecvFirstMessage(NetworkMessage& msg)
{
parseFirstPacket(msg);
}