mirror of
https://github.com/EQEmu/Server.git
synced 2026-09-05 07:16:37 +00:00
Headless client work
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
|
||||
|
||||
SET(hc_sources
|
||||
eq.cpp
|
||||
main.cpp
|
||||
login.cpp
|
||||
world.cpp
|
||||
)
|
||||
|
||||
SET(hc_headers
|
||||
eq.h
|
||||
login.h
|
||||
world.h
|
||||
)
|
||||
|
||||
@@ -0,0 +1,340 @@
|
||||
#include "eq.h"
|
||||
#include "../common/net/dns.h"
|
||||
|
||||
const char* eqcrypt_block(const char *buffer_in, size_t buffer_in_sz, char* buffer_out, bool enc) {
|
||||
DES_key_schedule k;
|
||||
DES_cblock v;
|
||||
|
||||
memset(&k, 0, sizeof(DES_key_schedule));
|
||||
memset(&v, 0, sizeof(DES_cblock));
|
||||
|
||||
if (!enc && buffer_in_sz && buffer_in_sz % 8 != 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
DES_ncbc_encrypt((const unsigned char*)buffer_in, (unsigned char*)buffer_out, (long)buffer_in_sz, &k, &v, enc);
|
||||
return buffer_out;
|
||||
}
|
||||
|
||||
EverQuest::EverQuest(const std::string &host, int port, const std::string &user, const std::string &pass, const std::string &server, const std::string &character)
|
||||
{
|
||||
m_host = host;
|
||||
m_port = port;
|
||||
m_user = user;
|
||||
m_pass = pass;
|
||||
m_server = server;
|
||||
m_character = character;
|
||||
m_dbid = 0;
|
||||
|
||||
EQ::Net::DNSLookup(m_host, port, false, [&](const std::string &addr) {
|
||||
if (addr.empty()) {
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Could not resolve address: {0}", m_host);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
m_host = addr;
|
||||
m_login_connection_manager.reset(new EQ::Net::DaybreakConnectionManager());
|
||||
|
||||
m_login_connection_manager->OnNewConnection(std::bind(&EverQuest::LoginOnNewConnection, this, std::placeholders::_1));
|
||||
m_login_connection_manager->OnConnectionStateChange(std::bind(&EverQuest::LoginOnStatusChangeReconnectEnabled, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
||||
m_login_connection_manager->OnPacketRecv(std::bind(&EverQuest::LoginOnPacketRecv, this, std::placeholders::_1, std::placeholders::_2));
|
||||
|
||||
m_login_connection_manager->Connect(m_host, m_port);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
EverQuest::~EverQuest()
|
||||
{
|
||||
}
|
||||
|
||||
void EverQuest::LoginOnNewConnection(std::shared_ptr<EQ::Net::DaybreakConnection> connection)
|
||||
{
|
||||
m_login_connection = connection;
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Connecting...");
|
||||
}
|
||||
|
||||
void EverQuest::LoginOnStatusChangeReconnectEnabled(std::shared_ptr<EQ::Net::DaybreakConnection> conn, EQ::Net::DbProtocolStatus from, EQ::Net::DbProtocolStatus to)
|
||||
{
|
||||
if (to == EQ::Net::StatusConnected) {
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Login connected.");
|
||||
LoginSendSessionReady();
|
||||
}
|
||||
|
||||
if (to == EQ::Net::StatusDisconnected) {
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Login connection lost before we got to world, reconnecting.");
|
||||
m_key.clear();
|
||||
m_dbid = 0;
|
||||
m_login_connection.reset();
|
||||
m_login_connection_manager->Connect(m_host, m_port);
|
||||
}
|
||||
}
|
||||
|
||||
void EverQuest::LoginOnStatusChangeReconnectDisabled(std::shared_ptr<EQ::Net::DaybreakConnection> conn, EQ::Net::DbProtocolStatus from, EQ::Net::DbProtocolStatus to)
|
||||
{
|
||||
if (to == EQ::Net::StatusDisconnected) {
|
||||
m_login_connection.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void EverQuest::LoginOnPacketRecv(std::shared_ptr<EQ::Net::DaybreakConnection> conn, const EQ::Net::Packet & p)
|
||||
{
|
||||
auto opcode = p.GetUInt16(0);
|
||||
switch (opcode) {
|
||||
case 0x0017: //OP_ChatMessage
|
||||
LoginSendLogin();
|
||||
break;
|
||||
case 0x0018:
|
||||
LoginProcessLoginResponse(p);
|
||||
break;
|
||||
case 0x0019:
|
||||
LoginProcessServerPacketList(p);
|
||||
break;
|
||||
case 0x0022:
|
||||
LoginProcessServerPlayResponse(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void EverQuest::LoginSendSessionReady()
|
||||
{
|
||||
EQ::Net::DynamicPacket p;
|
||||
p.PutUInt16(0, 1); //OP_SessionReady
|
||||
p.PutUInt32(2, 2);
|
||||
|
||||
m_login_connection->QueuePacket(p);
|
||||
}
|
||||
|
||||
void EverQuest::LoginSendLogin()
|
||||
{
|
||||
size_t buffer_len = m_user.length() + m_pass.length() + 2;
|
||||
std::unique_ptr<char[]> buffer(new char[buffer_len]);
|
||||
|
||||
strcpy(&buffer[0], m_user.c_str());
|
||||
strcpy(&buffer[m_user.length() + 1], m_pass.c_str());
|
||||
|
||||
size_t encrypted_len = buffer_len;
|
||||
|
||||
if (encrypted_len % 8 > 0) {
|
||||
encrypted_len = ((encrypted_len / 8) + 1) * 8;
|
||||
}
|
||||
|
||||
EQ::Net::DynamicPacket p;
|
||||
p.Resize(12 + encrypted_len);
|
||||
p.PutUInt16(0, 2); //OP_Login
|
||||
p.PutUInt32(2, 3);
|
||||
|
||||
eqcrypt_block(&buffer[0], buffer_len, (char*)p.Data() + 12, true);
|
||||
|
||||
m_login_connection->QueuePacket(p);
|
||||
}
|
||||
|
||||
void EverQuest::LoginSendServerRequest()
|
||||
{
|
||||
EQ::Net::DynamicPacket p;
|
||||
p.PutUInt16(0, 4); //OP_ServerListRequest
|
||||
p.PutUInt32(2, 4);
|
||||
|
||||
m_login_connection->QueuePacket(p);
|
||||
}
|
||||
|
||||
void EverQuest::LoginSendPlayRequest(uint32_t id)
|
||||
{
|
||||
EQ::Net::DynamicPacket p;
|
||||
p.PutUInt16(0, 0x000d);
|
||||
p.PutUInt16(2, 5);
|
||||
p.PutUInt32(4, 0);
|
||||
p.PutUInt32(8, 0);
|
||||
p.PutUInt32(12, id);
|
||||
|
||||
m_login_connection->QueuePacket(p);
|
||||
}
|
||||
|
||||
void EverQuest::LoginProcessLoginResponse(const EQ::Net::Packet & p)
|
||||
{
|
||||
auto encrypt_size = p.Length() - 12;
|
||||
if (encrypt_size % 8 > 0) {
|
||||
encrypt_size = (encrypt_size / 8) * 8;
|
||||
}
|
||||
|
||||
std::unique_ptr<char[]> decrypted(new char[encrypt_size]);
|
||||
|
||||
eqcrypt_block((char*)p.Data() + 12, encrypt_size, &decrypted[0], false);
|
||||
|
||||
EQ::Net::StaticPacket sp(&decrypted[0], encrypt_size);
|
||||
auto response_error = sp.GetUInt16(1);
|
||||
|
||||
if (response_error > 101) {
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Error logging in response code: {0}", response_error);
|
||||
LoginDisableReconnect();
|
||||
}
|
||||
else {
|
||||
m_key = sp.GetCString(12);
|
||||
m_dbid = sp.GetUInt32(8);
|
||||
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Logged in successfully with dbid {0} and key {1}", m_dbid, m_key);
|
||||
LoginSendServerRequest();
|
||||
}
|
||||
}
|
||||
|
||||
void EverQuest::LoginProcessServerPacketList(const EQ::Net::Packet & p)
|
||||
{
|
||||
m_world_servers.clear();
|
||||
auto number_of_servers = p.GetUInt32(18);
|
||||
size_t idx = 22;
|
||||
|
||||
for (auto i = 0U; i < number_of_servers; ++i) {
|
||||
WorldServer ws;
|
||||
ws.address = p.GetCString(idx);
|
||||
idx += (ws.address.length() + 1);
|
||||
|
||||
ws.type = p.GetInt32(idx);
|
||||
idx += 4;
|
||||
|
||||
auto id = p.GetUInt32(idx);
|
||||
idx += 4;
|
||||
|
||||
ws.long_name = p.GetCString(idx);
|
||||
idx += (ws.long_name.length() + 1);
|
||||
|
||||
ws.lang = p.GetCString(idx);
|
||||
idx += (ws.lang.length() + 1);
|
||||
|
||||
ws.region = p.GetCString(idx);
|
||||
idx += (ws.region.length() + 1);
|
||||
|
||||
ws.status = p.GetInt32(idx);
|
||||
idx += 4;
|
||||
|
||||
ws.players = p.GetInt32(idx);
|
||||
idx += 4;
|
||||
|
||||
m_world_servers[id] = ws;
|
||||
}
|
||||
|
||||
for (auto server : m_world_servers) {
|
||||
if (server.second.long_name.compare(m_server) == 0) {
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Found world server {0}, attempting to login.", m_server);
|
||||
LoginSendPlayRequest(server.first);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Got response from login server but could not find world server {0} disconnecting.", m_server);
|
||||
LoginDisableReconnect();
|
||||
}
|
||||
|
||||
void EverQuest::LoginProcessServerPlayResponse(const EQ::Net::Packet &p)
|
||||
{
|
||||
auto allowed = p.GetUInt8(12);
|
||||
|
||||
if (allowed) {
|
||||
auto server = p.GetUInt32(18);
|
||||
auto ws = m_world_servers.find(server);
|
||||
if (ws != m_world_servers.end()) {
|
||||
ConnectToWorld();
|
||||
LoginDisableReconnect();
|
||||
}
|
||||
}
|
||||
else {
|
||||
auto message = p.GetUInt16(13);
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Failed to login to server with message {0}");
|
||||
LoginDisableReconnect();
|
||||
}
|
||||
}
|
||||
|
||||
void EverQuest::LoginDisableReconnect()
|
||||
{
|
||||
m_login_connection_manager->OnConnectionStateChange(std::bind(&EverQuest::LoginOnStatusChangeReconnectDisabled, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
||||
m_login_connection->Close();
|
||||
}
|
||||
|
||||
void EverQuest::ConnectToWorld()
|
||||
{
|
||||
m_world_connection_manager.reset(new EQ::Net::DaybreakConnectionManager());
|
||||
m_world_connection_manager->OnNewConnection(std::bind(&EverQuest::WorldOnNewConnection, this, std::placeholders::_1));
|
||||
m_world_connection_manager->OnConnectionStateChange(std::bind(&EverQuest::WorldOnStatusChangeReconnectEnabled, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
||||
m_world_connection_manager->OnPacketRecv(std::bind(&EverQuest::WorldOnPacketRecv, this, std::placeholders::_1, std::placeholders::_2));
|
||||
m_world_connection_manager->Connect(m_host, 9000);
|
||||
}
|
||||
|
||||
void EverQuest::WorldOnNewConnection(std::shared_ptr<EQ::Net::DaybreakConnection> connection)
|
||||
{
|
||||
m_world_connection = connection;
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Connecting to world...");
|
||||
}
|
||||
|
||||
void EverQuest::WorldOnStatusChangeReconnectEnabled(std::shared_ptr<EQ::Net::DaybreakConnection> conn, EQ::Net::DbProtocolStatus from, EQ::Net::DbProtocolStatus to)
|
||||
{
|
||||
if (to == EQ::Net::StatusConnected) {
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "World connected.");
|
||||
WorldSendClientAuth();
|
||||
}
|
||||
|
||||
if (to == EQ::Net::StatusDisconnected) {
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "World connection lost, reconnecting.");
|
||||
m_world_connection.reset();
|
||||
m_world_connection_manager->Connect(m_host, 9000);
|
||||
}
|
||||
}
|
||||
|
||||
void EverQuest::WorldOnStatusChangeReconnectDisabled(std::shared_ptr<EQ::Net::DaybreakConnection> conn, EQ::Net::DbProtocolStatus from, EQ::Net::DbProtocolStatus to)
|
||||
{
|
||||
if (to == EQ::Net::StatusDisconnected) {
|
||||
m_world_connection.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void EverQuest::WorldOnPacketRecv(std::shared_ptr<EQ::Net::DaybreakConnection> conn, const EQ::Net::Packet & p)
|
||||
{
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Packet in:\n{0}", p.ToString());
|
||||
auto opcode = p.GetUInt16(0);
|
||||
switch (opcode) {
|
||||
case 0x7499:
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "OP_ApproveWorld");
|
||||
break;
|
||||
case 0x7ceb:
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "OP_LogServer");
|
||||
break;
|
||||
case 0x578f:
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "OP_EnterWorld");
|
||||
break;
|
||||
case 0x6259:
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "OP_PostEnterWorld");
|
||||
break;
|
||||
case 0x590d:
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "OP_ExpansionInfo");
|
||||
break;
|
||||
case 0x507a:
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "OP_GuildsList");
|
||||
break;
|
||||
case 0x5475:
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "OP_SendMaxCharacters");
|
||||
break;
|
||||
case 0x7acc:
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "OP_SendMembership");
|
||||
break;
|
||||
case 0x057b:
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "OP_SendMembershipDetails");
|
||||
break;
|
||||
case 0x00d2:
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "OP_SendCharInfo");
|
||||
break;
|
||||
default:
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Ignored packet: {0:#x}", opcode);
|
||||
}
|
||||
}
|
||||
|
||||
void EverQuest::WorldSendClientAuth()
|
||||
{
|
||||
EQ::Net::DynamicPacket p;
|
||||
p.Resize(2 + 464);
|
||||
|
||||
p.PutUInt16(0, 0x7a09U);
|
||||
std::string dbid_str = std::to_string(m_dbid);
|
||||
|
||||
p.PutCString(2, dbid_str.c_str());
|
||||
p.PutCString(2 + dbid_str.length() + 1, m_key.c_str());
|
||||
|
||||
m_world_connection->QueuePacket(p);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
#include "../common/eqemu_logsys.h"
|
||||
#include "../common/net/daybreak_connection.h"
|
||||
#include "../common/event/timer.h"
|
||||
#include <openssl/des.h>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
struct WorldServer
|
||||
{
|
||||
std::string long_name;
|
||||
std::string address;
|
||||
int type;
|
||||
std::string lang;
|
||||
std::string region;
|
||||
int status;
|
||||
int players;
|
||||
};
|
||||
|
||||
class EverQuest
|
||||
{
|
||||
public:
|
||||
EverQuest(const std::string &host, int port, const std::string &user, const std::string &pass, const std::string &server, const std::string &character);
|
||||
~EverQuest();
|
||||
|
||||
private:
|
||||
//Login
|
||||
void LoginOnNewConnection(std::shared_ptr<EQ::Net::DaybreakConnection> connection);
|
||||
void LoginOnStatusChangeReconnectEnabled(std::shared_ptr<EQ::Net::DaybreakConnection> conn, EQ::Net::DbProtocolStatus from, EQ::Net::DbProtocolStatus to);
|
||||
void LoginOnStatusChangeReconnectDisabled(std::shared_ptr<EQ::Net::DaybreakConnection> conn, EQ::Net::DbProtocolStatus from, EQ::Net::DbProtocolStatus to);
|
||||
void LoginOnPacketRecv(std::shared_ptr<EQ::Net::DaybreakConnection> conn, const EQ::Net::Packet &p);
|
||||
|
||||
void LoginSendSessionReady();
|
||||
void LoginSendLogin();
|
||||
void LoginSendServerRequest();
|
||||
void LoginSendPlayRequest(uint32_t id);
|
||||
void LoginProcessLoginResponse(const EQ::Net::Packet &p);
|
||||
void LoginProcessServerPacketList(const EQ::Net::Packet &p);
|
||||
void LoginProcessServerPlayResponse(const EQ::Net::Packet &p);
|
||||
|
||||
void LoginDisableReconnect();
|
||||
|
||||
std::unique_ptr<EQ::Net::DaybreakConnectionManager> m_login_connection_manager;
|
||||
std::shared_ptr<EQ::Net::DaybreakConnection> m_login_connection;
|
||||
std::map<uint32_t, WorldServer> m_world_servers;
|
||||
|
||||
//World
|
||||
void ConnectToWorld();
|
||||
|
||||
void WorldOnNewConnection(std::shared_ptr<EQ::Net::DaybreakConnection> connection);
|
||||
void WorldOnStatusChangeReconnectEnabled(std::shared_ptr<EQ::Net::DaybreakConnection> conn, EQ::Net::DbProtocolStatus from, EQ::Net::DbProtocolStatus to);
|
||||
void WorldOnStatusChangeReconnectDisabled(std::shared_ptr<EQ::Net::DaybreakConnection> conn, EQ::Net::DbProtocolStatus from, EQ::Net::DbProtocolStatus to);
|
||||
void WorldOnPacketRecv(std::shared_ptr<EQ::Net::DaybreakConnection> conn, const EQ::Net::Packet &p);
|
||||
|
||||
void WorldSendClientAuth();
|
||||
|
||||
std::unique_ptr<EQ::Net::DaybreakConnectionManager> m_world_connection_manager;
|
||||
std::shared_ptr<EQ::Net::DaybreakConnection> m_world_connection;
|
||||
|
||||
//Variables
|
||||
std::string m_host;
|
||||
int m_port;
|
||||
std::string m_user;
|
||||
std::string m_pass;
|
||||
std::string m_server;
|
||||
std::string m_character;
|
||||
|
||||
std::string m_key;
|
||||
uint32_t m_dbid;
|
||||
};
|
||||
+2
-1
@@ -1,4 +1,4 @@
|
||||
#include "login.h"
|
||||
/*#include "login.h"
|
||||
#include "../common/eqemu_logsys.h"
|
||||
#include <openssl/des.h>
|
||||
|
||||
@@ -252,3 +252,4 @@ void LoginConnection::ProcessServerPlayResponse(const EQ::Net::Packet &p)
|
||||
Kill();
|
||||
}
|
||||
}
|
||||
*/
|
||||
+27
-8
@@ -2,10 +2,10 @@
|
||||
#include "../common/eqemu_logsys.h"
|
||||
#include "../common/crash.h"
|
||||
#include "../common/platform.h"
|
||||
#include "../common/json_config.h"
|
||||
#include <thread>
|
||||
|
||||
#include "login.h"
|
||||
#include "world.h"
|
||||
#include "eq.h"
|
||||
|
||||
EQEmuLogSys Log;
|
||||
|
||||
@@ -16,12 +16,31 @@ int main() {
|
||||
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Starting EQEmu Headless Client.");
|
||||
|
||||
std::unique_ptr<LoginConnection> login_connection(new LoginConnection("testuser", "testpass", "127.0.0.1", 5999, "KLS Test"));
|
||||
std::unique_ptr<WorldConnection> world_connection;
|
||||
login_connection->OnCanLoginToWorld([&](const WorldServer &ws, const std::string &key, uint32_t dbid) {
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Connect to world server {1} - {0}:9000", ws.address, ws.long_name);
|
||||
world_connection.reset(new WorldConnection(key, dbid, ws.address));
|
||||
});
|
||||
auto config = EQ::JsonConfigFile::Load("hc.json");
|
||||
auto config_handle = config.RawHandle();
|
||||
|
||||
std::vector<std::unique_ptr<EverQuest>> eq_list;
|
||||
|
||||
try {
|
||||
for (int i = 0; i < config_handle.size(); ++i) {
|
||||
auto c = config_handle[i];
|
||||
|
||||
auto host = c["host"].asString();
|
||||
auto port = c["port"].asInt();
|
||||
auto user = c["user"].asString();
|
||||
auto pass = c["pass"].asString();
|
||||
auto server = c["server"].asString();
|
||||
auto character = c["character"].asString();
|
||||
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Connecting to {0}:{1} as Account '{2}' to Server '{3}' under Character '{4}'", host, port, user, server, character);
|
||||
|
||||
eq_list.push_back(std::unique_ptr<EverQuest>(new EverQuest(host, port, user, pass, server, character)));
|
||||
}
|
||||
}
|
||||
catch (std::exception &ex) {
|
||||
Log.OutF(Logs::General, Logs::Headless_Client, "Error parsing config file: {0}", ex.what());
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
EQ::EventLoop::Get().Process();
|
||||
|
||||
Reference in New Issue
Block a user