2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-08-15 16:11:24 +02:00
|
|
|
#include "protocollogin.h"
|
|
|
|
#include <framework/net/outputmessage.h>
|
|
|
|
#include <framework/net/rsa.h>
|
|
|
|
#include <framework/luascript/luainterface.h>
|
|
|
|
#include <boost/bind.hpp>
|
2011-08-31 18:49:51 +02:00
|
|
|
#include <otclient/core/thingstype.h>
|
|
|
|
#include <otclient/core/spritemanager.h>
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2011-11-16 21:07:52 +01:00
|
|
|
void ProtocolLogin::login(const std::string& host, int port, const std::string& accountName, const std::string& accountPassword)
|
2011-08-15 16:11:24 +02:00
|
|
|
{
|
|
|
|
if(accountName.empty() || accountPassword.empty()) {
|
|
|
|
callLuaField("onError", "You must enter an account name and password.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_accountName = accountName;
|
|
|
|
m_accountPassword = accountPassword;
|
|
|
|
|
2011-11-16 21:07:52 +01:00
|
|
|
connect(host, port);
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProtocolLogin::onConnect()
|
|
|
|
{
|
|
|
|
sendLoginPacket();
|
|
|
|
}
|
|
|
|
|
2011-08-16 05:27:46 +02:00
|
|
|
void ProtocolLogin::onRecv(InputMessage& inputMessage)
|
|
|
|
{
|
2011-12-01 23:25:32 +01:00
|
|
|
try {
|
|
|
|
while(!inputMessage.eof()) {
|
2012-05-02 02:41:42 +02:00
|
|
|
int opcode = inputMessage.getU8();
|
|
|
|
switch(opcode) {
|
2012-01-08 19:29:41 +01:00
|
|
|
case Proto::LoginServerError:
|
2011-12-01 23:25:32 +01:00
|
|
|
parseError(inputMessage);
|
|
|
|
break;
|
2012-01-08 19:29:41 +01:00
|
|
|
case Proto::LoginServerMotd:
|
2011-12-01 23:25:32 +01:00
|
|
|
parseMOTD(inputMessage);
|
|
|
|
break;
|
2012-01-08 19:29:41 +01:00
|
|
|
case Proto::LoginServerUpdateNeeded:
|
2011-12-01 23:25:32 +01:00
|
|
|
callLuaField("onError", "Client needs update.");
|
|
|
|
break;
|
2012-01-08 19:29:41 +01:00
|
|
|
case Proto::LoginServerCharacterList:
|
2011-12-01 23:25:32 +01:00
|
|
|
parseCharacterList(inputMessage);
|
|
|
|
break;
|
|
|
|
default:
|
2012-05-02 02:41:42 +02:00
|
|
|
Fw::throwException("unknown opt byte ", opcode);
|
2011-12-01 23:25:32 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-08-16 05:27:46 +02:00
|
|
|
}
|
2011-12-01 23:25:32 +01:00
|
|
|
} catch(Exception& e) {
|
|
|
|
logTraceError(e.what());
|
2011-08-16 05:27:46 +02:00
|
|
|
}
|
|
|
|
disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtocolLogin::onError(const boost::system::error_code& error)
|
|
|
|
{
|
2012-01-07 23:25:35 +01:00
|
|
|
callLuaField("onError", error.message(), true);
|
2012-01-13 16:41:57 +01:00
|
|
|
disconnect();
|
2011-08-16 05:27:46 +02:00
|
|
|
}
|
|
|
|
|
2011-08-15 16:11:24 +02:00
|
|
|
void ProtocolLogin::sendLoginPacket()
|
|
|
|
{
|
2012-05-11 20:02:57 +02:00
|
|
|
OutputMessage msg;
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2012-05-11 20:02:57 +02:00
|
|
|
msg.addU8(Proto::ClientEnterAccount);
|
2012-05-11 21:25:29 +02:00
|
|
|
msg.addU16(Proto::ClientOs);
|
2012-05-11 20:02:57 +02:00
|
|
|
msg.addU16(Proto::ClientVersion);
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2012-05-11 20:02:57 +02:00
|
|
|
msg.addU32(g_thingsType.getSignature()); // data signature
|
|
|
|
msg.addU32(g_sprites.getSignature()); // sprite signature
|
|
|
|
msg.addU32(Proto::PicSignature); // pic signature
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2012-05-11 20:02:57 +02:00
|
|
|
int paddingBytes = 128;
|
|
|
|
msg.addU8(0); // first RSA byte must be 0
|
|
|
|
paddingBytes -= 1;
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2011-08-16 06:24:20 +02:00
|
|
|
// xtea key
|
2011-08-16 05:27:46 +02:00
|
|
|
generateXteaKey();
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2012-05-11 20:02:57 +02:00
|
|
|
msg.addU32(m_xteaKey[0]);
|
|
|
|
msg.addU32(m_xteaKey[1]);
|
|
|
|
msg.addU32(m_xteaKey[2]);
|
|
|
|
msg.addU32(m_xteaKey[3]);
|
|
|
|
paddingBytes -= 16;
|
|
|
|
|
2012-05-11 20:35:17 +02:00
|
|
|
#if PROTOCOL>=854
|
2012-05-11 20:02:57 +02:00
|
|
|
enableChecksum();
|
|
|
|
|
|
|
|
msg.addString(m_accountName);
|
|
|
|
msg.addString(m_accountPassword);
|
|
|
|
paddingBytes -= 4 + m_accountName.length() + m_accountPassword.length();
|
|
|
|
#elif PROTOCOL>=810
|
|
|
|
msg.addU32(Fw::fromstring<uint32>(m_accountName));
|
|
|
|
msg.addString(m_accountPassword);
|
|
|
|
paddingBytes -= 6 + m_accountPassword.length();
|
|
|
|
#endif
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2012-05-11 20:02:57 +02:00
|
|
|
msg.addPaddingBytes(paddingBytes); // complete the 128 bytes for rsa encryption with zeros
|
|
|
|
Rsa::encrypt((char*)msg.getWriteBuffer() - 128, 128, Proto::RSA);
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2012-05-11 20:02:57 +02:00
|
|
|
send(msg);
|
2011-08-16 05:27:46 +02:00
|
|
|
enableXteaEncryption();
|
2011-08-15 16:11:24 +02:00
|
|
|
recv();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtocolLogin::parseError(InputMessage& inputMessage)
|
|
|
|
{
|
|
|
|
std::string error = inputMessage.getString();
|
2012-01-07 23:25:35 +01:00
|
|
|
callLuaField("onError", error, false);
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProtocolLogin::parseMOTD(InputMessage& inputMessage)
|
|
|
|
{
|
|
|
|
std::string motd = inputMessage.getString();
|
|
|
|
callLuaField("onMotd", motd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtocolLogin::parseCharacterList(InputMessage& inputMessage)
|
|
|
|
{
|
2011-08-26 20:00:22 +02:00
|
|
|
typedef std::tuple<std::string, std::string, std::string, int> CharacterInfo;
|
|
|
|
typedef std::vector<CharacterInfo> CharaterList;
|
|
|
|
CharaterList charList;
|
|
|
|
|
|
|
|
int numCharacters = inputMessage.getU8();
|
|
|
|
for(int i = 0; i < numCharacters; ++i) {
|
2011-08-15 16:11:24 +02:00
|
|
|
std::string name = inputMessage.getString();
|
|
|
|
std::string world = inputMessage.getString();
|
|
|
|
uint32 ip = inputMessage.getU32();
|
|
|
|
uint16 port = inputMessage.getU16();
|
2011-08-28 18:02:26 +02:00
|
|
|
charList.push_back(CharacterInfo(name, world, Fw::ip2str(ip), port));
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
2011-08-26 20:00:22 +02:00
|
|
|
int premDays = inputMessage.getU16();
|
|
|
|
|
|
|
|
callLuaField("onCharacterList", charList, premDays);
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|