Unfinished totally broken and far from done push

This commit is contained in:
Wirlaburla 2024-01-08 02:24:48 -06:00
parent 6dd3379cde
commit 8aa7714ae6
5 changed files with 156 additions and 1 deletions

View File

@ -2,6 +2,17 @@ cmake_minimum_required(VERSION 3.0)
project(pengobox) 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) install(TARGETS pengobox RUNTIME DESTINATION bin)

16
src/main.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <iostream>
#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("<SQL>: Initialization failed.");
exit(EXIT_FAILURE);
}
std::cout << "Hello, world!" << std::endl;
return 0;
}

53
src/network.cpp Normal file
View File

@ -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("<Network>: Socket failed.");
exit(EXIT_FAILURE);
}
if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0) {
perror("<Network>: 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("<Network>: Failed to bind.");
exit(EXIT_FAILURE);
}
printf("AutoServer listening on port %d.\n", AUTOPORT);
if (listen(master_socket, 5) < 0) {
perror("<Network>: 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;
}

12
src/packets/SessInit.cpp Normal file
View File

@ -0,0 +1,12 @@
#ifndef PropertyList
#include "../scape/propertylist.h"
#endif
class SessInit : public NetPacket {
public:
SessInit() {
}
private:
const int type = 6;
}

63
src/sql.h Normal file
View File

@ -0,0 +1,63 @@
#ifndef SQL_H
#define SQL_H
#include <sqlite3.h>
#include <random>
#include <string>
#include <openssl/sha.h>
#include <string.h>
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<int> 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