straight up boobin
This commit is contained in:
parent
de7672f266
commit
74bc6f1720
10 changed files with 71 additions and 58 deletions
|
@ -26,7 +26,7 @@ private:
|
|||
|
||||
class MasterClientPool : public Pool<MasterClient> {
|
||||
protected:
|
||||
bool ProcessClient(MasterClient& client) override {
|
||||
bool ProcessClient(MasterClient& client, const Queries* queries) override {
|
||||
// TODO implement
|
||||
return true;
|
||||
}
|
||||
|
@ -79,14 +79,11 @@ private:
|
|||
};
|
||||
|
||||
class MasterIntraPool : public Pool<MasterIntra> {
|
||||
public:
|
||||
MasterIntraPool();
|
||||
protected:
|
||||
void SetupQueries(Queries* queries) override;
|
||||
bool ProcessClient(MasterIntra& client, const Queries* queries) override {
|
||||
return client.Process(queries);
|
||||
}
|
||||
|
||||
void Stop() override;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -8,82 +8,73 @@ static struct {
|
|||
|
||||
/** MASTERINTRAPOOL CODE **/
|
||||
|
||||
sosc::MasterIntraPool::MasterIntraPool() {
|
||||
void sosc::MasterIntraPool::SetupQueries(Queries* queries) {
|
||||
#define QRY_LICENSE_CHECK 0
|
||||
this->queries.push_back(new db::Query(
|
||||
queries->push_back(new db::Query(
|
||||
"SELECT COUNT(*) FROM `SERVER_LICENSES` "
|
||||
"WHERE `KEY_ID` = ? AND `SECRET` = ?"
|
||||
));
|
||||
|
||||
#define QRY_LICENSE_VERIFY 1
|
||||
this->queries.push_back(new db::Query(
|
||||
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(
|
||||
queries->push_back(new db::Query(
|
||||
"SELECT `ALLOWANCE` FROM `SERVER_LICENSES` WHERE `KEY_ID` = ?"
|
||||
));
|
||||
|
||||
#define QRY_LICENSE_ACTIVE_COUNT 3
|
||||
this->queries.push_back(new db::Query(
|
||||
queries->push_back(new db::Query(
|
||||
"SELECT COUNT(*) FROM `SERVER_LIST` WHERE `LICENSE` = ?"
|
||||
, DB_USE_MEMORY));
|
||||
|
||||
#define QRY_LICENSE_ADD 4
|
||||
this->queries.push_back(new db::Query(
|
||||
queries->push_back(new db::Query(
|
||||
"INSERT OR IGNORE INTO `SERVER_LICENSES` "
|
||||
"(`KEY_ID`, `SECRET`, `ALLOWANCE`) "
|
||||
"VALUES (?, RANDOMBLOB(512), ?)"
|
||||
));
|
||||
|
||||
#define QRY_LICENSE_REMOVE 5
|
||||
this->queries.push_back(new db::Query(
|
||||
queries->push_back(new db::Query(
|
||||
"DELETE FROM `SERVER_LICENSES` "
|
||||
"WHERE `KEY_ID` = ?"
|
||||
));
|
||||
|
||||
#define QRY_LICENSE_MODIFY 6
|
||||
this->queries.push_back(new db::Query(
|
||||
queries->push_back(new db::Query(
|
||||
"UPDATE `SERVER_LICENSES` "
|
||||
"SET `ALLOWANCE` = ? WHERE `KEY_ID` = ?"
|
||||
));
|
||||
|
||||
#define QRY_SERVER_LIST_ADD 7
|
||||
this->queries.push_back(new db::Query(
|
||||
queries->push_back(new db::Query(
|
||||
"INSERT INTO `SERVER_LIST` "
|
||||
"(`NAME`, `LICENSE`, `IP_ADDR`, `PORT`) "
|
||||
"VALUES (?, ?, ?, ?)"
|
||||
, DB_USE_MEMORY));
|
||||
|
||||
#define QRY_SERVER_LIST_GET_ID 8
|
||||
this->queries.push_back(new db::Query(
|
||||
queries->push_back(new db::Query(
|
||||
"SELECT MAX(`ID`) FROM `SERVER_LIST`"
|
||||
, DB_USE_MEMORY));
|
||||
|
||||
#define QRY_SERVER_LIST_MODIFY 9
|
||||
this->queries.push_back(new db::Query(
|
||||
queries->push_back(new db::Query(
|
||||
"UPDATE `SERVER_LIST` SET "
|
||||
"`USERS` = ?, `MAX_USERS` = ? "
|
||||
"WHERE ID = ?"
|
||||
, DB_USE_MEMORY));
|
||||
|
||||
#define QRY_SERVER_LIST_DELETE 10
|
||||
this->queries.push_back(new db::Query(
|
||||
queries->push_back(new db::Query(
|
||||
"DELETE FROM `SERVER_LIST` WHERE `ID` = ?"
|
||||
, DB_USE_MEMORY));
|
||||
}
|
||||
|
||||
void sosc::MasterIntraPool::Stop() {
|
||||
Pool<MasterIntra>::Stop();
|
||||
|
||||
for(auto& query : this->queries) {
|
||||
query->Close();
|
||||
delete query;
|
||||
}
|
||||
}
|
||||
|
||||
/** MASTERINTRA CODE **/
|
||||
|
||||
sosc::MasterIntra::MasterIntra(const IntraClient& client) {
|
||||
|
@ -92,7 +83,7 @@ sosc::MasterIntra::MasterIntra(const IntraClient& client) {
|
|||
this->auth_attempts = 0;
|
||||
}
|
||||
|
||||
bool sosc::MasterIntra::Process(const db::QueryList* queries) {
|
||||
bool sosc::MasterIntra::Process(const Pool::Queries* queries) {
|
||||
Packet pck;
|
||||
int status = this->sock.Receive(&pck);
|
||||
if(status == PCK_ERR)
|
||||
|
@ -136,7 +127,7 @@ bool sosc::MasterIntra::Authentication(sosc::Packet& pck) {
|
|||
if(!pck.Check(4, PCK_ANY, 2, PCK_ANY, 512))
|
||||
return this->Close();
|
||||
|
||||
db::Query* query = &this->queries[QRY_LICENSE_CHECK];
|
||||
db::Query* query = this->queries->at(QRY_LICENSE_CHECK);
|
||||
query->Reset();
|
||||
query->BindText(pck[2], 0);
|
||||
query->BindBlob(pck[3], 1);
|
||||
|
|
|
@ -9,7 +9,7 @@ sosc::IntraClient::IntraClient() {
|
|||
this->cipher = nullptr;
|
||||
}
|
||||
|
||||
bool sosc::IntraClient::Open(std::string host, uint16_t port) {
|
||||
bool sosc::IntraClient::Open(const std::string& host, uint16_t port) {
|
||||
if(!this->client.Open(host, port))
|
||||
return false;
|
||||
|
||||
|
@ -17,7 +17,7 @@ bool sosc::IntraClient::Open(std::string host, uint16_t port) {
|
|||
return true;
|
||||
}
|
||||
|
||||
void sosc::IntraClient::Open(TcpClient client) {
|
||||
void sosc::IntraClient::Open(const TcpClient& client) {
|
||||
this->client = client;
|
||||
this->client_open = true;
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ bool sosc::IntraClient::Send(const Packet& packet) {
|
|||
if(this->IsCiphered())
|
||||
this->cipher->Parse(&packet_raw);
|
||||
|
||||
return this->client.Send(packet_raw) == 0;
|
||||
return this->client.Send(packet_raw);
|
||||
}
|
||||
|
||||
/****************************/
|
||||
|
|
|
@ -9,7 +9,7 @@ namespace sosc {
|
|||
class IntraClient {
|
||||
public:
|
||||
IntraClient();
|
||||
bool Open(std::string host, uint16_t port);
|
||||
bool Open(const std::string& host, uint16_t port);
|
||||
|
||||
bool IsCiphered() const;
|
||||
void SetCipher(cgc::Cipher* cipher);
|
||||
|
@ -30,7 +30,7 @@ public:
|
|||
this->client.Close();
|
||||
}
|
||||
private:
|
||||
void Open(TcpClient client);
|
||||
void Open(const TcpClient& client);
|
||||
|
||||
bool client_open;
|
||||
TcpClient client;
|
||||
|
|
|
@ -43,10 +43,10 @@ public:
|
|||
|
||||
virtual void Stop();
|
||||
|
||||
typedef std::vector<db::Query> Queries;
|
||||
typedef std::vector<db::Query*> Queries;
|
||||
protected:
|
||||
virtual void SetupQueries(Queries* queries) {};
|
||||
virtual bool ProcessClient(T& client, Queries* queries) = 0;
|
||||
virtual bool ProcessClient(T& client, const Queries* queries) = 0;
|
||||
private:
|
||||
bool IsStackFull(int stackCount) const;
|
||||
bool CanAddStack() const;
|
||||
|
@ -103,7 +103,7 @@ void Pool<T>::Start() {
|
|||
if(this->is_open)
|
||||
return;
|
||||
|
||||
this->queries = std::vector<db::Query>();
|
||||
this->queries = std::vector<db::Query*>();
|
||||
this->SetupQueries(&this->queries);
|
||||
for(int i = 0; i < this->info.initial_count; ++i) {
|
||||
this->stacks.push_back(new Stack(this));
|
||||
|
@ -181,8 +181,10 @@ void Pool<T>::Stop() {
|
|||
delete stack;
|
||||
}
|
||||
|
||||
for(auto& query : this->queries)
|
||||
query.Close();
|
||||
for(auto& query : this->queries) {
|
||||
query->Close();
|
||||
delete query;
|
||||
}
|
||||
|
||||
this->stacks.clear();
|
||||
this->is_open = false;
|
||||
|
@ -201,7 +203,7 @@ void Pool<T>::Stack::Start() {
|
|||
return;
|
||||
|
||||
for(auto& query : this->pool->queries)
|
||||
this->queries.push_back(db::Query(query));
|
||||
this->queries.push_back(new db::Query(*query));
|
||||
|
||||
this->is_open = true;
|
||||
this->is_running = true;
|
||||
|
@ -256,8 +258,10 @@ void Pool<T>::Stack::Stop() {
|
|||
if(!this->is_open || !this->is_running)
|
||||
return;
|
||||
|
||||
for(auto& query : this->queries)
|
||||
query.Close();
|
||||
for(auto& query : this->queries) {
|
||||
query->Close();
|
||||
delete query;
|
||||
}
|
||||
|
||||
this->is_running = false;
|
||||
this->thread->join();
|
||||
|
|
|
@ -7,13 +7,22 @@
|
|||
sosc::ScapeConnection::ScapeConnection() {
|
||||
this->client_open = false;
|
||||
this->handshaked = false;
|
||||
this->cipher = nullptr;
|
||||
}
|
||||
|
||||
void sosc::ScapeConnection::Open(TcpClient client) {
|
||||
void sosc::ScapeConnection::Open(const TcpClient& client) {
|
||||
this->client = client;
|
||||
this->client_open = true;
|
||||
}
|
||||
|
||||
bool sosc::ScapeConnection::IsCiphered() const {
|
||||
return this->cipher != nullptr;
|
||||
}
|
||||
|
||||
void sosc::ScapeConnection::SetCipher(cgc::Cipher* cipher) {
|
||||
this->cipher = cipher;
|
||||
}
|
||||
|
||||
int sosc::ScapeConnection::Handshake() {
|
||||
if(this->handshaked)
|
||||
return SOSC_SHAKE_DONE;
|
||||
|
@ -81,12 +90,16 @@ int sosc::ScapeConnection::Receive(Packet* packet, bool block) {
|
|||
return PCK_ERR;
|
||||
if(!block && !first_recv)
|
||||
return PCK_MORE;
|
||||
|
||||
|
||||
auto bufferSize = this->buffer.length();
|
||||
status = this->client.Receive
|
||||
(&this->buffer, SOSC_TCP_APPEND | (block ? SOSC_TCP_BLOCK : 0));
|
||||
|
||||
if(status == -1)
|
||||
return PCK_ERR;
|
||||
|
||||
if(this->IsCiphered())
|
||||
this->cipher->Parse(&this->buffer, bufferSize);
|
||||
|
||||
first_recv = false;
|
||||
}
|
||||
|
||||
|
@ -110,6 +123,9 @@ bool sosc::ScapeConnection::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);
|
||||
}
|
||||
|
||||
|
|
|
@ -4,11 +4,12 @@
|
|||
#include <queue>
|
||||
#include "../crypto/sha1.hpp"
|
||||
#include "../crypto/base64.hpp"
|
||||
#include "../crypto/cipher.hpp"
|
||||
#include "frame.hpp"
|
||||
#include "packet.hpp"
|
||||
#include "tcpsock.hpp"
|
||||
|
||||
#define SOSC_SHAKE_ERR -1
|
||||
#define SOSC_SHAKE_ERR (-1)
|
||||
#define SOSC_SHAKE_CONT 0
|
||||
#define SOSC_SHAKE_DONE 1
|
||||
|
||||
|
@ -16,7 +17,10 @@ namespace sosc {
|
|||
class ScapeConnection {
|
||||
public:
|
||||
ScapeConnection();
|
||||
|
||||
|
||||
bool IsCiphered() const;
|
||||
void SetCipher(cgc::Cipher* cipher);
|
||||
|
||||
int Handshake();
|
||||
int Receive(Packet* packet, bool block = false);
|
||||
bool Send(const Packet& packet);
|
||||
|
@ -38,11 +42,12 @@ public:
|
|||
this->client.Close();
|
||||
}
|
||||
private:
|
||||
void Open(TcpClient client);
|
||||
void Open(const TcpClient& client);
|
||||
|
||||
bool client_open;
|
||||
bool handshaked;
|
||||
TcpClient client;
|
||||
cgc::Cipher* cipher;
|
||||
|
||||
std::string buffer;
|
||||
std::string pck_frames;
|
||||
|
|
|
@ -46,7 +46,7 @@ public:
|
|||
bool Open(std::string host, uint16_t port);
|
||||
|
||||
int Receive(std::string* str, int flags = 0);
|
||||
int Send(const std::string& str);
|
||||
bool Send(const std::string& str);
|
||||
|
||||
bool IsDataReady();
|
||||
inline bool IsOpen() const {
|
||||
|
|
|
@ -94,9 +94,9 @@ int sosc::TcpClient::Receive(std::string* str, int flags) {
|
|||
return total_length;
|
||||
}
|
||||
|
||||
int sosc::TcpClient::Send(const std::string& str) {
|
||||
bool sosc::TcpClient::Send(const std::string& str) {
|
||||
if(!this->sock_open)
|
||||
return -1;
|
||||
return false;
|
||||
|
||||
std::string::size_type total_sent = 0;
|
||||
while(total_sent < str.length()) {
|
||||
|
@ -107,12 +107,12 @@ int sosc::TcpClient::Send(const std::string& str) {
|
|||
|
||||
if(sent == -1) {
|
||||
this->Close();
|
||||
return -1;
|
||||
return false;
|
||||
} else
|
||||
total_sent += sent;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool sosc::TcpClient::IsDataReady() {
|
||||
|
|
|
@ -110,9 +110,9 @@ int sosc::TcpClient::Receive(std::string* str, int flags) {
|
|||
return total_length;
|
||||
}
|
||||
|
||||
int sosc::TcpClient::Send(const std::string& str) {
|
||||
bool sosc::TcpClient::Send(const std::string& str) {
|
||||
if(!this->sock_open)
|
||||
return -1;
|
||||
return false;
|
||||
|
||||
std::string::size_type total_sent = 0;
|
||||
while(total_sent < str.length()) {
|
||||
|
@ -123,12 +123,12 @@ int sosc::TcpClient::Send(const std::string& str) {
|
|||
|
||||
if(sent == SOCKET_ERROR) {
|
||||
this->Close();
|
||||
return -1;
|
||||
return false;
|
||||
} else
|
||||
total_sent += sent;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool sosc::TcpClient::IsDataReady() {
|
||||
|
@ -155,7 +155,7 @@ void sosc::TcpClient::SetBlocking(bool will_block) {
|
|||
if(!this->sock_open)
|
||||
return;
|
||||
|
||||
u_long nblock = !will_block;
|
||||
auto nblock = (u_long)!will_block;
|
||||
ioctlsocket(this->sock, FIONBIO, &nblock);
|
||||
}
|
||||
|
||||
|
@ -190,7 +190,7 @@ bool sosc::TcpServer::Listen(uint16_t port) {
|
|||
hints.ai_protocol = IPPROTO_TCP;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
|
||||
if(getaddrinfo(NULL, TOSTR(port).c_str(), &hints, &result) != 0)
|
||||
if(getaddrinfo(nullptr, TOSTR(port).c_str(), &hints, &result) != 0)
|
||||
return false;
|
||||
|
||||
this->sock = socket(result->ai_family,
|
||||
|
|
Loading…
Reference in a new issue