mirror of
https://github.com/SantaSpeen/BeamMP-Server.git
synced 2025-08-18 10:55:36 +00:00
added 2 lua events fixed chat messages lagging
This commit is contained in:
parent
92cc1cb0fd
commit
303647a8c3
@ -80,7 +80,6 @@ void Client::AddNewCar(int ident,const std::string& Data){
|
||||
std::set<std::pair<int,std::string>> Client::GetAllCars(){
|
||||
return VehicleData;
|
||||
}
|
||||
|
||||
std::string Client::GetCarData(int ident){
|
||||
for(const std::pair<int,std::string>& 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<int,std::string>& 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();
|
||||
}
|
@ -24,6 +24,7 @@ public:
|
||||
bool isDownloading = true;
|
||||
std::set<std::pair<int,std::string>> 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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -3,10 +3,12 @@
|
||||
|
||||
void TCPServerMain();
|
||||
void UDPServerMain();
|
||||
|
||||
void SLoop();
|
||||
std::set<Client*> Clients;
|
||||
void NetMain() {
|
||||
std::thread TCP(TCPServerMain);
|
||||
TCP.detach();
|
||||
std::thread Sec(SLoop);
|
||||
Sec.detach();
|
||||
UDPServerMain();
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <any>
|
||||
|
||||
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;
|
||||
}
|
52
src/Network 2.0/Security.cpp
Normal file
52
src/Network 2.0/Security.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 7/9/2020
|
||||
///
|
||||
#include <random>
|
||||
#include <thread>
|
||||
#include "Client.hpp"
|
||||
#include "../Settings.hpp"
|
||||
#include <windows.h>
|
||||
void VehicleParser(Client*c, std::string Packet);
|
||||
int Rand(){
|
||||
std::random_device r;
|
||||
std::default_random_engine e1(r());
|
||||
std::uniform_int_distribution<int> 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;
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@
|
||||
#include <thread>
|
||||
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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user