4h of work

This commit is contained in:
Anonymous275 2020-04-12 01:14:16 +03:00
parent e60790e185
commit 9a47f651fc
11 changed files with 548 additions and 342 deletions

View File

@ -1,12 +1,13 @@
cmake_minimum_required(VERSION 3.15)
project(BeamMP-Server)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2")
include_directories(${PROJECT_SOURCE_DIR}/curl)
set(CMAKE_CXX_STANDARD 14)
add_executable(BeamMP-Server src/main.cpp src/http.cpp src/logger.cpp src/config.cpp src/Network/Server.cpp
src/Network/enet.h src/Network/DataParser.cpp src/heartbeat.cpp
src/Network/ClientHandler.cpp src/Network/functions.cpp src/Settings.hpp)
cmake_minimum_required(VERSION 3.15)
project(BeamMP-Server)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2")
include_directories(${PROJECT_SOURCE_DIR}/curl)
set(CMAKE_CXX_STANDARD 14)
add_executable(BeamMP-Server src/main.cpp src/http.cpp src/logger.cpp src/config.cpp src/Network/Server.cpp
src/Network/enet.h src/Network/DataParser.cpp src/heartbeat.cpp
src/Network/ClientHandler.cpp src/Network/functions.cpp src/Settings.hpp
src/Resources.cpp src/Network/TCPClientHandler.cpp)
target_link_libraries(BeamMP-Server winmm ws2_32 libcurl_a)

View File

@ -8,6 +8,7 @@
#include <cstdio>
#include "../logger.h"
void ParseData(ENetPacket*packet,ENetPeer*peer); //Data Parser
void OnConnect(ENetPeer*peer);
@ -82,3 +83,64 @@ void ServerMain(int Port, int MaxClients) {
enet_deinitialize();
return;
}
void CreateNewThread(void*);
void TCPMain(int Port){
info("Starting TCP Server on port " + to_string(Port));
WSADATA wsaData;
int iResult;
sockaddr_in addr{};
SOCKET sock,client;
addr.sin_family = AF_INET;
addr.sin_port = htons(Port);
iResult = WSAStartup(MAKEWORD(2,2),&wsaData);
if(iResult)
{
printf("WSA startup failed");
return;
}
sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sock == INVALID_SOCKET)
{
printf("Invalid socket");
return;
}
iResult = bind(sock,(sockaddr*)&addr,sizeof(sockaddr_in ));
if(iResult)
{
printf("bind failed %lu",GetLastError());
return;
}
iResult = listen(sock,SOMAXCONN);
if(iResult)
{
printf("iResult failed %lu",GetLastError());
return;
}
while(client = accept(sock,nullptr,nullptr))
{
if(client == INVALID_SOCKET)
{
printf("invalid client socket\n");
continue;
}
CreateNewThread((void*)&client);
}
}

View File

@ -0,0 +1,118 @@
///
/// Created by Anonymous275 on 4/11/2020
///
#include <thread>
#include <string>
#include <fstream>
#include <iostream>
#include <WinSock2.h>
#include "../Settings.hpp"
int ParseAndSend(SOCKET Client, std::string Data){
std::string Response, Packet;
char ID = Data.at(0);
int Prev = 0,DataSent = 0, Size = 0;
bool FileSent = true;
switch (ID){
case 'a' :
Response = FileList;
break;
case 'b' :
FileSent = false;
break;
}
std::string FLocation = Data.substr(1);
if(FileList.find(FLocation) == std::string::npos)return -1;
do {
if(!FileSent){
std::ifstream f;
f.open(FLocation.c_str(), std::ios::binary);
if(f.good()){
if(!Size){
Size = f.seekg(0, std::ios_base::end).tellg();
Response.resize(Size);
f.seekg(0, std::ios_base::beg);
f.read(&Response[0], Size);
f.close();
}else{f.close();}
if(DataSent != Size){
if((Size-DataSent) < 65535){
Packet = Response.substr(Prev,(Size-DataSent));
DataSent += (Size-DataSent);
Response.clear();
}else{
DataSent += 65535;
Packet = Response.substr(Prev,65535);
}
Prev = DataSent;
}else{
Size = DataSent = Prev = 0;
Response = "End of file";
FileSent = true;
Packet.clear();
}
}else{
FileSent = true;
Response = "Cannot Open File " + FLocation;
}
}
int iSendResult;
if(!Packet.empty())iSendResult = send(Client, Packet.c_str(), Packet.length(), 0);
else iSendResult = send(Client, Response.c_str(), Response.length(), 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(Client);
return -1;
}
}while(!FileSent);
return 0;
}
void Client(void* ClientData){
SOCKET Client = *(SOCKET*)ClientData;
printf("Client connected\n");
int iResult, iSendResult;
char recvbuf[65535];
int recvbuflen = 65535;
do {
iResult = recv(Client, recvbuf, recvbuflen, 0);
if (iResult > 0) {
//printf("Bytes received: %d\n", iResult);
std::string Data = recvbuf,Response;
Data.resize(iResult);
if(ParseAndSend(Client,Data) == -1)break;
// Echo the buffer back to the sender
/*iSendResult = send(Client, Response.c_str(), Response.length(), 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(Client);
return;
}
printf("Bytes sent: %d\n", iSendResult);*/
}
else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(Client);
break;
}
} while (iResult > 0);
std::cout << "Client Closed" << std::endl;
}
void CreateNewThread(void* ClientData){
std::cout << "New Client" << std::endl;
std::thread NewClient(Client,ClientData);
NewClient.detach();
}

22
src/Resources.cpp Normal file
View File

@ -0,0 +1,22 @@
///
/// Created by Anonymous275 on 4/11/2020
///
#include <iostream>
#include <algorithm>
#include <filesystem>
namespace fs = std::experimental::filesystem;
std::string FileList;
void HandleResources(const std::string& path){
struct stat info{};
if(stat( "Resources", &info) != 0){
_wmkdir(L"Resources");
}
for (const auto & entry : fs::directory_iterator(path)){
FileList += entry.path().string() + ";";
}
std::replace(FileList.begin(),FileList.end(),'\\','/');
}

View File

@ -1,13 +1,14 @@
///
/// Created by Anonymous275 on 4/10/2020
///
extern std::string MapName;
extern bool Private;
extern int MaxPlayers;
extern int UDPPort;
extern int TCPPort;
extern int PlayerCount;
extern std::string MapName;
extern std::string ServerName;
extern std::string Resource;
extern std::string ServerVersion;
extern std::string ClientVersion;
extern int PlayerCount;
extern std::string FileList;

View File

@ -1,125 +1,124 @@
///
/// Created by Anonymous275 on 1/28/2020
///
#include <iostream>
#include <fstream>
#include <string>
#include "logger.h"
using namespace std; //nameSpace STD
void GenerateConfig();
string RemoveComments(const string& Line);
string convertToString(char* a, int size);
void SetValues(const string& Line, int Index);
void SetMainValues(bool,int,int,string,string,string);
bool D;
int P;
int MP;
string M;
string S;
string F;
//Generates or Reads Config
void ParseConfig(){
ifstream InFileStream;
InFileStream.open("Server.cfg");
if(InFileStream.good()){ //Checks if Config Exists
info("Config Found Updating Values");
string line;
int index = 1;
while (getline(InFileStream, line)) {
if(line.rfind('#', 0) != 0){ //Checks if it starts as Comment
string CleanLine = RemoveComments(line); //Cleans it from the Comments
SetValues(CleanLine,index); //sets the values
index++;
}
}
SetMainValues(D,P,MP,M,S,F); //gives the values to Main
}else{
info("Config Not Found Generating A new One");
GenerateConfig();
}
InFileStream.close();
}
void SetValues(const string& Line, int Index) {
int i = 0, state = 0;
char Data[50] = "";
bool Switch = false;
if (Index > 3) { Switch = true; }
for (char c : Line) {
if (Switch) {
if (c == '\"') { state++; }
if (state > 0 && state < 2) {
Data[i] = c;
i++;
}
} else {
if (c == ' ') { state++; }
if (state > 1) {
Data[i] = c;
i++;
}
}
}
for (int C = 1; C <= i; C++){
Data[C-1] = Data[C];
}
string::size_type sz;
bool Boolean = (convertToString(Data,i-1).find("true") != string::npos);//searches for "true"
switch (Index){
case 1 :
D = Boolean;//checks and sets the Debug Value
break;
case 2 : P = stoi(Data, &sz);//sets the Port
break;
case 3 : MP = stoi(Data, &sz);//sets the Max Amount of player
break;
case 4 : M = Data; //Map
break;
case 5 : S = Data; //Name
case 6 : F = Data; //File name
}
}
//generates default Config
void GenerateConfig(){
ofstream FileStream;
FileStream.open ("Server.cfg");
FileStream << "# This is the BeamNG-MP Server Configuration File\n"
"Debug = false # true or false to enable debug console output\n"
"Port = 30814 # Port to run the server on\n"
"MaxPlayers = 10 # Maximum Amount of Clients\n"
"Map = \"levels/gridmap/level.json\"\n"
"Name = \"BeamNG-MP FTW\"\n"
"use = \"/Resources\"";
FileStream.close();
}
string RemoveComments(const string& Line){
int i = 0;
char Data[50] = "";
for(char c : Line) {
if(c == '#'){break;} //when it finds the # it will stop
Data[i] = c;
i++;
}
return convertToString(Data,i); //Converts it from a char array to string and returns it
}
//Converts a char array or pointer to string
string convertToString(char* a, int size)
{
int i;
string s;
for (i = 0; i < size; i++) {
s = s + a[i];
}
return s;
///
/// Created by Anonymous275 on 1/28/2020
///
#include <iostream>
#include <fstream>
#include <string>
#include "logger.h"
using namespace std; //nameSpace STD
void GenerateConfig();
string RemoveComments(const string& Line);
string convertToString(char* a, int size);
void SetValues(const string& Line, int Index);
void SetMainValues(bool,int,int,int,string,string,string);
bool D;
int P,FP,MP;
string M,S,F;
//Generates or Reads Config
void ParseConfig(){
ifstream InFileStream;
InFileStream.open("Server.cfg");
if(InFileStream.good()){ //Checks if Config Exists
info("Config Found Updating Values");
string line;
int index = 1;
while (getline(InFileStream, line)) {
if(line.rfind('#', 0) != 0){ //Checks if it starts as Comment
string CleanLine = RemoveComments(line); //Cleans it from the Comments
SetValues(CleanLine,index); //sets the values
index++;
}
}
SetMainValues(D,P,FP,MP,M,S,F); //gives the values to Main
}else{
info("Config Not Found Generating A new One");
GenerateConfig();
}
InFileStream.close();
}
void SetValues(const string& Line, int Index) {
int i = 0, state = 0;
char Data[50] = "";
bool Switch = false;
if (Index > 4) { Switch = true; }
for (char c : Line) {
if (Switch) {
if (c == '\"') { state++; }
if (state > 0 && state < 2) {
Data[i] = c;
i++;
}
} else {
if (c == ' ') { state++; }
if (state > 1) {
Data[i] = c;
i++;
}
}
}
for (int C = 1; C <= i; C++){
Data[C-1] = Data[C];
}
string::size_type sz;
bool Boolean = (convertToString(Data,i-1).find("true") != string::npos);//searches for "true"
switch (Index){
case 1 :
D = Boolean;//checks and sets the Debug Value
break;
case 2 : P = stoi(Data, &sz);//sets the Port
break;
case 3 : FP = stoi(Data, &sz);//sets the TCP File Port
break;
case 4 : MP = stoi(Data, &sz); //sets the Max Amount of player
break;
case 5 : M = Data; //Map
break;
case 6 : S = Data; //Name
case 7 : F = Data; //File name
}
}
//generates default Config
void GenerateConfig(){
ofstream FileStream;
FileStream.open ("Server.cfg");
FileStream << "# This is the BeamMP Server Configuration File\n"
"Debug = false # true or false to enable debug console output\n"
"Port = 30814 # Port to run the server on\n"
"FilePort = 30814 # Port to transfer Files\n"
"MaxPlayers = 10 # Maximum Amount of Clients\n"
"Map = \"/levels/gridmap/info.json\" # Default Map\n"
"Name = \"BeamMP New Server\" # Server Name\n"
"use = \"Resources\" # Resource file name";
FileStream.close();
}
string RemoveComments(const string& Line){
int i = 0;
char Data[50] = "";
for(char c : Line) {
if(c == '#'){break;} //when it finds the # it will stop
Data[i] = c;
i++;
}
return convertToString(Data,i); //Converts it from a char array to string and returns it
}
//Converts a char array or pointer to string
string convertToString(char* a, int size)
{
int i;
string s;
for (i = 0; i < size; i++) {
s = s + a[i];
}
return s;
}

View File

@ -17,13 +17,11 @@ void PostHTTP(const std::string& IP,const std::string& Fields);
void Heartbeat()
{
string UUID = HTTP_REQUEST("https://beamng-mp.com/new-server-startup",443);
std::cout << "UUID GEN : " << UUID << std::endl;
std::string State = Private ? "true" : "false";
while(true)
{
PostHTTP("https://beamng-mp.com/heartbeat","uuid="+UUID+"&players="+to_string(PlayerCount)+"&maxplayers="+to_string(MaxPlayers)+"&port="
+ to_string(UDPPort) + "&map=" + MapName + "&private="+State+"&serverversion="+ServerVersion+"&clientversion="+ClientVersion+"&name="+ServerName);
std::this_thread::sleep_for (std::chrono::seconds(5));
}
}

View File

@ -8,6 +8,7 @@
#include <iostream>
#include <string>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
@ -44,5 +45,4 @@ void PostHTTP(const std::string& IP,const std::string& Fields){
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
std::cout << "Buffer : " << readBuffer << std::endl;
}

View File

@ -1,113 +1,113 @@
///
/// Created by jojos38 on 28/01/2020
///
#include <fstream>
#include "logger.h"
#include <string>
void addToLog(basic_string<char> Data);
using namespace std;
int loggerlevel;
void setLoggerLevel(char level_string[]) {
if (!strcmp(level_string, "ALL"))
loggerlevel = 0;
if (!strcmp(level_string, "DEBUG"))
loggerlevel = 1;
if (!strcmp(level_string, "INFO"))
loggerlevel = 2;
if (!strcmp(level_string, "WARN"))
loggerlevel = 3;
if (!strcmp(level_string, "ERROR"))
loggerlevel = 4;
if (!strcmp(level_string, "OFF"))
loggerlevel = 5;
}
stringstream getDate() {
// current date/time based on current system
time_t now = time(nullptr);
tm* ltm = localtime(&now);
int month = 1 + ltm->tm_mon;
int day = ltm->tm_mday;
int hours = ltm->tm_hour;
int minutes = ltm->tm_min;
int seconds = ltm->tm_sec;
string month_string;
if (month < 10) month_string = "0" + to_string(month);
else month_string = to_string(month);
string day_string;
if (day < 10) day_string = "0" + to_string(day);
else day_string = to_string(day);
string hours_string;
if (hours < 10) hours_string = "0" + to_string(hours);
else hours_string = to_string(hours);
string minutes_string;
if (minutes < 10) minutes_string = "0" + to_string(minutes);
else minutes_string = to_string(minutes);
string seconds_string;
if (seconds < 10) seconds_string = "0" + to_string(seconds);
else seconds_string = to_string(seconds);
std::stringstream date;
date
<< "["
<< day_string << "/"
<< month_string << "/"
<< 1900 + ltm->tm_year << " "
<< hours_string << ":"
<< minutes_string << ":"
<< seconds_string
<< "] ";
return date;
}
void info(const std::string& toPrint) {
if (loggerlevel <= 2){
cout << getDate().str() << "[INFO] " << toPrint << endl;
addToLog(getDate().str() + "[INFO] " + toPrint + "\n");
}
}
void error(const std::string& toPrint) {
if (loggerlevel <= 4) {
cout << getDate().str() << "[ERROR] " << toPrint << endl;
addToLog(getDate().str() + "[ERROR] " + toPrint + "\n");
}
}
void warn(const std::string& toPrint) {
if (loggerlevel <= 3) {
cout << getDate().str() << "[WARN] " << toPrint << endl;
addToLog(getDate().str() + "[WARN] " + toPrint + "\n");
}
}
void debug(const std::string& toPrint) {
if (loggerlevel <= 1) {
cout << getDate().str() << "[DEBUG] " << toPrint << endl;
addToLog(getDate().str() + "[DEBUG] " + toPrint + "\n");
}
///
/// Created by jojos38 on 28/01/2020
///
#include <fstream>
#include "logger.h"
#include <string>
void addToLog(basic_string<char> Data);
using namespace std;
int loggerlevel;
void setLoggerLevel(char level_string[]) {
if (!strcmp(level_string, "ALL"))
loggerlevel = 0;
if (!strcmp(level_string, "DEBUG"))
loggerlevel = 1;
if (!strcmp(level_string, "INFO"))
loggerlevel = 2;
if (!strcmp(level_string, "WARN"))
loggerlevel = 3;
if (!strcmp(level_string, "ERROR"))
loggerlevel = 4;
if (!strcmp(level_string, "OFF"))
loggerlevel = 5;
}
stringstream getDate() {
// current date/time based on current system
time_t now = time(nullptr);
tm* ltm = localtime(&now);
int month = 1 + ltm->tm_mon;
int day = ltm->tm_mday;
int hours = ltm->tm_hour;
int minutes = ltm->tm_min;
int seconds = ltm->tm_sec;
string month_string;
if (month < 10) month_string = "0" + to_string(month);
else month_string = to_string(month);
string day_string;
if (day < 10) day_string = "0" + to_string(day);
else day_string = to_string(day);
string hours_string;
if (hours < 10) hours_string = "0" + to_string(hours);
else hours_string = to_string(hours);
string minutes_string;
if (minutes < 10) minutes_string = "0" + to_string(minutes);
else minutes_string = to_string(minutes);
string seconds_string;
if (seconds < 10) seconds_string = "0" + to_string(seconds);
else seconds_string = to_string(seconds);
std::stringstream date;
date
<< "["
<< day_string << "/"
<< month_string << "/"
<< 1900 + ltm->tm_year << " "
<< hours_string << ":"
<< minutes_string << ":"
<< seconds_string
<< "] ";
return date;
}
void info(const std::string& toPrint) {
if (loggerlevel <= 2){
cout << getDate().str() << "[INFO] " << toPrint << endl;
addToLog(getDate().str() + "[INFO] " + toPrint + "\n");
}
}
void error(const std::string& toPrint) {
if (loggerlevel <= 4) {
cout << getDate().str() << "[ERROR] " << toPrint << endl;
addToLog(getDate().str() + "[ERROR] " + toPrint + "\n");
}
}
void warn(const std::string& toPrint) {
if (loggerlevel <= 3) {
cout << getDate().str() << "[WARN] " << toPrint << endl;
addToLog(getDate().str() + "[WARN] " + toPrint + "\n");
}
}
void debug(const std::string& toPrint) {
if (loggerlevel <= 1) {
cout << getDate().str() << "[DEBUG] " << toPrint << endl;
addToLog(getDate().str() + "[DEBUG] " + toPrint + "\n");
}
}

View File

@ -1,16 +1,16 @@
//
// Created by Anonymous275 on 4/2/2020.
//
#include <iostream>
#include <ctime>
#include <sstream>
#include <string.h>
using namespace std;
extern int loggerlevel;
stringstream getDate();
void setLoggerLevel(char level_string[]);
void info(const std::string& toPrint);
void warn(const std::string& toPrint);
void error(const std::string& toPrint);
void debug(const std::string& toPrint);
//
// Created by Anonymous275 on 4/2/2020.
//
#include <iostream>
#include <ctime>
#include <sstream>
#include <string.h>
using namespace std;
extern int loggerlevel;
stringstream getDate();
void setLoggerLevel(char level_string[]);
void info(const std::string& toPrint);
void warn(const std::string& toPrint);
void error(const std::string& toPrint);
void debug(const std::string& toPrint);

View File

@ -1,74 +1,79 @@
///
/// Created by Anonymous275 on 28/01/2020
///
#include <iostream>
#include <string>
#include <fstream>
#include "logger.h"
#include <chrono>
#include <thread>
using namespace std; //nameSpace STD
void DebugData();
void LogInit();
void ParseConfig();
void ServerMain(int Port, int MaxClients);
bool Debug = false;
void addToLog(basic_string<char> Data);
void HeartbeatInit();
string MapName = "levels/gridmap/level.json";
bool Private = false;
int MaxPlayers = 10;
int UDPPort = 30814;
int TCPPort = 0;
string ServerName = "BeamMP Server";
string Resource = "/Resources";
string ServerVersion = "0.1";
string ClientVersion = "0.21";
//Entry
int main() {
LogInit();
ParseConfig();
HeartbeatInit();
if(Debug){ //checks if debug is on
DebugData(); //Prints Debug Data
}
setLoggerLevel("ALL");
ServerMain(UDPPort, MaxPlayers);
}
void DebugData(){
cout << "Debug : true" << "\n";
cout << "Port : " << UDPPort << "\n";
cout << "MaxPlayers : " << MaxPlayers << "\n";
cout << "MapName : " << MapName << "\n";
cout << "ServerName : " << ServerName << "\n";
cout << "File : " << Resource << "\n";
}
void SetMainValues(bool D, int P,int MP,string Name,string serverName,string filename){
Debug = D;
UDPPort = P;
MapName = Name;
ServerName = serverName;
MaxPlayers = MP;
Resource = filename;
}
void LogInit(){
ofstream LFS;
LFS.open ("Server.log");
LFS.close();
}
void addToLog(basic_string<char> Data){
ofstream LFS;
LFS.open ("Server.log", std::ios_base::app);
LFS << Data.c_str();
LFS.close();
}
///
/// Created by Anonymous275 on 28/01/2020
///
#include <iostream>
#include <string>
#include <fstream>
#include "logger.h"
#include <chrono>
#include <thread>
using namespace std;
void DebugData();
void LogInit();
void ParseConfig();
void ServerMain(int Port, int MaxClients);
bool Debug = false;
void addToLog(basic_string<char> Data);
void HeartbeatInit();
string MapName = "levels/gridmap/level.json";
bool Private = false;
int MaxPlayers = 10;
int UDPPort = 30814;
int TCPPort = 30814;
string ServerName = "BeamMP Server";
string Resource = "Resources";
string ServerVersion = "0.1";
string ClientVersion = "0.21";
void HandleResources(const std::string& path);
void TCPMain(int Port);
//Entry
int main() {
LogInit();
ParseConfig();
HandleResources(Resource);
HeartbeatInit();
if(Debug){ //checks if debug is on
DebugData(); //Prints Debug Data
}
setLoggerLevel("ALL");
std::thread TCPThread(TCPMain,TCPPort);
TCPThread.detach();
ServerMain(UDPPort, MaxPlayers);
}
void DebugData(){
cout << "Debug : true" << endl;
cout << "Port : " << UDPPort << endl;
cout << "TCP Port : " << TCPPort << endl;
cout << "MaxPlayers : " << MaxPlayers << endl;
cout << "MapName : " << MapName << endl;
cout << "ServerName : " << ServerName << endl;
cout << "File : " << Resource << endl;
}
void SetMainValues(bool D, int P, int FP,int MP,string Name,string serverName,string filename){
Debug = D;
UDPPort = P;
TCPPort = FP;
MapName = Name;
ServerName = serverName;
MaxPlayers = MP;
Resource = filename;
}
void LogInit(){
ofstream LFS;
LFS.open ("Server.log");
LFS.close();
}
void addToLog(basic_string<char> Data){
ofstream LFS;
LFS.open ("Server.log", std::ios_base::app);
LFS << Data.c_str();
LFS.close();
}