reformat all

This commit is contained in:
Lion Kortlepel
2020-12-23 23:34:30 +01:00
parent 0f4c08c068
commit a944565fb9
13 changed files with 171 additions and 147 deletions
+10 -10
View File
@@ -115,16 +115,16 @@ void GenerateConfig() {
std::ofstream FileStream; std::ofstream FileStream;
FileStream.open(("Server.cfg")); FileStream.open(("Server.cfg"));
FileStream << ("# This is the BeamMP Server Configuration File v0.60\n" FileStream << ("# This is the BeamMP Server Configuration File v0.60\n"
"Debug = false # true or false to enable debug console output\n" "Debug = false # true or false to enable debug console output\n"
"Private = true # Private?\n" "Private = true # Private?\n"
"Port = 30814 # Port to run the server on UDP and TCP\n" "Port = 30814 # Port to run the server on UDP and TCP\n"
"Cars = 1 # Max cars for every player\n" "Cars = 1 # Max cars for every player\n"
"MaxPlayers = 10 # Maximum Amount of Clients\n" "MaxPlayers = 10 # Maximum Amount of Clients\n"
"Map = \"/levels/gridmap/info.json\" # Default Map\n" "Map = \"/levels/gridmap/info.json\" # Default Map\n"
"Name = \"BeamMP New Server\" # Server Name\n" "Name = \"BeamMP New Server\" # Server Name\n"
"Desc = \"BeamMP Default Description\" # Server Description\n" "Desc = \"BeamMP Default Description\" # Server Description\n"
"use = \"Resources\" # Resource file name\n" "use = \"Resources\" # Resource file name\n"
"AuthKey = \"\" # Auth Key"); "AuthKey = \"\" # Auth Key");
FileStream.close(); FileStream.close();
} }
void Default() { void Default() {
+3 -3
View File
@@ -56,15 +56,15 @@ std::string RunPromise(const std::string& IP, const std::string& R) {
R = GenerateCall(); R = GenerateCall();
if (!CustomIP.empty()) if (!CustomIP.empty())
R += "&ip=" + CustomIP; R += "&ip=" + CustomIP;
std::string link ="https://beammp.com/heartbeatv2"; std::string link = "https://beammp.com/heartbeatv2";
T = RunPromise(link, R); T = RunPromise(link, R);
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::seconds(10)); std::this_thread::sleep_for(std::chrono::seconds(10));
std::string Backup = "https://backup1.beammp.com/heartbeatv2"; std::string Backup = "https://backup1.beammp.com/heartbeatv2";
T = RunPromise(Backup, R); T = RunPromise(Backup, R);
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");
} }
} }
+2 -1
View File
@@ -34,5 +34,6 @@ void InitRes() {
} }
} }
std::replace(FileList.begin(), FileList.end(), '\\', '/'); std::replace(FileList.begin(), FileList.end(), '\\', '/');
if(ModsLoaded)info("Loaded " + std::to_string(ModsLoaded) + " Mods"); if (ModsLoaded)
info("Loaded " + std::to_string(ModsLoaded) + " Mods");
} }
+2 -1
View File
@@ -27,7 +27,8 @@ void Args(int argc, char* argv[]) {
if (p != std::string::npos || n != 3 || CustomIP.substr(0, 3) == ("127")) { if (p != std::string::npos || n != 3 || CustomIP.substr(0, 3) == ("127")) {
CustomIP.clear(); CustomIP.clear();
warn("IP Specified is invalid! Ignoring"); warn("IP Specified is invalid! Ignoring");
} else info("Server started with custom IP"); } else
info("Server started with custom IP");
} }
} }
void InitServer(int argc, char* argv[]) { void InitServer(int argc, char* argv[]) {
+31 -21
View File
@@ -93,16 +93,20 @@ std::any TriggerLuaEvent(const std::string& Event, bool local, Lua* Caller, std:
if (Script->GetPluginName() == Caller->GetPluginName()) { if (Script->GetPluginName() == Caller->GetPluginName()) {
R = FutureWait(Script.get(), Script->GetRegistered(Event), arg, Wait); R = FutureWait(Script.get(), Script->GetRegistered(Event), arg, Wait);
Type = R.type().name(); Type = R.type().name();
if(Type.find("int") != std::string::npos){ if (Type.find("int") != std::string::npos) {
if(std::any_cast<int>(R))Ret++; if (std::any_cast<int>(R))
}else if(Event == "onPlayerAuth") return R; Ret++;
} else if (Event == "onPlayerAuth")
return R;
} }
}else{ } else {
R = FutureWait(Script.get(), Script->GetRegistered(Event), arg, Wait); R = FutureWait(Script.get(), Script->GetRegistered(Event), arg, Wait);
Type = R.type().name(); Type = R.type().name();
if(Type.find("int") != std::string::npos){ if (Type.find("int") != std::string::npos) {
if(std::any_cast<int>(R))Ret++; if (std::any_cast<int>(R))
}else if(Event == "onPlayerAuth") return R; Ret++;
} else if (Event == "onPlayerAuth")
return R;
} }
} }
} }
@@ -298,8 +302,10 @@ int lua_GetGuest(lua_State* L) {
if (lua_isnumber(L, 1)) { if (lua_isnumber(L, 1)) {
int ID = int(lua_tonumber(L, 1)); int ID = int(lua_tonumber(L, 1));
Client* c = GetClient(ID); Client* c = GetClient(ID);
if (c != nullptr)lua_pushboolean(L, c->isGuest); if (c != nullptr)
else return 0; lua_pushboolean(L, c->isGuest);
else
return 0;
} else { } else {
SendError(L, "GetGuest not enough arguments"); SendError(L, "GetGuest not enough arguments");
return 0; return 0;
@@ -360,7 +366,8 @@ int lua_dropPlayer(lua_State* L) {
c->SetStatus(-2); c->SetStatus(-2);
info(("Closing socket due to kick")); info(("Closing socket due to kick"));
CloseSocketProper(c->GetTCPSock()); CloseSocketProper(c->GetTCPSock());
} else SendError(L, ("DropPlayer not enough arguments")); } else
SendError(L, ("DropPlayer not enough arguments"));
return 0; return 0;
} }
int lua_sendChat(lua_State* L) { int lua_sendChat(lua_State* L) {
@@ -445,9 +452,9 @@ int lua_RemoteEvent(lua_State* L) {
} }
return 0; return 0;
} }
int lua_ServerExit(lua_State*L) { int lua_ServerExit(lua_State* L) {
if(lua_gettop(L) > 0){ if (lua_gettop(L) > 0) {
if(lua_isnumber(L,1)){ if (lua_isnumber(L, 1)) {
_Exit(int(lua_tointeger(L, 1))); _Exit(int(lua_tointeger(L, 1)));
} }
} }
@@ -589,8 +596,8 @@ std::any CallFunction(Lua* lua, const std::string& FuncName, std::shared_ptr<Lua
if (CheckLua(luaState, R)) { if (CheckLua(luaState, R)) {
if (lua_isnumber(luaState, -1)) { if (lua_isnumber(luaState, -1)) {
return int(lua_tointeger(luaState, -1)); return int(lua_tointeger(luaState, -1));
}else if(lua_isstring(luaState,-1)){ } else if (lua_isstring(luaState, -1)) {
return std::string(lua_tostring(luaState,-1)); return std::string(lua_tostring(luaState, -1));
} }
} }
} }
@@ -603,17 +610,20 @@ void Lua::SetPluginName(const std::string& Name) {
void Lua::SetFileName(const std::string& Name) { void Lua::SetFileName(const std::string& Name) {
_FileName = Name; _FileName = Name;
} }
int lua_TempFix(lua_State*L) { int lua_TempFix(lua_State* L) {
if (lua_isnumber(L, 1)) { if (lua_isnumber(L, 1)) {
int ID = int(lua_tonumber(L, 1)); int ID = int(lua_tonumber(L, 1));
Client* c = GetClient(ID); Client* c = GetClient(ID);
if (c == nullptr)return 0; if (c == nullptr)
return 0;
std::string Ret; std::string Ret;
if(c->isGuest){ if (c->isGuest) {
Ret = "Guest-" + c->GetName(); Ret = "Guest-" + c->GetName();
}else Ret = c->GetName(); } else
lua_pushstring(L,Ret.c_str()); Ret = c->GetName();
} else SendError(L, "GetDID not enough arguments"); lua_pushstring(L, Ret.c_str());
} else
SendError(L, "GetDID not enough arguments");
return 1; return 1;
} }
void Lua::Init() { void Lua::Init() {
+37 -36
View File
@@ -6,22 +6,22 @@
/// Created by Anonymous275 on 7/31/2020 /// Created by Anonymous275 on 7/31/2020
/// ///
#include <Lua/LuaSystem.hpp>
#include "Security/Enc.h"
#include "UnixCompat.h"
#include "Curl/Http.h" #include "Curl/Http.h"
#include "Settings.h" #include "Json.h"
#include "Network.h"
#include "Logger.h" #include "Logger.h"
#include "Network.h"
#include "Security/Enc.h"
#include "Settings.h"
#include "UnixCompat.h"
#include <Lua/LuaSystem.hpp>
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <thread> #include <thread>
#include "Json.h"
std::string GetClientInfo(const std::string& PK) { std::string GetClientInfo(const std::string& PK) {
if (!PK.empty()) { if (!PK.empty()) {
return PostHTTP("https://auth.beammp.com/pkToUser", R"({"key":")"+PK+"\"}",true);; return PostHTTP("https://auth.beammp.com/pkToUser", R"({"key":")" + PK + "\"}", true);
;
} }
return ""; return "";
} }
@@ -32,13 +32,12 @@ Client* CreateClient(SOCKET TCPSock) {
return c; return c;
} }
void ClientKick(Client* c, const std::string& R){ void ClientKick(Client* c, const std::string& R) {
info("Client kicked: " + R); info("Client kicked: " + R);
TCPSend(c, "E" + R); TCPSend(c, "E" + R);
CloseSocketProper(c->GetTCPSock()); CloseSocketProper(c->GetTCPSock());
} }
void Authentication(SOCKET TCPSock) { void Authentication(SOCKET TCPSock) {
DebugPrintTID(); DebugPrintTID();
auto* c = CreateClient(TCPSock); auto* c = CreateClient(TCPSock);
@@ -49,36 +48,36 @@ void Authentication(SOCKET TCPSock) {
if (Rc.size() > 3 && Rc.substr(0, 2) == "VC") { if (Rc.size() > 3 && Rc.substr(0, 2) == "VC") {
Rc = Rc.substr(2); Rc = Rc.substr(2);
if (Rc.length() > 4 || Rc != GetCVer()) { if (Rc.length() > 4 || Rc != GetCVer()) {
ClientKick(c,"Outdated Version!"); ClientKick(c, "Outdated Version!");
return; return;
} }
} else { } else {
ClientKick(c,"Invalid version header!"); ClientKick(c, "Invalid version header!");
return; return;
} }
TCPSend(c, "S"); TCPSend(c, "S");
Rc = TCPRcv(c); Rc = TCPRcv(c);
if(Rc.size() > 50){ if (Rc.size() > 50) {
ClientKick(c,"Invalid Key!"); ClientKick(c, "Invalid Key!");
return; return;
} }
Rc = GetClientInfo(Rc); Rc = GetClientInfo(Rc);
json::Document d; json::Document d;
d.Parse(Rc.c_str()); d.Parse(Rc.c_str());
if(Rc == "-1" || d.HasParseError()){ if (Rc == "-1" || d.HasParseError()) {
ClientKick(c,"Invalid key! Please restart your game."); ClientKick(c, "Invalid key! Please restart your game.");
return; return;
} }
if(d["username"].IsString() && d["roles"].IsString() && d["guest"].IsBool()){ if (d["username"].IsString() && d["roles"].IsString() && d["guest"].IsBool()) {
c->SetName(d["username"].GetString()); c->SetName(d["username"].GetString());
c->SetRoles(d["roles"].GetString()); c->SetRoles(d["roles"].GetString());
c->isGuest = d["guest"].GetBool(); c->isGuest = d["guest"].GetBool();
}else{ } else {
ClientKick(c,"Invalid authentication data!"); ClientKick(c, "Invalid authentication data!");
return; return;
} }
@@ -86,7 +85,7 @@ void Authentication(SOCKET TCPSock) {
for (auto& Cl : CI->Clients) { for (auto& Cl : CI->Clients) {
if (Cl != nullptr) { if (Cl != nullptr) {
if (Cl->GetName() == c->GetName() && Cl->isGuest == c->isGuest) { if (Cl->GetName() == c->GetName() && Cl->isGuest == c->isGuest) {
info("Old client (" +Cl->GetName()+ ") kicked: Reconnecting"); info("Old client (" + Cl->GetName() + ") kicked: Reconnecting");
CloseSocketProper(Cl->GetTCPSock()); CloseSocketProper(Cl->GetTCPSock());
Cl->SetStatus(-2); Cl->SetStatus(-2);
break; break;
@@ -94,14 +93,14 @@ void Authentication(SOCKET TCPSock) {
} }
} }
auto arg = std::make_unique<LuaArg>(LuaArg{{c->GetName(),c->GetRoles(),c->isGuest}}); auto arg = std::make_unique<LuaArg>(LuaArg { { c->GetName(), c->GetRoles(), c->isGuest } });
std::any Res = TriggerLuaEvent("onPlayerAuth",false,nullptr, std::move(arg), true); std::any Res = TriggerLuaEvent("onPlayerAuth", false, nullptr, std::move(arg), true);
std::string Type = Res.type().name(); std::string Type = Res.type().name();
if(Type.find("int") != std::string::npos && std::any_cast<int>(Res)){ if (Type.find("int") != std::string::npos && std::any_cast<int>(Res)) {
ClientKick(c,"you are not allowed on the server!"); ClientKick(c, "you are not allowed on the server!");
return; return;
}else if(Type.find("string") != std::string::npos){ } else if (Type.find("string") != std::string::npos) {
ClientKick(c,std::any_cast<std::string>(Res)); ClientKick(c, std::any_cast<std::string>(Res));
return; return;
} }
if (CI->Size() < MaxPlayers) { if (CI->Size() < MaxPlayers) {
@@ -109,34 +108,36 @@ void Authentication(SOCKET TCPSock) {
Client& Client = *c; Client& Client = *c;
CI->AddClient(std::move(c)); CI->AddClient(std::move(c));
TCPClient(&Client); TCPClient(&Client);
} else ClientKick(c,"Server full!"); } else
ClientKick(c, "Server full!");
} }
void HandleDownload(SOCKET TCPSock){ void HandleDownload(SOCKET TCPSock) {
char D; char D;
if(recv(TCPSock,&D,1,0) != 1){ if (recv(TCPSock, &D, 1, 0) != 1) {
CloseSocketProper(TCPSock); CloseSocketProper(TCPSock);
return; return;
} }
auto ID = uint8_t(D); auto ID = uint8_t(D);
for(auto& c : CI->Clients){ for (auto& c : CI->Clients) {
if(c->GetID() == ID){ if (c->GetID() == ID) {
c->SetDownSock(TCPSock); c->SetDownSock(TCPSock);
} }
} }
} }
void Identify(SOCKET TCPSock){ void Identify(SOCKET TCPSock) {
char Code; char Code;
if(recv(TCPSock,&Code,1,0) != 1) { if (recv(TCPSock, &Code, 1, 0) != 1) {
CloseSocketProper(TCPSock); CloseSocketProper(TCPSock);
return; return;
} }
if(Code == 'C'){ if (Code == 'C') {
Authentication(TCPSock); Authentication(TCPSock);
}else if(Code == 'D'){ } else if (Code == 'D') {
HandleDownload(TCPSock); HandleDownload(TCPSock);
}else CloseSocketProper(TCPSock); } else
CloseSocketProper(TCPSock);
} }
void TCPServerMain() { void TCPServerMain() {
+2 -2
View File
@@ -37,10 +37,10 @@ void Client::SetUDPAddr(sockaddr_in Addr) {
UDPADDR = Addr; UDPADDR = Addr;
} }
void Client::SetDownSock(SOCKET CSock){ void Client::SetDownSock(SOCKET CSock) {
SOCK[1] = CSock; SOCK[1] = CSock;
} }
SOCKET Client::GetDownSock(){ SOCKET Client::GetDownSock() {
return SOCK[1]; return SOCK[1];
} }
sockaddr_in Client::GetUDPAddr() { sockaddr_in Client::GetUDPAddr() {
+12 -12
View File
@@ -19,24 +19,24 @@
void Apply(Client* c, int VID, const std::string& pckt) { void Apply(Client* c, int VID, const std::string& pckt) {
Assert(c); Assert(c);
std::string Packet = pckt.substr(pckt.find('{')), VD = c->GetCarData(VID); std::string Packet = pckt.substr(pckt.find('{')), VD = c->GetCarData(VID);
std::string Header = VD.substr(0,VD.find('{')); std::string Header = VD.substr(0, VD.find('{'));
VD = VD.substr(VD.find('{')); VD = VD.substr(VD.find('{'));
rapidjson::Document Veh, Pack; rapidjson::Document Veh, Pack;
Veh.Parse(VD.c_str()); Veh.Parse(VD.c_str());
if(Veh.HasParseError()){ if (Veh.HasParseError()) {
error("Could not get vehicle config!"); error("Could not get vehicle config!");
return; return;
} }
Pack.Parse(Packet.c_str()); Pack.Parse(Packet.c_str());
if(Pack.HasParseError() || Pack.IsNull()){ if (Pack.HasParseError() || Pack.IsNull()) {
error("Could not get active vehicle config!"); error("Could not get active vehicle config!");
return; return;
} }
for(auto& M : Pack.GetObject()){ for (auto& M : Pack.GetObject()) {
if(Veh[M.name].IsNull()){ if (Veh[M.name].IsNull()) {
Veh.AddMember(M.name,M.value,Veh.GetAllocator()); Veh.AddMember(M.name, M.value, Veh.GetAllocator());
}else{ } else {
Veh[M.name] = Pack[M.name]; Veh[M.name] = Pack[M.name];
} }
} }
@@ -88,8 +88,8 @@ void VehicleParser(Client* c, const std::string& Pckt) {
} }
if (PID != -1 && VID != -1 && PID == c->GetID()) { if (PID != -1 && VID != -1 && PID == c->GetID()) {
auto Res = TriggerLuaEvent(("onVehicleEdited"), false, nullptr, auto Res = TriggerLuaEvent(("onVehicleEdited"), false, nullptr,
std::make_unique<LuaArg>(LuaArg { { c->GetID(), VID, Packet.substr(3) } }), std::make_unique<LuaArg>(LuaArg { { c->GetID(), VID, Packet.substr(3) } }),
true); true);
if (!std::any_cast<int>(Res)) { if (!std::any_cast<int>(Res)) {
SendToAll(c, Packet, false, true); SendToAll(c, Packet, false, true);
Apply(c, VID, Packet); Apply(c, VID, Packet);
@@ -151,7 +151,8 @@ void SyncClient(Client* c) {
if (client.get() != c) { if (client.get() != c) {
for (auto& v : client->GetAllCars()) { for (auto& v : client->GetAllCars()) {
if (v != nullptr) { if (v != nullptr) {
if(c->GetStatus() < 0)return; if (c->GetStatus() < 0)
return;
Respond(c, v->Data, true); Respond(c, v->Data, true);
std::this_thread::sleep_for(std::chrono::seconds(2)); std::this_thread::sleep_for(std::chrono::seconds(2));
} }
@@ -238,8 +239,7 @@ void GlobalParser(Client* c, const std::string& Pack) {
#endif #endif
if (Packet.length() < 4 || Packet.find(':', 3) == std::string::npos) if (Packet.length() < 4 || Packet.find(':', 3) == std::string::npos)
break; break;
Res = TriggerLuaEvent("onChatMessage", false, nullptr,std::make_unique<LuaArg>(LuaArg { Res = TriggerLuaEvent("onChatMessage", false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID(), c->GetName(), Packet.substr(Packet.find(':', 3) + 1) } }), true);
{ c->GetID(), c->GetName(), Packet.substr(Packet.find(':', 3) + 1) } }),true);
if (std::any_cast<int>(Res)) if (std::any_cast<int>(Res))
break; break;
SendToAll(nullptr, Packet, true, true); SendToAll(nullptr, Packet, true, true);
+3 -2
View File
@@ -33,7 +33,7 @@ std::string HttpRequest(const std::string& IP, int port) {
} }
std::string PostHTTP(const std::string& IP, const std::string& Fields, bool json) { std::string PostHTTP(const std::string& IP, const std::string& Fields, bool json) {
static auto *header = new curl_slist{(char*)"Content-Type: application/json"}; static auto* header = new curl_slist { (char*)"Content-Type: application/json" };
static std::mutex Lock; static std::mutex Lock;
std::scoped_lock Guard(Lock); std::scoped_lock Guard(Lock);
CURL* curl; CURL* curl;
@@ -43,7 +43,8 @@ std::string PostHTTP(const std::string& IP, const std::string& Fields, bool json
Assert(curl); Assert(curl);
if (curl) { if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, IP.c_str()); curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
if(json)curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); if (json)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Fields.size()); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Fields.size());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Fields.c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Fields.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
+12 -9
View File
@@ -5,12 +5,12 @@
/// ///
/// Created by Anonymous275 on 8/1/2020 /// Created by Anonymous275 on 8/1/2020
/// ///
#include "Lua/LuaSystem.hpp"
#include "Client.hpp" #include "Client.hpp"
#include "UnixCompat.h"
#include "Settings.h"
#include "Network.h"
#include "Logger.h" #include "Logger.h"
#include "Lua/LuaSystem.hpp"
#include "Network.h"
#include "Settings.h"
#include "UnixCompat.h"
#include <memory> #include <memory>
int OpenID() { int OpenID() {
@@ -87,19 +87,22 @@ void OnDisconnect(Client* c, bool kicked) {
SendToAll(c, Packet, false, true); SendToAll(c, Packet, false, true);
Packet.clear(); Packet.clear();
TriggerLuaEvent(("onPlayerDisconnect"), false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID() } }), false); TriggerLuaEvent(("onPlayerDisconnect"), false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID() } }), false);
if(c->GetTCPSock())CloseSocketProper(c->GetTCPSock()); if (c->GetTCPSock())
if(c->GetDownSock())CloseSocketProper(c->GetDownSock()); CloseSocketProper(c->GetTCPSock());
if (c->GetDownSock())
CloseSocketProper(c->GetDownSock());
CI->RemoveClient(c); CI->RemoveClient(c);
} }
void OnConnect(Client* c) { void OnConnect(Client* c) {
Assert(c); Assert(c);
info("Client connected"); info("Client connected");
c->SetID(OpenID()); c->SetID(OpenID());
info("Assigned ID " + std::to_string(c->GetID()) +" to " + c->GetName()); info("Assigned ID " + std::to_string(c->GetID()) + " to " + c->GetName());
TriggerLuaEvent("onPlayerConnecting", false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID() } }), false); TriggerLuaEvent("onPlayerConnecting", false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID() } }), false);
SyncResources(c); SyncResources(c);
if (c->GetStatus() < 0)return; if (c->GetStatus() < 0)
return;
Respond(c, "M" + MapName, true); //Send the Map on connect Respond(c, "M" + MapName, true); //Send the Map on connect
info(c->GetName() +" : Connected"); info(c->GetName() + " : Connected");
TriggerLuaEvent("onPlayerJoining", false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID() } }), false); TriggerLuaEvent("onPlayerJoining", false, nullptr, std::make_unique<LuaArg>(LuaArg { { c->GetID() } }), false);
} }
+45 -37
View File
@@ -6,20 +6,20 @@
/// Created by Anonymous275 on 8/1/2020 /// Created by Anonymous275 on 8/1/2020
/// ///
#include "Security/Enc.h"
#include "UnixCompat.h"
#include "Settings.h"
#include "Client.hpp" #include "Client.hpp"
#include <filesystem>
#include "Network.h"
#include "Logger.h" #include "Logger.h"
#include "Network.h"
#include "Security/Enc.h"
#include "Settings.h"
#include "UnixCompat.h"
#include <filesystem>
#include <fstream> #include <fstream>
bool TCPSendRaw(SOCKET C, char* Data, int32_t Size){ bool TCPSendRaw(SOCKET C, char* Data, int32_t Size) {
int64_t Sent = 0, Temp; int64_t Sent = 0, Temp;
do { do {
Temp = send(C, &Data[Sent], int(Size - Sent), 0); Temp = send(C, &Data[Sent], int(Size - Sent), 0);
if(Temp < 1) { if (Temp < 1) {
info("Socket Closed! " + std::to_string(C)); info("Socket Closed! " + std::to_string(C));
CloseSocketProper(C); CloseSocketProper(C);
return false; return false;
@@ -29,32 +29,37 @@ bool TCPSendRaw(SOCKET C, char* Data, int32_t Size){
return true; return true;
} }
void SplitLoad(Client*c,int64_t Sent,int64_t Size, bool D,const std::string& Name){ void SplitLoad(Client* c, int64_t Sent, int64_t Size, bool D, const std::string& Name) {
std::ifstream f(Name.c_str(), std::ios::binary); std::ifstream f(Name.c_str(), std::ios::binary);
int32_t Split = 0x7735940; //125MB int32_t Split = 0x7735940; //125MB
int64_t Diff;
char* Data; char* Data;
if(Size > Split)Data = new char[Split]; if (Size > Split)
else Data = new char[Size]; Data = new char[Split];
else
Data = new char[Size];
SOCKET TCPSock; SOCKET TCPSock;
if(D)TCPSock = c->GetDownSock(); if (D)
else TCPSock = c->GetTCPSock(); TCPSock = c->GetDownSock();
else
TCPSock = c->GetTCPSock();
info("Split load Socket " + std::to_string(TCPSock)); info("Split load Socket " + std::to_string(TCPSock));
while (c->GetStatus() > -1 && Sent < Size) { while (c->GetStatus() > -1 && Sent < Size) {
Diff = Size - Sent; int64_t Diff = Size - Sent;
if (Diff > Split) { if (Diff > Split) {
f.seekg(Sent, std::ios_base::beg); f.seekg(Sent, std::ios_base::beg);
f.read(Data, Split); f.read(Data, Split);
if(!TCPSendRaw(TCPSock, Data, Split)){ if (!TCPSendRaw(TCPSock, Data, Split)) {
if(c->GetStatus() > -1)c->SetStatus(-1); if (c->GetStatus() > -1)
c->SetStatus(-1);
break; break;
} }
Sent += Split; Sent += Split;
} else { } else {
f.seekg(Sent, std::ios_base::beg); f.seekg(Sent, std::ios_base::beg);
f.read(Data, Diff); f.read(Data, Diff);
if(!TCPSendRaw(TCPSock, Data, int32_t(Diff))){ if (!TCPSendRaw(TCPSock, Data, int32_t(Diff))) {
if(c->GetStatus() > -1)c->SetStatus(-1); if (c->GetStatus() > -1)
c->SetStatus(-1);
break; break;
} }
Sent += Diff; Sent += Diff;
@@ -64,40 +69,40 @@ void SplitLoad(Client*c,int64_t Sent,int64_t Size, bool D,const std::string& Nam
f.close(); f.close();
} }
void SendFile(Client* c, const std::string& Name) {
void SendFile(Client*c, const std::string& Name) {
Assert(c); Assert(c);
info(c->GetName() + " requesting : " + Name.substr(Name.find_last_of('/'))); info(c->GetName() + " requesting : " + Name.substr(Name.find_last_of('/')));
if(!std::filesystem::exists(Name)) { if (!std::filesystem::exists(Name)) {
TCPSend(c, "CO"); TCPSend(c, "CO");
warn("File " + Name + " could not be accessed!"); warn("File " + Name + " could not be accessed!");
return; return;
}else TCPSend(c, "AG"); } else
TCPSend(c, "AG");
///Wait for connections ///Wait for connections
int T = 0; int T = 0;
while(c->GetDownSock() < 1 && T < 50){ while (c->GetDownSock() < 1 && T < 50) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
T++; T++;
} }
if (c->GetDownSock() < 1) {
if(c->GetDownSock() < 1){
error("Client doesn't have a download socket!"); error("Client doesn't have a download socket!");
if(c->GetStatus() > -1)c->SetStatus(-1); if (c->GetStatus() > -1)
c->SetStatus(-1);
return; return;
} }
int64_t Size = std::filesystem::file_size(Name), MSize = Size / 2;
int64_t Size = std::filesystem::file_size(Name), MSize = Size/2; std::thread Dt(SplitLoad, c, 0, MSize, false, Name);
std::thread Dt(SplitLoad,c,0,MSize,false,Name);
Dt.detach(); Dt.detach();
SplitLoad(c,MSize,Size,true,Name); SplitLoad(c, MSize, Size, true, Name);
if(Dt.joinable())Dt.join(); if (Dt.joinable())
Dt.join();
} }
void Parse(Client* c, const std::string& Packet) { void Parse(Client* c, const std::string& Packet) {
@@ -115,7 +120,8 @@ void Parse(Client* c, const std::string& Packet) {
if (SubCode == 'R') { if (SubCode == 'R') {
debug("Sending Mod Info"); debug("Sending Mod Info");
std::string ToSend = FileList + FileSizes; std::string ToSend = FileList + FileSizes;
if (ToSend.empty())ToSend = "-"; if (ToSend.empty())
ToSend = "-";
TCPSend(c, ToSend); TCPSend(c, ToSend);
} }
return; return;
@@ -126,15 +132,17 @@ void Parse(Client* c, const std::string& Packet) {
void SyncResources(Client* c) { void SyncResources(Client* c) {
Assert(c); Assert(c);
if (c == nullptr)return; if (c == nullptr)
return;
#ifndef DEBUG #ifndef DEBUG
try { try {
#endif #endif
TCPSend(c, "P" + std::to_string(c->GetID())); TCPSend(c, "P" + std::to_string(c->GetID()));
std::string Data; std::string Data;
while (c->GetStatus() > -1){ while (c->GetStatus() > -1) {
Data = TCPRcv(c); Data = TCPRcv(c);
if(Data == "Done")break; if (Data == "Done")
break;
Parse(c, Data); Parse(c, Data);
} }
#ifndef DEBUG #ifndef DEBUG
+6 -5
View File
@@ -5,16 +5,17 @@
/// ///
/// Created by Anonymous275 on 8/1/2020 /// Created by Anonymous275 on 8/1/2020
/// ///
#include "Compressor.h"
#include "Logger.h"
#include "Network.h"
#include "Security/Enc.h" #include "Security/Enc.h"
#include "UnixCompat.h" #include "UnixCompat.h"
#include "Compressor.h"
#include "Network.h"
#include "Logger.h"
#include <thread> #include <thread>
bool TCPSend(Client* c, const std::string& Data) { bool TCPSend(Client* c, const std::string& Data) {
Assert(c); Assert(c);
if (c == nullptr)return false; if (c == nullptr)
return false;
int32_t Size, Sent, Temp; int32_t Size, Sent, Temp;
std::string Send(4, 0); std::string Send(4, 0);
@@ -129,7 +130,7 @@ void TCPClient(Client* c) {
return; return;
} }
OnConnect(c); OnConnect(c);
while (c->GetStatus() > -1){ while (c->GetStatus() > -1) {
GParser(c, TCPRcv(c)); GParser(c, TCPRcv(c));
} }
OnDisconnect(c, c->GetStatus() == -2); OnDisconnect(c, c->GetStatus() == -2);
+6 -8
View File
@@ -6,18 +6,18 @@
/// Created by Anonymous275 on 5/8/2020 /// Created by Anonymous275 on 5/8/2020
/// ///
#include "Security/Enc.h"
#include "Compressor.h"
#include "UnixCompat.h"
#include "Client.hpp" #include "Client.hpp"
#include "Settings.h" #include "Compressor.h"
#include "Network.h"
#include "Logger.h" #include "Logger.h"
#include "Network.h"
#include "Security/Enc.h"
#include "Settings.h"
#include "UnixCompat.h"
#include <cmath>
#include <cstring> #include <cstring>
#include <sstream> #include <sstream>
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <cmath>
SOCKET UDPSock; SOCKET UDPSock;
void UDPSend(Client* c, std::string Data) { void UDPSend(Client* c, std::string Data) {
@@ -62,7 +62,6 @@ void UDPSend(Client* c, std::string Data) {
#endif // WIN32 #endif // WIN32
} }
void SendLarge(Client* c, std::string Data) { void SendLarge(Client* c, std::string Data) {
Assert(c); Assert(c);
if (Data.length() > 400) { if (Data.length() > 400) {
@@ -88,7 +87,6 @@ std::string UDPRcvFromClient(sockaddr_in& client) {
return Ret.substr(0, Rcv); return Ret.substr(0, Rcv);
} }
void UDPParser(Client* c, std::string Packet) { void UDPParser(Client* c, std::string Packet) {
if (Packet.find("Zp") != std::string::npos && Packet.size() > 500) { if (Packet.find("Zp") != std::string::npos && Packet.size() > 500) {
abort(); abort();