From 2c32c63abb485b7a0603700261f94a970a3e5aa3 Mon Sep 17 00:00:00 2001 From: malloc Date: Thu, 5 Jul 2018 16:32:32 -0500 Subject: [PATCH] nitrify boob --- server/src/hosts/master.hpp | 3 ++- server/src/hosts/master_intra.cpp | 33 +++++++++++++++++++++++-------- server/src/sock/intrasock.cpp | 18 ++++++++++++++++- server/src/sock/intrasock.hpp | 7 ++++++- 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/server/src/hosts/master.hpp b/server/src/hosts/master.hpp index 9a9eaaa..7fd2d8f 100644 --- a/server/src/hosts/master.hpp +++ b/server/src/hosts/master.hpp @@ -72,8 +72,9 @@ private: bool authed; int auth_attempts; const int MAX_AUTH_ATTEMPTS = 3; - std::string license; + int32_t server_id; + std::string license; const db::QueryList* queries; }; diff --git a/server/src/hosts/master_intra.cpp b/server/src/hosts/master_intra.cpp index 4c3b245..af24a61 100644 --- a/server/src/hosts/master_intra.cpp +++ b/server/src/hosts/master_intra.cpp @@ -15,43 +15,54 @@ sosc::MasterIntraPool::MasterIntraPool() { "WHERE `KEY_ID` = ? AND `SECRET` = ?" )); -#define QRY_LICENSE_LIMIT 1 +#define QRY_LICENSE_VERIFY 1 + this->queries.push_back(new db::Query( + "SELECT COUNT(*) FROM `SERVER_LICENSES` " + "WHERE `KEY_ID` = ?" + )); + +#define QRY_LICENSE_LIMIT 2 this->queries.push_back(new db::Query( "SELECT `ALLOWANCE` FROM `SERVER_LICENSES` WHERE `KEY_ID` = ?" )); -#define QRY_LICENSE_ACTIVE_COUNT 2 +#define QRY_LICENSE_ACTIVE_COUNT 3 this->queries.push_back(new db::Query( "SELECT COUNT(*) FROM `SERVER_LIST` WHERE `LICENSE` = ?" , DB_USE_MEMORY)); -#define QRY_LICENSE_ADD 3 +#define QRY_LICENSE_ADD 4 this->queries.push_back(new db::Query( "INSERT OR IGNORE INTO `SERVER_LICENSES` " "(`KEY_ID`, `SECRET`, `ALLOWANCE`) " "VALUES (?, RANDOMBLOB(512), ?)" )); -#define QRY_LICENSE_REMOVE 4 +#define QRY_LICENSE_REMOVE 5 this->queries.push_back(new db::Query( "DELETE FROM `SERVER_LICENSES` " "WHERE `KEY_ID` = ?" )); -#define QRY_LICENSE_MODIFY 5 +#define QRY_LICENSE_MODIFY 6 this->queries.push_back(new db::Query( "UPDATE `SERVER_LICENSES` " "SET `ALLOWANCE` = ? WHERE `KEY_ID` = ?" )); -#define QRY_SERVER_LIST_ADD 6 +#define QRY_SERVER_LIST_ADD 7 this->queries.push_back(new db::Query( "INSERT INTO `SERVER_LIST` " "(`NAME`, `LICENSE`, `IP_ADDR`, `PORT`) " "VALUES (?, ?, ?, ?)" , DB_USE_MEMORY)); -#define QRY_SERVER_LIST_DELETE 7 +#define QRY_SERVER_LIST_GET_ID 8 + this->queries.push_back(new db::Query( + "SELECT MAX(`ID`) FROM `SERVER_LIST`" + , DB_USE_MEMORY)); + +#define QRY_SERVER_LIST_DELETE 9 this->queries.push_back(new db::Query( "DELETE FROM `SERVER_LIST` WHERE `ID` = ?" , DB_USE_MEMORY)); @@ -106,8 +117,9 @@ bool sosc::MasterIntra::InitAttempt(sosc::Packet& pck) { return this->Close( Packet(kEncryptionError, { net::htonv(0x101) })); - + this->cipher = cgc::Cipher(this->key); this->sock.Send(response); + this->sock.SetCipher(&this->cipher); } bool sosc::MasterIntra::Authentication(sosc::Packet& pck) { @@ -149,6 +161,10 @@ bool sosc::MasterIntra::Authentication(sosc::Packet& pck) { query->BindInt32(net::ntohv(pck[1]), 3); query->NonQuery(); + query = this->queries->at(QRY_SERVER_LIST_GET_ID); + query->Reset(); + this->server_id = query->ScalarInt32(); + _ctx.license_check_mtx.unlock(); this->sock.Send(Packet(kPositiveAck, { packetId })); @@ -187,6 +203,7 @@ bool sosc::MasterIntra::StatusUpdate(sosc::Packet &pck) { return this->Close(); + return true; } diff --git a/server/src/sock/intrasock.cpp b/server/src/sock/intrasock.cpp index ec23170..1baaa5c 100644 --- a/server/src/sock/intrasock.cpp +++ b/server/src/sock/intrasock.cpp @@ -6,6 +6,7 @@ sosc::IntraClient::IntraClient() { this->client_open = false; + this->cipher = nullptr; } bool sosc::IntraClient::Open(std::string host, uint16_t port) { @@ -21,6 +22,15 @@ void sosc::IntraClient::Open(TcpClient client) { this->client_open = true; } +bool sosc::IntraClient::IsCiphered() const { + return this->cipher != nullptr; +} + +void sosc::IntraClient::SetCipher(cgc::Cipher *cipher) { + this->cipher = cipher; + cipher->Parse(&this->buffer); +} + int sosc::IntraClient::Receive(Packet* packet, bool block) { if(!this->client_open) return PCK_ERR; @@ -32,9 +42,12 @@ int sosc::IntraClient::Receive(Packet* packet, bool block) { return PCK_ERR; if(!block && !first_recv) return PCK_MORE; - + + std::string::size_type offset = this->buffer.size(); status = this->client.Receive (&this->buffer, SOSC_TCP_APPEND | (block ? SOSC_TCP_BLOCK : 0)); + if(this->IsCiphered()) + this->cipher->Parse(&this->buffer, offset); if(status == -1) return PCK_ERR; @@ -50,6 +63,9 @@ bool sosc::IntraClient::Send(const Packet& packet) { std::string packet_raw; packet.ToString(&packet_raw); + if(this->IsCiphered()) + this->cipher->Parse(&packet_raw); + return this->client.Send(packet_raw) == 0; } diff --git a/server/src/sock/intrasock.hpp b/server/src/sock/intrasock.hpp index 1e736d6..e527ab2 100644 --- a/server/src/sock/intrasock.hpp +++ b/server/src/sock/intrasock.hpp @@ -3,13 +3,17 @@ #include "tcpsock.hpp" #include "packet.hpp" +#include "../crypto/cipher.hpp" namespace sosc { class IntraClient { public: IntraClient(); bool Open(std::string host, uint16_t port); - + + bool IsCiphered() const; + void SetCipher(cgc::Cipher* cipher); + int Receive(Packet* packet, bool block = false); bool Send(const Packet& packet); @@ -31,6 +35,7 @@ private: bool client_open; TcpClient client; std::string buffer; + cgc::Cipher* cipher; friend class IntraServer; };