scroob applied

This commit is contained in:
Alec Obradovich 2018-11-05 06:57:40 -06:00
parent 9932c52494
commit 9b3487d567
10 changed files with 72 additions and 41 deletions

Binary file not shown.

View file

@ -94,18 +94,21 @@ int sosc::Packet::Parse(const std::string& data, std::string* extra) {
switch(raw[ptr]) {
default:
region_length = raw[ptr];
++ptr;
break;
case 254:
if(ptr + 2 >= length)
return PCK_ERR;
region_length = net::ntohv<uint16_t>(data, ptr + 1);
ptr += 3;
break;
case 255:
if(ptr + 4 >= length)
return PCK_ERR;
region_length = net::ntohv<uint32_t>(data, ptr + 1);
ptr += 5;
break;
}
@ -113,12 +116,12 @@ int sosc::Packet::Parse(const std::string& data, std::string* extra) {
body_length += region_length;
}
if(body_length - ptr != 0)
if(body_length + ptr < expected_length)
return PCK_ERR;
for(int i = 0; i < region_count; ++i) {
this->regions[i] = data.substr(ptr, region_lengths[i]);
ptr += region_lengths[i];
for(const auto region_length : region_lengths) {
this->regions.push_back(data.substr(ptr, region_length));
ptr += region_length;
}
if(length > expected_length && extra != nullptr)

View file

@ -24,8 +24,14 @@ public:
bool Close();
bool Close(const Packet& message);
~MasterClient();
~MasterClient() { this->Close(); };
private:
bool IsAuthed();
bool ProcessLogin(Packet& pck);
bool ProcessRegistration(Packet& pck);
bool ListServers(Packet& pck);
enum MasterToClientId {
kLoginResponse = 0,
kRegisterResponse,
@ -69,7 +75,7 @@ public:
bool Close();
bool Close(const Packet& message);
~MasterIntra();
~MasterIntra() { this->Close(); }
private:
bool Authentication(Packet& pck);
bool StatusUpdate(Packet& pck);

View file

@ -20,25 +20,31 @@ void sosc::MasterClientPool::SetupQueries(db::Queries *queries) {
"VALUES (?, ?, ?, 0, CURRENT_TIMESTAMP)"
));
#define QRY_USER_GET_PWD_HASH 2
#define QRY_USER_NAME_EXISTS 2
queries->push_back(new db::Query(
"SELECT `ID`, `PASS_HASH` FROM `USERS` "
"WHERE `USERNAME` = ?"
"SELECT COUNT(*) FROM `USERS` "
"WHERE LOWER(`USERNAME`) = LOWER(?)"
));
#define QRY_USER_GENERATE_KEY 3
#define QRY_USER_GET_PWD_HASH 3
queries->push_back(new db::Query(
"SELECT `ID`, `PASS_HASH` FROM `USERS` "
"WHERE LOWER(`USERNAME`) = LOWER(?)"
));
#define QRY_USER_GENERATE_KEY 4
queries->push_back(new db::Query(
"INSERT OR IGNORE INTO `USER_KEYS` "
"(`ID`, `SECRET`) VALUES (?, RANDOMBLOB(128))"
));
#define QRY_USER_GET_KEY 4
#define QRY_USER_GET_KEY 5
queries->push_back(new db::Query(
"SELECT `SECRET` FROM `USER_KEYS` "
"WHERE `ID` = ?"
));
#define QRY_USER_CHECK_KEY 5
#define QRY_USER_CHECK_KEY 6
queries->push_back(new db::Query(
"SELECT COUNT(*) FROM `USER_KEYS` "
"WHERE `ID` = ? AND `SECRET` = ?"
@ -67,19 +73,20 @@ bool sosc::MasterClient::Process(const db::Queries *queries) {
this->queries = queries;
switch(pck.GetId()) {
case kLoginRequest:
break;
return ProcessLogin(pck);
case kRegisterRequest:
break;
return ProcessRegistration(pck);
case kServerListRequest:
break;
return ListServers(pck);
default:
return this->Close();
}
}
bool sosc::MasterClient::IsAuthed() {
}
bool sosc::MasterClient::Close() {
this->sock.Close();
return false;
@ -87,5 +94,5 @@ bool sosc::MasterClient::Close() {
bool sosc::MasterClient::Close(const Packet& message) {
this->sock.Send(message);
this->Close();
return this->Close();
}

View file

@ -4,3 +4,12 @@ sosc::SlaveClient::SlaveClient(const ScapeConnection& client) {
this->sock = client;
}
bool sosc::SlaveClient::Close() {
this->sock.Close();
return false;
}
bool sosc::SlaveClient::Close(const Packet& message) {
this->sock.Send(message);
return this->Close();
}

View file

@ -13,7 +13,10 @@ public:
SlaveClient() = delete;
explicit SlaveClient(const ScapeConnection& client);
~SlaveClient();
bool Close();
bool Close(const Packet& message);
~SlaveClient() { this->Close(); }
private:
ScapeConnection sock;
};

View file

@ -28,6 +28,7 @@ int main(int argc, char **argv) {
return -1;
if(argv[1][0] == 'm') {
master_client(8008, sosc::poolinfo_t());
//master_intra(1234, sosc::poolinfo_t());
} else {
@ -41,7 +42,7 @@ bool master_intra(uint16_t port, const sosc::poolinfo_t& info) {
using namespace sosc;
IntraServer server;
IntraClient* client;
IntraClient client;
if(!server.Listen(port))
return false;
@ -49,8 +50,8 @@ bool master_intra(uint16_t port, const sosc::poolinfo_t& info) {
pool.Configure(info);
pool.Start();
while(server.Accept(client = new IntraClient()))
pool.AddClient(MasterIntra(client));
while(server.Accept(&client))
pool.AddClient(new MasterIntra(client));
pool.Stop();
return true;
@ -60,7 +61,7 @@ bool master_client(uint16_t port, const sosc::poolinfo_t& info) {
using namespace sosc;
ScapeServer server;
ScapeConnection* client;
ScapeConnection client;
if(!server.Listen(port, true))
return false;
@ -68,8 +69,8 @@ bool master_client(uint16_t port, const sosc::poolinfo_t& info) {
pool.Configure(info);
pool.Start();
while(server.Accept(client = new ScapeConnection()))
pool.AddClient(MasterClient(client));
while(server.Accept(&client))
pool.AddClient(new MasterClient(client));
pool.Stop();
return true;
@ -79,7 +80,7 @@ bool slave(uint16_t port, const sosc::poolinfo_t& info) {
using namespace sosc;
ScapeServer server;
ScapeConnection* client;
ScapeConnection client;
if(!server.Listen(port))
return false;
@ -87,8 +88,8 @@ bool slave(uint16_t port, const sosc::poolinfo_t& info) {
pool.Configure(info);
pool.Start();
while(server.Accept(client = new ScapeConnection()))
pool.AddClient(SlaveClient(client));
while(server.Accept(&client))
pool.AddClient(new SlaveClient(client));
pool.Stop();
return true;

View file

@ -240,7 +240,7 @@ void Pool<T,U>::Stack::StackThread() {
while(this->is_running) {
for(auto client = this->clients.begin();
client != this->clients.end() && this->clients.size() > 0;
client != this->clients.end();
++client)
{
if(!this->is_running)
@ -248,10 +248,10 @@ void Pool<T,U>::Stack::StackThread() {
this->clients_mtx.lock();
if(!this->pool->ProcessClient
(client, &this->pool->context, &this->queries))
(*client, &this->pool->context, &this->queries))
{
this->clients.erase(client);
delete client;
delete *client;
client = this->clients.erase(client);
}
this->clients_mtx.unlock();
}

View file

@ -81,8 +81,8 @@ function pack(id, regions) {
throw Error("REGIONS ARGUMENT MALFORMED");
let pck;
let size = 8;
let head_size = size;
let size = 0;
let head_size = 8;
for(let i in regions) {
if(typeof regions[i] === "string")
regions[i] = regions[i].toByteArray();
@ -103,8 +103,8 @@ function pack(id, regions) {
pck = new Uint8Array(size);
pck.set(MAHOU, 0);
pck.set(size.packUint32(), 2);
pck[6] = regions.length;
pck[7] = Math.min(id, 255);
pck[6] = Math.min(id, 255);
pck[7] = regions.length;
let head_ptr = 8;
let body_ptr = head_size;
@ -127,6 +127,8 @@ function pack(id, regions) {
body_ptr += length;
}
console.log(regions);
console.log(pck);
return pck;
}

View file

@ -2,10 +2,10 @@ body {
padding-top: 36px;
font-family: monospace;
text-align: center;
/*background: #000;
color: #fff;*/
background: #fff;
color: #000;
background: #000;
color: #fff;
/*background: #fff;
color: #000;*/
}
.hidden {