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,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");
}