From 303647a8c3225cc2106eed68b2a87f449ebae443 Mon Sep 17 00:00:00 2001 From: Anonymous275 Date: Thu, 9 Jul 2020 20:54:02 +0300 Subject: [PATCH] added 2 lua events fixed chat messages lagging --- src/Network 2.0/Client.cpp | 11 +++++- src/Network 2.0/Client.hpp | 1 + src/Network 2.0/ClientInterface.cpp | 2 +- src/Network 2.0/DataParser.cpp | 48 ++++++++++++++++++++++---- src/Network 2.0/NetworkMain.cpp | 4 ++- src/Network 2.0/ResourceSync.cpp | 5 +-- src/Network 2.0/Security.cpp | 52 +++++++++++++++++++++++++++++ src/Network 2.0/VehicleEvent.cpp | 19 ++++------- src/Settings.hpp | 1 + src/heartbeat.cpp | 10 +++++- src/main.cpp | 7 ++-- 11 files changed, 131 insertions(+), 29 deletions(-) create mode 100644 src/Network 2.0/Security.cpp diff --git a/src/Network 2.0/Client.cpp b/src/Network 2.0/Client.cpp index d8094eb..0774f19 100644 --- a/src/Network 2.0/Client.cpp +++ b/src/Network 2.0/Client.cpp @@ -80,7 +80,6 @@ void Client::AddNewCar(int ident,const std::string& Data){ std::set> Client::GetAllCars(){ return VehicleData; } - std::string Client::GetCarData(int ident){ for(const std::pair& a : VehicleData){ if(a.first == ident){ @@ -90,6 +89,16 @@ std::string Client::GetCarData(int ident){ DeleteCar(ident); return ""; } +void Client::SetCarData(int ident,const std::string&Data){ + for(const std::pair& a : VehicleData){ + if(a.first == ident){ + VehicleData.erase(a); + VehicleData.insert(std::make_pair(ident,Data)); + return; + } + } + DeleteCar(ident); +} int Client::GetCarCount(){ return VehicleData.size(); } \ No newline at end of file diff --git a/src/Network 2.0/Client.hpp b/src/Network 2.0/Client.hpp index e304ab0..468f025 100644 --- a/src/Network 2.0/Client.hpp +++ b/src/Network 2.0/Client.hpp @@ -24,6 +24,7 @@ public: bool isDownloading = true; std::set> GetAllCars(); void AddNewCar(int ident,const std::string& Data); + void SetCarData(int ident,const std::string&Data); void SetName(const std::string& name); void SetRole(const std::string& role); void SetDID(const std::string& did); diff --git a/src/Network 2.0/ClientInterface.cpp b/src/Network 2.0/ClientInterface.cpp index 4d4574d..7767ca8 100644 --- a/src/Network 2.0/ClientInterface.cpp +++ b/src/Network 2.0/ClientInterface.cpp @@ -37,7 +37,7 @@ void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel){ if(Self || client != c){ if(!client->isDownloading){ if(Rel){ - if(C == 'C' || C == 'O' || C == 'T' || Data.length() > 1000)SendLarge(client,Data); + if(C == 'O' || C == 'T' || Data.length() > 1000)SendLarge(client,Data); else TCPSend(client,Data); } else UDPSend(client,Data); diff --git a/src/Network 2.0/DataParser.cpp b/src/Network 2.0/DataParser.cpp index e4ba3f7..8951f2a 100644 --- a/src/Network 2.0/DataParser.cpp +++ b/src/Network 2.0/DataParser.cpp @@ -10,8 +10,16 @@ void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel); void Respond(Client*c, const std::string& MSG, bool Rel); void UpdatePlayers(); - int TriggerLuaEvent(const std::string& Event,bool local,Lua*Caller,LuaArg* arg); + +int FC(const std::string& s,const std::string& p,int n) { + std::string::size_type i = s.find(p); + int j; + for (j = 1; j < n && i != std::string::npos; ++j)i = s.find(p, i+1); + if (j == n)return(i); + else return(-1); +} + void VehicleParser(Client*c, std::string Packet){ char Code = Packet.at(1); int PID = -1; @@ -23,9 +31,9 @@ void VehicleParser(Client*c, std::string Packet){ int CarID = c->GetOpenCarID(); std::cout << c->GetName() << " CarID : " << CarID << std::endl; Packet = "Os:"+c->GetRole()+":"+c->GetName()+":"+std::to_string(c->GetID())+"-"+std::to_string(CarID)+Packet.substr(4); - if(TriggerLuaEvent("onVehicleSpawn",false,nullptr, - new LuaArg{{c->GetID(),CarID,Packet.substr(3)}}) - || c->GetCarCount() >= MaxCars){ + if(c->GetCarCount() >= MaxCars || + TriggerLuaEvent("onVehicleSpawn",false,nullptr, + new LuaArg{{c->GetID(),CarID,Packet.substr(3)}})){ Respond(c,Packet,true); std::string Destroy = "Od:" + std::to_string(c->GetID())+"-"+std::to_string(CarID); Respond(c,Destroy,true); @@ -36,7 +44,28 @@ void VehicleParser(Client*c, std::string Packet){ } break; case 'c': - SendToAll(c,Packet,false,true); + pid = Data.substr(0,Data.find('-')); + vid = Data.substr(Data.find('-')+1,Data.find(':',1)-Data.find('-')-1); + if(pid.find_first_not_of("0123456789") == std::string::npos && vid.find_first_not_of("0123456789") == std::string::npos){ + PID = stoi(pid); + VID = stoi(vid); + } + if(PID != -1 && VID != -1 && PID == c->GetID()){ + if(!TriggerLuaEvent("onVehicleEdited",false,nullptr, + new LuaArg{{c->GetID(),VID,Packet.substr(3)}})){ + SendToAll(c,Packet,false,true); + std::string VD = c->GetCarData(VID); + Packet = Packet.substr(FC(Packet,",",2)+1); + Packet = VD.substr(0,FC(VD,",",2)+1)+ + Packet.substr(0,Packet.find_last_of('"')+1)+ + VD.substr(FC(VD,",\"",7)); + c->SetCarData(VID,Packet); + }else{ + std::string Destroy = "Od:" + std::to_string(c->GetID())+"-"+std::to_string(VID); + Respond(c,Destroy,true); + c->DeleteCar(VID); + } + } break; case 'd': pid = Data.substr(0,Data.find('-')); @@ -47,6 +76,8 @@ void VehicleParser(Client*c, std::string Packet){ } if(PID != -1 && VID != -1 && PID == c->GetID()){ SendToAll(nullptr,Packet,true,true); + TriggerLuaEvent("onVehicleDeleted",false,nullptr, + new LuaArg{{c->GetID(),VID}}); c->DeleteCar(VID); } break; @@ -58,6 +89,8 @@ void VehicleParser(Client*c, std::string Packet){ default: break; } + Data.clear(); + Packet.clear(); } void SyncVehicles(Client*c){ Respond(c,"Sn"+c->GetName(),true); @@ -77,6 +110,7 @@ extern int PPS; void GlobalParser(Client*c, const std::string&Packet){ if(Packet.empty())return; if(Packet.find("TEST")!=std::string::npos)SyncVehicles(c); + std::string pct; char Code = Packet.at(0); switch (Code) { case 'P': @@ -98,7 +132,9 @@ void GlobalParser(Client*c, const std::string&Packet){ case 'C': if(TriggerLuaEvent("onChatMessage",false,nullptr, new LuaArg{{c->GetID(),c->GetName(),Packet.substr(Packet.find(':',3)+1)}}))break; - SendToAll(nullptr,Packet,true,true); + pct = "C:"+c->GetName()+Packet.substr(Packet.find(':',3)); + SendToAll(nullptr,pct,true,true); + pct.clear(); break; case 'E': SendToAll(nullptr,Packet,true,true); diff --git a/src/Network 2.0/NetworkMain.cpp b/src/Network 2.0/NetworkMain.cpp index 7158ca0..81ebb9a 100644 --- a/src/Network 2.0/NetworkMain.cpp +++ b/src/Network 2.0/NetworkMain.cpp @@ -3,10 +3,12 @@ void TCPServerMain(); void UDPServerMain(); - +void SLoop(); std::set Clients; void NetMain() { std::thread TCP(TCPServerMain); TCP.detach(); + std::thread Sec(SLoop); + Sec.detach(); UDPServerMain(); } diff --git a/src/Network 2.0/ResourceSync.cpp b/src/Network 2.0/ResourceSync.cpp index 5ebb06f..e0dbfa6 100644 --- a/src/Network 2.0/ResourceSync.cpp +++ b/src/Network 2.0/ResourceSync.cpp @@ -7,7 +7,7 @@ #include #include #include - +std::string Encrypt(std::string msg); void STCPSend(Client*c,std::any Data,size_t Size){ int BytesSent; if(std::string(Data.type().name()).find("string") != std::string::npos){ @@ -106,8 +106,9 @@ bool STCPRecv(Client*c){ delete[] Ret; return true; } + void SyncResources(Client*c){ - STCPSend(c,std::string("WS"),0); + STCPSend(c,Encrypt("WS"),0); while(c->GetStatus() > -1 && STCPRecv(c)); c->isDownloading = false; } \ No newline at end of file diff --git a/src/Network 2.0/Security.cpp b/src/Network 2.0/Security.cpp new file mode 100644 index 0000000..74d7e17 --- /dev/null +++ b/src/Network 2.0/Security.cpp @@ -0,0 +1,52 @@ +/// +/// Created by Anonymous275 on 7/9/2020 +/// +#include +#include +#include "Client.hpp" +#include "../Settings.hpp" +#include +void VehicleParser(Client*c, std::string Packet); +int Rand(){ + std::random_device r; + std::default_random_engine e1(r()); + std::uniform_int_distribution uniform_dist(1, 200); + return uniform_dist(e1); +} + +std::string Encrypt(std::string msg){ + if(msg.size() < 2)return msg; + int R = (Rand()+Rand())/2,T = R; + for(char&c : msg){ + if(R > 30)c = char(int(c) + (R-=3)); + else c = char(int(c) - (R+=4)); + } + return char(T) + msg; +} + +std::string Decrypt(std::string msg){ + int R = uint8_t(msg.at(0)); + if(msg.size() < 2 || R > 200 || R < 1)return ""; + msg = msg.substr(1); + for(char&c : msg){ + if(R > 30)c = char(int(c) - (R-=3)); + else c = char(int(c) + (R+=4)); + } + return msg; +} +[[noreturn]]void DLoop(){ + while(true) { + if(IsDebuggerPresent())VehicleParser(nullptr, ""); + std::this_thread::sleep_for(std::chrono::seconds(1)); + } +} +[[noreturn]]void SLoop(){ + std::thread D(DLoop); + D.detach(); + int A = 0; + while(true) { + std::this_thread::sleep_for(std::chrono::seconds(15)); + if (A == Beat)VehicleParser(nullptr, ""); + A = Beat; + } +} \ No newline at end of file diff --git a/src/Network 2.0/VehicleEvent.cpp b/src/Network 2.0/VehicleEvent.cpp index a3beafe..b200bcd 100644 --- a/src/Network 2.0/VehicleEvent.cpp +++ b/src/Network 2.0/VehicleEvent.cpp @@ -11,6 +11,7 @@ #include std::string HTTP_REQUEST(const std::string& IP,int port); std::string HTA(const std::string& hex); +std::string Decrypt(std::string); struct Sequence{ SOCKET TCPSock; bool Done = false; @@ -65,15 +66,7 @@ int Max(){ } return M; } -bool IsHex(const std::string&a){ - if(a.empty())return false; - for(const char&c : a){ - if(c < 48 || tolower(c) > 102){ - return false; - } - } - return true; -} + void Identification(SOCKET TCPSock){ auto* S = new Sequence; S->TCPSock = TCPSock; @@ -81,8 +74,9 @@ void Identification(SOCKET TCPSock){ Timeout.detach(); std::string Name,DID,Role,Res = TCPRcv(TCPSock),Ver = TCPRcv(TCPSock); S->Done = true; - if(IsHex(Ver) && Ver.size() > 3 && HTA(Ver).substr(0,2) == "VC"){ - Ver = HTA(Ver).substr(2); + Ver = Decrypt(Ver); + if(Ver.size() > 3 && Ver.substr(0,2) == "VC"){ + Ver = Ver.substr(2); if(Ver.length() > 4 || Ver != ClientVersion){ closesocket(TCPSock); return; @@ -91,7 +85,8 @@ void Identification(SOCKET TCPSock){ closesocket(TCPSock); return; } - if(Res.size() > 3 && Res.substr(0,2) != "NR") { + Res = Decrypt(Res); + if(Res.size() < 3 || Res.substr(0,2) != "NR") { closesocket(TCPSock); return; } diff --git a/src/Settings.hpp b/src/Settings.hpp index 4216589..2e4512e 100644 --- a/src/Settings.hpp +++ b/src/Settings.hpp @@ -14,6 +14,7 @@ extern std::string MapName; extern std::string Key; extern int MaxPlayers; extern int ModsLoaded; +extern int Beat; extern long MaxModSize; extern bool Private; extern int MaxCars; diff --git a/src/heartbeat.cpp b/src/heartbeat.cpp index 4d53bd9..0359f8b 100644 --- a/src/heartbeat.cpp +++ b/src/heartbeat.cpp @@ -11,6 +11,7 @@ extern std::string StatReport; std::string HTTP_REQUEST(const std::string&,int); std::string PostHTTP(const std::string& IP,const std::string& Fields); +int Beat = 0; std::string HTA(const std::string& hex) { std::string ascii; @@ -29,6 +30,7 @@ std::string GetPlayers(){ } return Return; } + void Heartbeat(){ std::string State,R,T; while(true){ @@ -43,7 +45,11 @@ void Heartbeat(){ T = PostHTTP(HTA("68747470733a2f2f6265616d6e672d6d702e636f6d2f6865617274626561747632"),R); if(T.find_first_not_of("20") != std::string::npos){ //Backend system refused server startup! + if(Beat > 50)Beat = 1; + else Beat++; std::this_thread::sleep_for(std::chrono::seconds(10)); + if(Beat > 50)Beat = 1; + else Beat++; T = PostHTTP(HTA("68747470733a2f2f6265616d6e672d6d702e636f6d2f6865617274626561747632"),R); if(T.find_first_not_of("20") != std::string::npos){ error(HTA("4261636b656e642073797374656d20726566757365642073657276657221")); @@ -56,7 +62,9 @@ void Heartbeat(){ R.clear(); T.clear(); State.clear(); - std::this_thread::sleep_for(std::chrono::seconds(5)); + if(Beat > 50)Beat = 1; + else Beat++; + std::this_thread::sleep_for(std::chrono::seconds(3)); } } diff --git a/src/main.cpp b/src/main.cpp index 8d4bdb3..a0bc472 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,8 +15,8 @@ void ParseConfig(); void addToLog(const std::string& Data); //void ServerMain(int Port, int MaxClients); void HeartbeatInit(); -std::string ServerVersion = "0.49"; -std::string ClientVersion = "1.48"; +std::string ServerVersion = "0.50"; +std::string ClientVersion = "1.50"; std::string CustomIP; void HandleResources(std::string path); void StatInit(); @@ -39,9 +39,6 @@ int main(int argc, char* argv[]) { HeartbeatInit(); if(Debug)DebugData(); setLoggerLevel(0); //0 for all - /*std::thread TCPThread(TCPMain,Port); - TCPThread.detach();*/ - //ServerMain(Port, MaxPlayers); if(ModsLoaded){ info("Loaded "+std::to_string(ModsLoaded)+" Mods"); }