mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-12-17 03:37:08 -06:00
Compare commits
2 Commits
raw-parsin
...
web-dashbo
| Author | SHA1 | Date | |
|---|---|---|---|
| d532a9b063 | |||
| 5453d163a3 |
@@ -89,6 +89,7 @@ elseif(MSVC)
|
||||
add_compile_options("/wd4267" "/utf-8" "/volatile:iso" "/Zc:inline")
|
||||
elseif(WIN32)
|
||||
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
||||
add_compile_definitions(NOMINMAX)
|
||||
endif()
|
||||
|
||||
# Our output dir
|
||||
@@ -253,6 +254,7 @@ include_directories(
|
||||
"thirdparty/MD5"
|
||||
"thirdparty/nlohmann"
|
||||
"thirdparty/mongoose"
|
||||
"thirdparty/inja"
|
||||
)
|
||||
|
||||
# Add system specfic includes for Apple, Windows and Other Unix OS' (including Linux)
|
||||
@@ -322,6 +324,7 @@ endif()
|
||||
add_subdirectory(dWorldServer)
|
||||
add_subdirectory(dAuthServer)
|
||||
add_subdirectory(dChatServer)
|
||||
add_subdirectory(dDashboardServer)
|
||||
add_subdirectory(dMasterServer) # Add MasterServer last so it can rely on the other binaries
|
||||
|
||||
target_precompile_headers(
|
||||
|
||||
@@ -26,12 +26,14 @@ void HandleHTTPPlayersRequest(HTTPReply& reply, std::string body) {
|
||||
const json data = Game::playerContainer;
|
||||
reply.status = data.empty() ? eHTTPStatusCode::NO_CONTENT : eHTTPStatusCode::OK;
|
||||
reply.message = data.empty() ? "{\"error\":\"No Players Online\"}" : data.dump();
|
||||
reply.contentType = ContentType::JSON;
|
||||
}
|
||||
|
||||
void HandleHTTPTeamsRequest(HTTPReply& reply, std::string body) {
|
||||
const json data = TeamContainer::GetTeamContainer();
|
||||
reply.status = data.empty() ? eHTTPStatusCode::NO_CONTENT : eHTTPStatusCode::OK;
|
||||
reply.message = data.empty() ? "{\"error\":\"No Teams Online\"}" : data.dump();
|
||||
reply.contentType = ContentType::JSON;
|
||||
}
|
||||
|
||||
void HandleHTTPAnnounceRequest(HTTPReply& reply, std::string body) {
|
||||
@@ -39,6 +41,7 @@ void HandleHTTPAnnounceRequest(HTTPReply& reply, std::string body) {
|
||||
if (!data) {
|
||||
reply.status = eHTTPStatusCode::BAD_REQUEST;
|
||||
reply.message = "{\"error\":\"Invalid JSON\"}";
|
||||
reply.contentType = ContentType::JSON;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -47,6 +50,7 @@ void HandleHTTPAnnounceRequest(HTTPReply& reply, std::string body) {
|
||||
if (!check.empty()) {
|
||||
reply.status = eHTTPStatusCode::BAD_REQUEST;
|
||||
reply.message = check;
|
||||
reply.contentType = ContentType::JSON;
|
||||
} else {
|
||||
|
||||
ChatPackets::Announcement announcement;
|
||||
@@ -56,6 +60,7 @@ void HandleHTTPAnnounceRequest(HTTPReply& reply, std::string body) {
|
||||
|
||||
reply.status = eHTTPStatusCode::OK;
|
||||
reply.message = "{\"status\":\"Announcement Sent\"}";
|
||||
reply.contentType = ContentType::JSON;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
8
dDashboardServer/CMakeLists.txt
Normal file
8
dDashboardServer/CMakeLists.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
set(DDASHBOARDSERVER_SOURCES
|
||||
"DashboardWeb.cpp"
|
||||
)
|
||||
|
||||
add_executable(DashboardServer "DashboardServer.cpp" "DashboardWeb.cpp")
|
||||
target_link_libraries(DashboardServer ${COMMON_LIBRARIES} dServer dWeb)
|
||||
target_include_directories(DashboardServer PRIVATE ${PROJECT_SOURCE_DIR}/dServer ${PROJECT_SOURCE_DIR}/dWeb)
|
||||
add_compile_definitions(DashboardServer PRIVATE PROJECT_VERSION="\"${PROJECT_VERSION}\"")
|
||||
182
dDashboardServer/DashboardServer.cpp
Normal file
182
dDashboardServer/DashboardServer.cpp
Normal file
@@ -0,0 +1,182 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
//DLU Includes:
|
||||
#include "dCommonVars.h"
|
||||
#include "dServer.h"
|
||||
#include "Logger.h"
|
||||
#include "Database.h"
|
||||
#include "dConfig.h"
|
||||
#include "Diagnostics.h"
|
||||
#include "AssetManager.h"
|
||||
#include "BinaryPathFinder.h"
|
||||
#include "ServiceType.h"
|
||||
#include "StringifiedEnum.h"
|
||||
|
||||
#include "Game.h"
|
||||
#include "Server.h"
|
||||
|
||||
//RakNet includes:
|
||||
#include "RakNetDefines.h"
|
||||
#include "MessageIdentifiers.h"
|
||||
|
||||
#include "DashboardWeb.h"
|
||||
|
||||
namespace Game {
|
||||
Logger* logger = nullptr;
|
||||
dServer* server = nullptr;
|
||||
dConfig* config = nullptr;
|
||||
AssetManager* assetManager = nullptr;
|
||||
Game::signal_t lastSignal = 0;
|
||||
std::mt19937 randomEngine;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
constexpr uint32_t dashboardFramerate = mediumFramerate;
|
||||
constexpr uint32_t dashboardFrameDelta = mediumFrameDelta;
|
||||
Diagnostics::SetProcessName("Dashboard");
|
||||
Diagnostics::SetProcessFileName(argv[0]);
|
||||
Diagnostics::Initialize();
|
||||
|
||||
std::signal(SIGINT, Game::OnSignal);
|
||||
std::signal(SIGTERM, Game::OnSignal);
|
||||
|
||||
Game::config = new dConfig("dashboardconfig.ini");
|
||||
|
||||
//Create all the objects we need to run our service:
|
||||
Server::SetupLogger("DashboardServer");
|
||||
if (!Game::logger) return EXIT_FAILURE;
|
||||
Game::config->LogSettings();
|
||||
|
||||
//Read our config:
|
||||
|
||||
LOG("Starting Dashboard server...");
|
||||
LOG("Version: %s", PROJECT_VERSION);
|
||||
LOG("Compiled on: %s", __TIMESTAMP__);
|
||||
|
||||
try {
|
||||
std::string clientPathStr = Game::config->GetValue("client_location");
|
||||
if (clientPathStr.empty()) clientPathStr = "./res";
|
||||
std::filesystem::path clientPath = std::filesystem::path(clientPathStr);
|
||||
if (clientPath.is_relative()) {
|
||||
clientPath = BinaryPathFinder::GetBinaryDir() / clientPath;
|
||||
}
|
||||
|
||||
Game::assetManager = new AssetManager(clientPath);
|
||||
} catch (std::runtime_error& ex) {
|
||||
LOG("Got an error while setting up assets: %s", ex.what());
|
||||
delete Game::logger;
|
||||
delete Game::config;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
//Connect to the Database
|
||||
try {
|
||||
Database::Connect();
|
||||
} catch (std::exception& ex) {
|
||||
LOG("Got an error while connecting to the database: %s", ex.what());
|
||||
Database::Destroy("DashboardServer");
|
||||
delete Game::logger;
|
||||
delete Game::config;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// setup the chat api web server
|
||||
const uint32_t web_server_port = GeneralUtils::TryParse<uint32_t>(Game::config->GetValue("web_server_port")).value_or(80);
|
||||
if (!Game::web.Startup("localhost", web_server_port)) {
|
||||
// if we want the web server and it fails to start, exit
|
||||
LOG("Failed to start web server, shutting down.");
|
||||
Database::Destroy("DashboardServer");
|
||||
delete Game::logger;
|
||||
delete Game::config;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
DashboardWeb::RegisterRoutes();
|
||||
|
||||
//Find out the master's IP:
|
||||
std::string masterIP;
|
||||
uint32_t masterPort = 1000;
|
||||
std::string masterPassword;
|
||||
auto masterInfo = Database::Get()->GetMasterInfo();
|
||||
if (masterInfo) {
|
||||
masterIP = masterInfo->ip;
|
||||
masterPort = masterInfo->port;
|
||||
masterPassword = masterInfo->password;
|
||||
}
|
||||
|
||||
//It's safe to pass 'localhost' here, as the IP is only used as the external IP.
|
||||
std::string ourIP = "localhost";
|
||||
const uint32_t maxClients = GeneralUtils::TryParse<uint32_t>(Game::config->GetValue("max_clients")).value_or(999);
|
||||
const uint32_t ourPort = GeneralUtils::TryParse<uint32_t>(Game::config->GetValue("dashboard_server_port")).value_or(2006);
|
||||
const auto externalIPString = Game::config->GetValue("external_ip");
|
||||
if (!externalIPString.empty()) ourIP = externalIPString;
|
||||
|
||||
Game::server = new dServer(ourIP, ourPort, 0, maxClients, false, true, Game::logger, masterIP, masterPort, ServiceType::COMMON, Game::config, &Game::lastSignal, masterPassword);
|
||||
|
||||
Game::randomEngine = std::mt19937(time(0));
|
||||
|
||||
//Run it until server gets a kill message from Master:
|
||||
auto t = std::chrono::high_resolution_clock::now();
|
||||
Packet* packet = nullptr;
|
||||
constexpr uint32_t logFlushTime = 30 * dashboardFramerate; // 30 seconds in frames
|
||||
constexpr uint32_t sqlPingTime = 10 * 60 * dashboardFramerate; // 10 minutes in frames
|
||||
uint32_t framesSinceLastFlush = 0;
|
||||
uint32_t framesSinceMasterDisconnect = 0;
|
||||
uint32_t framesSinceLastSQLPing = 0;
|
||||
|
||||
auto lastTime = std::chrono::high_resolution_clock::now();
|
||||
|
||||
Game::logger->Flush(); // once immediately before main loop
|
||||
while (!Game::ShouldShutdown()) {
|
||||
// Check if we're still connected to master:
|
||||
if (!Game::server->GetIsConnectedToMaster()) {
|
||||
framesSinceMasterDisconnect++;
|
||||
|
||||
if (framesSinceMasterDisconnect >= dashboardFramerate)
|
||||
break; //Exit our loop, shut down.
|
||||
} else framesSinceMasterDisconnect = 0;
|
||||
|
||||
const auto currentTime = std::chrono::high_resolution_clock::now();
|
||||
const float deltaTime = std::chrono::duration<float>(currentTime - lastTime).count();
|
||||
lastTime = currentTime;
|
||||
|
||||
// Check and handle web requests:
|
||||
Game::web.ReceiveRequests();
|
||||
|
||||
//Push our log every 30s:
|
||||
if (framesSinceLastFlush >= logFlushTime) {
|
||||
Game::logger->Flush();
|
||||
framesSinceLastFlush = 0;
|
||||
} else framesSinceLastFlush++;
|
||||
|
||||
//Every 10 min we ping our sql server to keep it alive hopefully:
|
||||
if (framesSinceLastSQLPing >= sqlPingTime) {
|
||||
//Find out the master's IP for absolutely no reason:
|
||||
std::string masterIP;
|
||||
uint32_t masterPort;
|
||||
|
||||
auto masterInfo = Database::Get()->GetMasterInfo();
|
||||
if (masterInfo) {
|
||||
masterIP = masterInfo->ip;
|
||||
masterPort = masterInfo->port;
|
||||
}
|
||||
|
||||
framesSinceLastSQLPing = 0;
|
||||
} else framesSinceLastSQLPing++;
|
||||
|
||||
//Sleep our thread since auth can afford to.
|
||||
t += std::chrono::milliseconds(dashboardFrameDelta); //Chat can run at a lower "fps"
|
||||
std::this_thread::sleep_until(t);
|
||||
}
|
||||
|
||||
//Delete our objects here:
|
||||
Database::Destroy("DashboardServer");
|
||||
delete Game::server;
|
||||
delete Game::logger;
|
||||
delete Game::config;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
59
dDashboardServer/DashboardWeb.cpp
Normal file
59
dDashboardServer/DashboardWeb.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "DashboardWeb.h"
|
||||
|
||||
// thanks bill gates
|
||||
#ifdef _WIN32
|
||||
#undef min
|
||||
#undef max
|
||||
#endif
|
||||
#include "inja.hpp"
|
||||
|
||||
#include "eHTTPMethod.h"
|
||||
|
||||
|
||||
// simple home page with inja
|
||||
void HandleHTTPHomeRequest(HTTPReply& reply, std::string body) {
|
||||
try {
|
||||
inja::Environment env;
|
||||
env.set_trim_blocks(true);
|
||||
env.set_lstrip_blocks(true);
|
||||
|
||||
nlohmann::json data;
|
||||
data["title"] = "Darkflame Universe Dashboard";
|
||||
data["header"] = "Welcome to the Darkflame Universe Dashboard";
|
||||
data["message"] = "This is a simple dashboard page served using Inja templating engine.";
|
||||
|
||||
const std::string template_str = R"(
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>{{ title }}</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{ header }}</h1>
|
||||
<p>{{ message }}</p>
|
||||
</body>
|
||||
</html>
|
||||
)";
|
||||
|
||||
std::string rendered = env.render(template_str, data);
|
||||
reply.message = rendered;
|
||||
reply.status = eHTTPStatusCode::OK;
|
||||
reply.contentType = ContentType::HTML;
|
||||
} catch (const std::exception& e) {
|
||||
reply.status = eHTTPStatusCode::INTERNAL_SERVER_ERROR;
|
||||
reply.message = "Internal Server Error";
|
||||
reply.contentType = ContentType::PLAIN;
|
||||
}
|
||||
}
|
||||
|
||||
namespace DashboardWeb {
|
||||
void RegisterRoutes() {
|
||||
Game::web.RegisterHTTPRoute({
|
||||
.path = "/",
|
||||
.method = eHTTPMethod::GET,
|
||||
.handle = HandleHTTPHomeRequest
|
||||
});
|
||||
}
|
||||
}
|
||||
11
dDashboardServer/DashboardWeb.h
Normal file
11
dDashboardServer/DashboardWeb.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef __DASHBOARDWEB_H__
|
||||
#define __DASHBOARDWEB_H__
|
||||
|
||||
#include "Web.h"
|
||||
|
||||
namespace DashboardWeb {
|
||||
void RegisterRoutes();
|
||||
};
|
||||
|
||||
|
||||
#endif // __DASHBOARDWEB_H__
|
||||
@@ -379,6 +379,7 @@ int main(int argc, char** argv) {
|
||||
Game::im->GetInstance(0, false, 0);
|
||||
Game::im->GetInstance(1000, false, 0);
|
||||
StartAuthServer();
|
||||
StartDashboardServer();
|
||||
}
|
||||
|
||||
auto t = std::chrono::high_resolution_clock::now();
|
||||
|
||||
@@ -147,3 +147,39 @@ uint32_t StartWorldServer(LWOMAPID mapID, uint16_t port, LWOINSTANCEID lastInsta
|
||||
LOG("WorldServer PID is %d", world_pid);
|
||||
return world_pid;
|
||||
}
|
||||
|
||||
uint32_t StartDashboardServer() {
|
||||
if (Game::ShouldShutdown()) {
|
||||
LOG("Currently shutting down. dashboard will not be restarted.");
|
||||
return 0;
|
||||
}
|
||||
auto dashboard_path = BinaryPathFinder::GetBinaryDir() / "DashboardServer";
|
||||
#ifdef _WIN32
|
||||
dashboard_path.replace_extension(".exe");
|
||||
auto dashboard_startup = startup;
|
||||
auto dashboard_info = PROCESS_INFORMATION{};
|
||||
if (!CreateProcessW(dashboard_path.wstring().data(), dashboard_path.wstring().data(),
|
||||
nullptr, nullptr, false, 0, nullptr, nullptr,
|
||||
&dashboard_startup, &dashboard_info))
|
||||
{
|
||||
LOG("Failed to launch DashboardServer");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// get pid and close unused handles
|
||||
auto dashboard_pid = dashboard_info.dwProcessId;
|
||||
CloseHandle(dashboard_info.hProcess);
|
||||
CloseHandle(dashboard_info.hThread);
|
||||
#else // *nix systems
|
||||
const auto dashboard_pid = fork();
|
||||
if (dashboard_pid < 0) {
|
||||
LOG("Failed to launch DashboardServer");
|
||||
return 0;
|
||||
} else if (dashboard_pid == 0) {
|
||||
// We are the child process
|
||||
execl(dashboard_path.string().c_str(), dashboard_path.string().c_str(), nullptr);
|
||||
}
|
||||
#endif
|
||||
LOG("DashboardServer PID is %d", dashboard_pid);
|
||||
return dashboard_pid;
|
||||
}
|
||||
@@ -4,3 +4,4 @@
|
||||
uint32_t StartAuthServer();
|
||||
uint32_t StartChatServer();
|
||||
uint32_t StartWorldServer(LWOMAPID mapID, uint16_t port, LWOINSTANCEID lastInstanceID, int maxPlayers, LWOCLONEID cloneID);
|
||||
uint32_t StartDashboardServer();
|
||||
|
||||
@@ -13,7 +13,6 @@ namespace Game {
|
||||
}
|
||||
|
||||
namespace {
|
||||
const char* jsonContentType = "Content-Type: application/json\r\n";
|
||||
const std::string wsSubscribed = "{\"status\":\"subscribed\"}";
|
||||
const std::string wsUnsubscribed = "{\"status\":\"unsubscribed\"}";
|
||||
std::map<std::pair<eHTTPMethod, std::string>, HTTPRoute> g_HTTPRoutes;
|
||||
@@ -73,7 +72,7 @@ void HandleHTTPMessage(mg_connection* connection, const mg_http_message* http_ms
|
||||
reply.status = eHTTPStatusCode::UNAUTHORIZED;
|
||||
reply.message = "{\"error\":\"Unauthorized\"}";
|
||||
}
|
||||
mg_http_reply(connection, static_cast<int>(reply.status), jsonContentType, reply.message.c_str());
|
||||
mg_http_reply(connection, static_cast<int>(reply.status), reply.contentType.c_str(), reply.message.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
26
dWeb/Web.h
26
dWeb/Web.h
@@ -20,10 +20,36 @@ enum class eHTTPMethod;
|
||||
// Forward declaration for mongoose manager
|
||||
typedef struct mg_mgr mg_mgr;
|
||||
|
||||
namespace ContentType {
|
||||
const std::string JSON = "Content-Type: application/json\r\n";
|
||||
const std::string HTML = "Content-Type: text/html\r\n";
|
||||
const std::string PLAIN = "Content-Type: text/plain\r\n";
|
||||
const std::string CSS = "Content-Type: text/css\r\n";
|
||||
const std::string JAVASCRIPT = "Content-Type: application/javascript\r\n";
|
||||
const std::string ICO = "Content-Type: image/x-icon\r\n";
|
||||
const std::string PNG = "Content-Type: image/png\r\n";
|
||||
const std::string SVG = "Content-Type: image/svg+xml\r\n";
|
||||
const std::string JPG = "Content-Type: image/jpeg\r\n";
|
||||
const std::string GIF = "Content-Type: image/gif\r\n";
|
||||
const std::string WEBP = "Content-Type: image/webp\r\n";
|
||||
const std::string MP4 = "Content-Type: video/mp4\r\n";
|
||||
const std::string OGG = "Content-Type: audio/ogg\r\n";
|
||||
const std::string MP3 = "Content-Type: audio/mpeg\r\n";
|
||||
const std::string BINARY = "Content-Type: application/octet-stream\r\n";
|
||||
const std::string FORM = "Content-Type: application/x-www-form-urlencoded\r\n";
|
||||
const std::string MULTIPART = "Content-Type: multipart/form-data\r\n";
|
||||
const std::string ZIP = "Content-Type: application/zip\r\n";
|
||||
const std::string PDF = "Content-Type: application/pdf\r\n";
|
||||
const std::string XML = "Content-Type: application/xml\r\n";
|
||||
const std::string CSV = "Content-Type: text/csv\r\n";
|
||||
const std::string YAML = "Content-Type: application/x-yaml\r\n";
|
||||
}
|
||||
|
||||
// For passing HTTP messages between functions
|
||||
struct HTTPReply {
|
||||
eHTTPStatusCode status = eHTTPStatusCode::NOT_FOUND;
|
||||
std::string message = "{\"error\":\"Not Found\"}";
|
||||
std::string contentType = ContentType::JSON;
|
||||
};
|
||||
|
||||
// HTTP route structure
|
||||
|
||||
0
resources/dashboardconfig.ini
Normal file
0
resources/dashboardconfig.ini
Normal file
3031
thirdparty/inja/inja.hpp
vendored
Normal file
3031
thirdparty/inja/inja.hpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user