From 8aa7714ae66025d61e52315a8935a6d5ca498293 Mon Sep 17 00:00:00 2001 From: Wirlaburla Date: Mon, 8 Jan 2024 02:24:48 -0600 Subject: [PATCH] Unfinished totally broken and far from done push --- CMakeLists.txt | 13 ++++++++- src/main.cpp | 16 ++++++++++ src/network.cpp | 53 +++++++++++++++++++++++++++++++++ src/packets/SessInit.cpp | 12 ++++++++ src/sql.h | 63 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 src/main.cpp create mode 100644 src/network.cpp create mode 100644 src/packets/SessInit.cpp create mode 100644 src/sql.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b06dfe..56c72c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,17 @@ cmake_minimum_required(VERSION 3.0) project(pengobox) -add_executable(pengobox main.cpp) +SET(CMAKE_CXX_STANDARD 17) +SET(CMAKE_CXX_STANDARD_REQUIRED True) + +find_package(SQLite3 REQUIRED) +find_package(OpenSSL REQUIRED) + +add_executable( + pengobox + src/main.cpp +) + +target_link_libraries(pengobox PUBLIC pthread dl ssl crypto sqlite3) install(TARGETS pengobox RUNTIME DESTINATION bin) diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..b79018b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,16 @@ +#include + +#include "sql.h" + +int main(int argc, char **argv) { + std::cout << "[Info]\tStarting PengoBox..." << std::endl; + pengoSQL* s = new pengoSQL(); + // Check it's not stupid. + if (s->response_code != SQLITE_OK) { + // It's stupid. + perror(": Initialization failed."); + exit(EXIT_FAILURE); + } + std::cout << "Hello, world!" << std::endl; + return 0; +} diff --git a/src/network.cpp b/src/network.cpp new file mode 100644 index 0000000..126938d --- /dev/null +++ b/src/network.cpp @@ -0,0 +1,53 @@ +#define MAX_CLIENTS 100 +#define AUTOPORT 5150 +#define RoomPortMin 6000 +#define RoomPortMax 6100 + +class MasterNetwork { +public: + MasterNetwork() { + fd_set readfds; + for (i = 0; i < MAX_CLIENTS; i++) { + clients[i] = 0; + } + if ((master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0) { + perror(": Socket failed."); + exit(EXIT_FAILURE); + } + if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0) { + perror(": setsockopt failed."); + exit(EXIT_FAILURE); + } + + address.sin_family = AF_INET; + address.sin_addr.s_addr = INADDR_ANY; + address.sin_port = htons(AUTOPORT); + + if (bind(master_socket, (struct sockaddr *)&address, sizeof(address)) < 0) { + perror(": Failed to bind."); + exit(EXIT_FAILURE); + } + printf("AutoServer listening on port %d.\n", AUTOPORT); + + if (listen(master_socket, 5) < 0) { + perror(": Unable to listen."); + exit(EXIT_FAILURE); + } + + addrlen = sizeof(address); + + while (true) { + FD_ZERO(&readfds); + FD_SET(master_socket, &readfds); + max_sd = master_socket; + } + } +private: + int master_socket; + int max_sd; + int clients[MAX_CLIENTS]; + int fresh_socket; + struct sockaddr_in address; + char buffer[256]; + int addrlen; +} \ No newline at end of file diff --git a/src/packets/SessInit.cpp b/src/packets/SessInit.cpp new file mode 100644 index 0000000..c835582 --- /dev/null +++ b/src/packets/SessInit.cpp @@ -0,0 +1,12 @@ +#ifndef PropertyList +#include "../scape/propertylist.h" +#endif + +class SessInit : public NetPacket { +public: + SessInit() { + + } +private: + const int type = 6; +} \ No newline at end of file diff --git a/src/sql.h b/src/sql.h new file mode 100644 index 0000000..133916c --- /dev/null +++ b/src/sql.h @@ -0,0 +1,63 @@ +#ifndef SQL_H +#define SQL_H +#include +#include +#include +#include +#include + +unsigned char* sha256(const char* text) { + unsigned int len = strlen ((const char*) text); + unsigned char hash [32]; + SHA256((const unsigned char*)text, len, hash); + unsigned char* output = hash; + return output; +} + +class pengoSQL { +public: + sqlite3 *db; + int response_code; + char* response_message = 0; + + pengoSQL() { + response_code = sqlite3_open("pbox.db", &db); + if (response_code == SQLITE_OK) { + response_code = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS Accounts(name TEXT PRIMARY KEY NOT NULL, password CHAR(32) NOT NULL, serial CHAR(16), email CHAR(255));", NULL, 0, &response_message); + } + + if (!userExists("admin")) { + std::mt19937 generator{std::random_device{}()}; + std::uniform_int_distribution distribution{'a', 'z'}; + std::string pw(16, '\0'); + for(auto& dis: pw) + dis = distribution(generator); + if (createUser("admin", pw.c_str(), "0000000000000000")) printf("Admin user created.\n\tUsername: admin\n\tPassword: %s\n", pw.c_str()); + else printf("Error creating admin user.\n"); + } + }; + + bool userExists(const char* username) { + sqlite3_stmt* stmt; + sqlite3_prepare_v2(db, "SELECT * FROM Accounts WHERE username = ?;", -1, &stmt, 0); + sqlite3_bind_text(stmt, 1, username, -1, NULL); + int step = sqlite3_step(stmt); + sqlite3_finalize(stmt); + if (step == SQLITE_ROW) { return true; } + return false; + } + + bool createUser(const char* username, const char* password, const char* serial) { + sqlite3_stmt* stmt; + int result = sqlite3_prepare_v2(db, "INSERT INTO Accounts(name, password) VALUES (?, ?)", -1, &stmt, 0); + sqlite3_bind_text(stmt, 1, username, -1, NULL); + sqlite3_bind_text(stmt, 2, (const char*)sha256(password), -1, NULL); + sqlite3_bind_text(stmt, 3, serial, -1, NULL); + sqlite3_step(stmt); + return result == SQLITE_DONE; + } + + void close() { sqlite3_close(db); } +}; + +#endif