mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 15:26:59 +00:00
implement GetOSName, start working on HttpsPOST
This commit is contained in:
parent
b1caf5c29a
commit
80432eb718
@ -9,4 +9,4 @@ std::string POST(const std::string& host, const std::string& target, const std::
|
||||
namespace Status {
|
||||
std::string ToString(int code);
|
||||
}
|
||||
}
|
||||
}
|
14
src/Http.cpp
14
src/Http.cpp
@ -16,10 +16,12 @@ namespace ssl = net::ssl; // from <boost/asio/ssl.hpp>
|
||||
using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
|
||||
std::string Http::GET(const std::string& host, int port, const std::string& target, unsigned int* status) {
|
||||
|
||||
try {
|
||||
// Check command line arguments.
|
||||
int version = 11;
|
||||
|
||||
|
||||
// The io_context is required for all I/O
|
||||
net::io_context ioc;
|
||||
|
||||
@ -64,6 +66,7 @@ std::string Http::GET(const std::string& host, int port, const std::string& targ
|
||||
// This buffer is used for reading and must be persisted
|
||||
beast::flat_buffer buffer;
|
||||
|
||||
|
||||
// Declare a container to hold the response
|
||||
http::response<http::string_body> res;
|
||||
|
||||
@ -135,11 +138,10 @@ std::string Http::POST(const std::string& host, const std::string& target, const
|
||||
|
||||
req.set(http::field::host, host);
|
||||
if (!body.empty()) {
|
||||
if (json) {
|
||||
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_type, ContentType); // "application/json"
|
||||
// "application/x-www-form-urlencoded"
|
||||
|
||||
|
||||
req.set(http::field::content_length, std::to_string(body.size()));
|
||||
req.body() = body;
|
||||
// info("body is " + body + " (" + req.body() + ")");
|
||||
@ -278,4 +280,4 @@ std::string Http::Status::ToString(int code) {
|
||||
} else {
|
||||
return "Unassigned";
|
||||
}
|
||||
}
|
||||
}
|
@ -39,6 +39,7 @@ void THeartbeatThread::operator()() {
|
||||
if (status < 0) {
|
||||
status = 0;
|
||||
}
|
||||
|
||||
auto Lock = Sentry.CreateExclusiveContext();
|
||||
Sentry.SetContext("heartbeat",
|
||||
{ { "response-body", T },
|
||||
@ -48,6 +49,7 @@ void THeartbeatThread::operator()() {
|
||||
Sentry.Log(SentryLevel::Error, "default", Http::Status::ToString(status) + " (" + std::to_string(status) + ")");
|
||||
};
|
||||
|
||||
|
||||
auto Target = "/heartbeat";
|
||||
int ResponseCode = -1;
|
||||
T = Http::POST(Application::GetBackendHostname(), Target, {}, Body, false, &ResponseCode);
|
||||
@ -63,6 +65,7 @@ void THeartbeatThread::operator()() {
|
||||
T = Http::POST(Application::GetBackup2Hostname(), Target, {}, Body, false, &ResponseCode);
|
||||
if (T.substr(0, 2) != "20" || ResponseCode != 200) {
|
||||
warn("Backend system refused server! Server will not show in the public server list.");
|
||||
|
||||
isAuth = false;
|
||||
SentryReportError(Application::GetBackup2Hostname() + Target, ResponseCode);
|
||||
}
|
||||
@ -127,4 +130,4 @@ std::string THeartbeatThread::GetPlayers() {
|
||||
return Return;
|
||||
}
|
||||
/*THeartbeatThread::~THeartbeatThread() {
|
||||
}*/
|
||||
}*/
|
@ -70,12 +70,17 @@ void TLuaEngine::FolderList(const std::string& Path, bool HotSwap) {
|
||||
for (const auto& entry : fs::directory_iterator(Path)) {
|
||||
if (fs::is_directory(entry)) {
|
||||
RegisterFiles(entry.path(), HotSwap);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TLuaEngine::RegisterFiles(const fs::path& Path, bool HotSwap) {
|
||||
std::string Name = Path.filename().string();
|
||||
std::string Name = Path.substr(Path.find_last_of('/') + 1);
|
||||
#else
|
||||
|
||||
#endif
|
||||
if (!HotSwap)
|
||||
info(("Loading plugin : ") + Name);
|
||||
std::vector<fs::path> Entries;
|
||||
@ -104,10 +109,11 @@ void TLuaEngine::RegisterFiles(const fs::path& Path, bool HotSwap) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool TLuaEngine::IsNewFile(const std::string& Path) {
|
||||
for (auto& Script : mLuaFiles) {
|
||||
if (fs::absolute(Path) == fs::absolute(Script->GetFileName()))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
127
src/TLuaFile.cpp
127
src/TLuaFile.cpp
@ -3,6 +3,7 @@
|
||||
#include "Common.h"
|
||||
#include "CustomAssert.h"
|
||||
#include "Defer.h"
|
||||
#include "Http.h"
|
||||
#include "TLuaEngine.h"
|
||||
#include "TNetwork.h"
|
||||
#include "TServer.h"
|
||||
@ -10,7 +11,30 @@
|
||||
#include <future>
|
||||
#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);
|
||||
std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArg> Arg);
|
||||
@ -40,11 +64,13 @@ std::shared_ptr<TLuaArg> CreateArg(lua_State* L, int T, int S) {
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
void ClearStack(lua_State* L) {
|
||||
lua_settop(L, 0);
|
||||
}
|
||||
|
||||
std::any Trigger(TLuaFile* lua, const std::string& R, std::shared_ptr<TLuaArg> arg) {
|
||||
|
||||
std::lock_guard<std::mutex> lockGuard(lua->Lock);
|
||||
std::packaged_task<std::any(std::shared_ptr<TLuaArg>)> task([lua, R](std::shared_ptr<TLuaArg> arg) { return CallFunction(lua, R, arg); });
|
||||
std::future<std::any> f1 = task.get_future();
|
||||
@ -198,6 +224,7 @@ void ExecuteAsync(TLuaFile* lua, const std::string& FuncName) {
|
||||
}
|
||||
|
||||
void CallAsync(TLuaFile* lua, const std::string& Func, int U) {
|
||||
|
||||
lua->SetStopThread(false);
|
||||
int D = 1000 / U;
|
||||
while (!lua->GetStopThread()) {
|
||||
@ -585,6 +612,42 @@ int lua_Set(lua_State* L) {
|
||||
|
||||
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" {
|
||||
int lua_Print(lua_State* L) {
|
||||
@ -754,6 +817,7 @@ std::string TLuaFile::GetOrigin() {
|
||||
}
|
||||
|
||||
std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArg> Arg) {
|
||||
|
||||
lua_State* luaState = lua->GetState();
|
||||
lua_getglobal(luaState, FuncName.c_str());
|
||||
if (lua_isfunction(luaState, -1)) {
|
||||
@ -787,32 +851,48 @@ void TLuaFile::SetFileName(const std::string& 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() {
|
||||
Assert(mLuaState);
|
||||
luaL_openlibs(mLuaState);
|
||||
lua_register(mLuaState, "GetPlayerIdentifiers", lua_GetIdentifiers);
|
||||
lua_register(mLuaState, "TriggerGlobalEvent", lua_TriggerEventG);
|
||||
lua_register(mLuaState, "TriggerLocalEvent", lua_TriggerEventL);
|
||||
lua_register(mLuaState, "TriggerClientEvent", lua_RemoteEvent);
|
||||
lua_register(mLuaState, "GetPlayerCount", lua_GetPlayerCount);
|
||||
lua_register(mLuaState, "isPlayerConnected", lua_isConnected);
|
||||
lua_register(mLuaState, "RegisterEvent", lua_RegisterEvent);
|
||||
lua_register(mLuaState, "GetPlayerName", lua_GetPlayerName);
|
||||
lua_register(mLuaState, "RemoveVehicle", lua_RemoveVehicle);
|
||||
lua_register(mLuaState, "GetPlayerDiscordID", lua_TempFix);
|
||||
lua_register(mLuaState, "CreateThread", lua_CreateThread);
|
||||
lua_register(mLuaState, "GetPlayerVehicles", lua_GetCars);
|
||||
lua_register(mLuaState, "SendChatMessage", lua_sendChat);
|
||||
lua_register(mLuaState, "GetPlayers", lua_GetAllPlayers);
|
||||
lua_register(mLuaState, "GetPlayerGuest", lua_GetGuest);
|
||||
lua_register(mLuaState, "StopThread", lua_StopThread);
|
||||
lua_register(mLuaState, "DropPlayer", lua_dropPlayer);
|
||||
lua_register(mLuaState, "GetPlayerHWID", lua_HWID);
|
||||
|
||||
LuaTable::Begin(mLuaState);
|
||||
LuaTable::InsertFunction(mLuaState, "GetPlayerIdentifiers", lua_GetIdentifiers);
|
||||
LuaTable::InsertFunction(mLuaState, "TriggerGlobalEvent", lua_TriggerEventG);
|
||||
LuaTable::InsertFunction(mLuaState, "TriggerLocalEvent", lua_TriggerEventL);
|
||||
LuaTable::InsertFunction(mLuaState, "TriggerClientEvent", lua_RemoteEvent);
|
||||
LuaTable::InsertFunction(mLuaState, "GetPlayerCount", lua_GetPlayerCount);
|
||||
LuaTable::InsertFunction(mLuaState, "IsPlayerConnected", lua_isConnected);
|
||||
LuaTable::InsertFunction(mLuaState, "RegisterEvent", lua_RegisterEvent);
|
||||
LuaTable::InsertFunction(mLuaState, "GetPlayerName", lua_GetPlayerName);
|
||||
LuaTable::InsertFunction(mLuaState, "RemoveVehicle", lua_RemoveVehicle);
|
||||
LuaTable::InsertFunction(mLuaState, "GetPlayerDiscordID", lua_TempFix);
|
||||
LuaTable::InsertFunction(mLuaState, "CreateThread", lua_CreateThread);
|
||||
LuaTable::InsertFunction(mLuaState, "GetPlayerVehicles", lua_GetCars);
|
||||
LuaTable::InsertFunction(mLuaState, "SendChatMessage", lua_sendChat);
|
||||
LuaTable::InsertFunction(mLuaState, "GetPlayers", lua_GetAllPlayers);
|
||||
LuaTable::InsertFunction(mLuaState, "GetPlayerGuest", lua_GetGuest);
|
||||
LuaTable::InsertFunction(mLuaState, "StopThread", lua_StopThread);
|
||||
LuaTable::InsertFunction(mLuaState, "DropPlayer", lua_dropPlayer);
|
||||
lua_register(mLuaState, "Register", lua_Register);
|
||||
lua_register(mLuaState, "exit", lua_ServerExit);
|
||||
lua_register(mLuaState, "Sleep", lua_Sleep);
|
||||
LuaTable::InsertFunction(mLuaState, "GetPlayerHWID", lua_HWID);
|
||||
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, "Set", lua_Set);
|
||||
lua_register(mLuaState, "exit", lua_ServerExit);
|
||||
if (!mConsole)
|
||||
Reload();
|
||||
}
|
||||
@ -887,6 +967,7 @@ void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg) {
|
||||
warn(a + (" | Incorrect Call of ") + msg);
|
||||
}
|
||||
|
||||
|
||||
void TLuaArg::PushArgs(lua_State* State) {
|
||||
for (std::any arg : args) {
|
||||
if (!arg.has_value()) {
|
||||
@ -911,4 +992,4 @@ void TLuaArg::PushArgs(lua_State* State) {
|
||||
error("what in the hell is " + std::string(arg.type().name()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -992,4 +992,4 @@ std::string TNetwork::UDPRcvFromClient(sockaddr_in& client) const {
|
||||
return "";
|
||||
}
|
||||
return std::string(Ret.begin(), Ret.begin() + Rcv);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user