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