more cleanup

This commit is contained in:
Anonymous275
2020-12-20 14:11:17 +02:00
parent 49dd577c36
commit 4b9742553a
33 changed files with 316 additions and 904 deletions

View File

@@ -1,14 +1,17 @@
// Copyright (c) 2020 Anonymous275.
// BeamMP Launcher code is not in the public domain and is not free software.
// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries.
// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository.
///
/// Created by Anonymous275 on 7/20/2020
///
#include "Network/network.h"
#include "Security/Init.h"
#include "Security/Enc.h"
#include "Curl/http.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include "Startup.h"
#include "Memory.h"
#include "Logger.h"
#include <charconv>
#include <thread>
@@ -19,11 +22,10 @@ std::set<std::string>* ConfList = nullptr;
bool TCPTerminate = false;
int DEFAULT_PORT = 4444;
bool Terminate = false;
bool LoginAuth = false;
std::string UlStatus;
std::string MStatus;
bool once = false;
bool ModLoaded;
bool LoginAuth = false;
int ping = -1;
void StartSync(const std::string &Data){
@@ -43,7 +45,7 @@ void StartSync(const std::string &Data){
ping = -1;
std::thread GS(TCPGameServer,IP,std::stoi(Data.substr(Data.find(':')+1)));
GS.detach();
info(Sec("Connecting to server"));
info("Connecting to server");
}
void Parse(std::string Data,SOCKET CSocket){
char Code = Data.at(0), SubCode = 0;
@@ -118,16 +120,11 @@ void Parse(std::string Data,SOCKET CSocket){
if(!Data.empty() && CSocket != -1){
int res = send(CSocket, (Data+"\n").c_str(), int(Data.size())+1, 0);
if(res < 0){
debug(Sec("(Core) send failed with error: ") + std::to_string(WSAGetLastError()));
debug("(Core) send failed with error: " + std::to_string(WSAGetLastError()));
}
}
}
void GameHandler(SOCKET Client){
if (!once){
std::thread Memory(MemoryInit);
Memory.detach();
once = true;
}
int32_t Size,Temp,Rcv;
char Header[10] = {0};
@@ -144,7 +141,7 @@ void GameHandler(SOCKET Client){
}while(Header[Rcv++] != '>');
if(Temp < 1)break;
if(std::from_chars(Header,&Header[Rcv],Size).ptr[0] != '>'){
debug(Sec("(Core) Invalid lua Header -> ") + std::string(Header,Rcv));
debug("(Core) Invalid lua Header -> " + std::string(Header,Rcv));
break;
}
std::string Ret(Size,0);
@@ -161,16 +158,16 @@ void GameHandler(SOCKET Client){
Respond.detach();
}while(Temp > 0);
if (Temp == 0) {
debug(Sec("(Core) Connection closing"));
debug("(Core) Connection closing");
} else {
debug(Sec("(Core) recv failed with error: ") + std::to_string(WSAGetLastError()));
debug("(Core) recv failed with error: " + std::to_string(WSAGetLastError()));
}
NetReset();
KillSocket(Client);
}
void localRes(){
MStatus = " ";
UlStatus = Sec("Ulstart");
UlStatus = "Ulstart";
if(ConfList != nullptr){
ConfList->clear();
delete ConfList;
@@ -179,13 +176,13 @@ void localRes(){
ConfList = new std::set<std::string>;
}
void CoreMain() {
debug(Sec("Core Network on start!"));
debug("Core Network on start!");
WSADATA wsaData;
SOCKET LSocket,CSocket;
struct addrinfo *res = nullptr;
struct addrinfo hints{};
int iRes = WSAStartup(514, &wsaData); //2.2
if (iRes)debug(Sec("WSAStartup failed with error: ") + std::to_string(iRes));
if (iRes)debug("WSAStartup failed with error: " + std::to_string(iRes));
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
@@ -193,20 +190,20 @@ void CoreMain() {
hints.ai_flags = AI_PASSIVE;
iRes = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT).c_str(), &hints, &res);
if (iRes){
debug(Sec("(Core) addr info failed with error: ") + std::to_string(iRes));
debug("(Core) addr info failed with error: " + std::to_string(iRes));
WSACleanup();
return;
}
LSocket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (LSocket == -1){
debug(Sec("(Core) socket failed with error: ") + std::to_string(WSAGetLastError()));
debug("(Core) socket failed with error: " + std::to_string(WSAGetLastError()));
freeaddrinfo(res);
WSACleanup();
return;
}
iRes = bind(LSocket, res->ai_addr, int(res->ai_addrlen));
if (iRes == SOCKET_ERROR) {
error(Sec("(Core) bind failed with error: ") + std::to_string(WSAGetLastError()));
error("(Core) bind failed with error: " + std::to_string(WSAGetLastError()));
freeaddrinfo(res);
KillSocket(LSocket);
WSACleanup();
@@ -214,7 +211,7 @@ void CoreMain() {
}
iRes = listen(LSocket, SOMAXCONN);
if (iRes == SOCKET_ERROR) {
debug(Sec("(Core) listen failed with error: ") + std::to_string(WSAGetLastError()));
debug("(Core) listen failed with error: " + std::to_string(WSAGetLastError()));
freeaddrinfo(res);
KillSocket(LSocket);
WSACleanup();
@@ -223,13 +220,13 @@ void CoreMain() {
do{
CSocket = accept(LSocket, nullptr, nullptr);
if (CSocket == -1) {
error(Sec("(Core) accept failed with error: ") + std::to_string(WSAGetLastError()));
error("(Core) accept failed with error: " + std::to_string(WSAGetLastError()));
continue;
}
localRes();
info(Sec("Game Connected!"));
info("Game Connected!");
GameHandler(CSocket);
warn(Sec("Game Reconnecting..."));
warn("Game Reconnecting...");
}while(CSocket);
KillSocket(LSocket);
WSACleanup();
@@ -237,7 +234,7 @@ void CoreMain() {
int Handle(EXCEPTION_POINTERS *ep){
char* hex = new char[100];
sprintf_s(hex,100, "%lX", ep->ExceptionRecord->ExceptionCode);
except(Sec("(Core) Code : ") + std::string(hex));
except("(Core) Code : " + std::string(hex));
delete [] hex;
return 1;
}

View File

@@ -1,3 +1,7 @@
// Copyright (c) 2020 Anonymous275.
// BeamMP Launcher code is not in the public domain and is not free software.
// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries.
// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository.
///
/// Created by Anonymous275 on 9/25/2020
///
@@ -5,19 +9,19 @@
#include <string>
#include <winsock.h>
#include "Logger.h"
#include "Security/Enc.h"
std::string GetAddr(const std::string&IP){
if(IP.find_first_not_of("0123456789.") == -1)return IP;
WSADATA wsaData;
hostent *host;
if(WSAStartup(514, &wsaData) != 0){
error(Sec("WSA Startup Failed!"));
error("WSA Startup Failed!");
WSACleanup();
return "";
}
host = gethostbyname(IP.c_str());
if(!host){
error(Sec("DNS lookup failed! on ") + IP);
error("DNS lookup failed! on " + IP);
WSACleanup();
return "DNS";
}

View File

@@ -1,9 +1,13 @@
// Copyright (c) 2020 Anonymous275.
// BeamMP Launcher code is not in the public domain and is not free software.
// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries.
// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository.
///
/// Created by Anonymous275 on 7/25/2020
///
#include "Network/network.h"
#include "Security/Init.h"
#include "Security/Enc.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include "Logger.h"
@@ -20,23 +24,23 @@ SOCKET GSocket = -1;
int KillSocket(uint64_t Dead){
if(Dead == (SOCKET)-1){
debug(Sec("Kill socket got -1 returning..."));
debug("Kill socket got -1 returning...");
return 0;
}
shutdown(Dead,SD_BOTH);
int a = closesocket(Dead);
if(a != 0){
warn(Sec("Failed to close socket!"));
warn("Failed to close socket!");
}
return a;
}
bool CheckBytes(uint32_t Bytes){
if(Bytes == 0){
debug(Sec("(Proxy) Connection closing"));
debug("(Proxy) Connection closing");
return false;
}else if(Bytes < 0){
debug(Sec("(Proxy) send failed with error: ") + std::to_string(WSAGetLastError()));
debug("(Proxy) send failed with error: " + std::to_string(WSAGetLastError()));
return false;
}
return true;
@@ -80,7 +84,7 @@ void ServerSend(std::string Data, bool Rel){
}else UDPSend(Data);
if (DLen > 1000) {
debug(Sec("(Launcher->Server) Bytes sent: ") + std::to_string(Data.length()) + " : "
debug("(Launcher->Server) Bytes sent: " + std::to_string(Data.length()) + " : "
+ Data.substr(0, 10)
+ Data.substr(Data.length() - 10));
}else if(C == 'Z'){
@@ -92,20 +96,20 @@ void NetReset(){
TCPTerminate = false;
GConnected = false;
Terminate = false;
UlStatus = Sec("Ulstart");
UlStatus = "Ulstart";
MStatus = " ";
if(UDPSock != (SOCKET)(-1)){
debug(Sec("Terminating UDP Socket : ") + std::to_string(TCPSock));
debug("Terminating UDP Socket : " + std::to_string(TCPSock));
KillSocket(UDPSock);
}
UDPSock = -1;
if(TCPSock != (SOCKET)(-1)){
debug(Sec("Terminating TCP Socket : ") + std::to_string(TCPSock));
debug("Terminating TCP Socket : " + std::to_string(TCPSock));
KillSocket(TCPSock);
}
TCPSock = -1;
if(GSocket != (SOCKET)(-1)){
debug(Sec("Terminating GTCP Socket : ") + std::to_string(GSocket));
debug("Terminating GTCP Socket : " + std::to_string(GSocket));
KillSocket(GSocket);
}
GSocket = -1;
@@ -118,7 +122,7 @@ SOCKET SetupListener(){
WSADATA wsaData;
int iRes = WSAStartup(514, &wsaData); //2.2
if (iRes != 0) {
error(Sec("(Proxy) WSAStartup failed with error: ") + std::to_string(iRes));
error("(Proxy) WSAStartup failed with error: " + std::to_string(iRes));
return -1;
}
ZeroMemory(&hints, sizeof(hints));
@@ -128,19 +132,19 @@ SOCKET SetupListener(){
hints.ai_flags = AI_PASSIVE;
iRes = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT+1).c_str(), &hints, &result);
if (iRes != 0) {
error(Sec("(Proxy) info failed with error: ") + std::to_string(iRes));
error("(Proxy) info failed with error: " + std::to_string(iRes));
WSACleanup();
}
GSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (GSocket == -1) {
error(Sec("(Proxy) socket failed with error: ") + std::to_string(WSAGetLastError()));
error("(Proxy) socket failed with error: " + std::to_string(WSAGetLastError()));
freeaddrinfo(result);
WSACleanup();
return -1;
}
iRes = bind(GSocket, result->ai_addr, (int) result->ai_addrlen);
if (iRes == SOCKET_ERROR) {
error(Sec("(Proxy) bind failed with error: ") + std::to_string(WSAGetLastError()));
error("(Proxy) bind failed with error: " + std::to_string(WSAGetLastError()));
freeaddrinfo(result);
KillSocket(GSocket);
WSACleanup();
@@ -149,7 +153,7 @@ SOCKET SetupListener(){
freeaddrinfo(result);
iRes = listen(GSocket, SOMAXCONN);
if (iRes == SOCKET_ERROR) {
error(Sec("(Proxy) listen failed with error: ") + std::to_string(WSAGetLastError()));
error("(Proxy) listen failed with error: " + std::to_string(WSAGetLastError()));
KillSocket(GSocket);
WSACleanup();
return -1;
@@ -158,7 +162,7 @@ SOCKET SetupListener(){
}
void AutoPing(){
while(!Terminate){
ServerSend(Sec("p"),false);
ServerSend("p",false);
PingStart = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(std::chrono::seconds (1));
}
@@ -197,10 +201,10 @@ void NetMain(const std::string& IP, int Port){
void TCPGameServer(const std::string& IP, int Port){
GSocket = SetupListener();
while (!TCPTerminate && GSocket != -1){
debug(Sec("MAIN LOOP OF GAME SERVER"));
debug("MAIN LOOP OF GAME SERVER");
GConnected = false;
if(!CServer){
warn(Sec("Connection still alive terminating"));
warn("Connection still alive terminating");
NetReset();
TCPTerminate = true;
Terminate = true;
@@ -212,10 +216,10 @@ void TCPGameServer(const std::string& IP, int Port){
}
CSocket = accept(GSocket, nullptr, nullptr);
if (CSocket == -1) {
debug(Sec("(Proxy) accept failed with error: ") + std::to_string(WSAGetLastError()));
debug("(Proxy) accept failed with error: " + std::to_string(WSAGetLastError()));
break;
}
debug(Sec("(Proxy) Game Connected!"));
debug("(Proxy) Game Connected!");
GConnected = true;
if(CServer){
std::thread t1(NetMain, IP, Port);
@@ -235,7 +239,7 @@ void TCPGameServer(const std::string& IP, int Port){
}while(Header[Rcv++] != '>');
if(Temp < 1 || TCPTerminate)break;
if(std::from_chars(Header,&Header[Rcv],Size).ptr[0] != '>'){
debug(Sec("(Game) Invalid lua Header -> ") + std::string(Header,Rcv));
debug("(Game) Invalid lua Header -> " + std::string(Header,Rcv));
break;
}
std::string Ret(Size,0);
@@ -250,12 +254,12 @@ void TCPGameServer(const std::string& IP, int Port){
ServerSend(Ret,false);
}while(Temp > 0 && !TCPTerminate);
if(Temp == 0)debug(Sec("(Proxy) Connection closing"));
else debug(Sec("(Proxy) recv failed error : ") + std::to_string(WSAGetLastError()));
if(Temp == 0)debug("(Proxy) Connection closing");
else debug("(Proxy) recv failed error : " + std::to_string(WSAGetLastError()));
}
TCPTerminate = true;
GConnected = false;
Terminate = true;
if(CSocket != SOCKET_ERROR)KillSocket(CSocket);
debug(Sec("END OF GAME SERVER"));
debug("END OF GAME SERVER");
}

View File

@@ -1,9 +1,13 @@
// Copyright (c) 2020 Anonymous275.
// BeamMP Launcher code is not in the public domain and is not free software.
// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries.
// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository.
///
/// Created by Anonymous275 on 7/18/2020
///
#include "Security/Game.h"
#include "Security/Enc.h"
#include <curl/curl.h>
#include <iostream>
#include <mutex>
@@ -42,7 +46,7 @@ int progress_bar (void *bar, double t, double d){
nb_bar = 25;
progress_bar_adv = round(d/t*nb_bar);
std::cout<<"\r";
std::cout<<Sec("Progress : [ ");
std::cout<< "Progress : [ ";
if(t!=0)std::cout<<round(d/t*100);else std::cout<<0;
std::cout << "% ] [";
int i;
@@ -60,7 +64,7 @@ struct File {
static size_t my_fwrite(void *buffer,size_t size,size_t nmemb,void *stream){
auto *out = (struct File*)stream;
if(!out->stream) {
fopen_s(&out->stream,out->filename,Sec("wb"));
fopen_s(&out->stream,out->filename,"wb");
if(!out->stream)return -1;
}
return fwrite(buffer, size, nmemb, out->stream);
@@ -84,7 +88,6 @@ int Download(const std::string& URL,const std::string& Path,bool close){
if(res != CURLE_OK)return res;
}
if(file.stream)fclose(file.stream);
if(!close)SecureMods();
std::cout << std::endl;
return -1;
}

View File

@@ -1,10 +1,13 @@
// Copyright (c) 2020 Anonymous275.
// BeamMP Launcher code is not in the public domain and is not free software.
// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries.
// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository.
///
/// Created by Anonymous275 on 4/11/2020
///
#include "Network/network.h"
#include "Security/Init.h"
#include "Security/Enc.h"
#include <WS2tcpip.h>
#include <filesystem>
#include "Startup.h"
@@ -101,8 +104,8 @@ std::string Auth(SOCKET Sock){
}
void UpdateUl(bool D,const std::string&msg){
if(D)UlStatus = "UlDownloading Resource: " + msg;
else UlStatus = "UlLoading Resource: " + msg;
if(D)UlStatus = "UlDownloading Resource " + msg;
else UlStatus = "UlLoading Resource " + msg;
}
void AsyncUpdate(uint64_t& Rcv,uint64_t Size,const std::string& Name){
@@ -244,10 +247,13 @@ void SyncResources(SOCKET Sock){
if (fs::exists(a)) {
if (FS->find_first_not_of("0123456789") != std::string::npos)continue;
if (fs::file_size(a) == std::stoi(*FS)){
UpdateUl(false,"(" + std::to_string(Pos) + "/" + std::to_string(Amount) + "): " + a.substr(a.find_last_of('/')));
UpdateUl(false,std::to_string(Pos) + "/" + std::to_string(Amount) + ": " + a.substr(a.find_last_of('/')));
std::this_thread::sleep_for(std::chrono::milliseconds(50));
try {
fs::copy_file(a, "BeamNG/mods" + a.substr(a.find_last_of('/')),
if(!fs::exists(GetGamePath() + "mods/multiplayer")){
fs::create_directory(GetGamePath() + "mods/multiplayer");
}
fs::copy_file(a, GetGamePath() + "mods/multiplayer" + a.substr(a.find_last_of('/')),
fs::copy_options::overwrite_existing);
} catch (std::exception& e) {
error("Failed copy to the mods folder! " + std::string(e.what()));
@@ -271,12 +277,12 @@ void SyncResources(SOCKET Sock){
break;
}
std::string Name = "(" + std::to_string(Pos) + "/" + std::to_string(Amount) + "): " + FName;
std::string Name = std::to_string(Pos) + "/" + std::to_string(Amount) + ": " + FName;
Data = MultiDownload(Sock,DSock,std::stoull(*FS), Name);
if(Terminate)break;
UpdateUl(false,"("+std::to_string(Pos)+"/"+std::to_string(Amount)+"): "+FName);
UpdateUl(false,std::to_string(Pos)+"/"+std::to_string(Amount)+": "+FName);
std::ofstream LFS;
LFS.open(a.c_str(), std::ios_base::app | std::ios::binary);
if (LFS.is_open()) {
@@ -286,7 +292,12 @@ void SyncResources(SOCKET Sock){
}while(fs::file_size(a) != std::stoi(*FS) && !Terminate);
KillSocket(DSock);
if(!Terminate)fs::copy_file(a,"BeamNG/mods"+FName, fs::copy_options::overwrite_existing);
if(!Terminate){
if(!fs::exists(GetGamePath() + "mods/multiplayer")){
fs::create_directory(GetGamePath() + "mods/multiplayer");
}
fs::copy_file(a,GetGamePath() + "mods/multiplayer" + FName, fs::copy_options::overwrite_existing);
}
WaitForConfirm();
}
if(!Terminate){

View File

@@ -1,9 +1,13 @@
// Copyright (c) 2020 Anonymous275.
// BeamMP Launcher code is not in the public domain and is not free software.
// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries.
// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository.
///
/// Created by Anonymous275 on 5/8/2020
///
#include "Zlib/Compressor.h"
#include "Network/network.h"
#include "Security/Enc.h"
#include <WS2tcpip.h>
#include "Logger.h"
#include <string>
@@ -20,7 +24,7 @@ void UDPSend(std::string Data){
}
std::string Packet = char(ClientID+1) + std::string(":") + Data;
int sendOk = sendto(UDPSock, Packet.c_str(), int(Packet.size()), 0, (sockaddr*)ToServer, sizeof(*ToServer));
if (sendOk == SOCKET_ERROR)error(Sec("Error Code : ") + std::to_string(WSAGetLastError()));
if (sendOk == SOCKET_ERROR)error("Error Code : " + std::to_string(WSAGetLastError()));
}
@@ -51,7 +55,7 @@ void UDPRcv(){
void UDPClientMain(const std::string& IP,int Port){
WSADATA data;
if (WSAStartup(514, &data)){
error(Sec("Can't start Winsock!"));
error("Can't start Winsock!");
return;
}
delete ToServer;
@@ -60,6 +64,7 @@ void UDPClientMain(const std::string& IP,int Port){
ToServer->sin_port = htons(Port);
inet_pton(AF_INET, IP.c_str(), &ToServer->sin_addr);
UDPSock = socket(AF_INET, SOCK_DGRAM, 0);
GameSend("P"+std::to_string(ClientID));
TCPSend("H",TCPSock);
UDPSend("p");
while(!Terminate)UDPRcv();

View File

@@ -1,3 +1,7 @@
// Copyright (c) 2020 Anonymous275.
// BeamMP Launcher code is not in the public domain and is not free software.
// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries.
// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository.
///
/// Created by Anonymous275 on 5/8/2020
///
@@ -8,7 +12,7 @@
#include <iostream>
#include <WS2tcpip.h>
#include <Zlib/Compressor.h>
#include "Security/Enc.h"
#include "Network/network.h"
@@ -119,7 +123,7 @@ void TCPClientMain(const std::string& IP,int Port){
TCPSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(TCPSock == -1){
printf(Sec("Client: socket failed! Error code: %d\n"), WSAGetLastError());
printf("Client: socket failed! Error code: %d\n", WSAGetLastError());
WSACleanup();
return;
}