mirror of
https://github.com/SantaSpeen/BeamMP-Server.git
synced 2026-06-18 15:20:53 +00:00
implement GetOSName, start working on HttpsPOST
This commit is contained in:
+2
-2
@@ -5,5 +5,5 @@
|
|||||||
|
|
||||||
namespace Http {
|
namespace Http {
|
||||||
std::string GET(const std::string& host, int port, const std::string& target);
|
std::string GET(const std::string& host, int port, const std::string& target);
|
||||||
std::string POST(const std::string& host, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, bool json);
|
std::string POST(const std::string& host, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, const std::string& ContentType);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-7
@@ -54,7 +54,7 @@ std::string Http::GET(const std::string& host, int port, const std::string& targ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Http::POST(const std::string& host, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, bool json) {
|
std::string Http::POST(const std::string& host, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, const std::string& ContentType) {
|
||||||
try {
|
try {
|
||||||
net::io_context io;
|
net::io_context io;
|
||||||
|
|
||||||
@@ -96,12 +96,8 @@ std::string Http::POST(const std::string& host, const std::string& target, const
|
|||||||
|
|
||||||
req.set(http::field::host, host);
|
req.set(http::field::host, host);
|
||||||
if (!body.empty()) {
|
if (!body.empty()) {
|
||||||
if (json) {
|
req.set(http::field::content_type, ContentType); // "application/json"
|
||||||
// FIXME: json is untested.
|
// "application/x-www-form-urlencoded"
|
||||||
req.set(http::field::content_type, "application/json");
|
|
||||||
} else {
|
|
||||||
req.set(http::field::content_type, "application/x-www-form-urlencoded");
|
|
||||||
}
|
|
||||||
req.set(http::field::content_length, std::to_string(body.size()));
|
req.set(http::field::content_length, std::to_string(body.size()));
|
||||||
req.body() = body;
|
req.body() = body;
|
||||||
// info("body is " + body + " (" + req.body() + ")");
|
// info("body is " + body + " (" + req.body() + ")");
|
||||||
|
|||||||
@@ -35,12 +35,12 @@ void THeartbeatThread::operator()() {
|
|||||||
|
|
||||||
Body += "&pps=" + Application::PPS();
|
Body += "&pps=" + Application::PPS();
|
||||||
|
|
||||||
T = Http::POST(Application::GetBackendHostname(), "/heartbeat", {}, Body, false);
|
T = Http::POST(Application::GetBackendHostname(), "/heartbeat", {}, Body, "application/x-www-form-urlencoded");
|
||||||
|
|
||||||
if (T.substr(0, 2) != "20") {
|
if (T.substr(0, 2) != "20") {
|
||||||
//Backend system refused server startup!
|
//Backend system refused server startup!
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
T = Http::POST(Application::GetBackendHostname(), "/heartbeat", {}, Body, false);
|
T = Http::POST(Application::GetBackendHostname(), "/heartbeat", {}, Body, "application/x-www-form-urlencoded");
|
||||||
// TODO backup2 + HTTP flag (no TSL)
|
// TODO backup2 + HTTP flag (no TSL)
|
||||||
if (T.substr(0, 2) != "20") {
|
if (T.substr(0, 2) != "20") {
|
||||||
warn("Backend system refused server! Server might not show in the public list");
|
warn("Backend system refused server! Server might not show in the public list");
|
||||||
|
|||||||
@@ -75,7 +75,11 @@ void TLuaEngine::FolderList(const std::string& Path, bool HotSwap) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TLuaEngine::RegisterFiles(const std::string& Path, bool HotSwap) {
|
void TLuaEngine::RegisterFiles(const std::string& Path, bool HotSwap) {
|
||||||
|
#if defined(__linux) || defined(__linux__)
|
||||||
|
std::string Name = Path.substr(Path.find_last_of('/') + 1);
|
||||||
|
#else
|
||||||
std::string Name = Path.substr(Path.find_last_of('\\') + 1);
|
std::string Name = Path.substr(Path.find_last_of('\\') + 1);
|
||||||
|
#endif
|
||||||
if (!HotSwap)
|
if (!HotSwap)
|
||||||
info(("Loading plugin : ") + Name);
|
info(("Loading plugin : ") + Name);
|
||||||
for (const auto& entry : fs::directory_iterator(Path)) {
|
for (const auto& entry : fs::directory_iterator(Path)) {
|
||||||
|
|||||||
+101
-23
@@ -2,6 +2,7 @@
|
|||||||
#include "Client.h"
|
#include "Client.h"
|
||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
#include "CustomAssert.h"
|
#include "CustomAssert.h"
|
||||||
|
#include "Http.h"
|
||||||
#include "TLuaEngine.h"
|
#include "TLuaEngine.h"
|
||||||
#include "TNetwork.h"
|
#include "TNetwork.h"
|
||||||
#include "TServer.h"
|
#include "TServer.h"
|
||||||
@@ -9,7 +10,30 @@
|
|||||||
#include <future>
|
#include <future>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
// TODO: REWRITE
|
namespace LuaTable {
|
||||||
|
void Begin(lua_State* L) {
|
||||||
|
lua_newtable(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
void End(lua_State* L, const std::string& name) {
|
||||||
|
lua_setglobal(L, name.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void BeginEntry(lua_State* L, const std::string& name) {
|
||||||
|
lua_pushstring(L, name.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void EndEntry(lua_State* L) {
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InsertFunction(lua_State* L, const std::string& name, lua_CFunction func) {
|
||||||
|
BeginEntry(L, name);
|
||||||
|
lua_pushcfunction(L, func);
|
||||||
|
EndEntry(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg);
|
void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg);
|
||||||
std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArg> Arg);
|
std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArg> Arg);
|
||||||
@@ -39,6 +63,7 @@ std::shared_ptr<TLuaArg> CreateArg(lua_State* L, int T, int S) {
|
|||||||
}
|
}
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClearStack(lua_State* L) {
|
void ClearStack(lua_State* L) {
|
||||||
lua_settop(L, 0);
|
lua_settop(L, 0);
|
||||||
}
|
}
|
||||||
@@ -561,6 +586,42 @@ int lua_Set(lua_State* L) {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
// CallInPlugin(PluginName, FunctionName)
|
||||||
|
int lua_CallInPlugin(lua_State* L) {
|
||||||
|
if (!lua_isstring(L, 1)) {
|
||||||
|
SendError(Engine(), L, "CallInPlugin expects a string as 1. argument.");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (!lua_isstring(L, 2)) {
|
||||||
|
SendError(Engine(), L, "CallInPlugin expects a string as 2. argument.");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const char* PluginName = lua_tostring(L, 1);
|
||||||
|
const char* FunctionName = lua_tostring(L, 2);
|
||||||
|
|
||||||
|
bool FoundPlugin = false;
|
||||||
|
for (const auto& File : Engine().LuaFiles()) {
|
||||||
|
if (File->GetPluginName() == PluginName) {
|
||||||
|
FoundPlugin = true;
|
||||||
|
auto State = File->GetState();
|
||||||
|
lua_getglobal(State, FunctionName);
|
||||||
|
if (!lua_isfunction(State, -1)) {
|
||||||
|
SendError(Engine(), L, "CallInPlugin: \"" + std::string(FunctionName) + "\" in plugin \"" + std::string(PluginName) + "\" is not a function.");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
ClearStack(State);
|
||||||
|
CallFunction(File.get(), FunctionName, nullptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!FoundPlugin) {
|
||||||
|
SendError(Engine(), L, "CallInPlugin: Could not find plugin called \"" + std::string(PluginName) + "\"");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
int lua_Print(lua_State* L) {
|
int lua_Print(lua_State* L) {
|
||||||
@@ -763,32 +824,48 @@ void TLuaFile::SetFileName(const std::string& Name) {
|
|||||||
mFileName = Name;
|
mFileName = Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetOSName() -> Linux || Windows || Other
|
||||||
|
int lua_GetOSName(lua_State* L) {
|
||||||
|
#if defined(__linux) || defined(__linux__)
|
||||||
|
lua_pushstring(L, "Linux");
|
||||||
|
#elif defined(WIN32)
|
||||||
|
lua_pushstring(L, "Windows");
|
||||||
|
#else
|
||||||
|
lua_pushstring(L, "Unknown");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void TLuaFile::Load() {
|
void TLuaFile::Load() {
|
||||||
Assert(mLuaState);
|
Assert(mLuaState);
|
||||||
luaL_openlibs(mLuaState);
|
luaL_openlibs(mLuaState);
|
||||||
lua_register(mLuaState, "GetPlayerIdentifiers", lua_GetIdentifiers);
|
|
||||||
lua_register(mLuaState, "TriggerGlobalEvent", lua_TriggerEventG);
|
LuaTable::Begin(mLuaState);
|
||||||
lua_register(mLuaState, "TriggerLocalEvent", lua_TriggerEventL);
|
LuaTable::InsertFunction(mLuaState, "GetPlayerIdentifiers", lua_GetIdentifiers);
|
||||||
lua_register(mLuaState, "TriggerClientEvent", lua_RemoteEvent);
|
LuaTable::InsertFunction(mLuaState, "TriggerGlobalEvent", lua_TriggerEventG);
|
||||||
lua_register(mLuaState, "GetPlayerCount", lua_GetPlayerCount);
|
LuaTable::InsertFunction(mLuaState, "TriggerLocalEvent", lua_TriggerEventL);
|
||||||
lua_register(mLuaState, "isPlayerConnected", lua_isConnected);
|
LuaTable::InsertFunction(mLuaState, "TriggerClientEvent", lua_RemoteEvent);
|
||||||
lua_register(mLuaState, "RegisterEvent", lua_RegisterEvent);
|
LuaTable::InsertFunction(mLuaState, "GetPlayerCount", lua_GetPlayerCount);
|
||||||
lua_register(mLuaState, "GetPlayerName", lua_GetPlayerName);
|
LuaTable::InsertFunction(mLuaState, "IsPlayerConnected", lua_isConnected);
|
||||||
lua_register(mLuaState, "RemoveVehicle", lua_RemoveVehicle);
|
LuaTable::InsertFunction(mLuaState, "RegisterEvent", lua_RegisterEvent);
|
||||||
lua_register(mLuaState, "GetPlayerDiscordID", lua_TempFix);
|
LuaTable::InsertFunction(mLuaState, "GetPlayerName", lua_GetPlayerName);
|
||||||
lua_register(mLuaState, "CreateThread", lua_CreateThread);
|
LuaTable::InsertFunction(mLuaState, "RemoveVehicle", lua_RemoveVehicle);
|
||||||
lua_register(mLuaState, "GetPlayerVehicles", lua_GetCars);
|
LuaTable::InsertFunction(mLuaState, "GetPlayerDiscordID", lua_TempFix);
|
||||||
lua_register(mLuaState, "SendChatMessage", lua_sendChat);
|
LuaTable::InsertFunction(mLuaState, "CreateThread", lua_CreateThread);
|
||||||
lua_register(mLuaState, "GetPlayers", lua_GetAllPlayers);
|
LuaTable::InsertFunction(mLuaState, "GetPlayerVehicles", lua_GetCars);
|
||||||
lua_register(mLuaState, "GetPlayerGuest", lua_GetGuest);
|
LuaTable::InsertFunction(mLuaState, "SendChatMessage", lua_sendChat);
|
||||||
lua_register(mLuaState, "StopThread", lua_StopThread);
|
LuaTable::InsertFunction(mLuaState, "GetPlayers", lua_GetAllPlayers);
|
||||||
lua_register(mLuaState, "DropPlayer", lua_dropPlayer);
|
LuaTable::InsertFunction(mLuaState, "GetPlayerGuest", lua_GetGuest);
|
||||||
lua_register(mLuaState, "GetPlayerHWID", lua_HWID);
|
LuaTable::InsertFunction(mLuaState, "StopThread", lua_StopThread);
|
||||||
|
LuaTable::InsertFunction(mLuaState, "DropPlayer", lua_dropPlayer);
|
||||||
lua_register(mLuaState, "Register", lua_Register);
|
lua_register(mLuaState, "Register", lua_Register);
|
||||||
lua_register(mLuaState, "exit", lua_ServerExit);
|
LuaTable::InsertFunction(mLuaState, "GetPlayerHWID", lua_HWID);
|
||||||
lua_register(mLuaState, "Sleep", lua_Sleep);
|
LuaTable::InsertFunction(mLuaState, "Sleep", lua_Sleep);
|
||||||
|
LuaTable::InsertFunction(mLuaState, "Set", lua_Set);
|
||||||
|
LuaTable::InsertFunction(mLuaState, "GetOSName", lua_GetOSName);
|
||||||
|
LuaTable::End(mLuaState, "MP");
|
||||||
|
|
||||||
lua_register(mLuaState, "print", lua_Print);
|
lua_register(mLuaState, "print", lua_Print);
|
||||||
lua_register(mLuaState, "Set", lua_Set);
|
lua_register(mLuaState, "exit", lua_ServerExit);
|
||||||
if (!mConsole)
|
if (!mConsole)
|
||||||
Reload();
|
Reload();
|
||||||
}
|
}
|
||||||
@@ -863,6 +940,7 @@ void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg) {
|
|||||||
warn(a + (" | Incorrect Call of ") + msg);
|
warn(a + (" | Incorrect Call of ") + msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void TLuaArg::PushArgs(lua_State* State) {
|
void TLuaArg::PushArgs(lua_State* State) {
|
||||||
for (std::any arg : args) {
|
for (std::any arg : args) {
|
||||||
if (!arg.has_value()) {
|
if (!arg.has_value()) {
|
||||||
@@ -887,4 +965,4 @@ void TLuaArg::PushArgs(lua_State* State) {
|
|||||||
error("what in the hell is " + std::string(arg.type().name()));
|
error("what in the hell is " + std::string(arg.type().name()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -281,7 +281,7 @@ void TNetwork::Authentication(SOCKET TCPSock) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!Rc.empty()) {
|
if (!Rc.empty()) {
|
||||||
Rc = Http::POST(Application::GetBackendUrlForAuth(), "/pkToUser", {}, R"({"key":")" + Rc + "\"}", true);
|
Rc = Http::POST(Application::GetBackendUrlForAuth(), "/pkToUser", {}, R"({"key":")" + Rc + "\"}", "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
json::Document AuthResponse;
|
json::Document AuthResponse;
|
||||||
|
|||||||
Reference in New Issue
Block a user