mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2025-07-03 08:26:01 +00:00
Discord integration,fixed game not launching with the mod, faster connection
This commit is contained in:
parent
20dccf988a
commit
0e44138f51
@ -11,10 +11,9 @@ void Print(const std::string&MSG){
|
|||||||
//std::cout << MSG << std::endl;
|
//std::cout << MSG << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string Compress(const std::string&Data){
|
std::string Compress(const std::string&Data){
|
||||||
std::string b;
|
std::string b;
|
||||||
b.resize(Data.size());
|
b.resize(Data.length());
|
||||||
z_stream defstream;
|
z_stream defstream;
|
||||||
defstream.zalloc = Z_NULL;
|
defstream.zalloc = Z_NULL;
|
||||||
defstream.zfree = Z_NULL;
|
defstream.zfree = Z_NULL;
|
||||||
@ -52,7 +51,7 @@ std::string Decompress(const std::string&Data)
|
|||||||
inflateEnd(&infstream);
|
inflateEnd(&infstream);
|
||||||
for(int i = int(c.length())-1;i >= 0;i--){
|
for(int i = int(c.length())-1;i >= 0;i--){
|
||||||
if(c.at(i) != '\0'){
|
if(c.at(i) != '\0'){
|
||||||
c.resize(i+2);
|
c.resize(i+1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,9 +19,10 @@ extern std::string MStatus;
|
|||||||
extern int ping;
|
extern int ping;
|
||||||
extern bool Terminate;
|
extern bool Terminate;
|
||||||
extern bool TCPTerminate;
|
extern bool TCPTerminate;
|
||||||
|
extern bool MPDEV;
|
||||||
void StartSync(const std::string &Data){
|
void StartSync(const std::string &Data){
|
||||||
std::thread t1(SyncResources,Data.substr(1,Data.find(':')-1),std::stoi(Data.substr(Data.find(':')+1)));
|
//std::thread t1(SyncResources,Data.substr(1,Data.find(':')-1),std::stoi(Data.substr(Data.find(':')+1)));
|
||||||
//std::thread t1(SyncResources,"127.0.0.1",30814);
|
std::thread t1(SyncResources,"127.0.0.1",30814);
|
||||||
t1.detach();
|
t1.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,8 +59,9 @@ std::string Parse(const std::string& Data){
|
|||||||
|
|
||||||
|
|
||||||
[[noreturn]] void CoreNetworkThread(){
|
[[noreturn]] void CoreNetworkThread(){
|
||||||
|
std::cout << "Ready!" << std::endl;
|
||||||
do{
|
do{
|
||||||
std::cout << "Core Network on start!" << std::endl;
|
if(MPDEV)std::cout << "Core Network on start!" << std::endl;
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
int iResult;
|
int iResult;
|
||||||
SOCKET ListenSocket = INVALID_SOCKET;
|
SOCKET ListenSocket = INVALID_SOCKET;
|
||||||
@ -75,7 +77,7 @@ std::string Parse(const std::string& Data){
|
|||||||
// Initialize Winsock
|
// Initialize Winsock
|
||||||
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
|
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
|
||||||
if (iResult != 0) {
|
if (iResult != 0) {
|
||||||
std::cout <<"WSAStartup failed with error: " << iResult << std::endl;
|
if(MPDEV)std::cout <<"WSAStartup failed with error: " << iResult << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZeroMemory(&hints, sizeof(hints));
|
ZeroMemory(&hints, sizeof(hints));
|
||||||
@ -87,14 +89,14 @@ std::string Parse(const std::string& Data){
|
|||||||
// Resolve the server address and port
|
// Resolve the server address and port
|
||||||
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
|
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
|
||||||
if ( iResult != 0 ) {
|
if ( iResult != 0 ) {
|
||||||
std::cout << "(Core) getaddrinfo failed with error: " << iResult << std::endl;
|
if(MPDEV)std::cout << "(Core) getaddrinfo failed with error: " << iResult << std::endl;
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a socket for connecting to server
|
// Create a socket for connecting to server
|
||||||
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
||||||
if (ListenSocket == INVALID_SOCKET) {
|
if (ListenSocket == INVALID_SOCKET) {
|
||||||
std::cout << "(Core) socket failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Core) socket failed with error: " << WSAGetLastError() << std::endl;
|
||||||
freeaddrinfo(result);
|
freeaddrinfo(result);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
}
|
}
|
||||||
@ -102,7 +104,7 @@ std::string Parse(const std::string& Data){
|
|||||||
// Setup the TCP listening socket
|
// Setup the TCP listening socket
|
||||||
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
|
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
|
||||||
if (iResult == SOCKET_ERROR) {
|
if (iResult == SOCKET_ERROR) {
|
||||||
std::cout << "(Core) bind failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Core) bind failed with error: " << WSAGetLastError() << std::endl;
|
||||||
freeaddrinfo(result);
|
freeaddrinfo(result);
|
||||||
closesocket(ListenSocket);
|
closesocket(ListenSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
@ -112,13 +114,13 @@ std::string Parse(const std::string& Data){
|
|||||||
|
|
||||||
iResult = listen(ListenSocket, SOMAXCONN);
|
iResult = listen(ListenSocket, SOMAXCONN);
|
||||||
if (iResult == SOCKET_ERROR) {
|
if (iResult == SOCKET_ERROR) {
|
||||||
std::cout << "(Core) listen failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Core) listen failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ListenSocket);
|
closesocket(ListenSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
}
|
}
|
||||||
ClientSocket = accept(ListenSocket, nullptr, nullptr);
|
ClientSocket = accept(ListenSocket, nullptr, nullptr);
|
||||||
if (ClientSocket == INVALID_SOCKET) {
|
if (ClientSocket == INVALID_SOCKET) {
|
||||||
std::cout << "(Core) accept failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Core) accept failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ListenSocket);
|
closesocket(ListenSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
}
|
}
|
||||||
@ -135,16 +137,16 @@ std::string Parse(const std::string& Data){
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if (iResult == 0)
|
else if (iResult == 0)
|
||||||
std::cout << "(Core) Connection closing...\n";
|
if(MPDEV)std::cout << "(Core) Connection closing...\n";
|
||||||
else {
|
else {
|
||||||
std::cout << "(Core) recv failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Core) recv failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ClientSocket);
|
closesocket(ClientSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
}
|
}
|
||||||
if(!Response.empty()){
|
if(!Response.empty()){
|
||||||
iSendResult = send( ClientSocket, Response.c_str(), Response.length(), 0);
|
iSendResult = send( ClientSocket, Response.c_str(), Response.length(), 0);
|
||||||
if (iSendResult == SOCKET_ERROR) {
|
if (iSendResult == SOCKET_ERROR) {
|
||||||
std::cout << "send failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "send failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ClientSocket);
|
closesocket(ClientSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
}else{
|
}else{
|
||||||
@ -155,7 +157,7 @@ std::string Parse(const std::string& Data){
|
|||||||
|
|
||||||
iResult = shutdown(ClientSocket, SD_SEND);
|
iResult = shutdown(ClientSocket, SD_SEND);
|
||||||
if (iResult == SOCKET_ERROR) {
|
if (iResult == SOCKET_ERROR) {
|
||||||
std::cout << "(Core) shutdown failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Core) shutdown failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ClientSocket);
|
closesocket(ClientSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
Sleep(500);
|
Sleep(500);
|
||||||
|
59
Discord.cpp
59
Discord.cpp
@ -11,33 +11,37 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
extern bool MPDEV;
|
||||||
static const char* APPLICATION_ID = "345229890980937739";
|
static const char* APPLICATION_ID = "629743237988352010";
|
||||||
static int FrustrationLevel = 0;
|
static int FrustrationLevel = 0;
|
||||||
static int64_t StartTime;
|
static int64_t StartTime;
|
||||||
static int SendPresence = 1;
|
static int SendPresence = 1;
|
||||||
static std::vector<std::string> LocalInfo;
|
static std::vector<std::string> LocalInfo;
|
||||||
|
|
||||||
|
std::vector<std::string> GetDiscordInfo(){
|
||||||
|
return LocalInfo;
|
||||||
|
}
|
||||||
|
|
||||||
static void updateDiscordPresence()
|
static void updateDiscordPresence()
|
||||||
{
|
{
|
||||||
if (SendPresence) {
|
if (SendPresence) {
|
||||||
char buffer[256];
|
char buffer[256];
|
||||||
DiscordRichPresence discordPresence;
|
DiscordRichPresence discordPresence;
|
||||||
memset(&discordPresence, 0, sizeof(discordPresence));
|
memset(&discordPresence, 0, sizeof(discordPresence));
|
||||||
discordPresence.state = "West of House";
|
discordPresence.state = "Playing with friends!";
|
||||||
sprintf(buffer, "Frustration level: %d", FrustrationLevel);
|
//sprintf(buffer, "Frustration level: %d", FrustrationLevel);
|
||||||
discordPresence.details = buffer;
|
//discordPresence.details = buffer;
|
||||||
discordPresence.startTimestamp = StartTime;
|
discordPresence.startTimestamp = StartTime;
|
||||||
discordPresence.endTimestamp = time(0) + 5 * 60;
|
//discordPresence.endTimestamp = time(0) + 5 * 60;
|
||||||
discordPresence.largeImageKey = "canary-large";
|
discordPresence.largeImageKey = "mainlogo";
|
||||||
discordPresence.smallImageKey = "ptb-small";
|
//discordPresence.smallImageKey = "logo";
|
||||||
discordPresence.partyId = "party1234";
|
//discordPresence.partyId = "party1234";
|
||||||
discordPresence.partySize = 1;
|
//discordPresence.partySize = 1;
|
||||||
discordPresence.partyMax = 6;
|
//discordPresence.partyMax = 6;
|
||||||
discordPresence.matchSecret = "xyzzy";
|
//discordPresence.matchSecret = "xyzzy";
|
||||||
discordPresence.joinSecret = "join";
|
//discordPresence.joinSecret = "join";
|
||||||
discordPresence.spectateSecret = "look";
|
//discordPresence.spectateSecret = "look";
|
||||||
discordPresence.instance = 0;
|
//discordPresence.instance = 0;
|
||||||
Discord_UpdatePresence(&discordPresence);
|
Discord_UpdatePresence(&discordPresence);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -58,22 +62,22 @@ static void handleDiscordReady(const DiscordUser* connectedUser)
|
|||||||
|
|
||||||
static void handleDiscordDisconnected(int errcode, const char* message)
|
static void handleDiscordDisconnected(int errcode, const char* message)
|
||||||
{
|
{
|
||||||
printf("\nDiscord: disconnected (%d: %s)\n", errcode, message);
|
if(MPDEV)printf("\nDiscord: disconnected (%d: %s)\n", errcode, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handleDiscordError(int errcode, const char* message)
|
static void handleDiscordError(int errcode, const char* message)
|
||||||
{
|
{
|
||||||
printf("\nDiscord: error (%d: %s)\n", errcode, message);
|
if(MPDEV)printf("\nDiscord: error (%d: %s)\n", errcode, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handleDiscordJoin(const char* secret)
|
static void handleDiscordJoin(const char* secret)
|
||||||
{
|
{
|
||||||
printf("\nDiscord: join (%s)\n", secret);
|
if(MPDEV)printf("\nDiscord: join (%s)\n", secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handleDiscordSpectate(const char* secret)
|
static void handleDiscordSpectate(const char* secret)
|
||||||
{
|
{
|
||||||
printf("\nDiscord: spectate (%s)\n", secret);
|
if(MPDEV)printf("\nDiscord: spectate (%s)\n", secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handleDiscordJoinRequest(const DiscordUser* request)
|
static void handleDiscordJoinRequest(const DiscordUser* request)
|
||||||
@ -122,29 +126,30 @@ static void discordInit()
|
|||||||
Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL);
|
Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::vector<std::string> Loop()
|
static void Loop()
|
||||||
{
|
{
|
||||||
char line[512];
|
char line[512];
|
||||||
char* space;
|
char* space;
|
||||||
|
|
||||||
StartTime = time(0);
|
StartTime = time(0);
|
||||||
|
|
||||||
while (LocalInfo.size() < 3) {
|
while (true) {
|
||||||
//updateDiscordPresence();
|
updateDiscordPresence();
|
||||||
|
|
||||||
#ifdef DISCORD_DISABLE_IO_THREAD
|
#ifdef DISCORD_DISABLE_IO_THREAD
|
||||||
Discord_UpdateConnection();
|
Discord_UpdateConnection();
|
||||||
#endif
|
#endif
|
||||||
Discord_RunCallbacks();
|
Discord_RunCallbacks();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
if(LocalInfo.empty()){
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||||
|
}else std::this_thread::sleep_for(std::chrono::milliseconds(2000));
|
||||||
}
|
}
|
||||||
return LocalInfo;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> Discord_Main()
|
void Discord_Main()
|
||||||
{
|
{
|
||||||
discordInit();
|
discordInit();
|
||||||
return Loop();
|
Loop();
|
||||||
//Discord_Shutdown();
|
Discord_Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,8 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
namespace fs = std::experimental::filesystem;
|
namespace fs = std::experimental::filesystem;
|
||||||
|
void Exit(const std::string& Msg);
|
||||||
|
extern bool MPDEV;
|
||||||
std::string UlStatus = "Ulstart";
|
std::string UlStatus = "Ulstart";
|
||||||
std::string MStatus = " ";
|
std::string MStatus = " ";
|
||||||
|
|
||||||
@ -30,7 +31,7 @@ std::vector<std::string> Split(const std::string& String,const std::string& deli
|
|||||||
}
|
}
|
||||||
void ProxyThread(const std::string& IP, int port);
|
void ProxyThread(const std::string& IP, int port);
|
||||||
void SyncResources(const std::string&IP,int Port){
|
void SyncResources(const std::string&IP,int Port){
|
||||||
std::cout << "Called" << std::endl;
|
if(MPDEV)std::cout << "Called" << std::endl;
|
||||||
std::string FileList;
|
std::string FileList;
|
||||||
struct stat info{};
|
struct stat info{};
|
||||||
if(stat( "Resources", &info) != 0){
|
if(stat( "Resources", &info) != 0){
|
||||||
@ -50,7 +51,7 @@ void SyncResources(const std::string&IP,int Port){
|
|||||||
|
|
||||||
if(SendingSocket == INVALID_SOCKET)
|
if(SendingSocket == INVALID_SOCKET)
|
||||||
{
|
{
|
||||||
printf("Client: socket() failed! Error code: %d\n", WSAGetLastError());
|
if(MPDEV)printf("Client: socket() failed! Error code: %d\n", WSAGetLastError());
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -65,7 +66,7 @@ void SyncResources(const std::string&IP,int Port){
|
|||||||
|
|
||||||
if(RetCode != 0)
|
if(RetCode != 0)
|
||||||
{
|
{
|
||||||
printf("Client: connect() failed! Error code: %ld\n", WSAGetLastError());
|
if(MPDEV)printf("Client: connect() failed! Error code: %ld\n", WSAGetLastError());
|
||||||
closesocket(SendingSocket);
|
closesocket(SendingSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
return;
|
return;
|
||||||
@ -132,30 +133,30 @@ void SyncResources(const std::string&IP,int Port){
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (iResult == 0)
|
else if (iResult == 0)
|
||||||
printf("Connection closing...\n");
|
if(MPDEV)printf("Connection closing...\n");
|
||||||
else {
|
else {
|
||||||
printf("(Resources) recv failed with error: %d\n", WSAGetLastError());
|
if(MPDEV)printf("(Resources) recv failed with error: %d\n", WSAGetLastError());
|
||||||
closesocket(SendingSocket);
|
closesocket(SendingSocket);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}while (iResult > 0);
|
}while (iResult > 0);
|
||||||
if(BytesSent == SOCKET_ERROR)
|
if(BytesSent == SOCKET_ERROR)
|
||||||
printf("Client: send() error %d.\n", WSAGetLastError());
|
if(MPDEV)printf("Client: send() error %d.\n", WSAGetLastError());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if( shutdown(SendingSocket, SD_SEND) != 0)
|
if( shutdown(SendingSocket, SD_SEND) != 0)
|
||||||
printf("Client: Well, there is something wrong with the shutdown() The error code: %d\n", WSAGetLastError());
|
if(MPDEV)printf("Client: Well, there is something wrong with the shutdown() The error code: %d\n", WSAGetLastError());
|
||||||
|
|
||||||
|
|
||||||
if(closesocket(SendingSocket) != 0)
|
if(closesocket(SendingSocket) != 0)
|
||||||
printf("Client: Cannot close \"SendingSocket\" socket. Error code: %d\n", WSAGetLastError());
|
if(MPDEV)printf("Client: Cannot close \"SendingSocket\" socket. Error code: %d\n", WSAGetLastError());
|
||||||
|
|
||||||
|
|
||||||
if(WSACleanup() != 0)
|
if(WSACleanup() != 0)
|
||||||
printf("Client: WSACleanup() failed!...\n");
|
if(MPDEV)printf("Client: WSACleanup() failed!...\n");
|
||||||
|
|
||||||
UlStatus = "Uldone";
|
UlStatus = "Uldone";
|
||||||
std::cout << "Done!" << std::endl;
|
std::cout << "Done!" << std::endl;
|
||||||
ProxyThread(IP,Port);
|
ProxyThread(IP,Port+1);
|
||||||
}
|
}
|
19
Security.cpp
19
Security.cpp
@ -6,6 +6,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#define MAX_KEY_LENGTH 255
|
#define MAX_KEY_LENGTH 255
|
||||||
#define MAX_VALUE_NAME 16383
|
#define MAX_VALUE_NAME 16383
|
||||||
|
|
||||||
@ -110,9 +112,10 @@ std::string QueryKey(HKEY hKey,int ID)
|
|||||||
std::string data = reinterpret_cast<const char *const>(buffer);
|
std::string data = reinterpret_cast<const char *const>(buffer);
|
||||||
std::string key = achValue;
|
std::string key = achValue;
|
||||||
switch (ID){
|
switch (ID){
|
||||||
case 1: if(data.find(HTA("737465616d")) != std::string::npos) {return data;} break;
|
case 1: if(data.find(HTA("737465616d")) != std::string::npos)return data; break;
|
||||||
case 2: if(key == HTA("4e616d65") && data == HTA("4265616d4e472e6472697665")) {return data;} break;
|
case 2: if(key == HTA("4e616d65") && data == HTA("4265616d4e472e6472697665"))return data; break;
|
||||||
case 3: return data.substr(0,data.length()-2);
|
case 3: return data.substr(0,data.length()-2); break;
|
||||||
|
case 4: if(key == HTA("75736572706174685f6f76657272696465"))return data;
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -156,10 +159,11 @@ std::vector<std::string> Check(){
|
|||||||
while (fgets(Buffer.data(), Buffer.size(), pipe.get()) != nullptr) {
|
while (fgets(Buffer.data(), Buffer.size(), pipe.get()) != nullptr) {
|
||||||
result += Buffer.data();
|
result += Buffer.data();
|
||||||
}
|
}
|
||||||
if(result.size() > 100 && result.find_last_of("Byte") != std::string::npos){
|
std::string File = HTA("3238343136302e6a736f6e");
|
||||||
int pos = result.find_last_of("Byte");
|
if(result.size() > 100 && result.find(File) != std::string::npos){
|
||||||
while(result.substr(pos,4) != "File"){pos--;}
|
int pos = int(result.find(File)) + 9;
|
||||||
while(!isdigit(result.at(pos))){pos--;}
|
while(pos != result.length() && !isdigit(result.at(pos))){pos++;}
|
||||||
|
if(pos - result.length() < 5)Exit(MSG2 + " Code 2");
|
||||||
if((result.substr(pos,1).at(0) - 48) == 0) Exit(MSG1 + " Code 2");
|
if((result.substr(pos,1).at(0) - 48) == 0) Exit(MSG1 + " Code 2");
|
||||||
}else Exit(MSG2 + " Code 2");
|
}else Exit(MSG2 + " Code 2");
|
||||||
result.clear();
|
result.clear();
|
||||||
@ -167,7 +171,6 @@ std::vector<std::string> Check(){
|
|||||||
Result.clear();
|
Result.clear();
|
||||||
TraceBack++;
|
TraceBack++;
|
||||||
}else{Exit(MSG2 + ". Code: 4");}
|
}else{Exit(MSG2 + ". Code: 4");}
|
||||||
|
|
||||||
K1.clear();
|
K1.clear();
|
||||||
RegCloseKey(hKey);
|
RegCloseKey(hKey);
|
||||||
dwRegOPenKey = OpenKey(HKEY_CURRENT_USER, K2.c_str(), &hKey);
|
dwRegOPenKey = OpenKey(HKEY_CURRENT_USER, K2.c_str(), &hKey);
|
||||||
|
@ -10,9 +10,10 @@
|
|||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
extern bool TCPTerminate;
|
extern bool TCPTerminate;
|
||||||
|
extern bool MPDEV;
|
||||||
void Print(const std::string&MSG);
|
void Print(const std::string&MSG);
|
||||||
std::queue<std::string> VNTCPQueue;
|
std::queue<std::string> VNTCPQueue;
|
||||||
extern std::queue<std::string> RUDPToSend;
|
void RUDPSEND(const std::string&Data);
|
||||||
#define DEFAULT_PORT "4446"
|
#define DEFAULT_PORT "4446"
|
||||||
|
|
||||||
void Responder(const SOCKET *CS){
|
void Responder(const SOCKET *CS){
|
||||||
@ -23,12 +24,12 @@ void Responder(const SOCKET *CS){
|
|||||||
VNTCPQueue.front() += "\n";
|
VNTCPQueue.front() += "\n";
|
||||||
iSendResult = send(ClientSocket, VNTCPQueue.front().c_str(), VNTCPQueue.front().length(), 0);
|
iSendResult = send(ClientSocket, VNTCPQueue.front().c_str(), VNTCPQueue.front().length(), 0);
|
||||||
if (iSendResult == SOCKET_ERROR) {
|
if (iSendResult == SOCKET_ERROR) {
|
||||||
std::cout << "(VN) send failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(VN) send failed with error: " << WSAGetLastError() << std::endl;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
if(iSendResult > 1000){
|
if(iSendResult > 1000){
|
||||||
std::cout << "(Launcher->Game VN) Bytes sent: " << iSendResult << " : " << VNTCPQueue.front().substr(0,10)
|
if(MPDEV){std::cout << "(Launcher->Game VN) Bytes sent: " << iSendResult << " : " << VNTCPQueue.front().substr(0,10)
|
||||||
<< VNTCPQueue.front().substr(VNTCPQueue.front().length()-10) << std::endl;
|
<< VNTCPQueue.front().substr(VNTCPQueue.front().length()-10) << std::endl;}
|
||||||
}
|
}
|
||||||
VNTCPQueue.pop();
|
VNTCPQueue.pop();
|
||||||
}
|
}
|
||||||
@ -36,10 +37,11 @@ void Responder(const SOCKET *CS){
|
|||||||
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
|
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
std::string Compress(const std::string&Data);
|
||||||
|
std::string Decompress(const std::string&Data);
|
||||||
void VehicleNetworkStart(){
|
void VehicleNetworkStart(){
|
||||||
do {
|
do {
|
||||||
std::cout << "VN on Start" << std::endl;
|
if(MPDEV)std::cout << "VN on Start" << std::endl;
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
int iResult;
|
int iResult;
|
||||||
SOCKET ListenSocket = INVALID_SOCKET;
|
SOCKET ListenSocket = INVALID_SOCKET;
|
||||||
@ -55,7 +57,7 @@ void VehicleNetworkStart(){
|
|||||||
// Initialize Winsock
|
// Initialize Winsock
|
||||||
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||||
if (iResult != 0) {
|
if (iResult != 0) {
|
||||||
std::cout << "(VN) WSAStartup failed with error: " << iResult << std::endl;
|
if(MPDEV)std::cout << "(VN) WSAStartup failed with error: " << iResult << std::endl;
|
||||||
std::cin.get();
|
std::cin.get();
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
@ -69,7 +71,7 @@ void VehicleNetworkStart(){
|
|||||||
// Resolve the server address and port
|
// Resolve the server address and port
|
||||||
iResult = getaddrinfo(nullptr, DEFAULT_PORT, &hints, &result);
|
iResult = getaddrinfo(nullptr, DEFAULT_PORT, &hints, &result);
|
||||||
if (iResult != 0) {
|
if (iResult != 0) {
|
||||||
std::cout << "(VN) getaddrinfo failed with error: " << iResult << std::endl;
|
if(MPDEV)std::cout << "(VN) getaddrinfo failed with error: " << iResult << std::endl;
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -77,7 +79,7 @@ void VehicleNetworkStart(){
|
|||||||
// Create a socket for connecting to server
|
// Create a socket for connecting to server
|
||||||
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
||||||
if (ListenSocket == INVALID_SOCKET) {
|
if (ListenSocket == INVALID_SOCKET) {
|
||||||
std::cout << "(VN) socket failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(VN) socket failed with error: " << WSAGetLastError() << std::endl;
|
||||||
freeaddrinfo(result);
|
freeaddrinfo(result);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
break;
|
break;
|
||||||
@ -86,7 +88,7 @@ void VehicleNetworkStart(){
|
|||||||
// Setup the TCP listening socket
|
// Setup the TCP listening socket
|
||||||
iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen);
|
iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen);
|
||||||
if (iResult == SOCKET_ERROR) {
|
if (iResult == SOCKET_ERROR) {
|
||||||
std::cout << "(VN) bind failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(VN) bind failed with error: " << WSAGetLastError() << std::endl;
|
||||||
freeaddrinfo(result);
|
freeaddrinfo(result);
|
||||||
closesocket(ListenSocket);
|
closesocket(ListenSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
@ -97,20 +99,20 @@ void VehicleNetworkStart(){
|
|||||||
|
|
||||||
iResult = listen(ListenSocket, SOMAXCONN);
|
iResult = listen(ListenSocket, SOMAXCONN);
|
||||||
if (iResult == SOCKET_ERROR) {
|
if (iResult == SOCKET_ERROR) {
|
||||||
std::cout << "(VN) listen failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(VN) listen failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ListenSocket);
|
closesocket(ListenSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ClientSocket = accept(ListenSocket, NULL, NULL);
|
ClientSocket = accept(ListenSocket, NULL, NULL);
|
||||||
if (ClientSocket == INVALID_SOCKET) {
|
if (ClientSocket == INVALID_SOCKET) {
|
||||||
std::cout << "(VN) accept failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(VN) accept failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ListenSocket);
|
closesocket(ListenSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
closesocket(ListenSocket);
|
closesocket(ListenSocket);
|
||||||
std::cout << "(VN) Game Connected!" << std::endl;
|
if(MPDEV)std::cout << "(VN) Game Connected!" << std::endl;
|
||||||
|
|
||||||
std::thread TCPSend(Responder,&ClientSocket);
|
std::thread TCPSend(Responder,&ClientSocket);
|
||||||
TCPSend.detach();
|
TCPSend.detach();
|
||||||
@ -123,18 +125,26 @@ void VehicleNetworkStart(){
|
|||||||
memcpy(&buff[0],recvbuf,iResult);
|
memcpy(&buff[0],recvbuf,iResult);
|
||||||
buff.resize(iResult);
|
buff.resize(iResult);
|
||||||
//Print(buff);
|
//Print(buff);
|
||||||
if(buff.length() > 3)RUDPToSend.push(buff);
|
if(MPDEV) {
|
||||||
|
std::string cmp = Compress(buff), dcm = Decompress(cmp);
|
||||||
|
std::cout << "Compressed Size : " << cmp.length() << std::endl;
|
||||||
std::cout << "(Game->Launcher VN) Data : " << buff.length() << std::endl;
|
std::cout << "Decompressed Size : " << dcm.length() << std::endl;
|
||||||
|
if (cmp == dcm) {
|
||||||
|
std::cout << "Success!" << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << "Fail!" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RUDPSEND(buff);
|
||||||
|
//std::cout << "(Game->Launcher VN) Data : " << buff.length() << std::endl;
|
||||||
} else if (iResult == 0) {
|
} else if (iResult == 0) {
|
||||||
std::cout << "(VN) Connection closing...\n";
|
if(MPDEV)std::cout << "(VN) Connection closing...\n";
|
||||||
closesocket(ClientSocket);
|
closesocket(ClientSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
std::cout << "(VN) recv failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(VN) recv failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ClientSocket);
|
closesocket(ClientSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
continue;
|
continue;
|
||||||
@ -143,7 +153,7 @@ void VehicleNetworkStart(){
|
|||||||
|
|
||||||
iResult = shutdown(ClientSocket, SD_SEND);
|
iResult = shutdown(ClientSocket, SD_SEND);
|
||||||
if (iResult == SOCKET_ERROR) {
|
if (iResult == SOCKET_ERROR) {
|
||||||
std::cout << "(VN) shutdown failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(VN) shutdown failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ClientSocket);
|
closesocket(ClientSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
continue;
|
continue;
|
||||||
|
@ -601,7 +601,7 @@ enum {
|
|||||||
ENET_PEER_WINDOW_SIZE_SCALE = 64 * 1024,
|
ENET_PEER_WINDOW_SIZE_SCALE = 64 * 1024,
|
||||||
ENET_PEER_TIMEOUT_LIMIT = 32,
|
ENET_PEER_TIMEOUT_LIMIT = 32,
|
||||||
ENET_PEER_TIMEOUT_MINIMUM = 5000,
|
ENET_PEER_TIMEOUT_MINIMUM = 5000,
|
||||||
ENET_PEER_TIMEOUT_MAXIMUM = 30000,
|
ENET_PEER_TIMEOUT_MAXIMUM = 6000,
|
||||||
ENET_PEER_PING_INTERVAL = 500,
|
ENET_PEER_PING_INTERVAL = 500,
|
||||||
ENET_PEER_UNSEQUENCED_WINDOWS = 64,
|
ENET_PEER_UNSEQUENCED_WINDOWS = 64,
|
||||||
ENET_PEER_UNSEQUENCED_WINDOW_SIZE = 1024,
|
ENET_PEER_UNSEQUENCED_WINDOW_SIZE = 1024,
|
||||||
|
128
main.cpp
128
main.cpp
@ -3,20 +3,25 @@
|
|||||||
////
|
////
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <urlmon.h>
|
||||||
#include <vector>
|
|
||||||
#include <direct.h>
|
#include <direct.h>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <urlmon.h>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#pragma comment(lib, "urlmon.lib")
|
#pragma comment(lib, "urlmon.lib")
|
||||||
|
|
||||||
std::string HTTP_REQUEST(const std::string&url,int port);
|
std::string HTTP_REQUEST(const std::string&url,int port);
|
||||||
std::vector<std::string> Discord_Main();
|
void CheckForUpdates(const std::string& CV);
|
||||||
|
std::vector<std::string> GetDiscordInfo();
|
||||||
|
std::string QueryKey(HKEY hKey,int ID);
|
||||||
std::vector<std::string> GlobalInfo;
|
std::vector<std::string> GlobalInfo;
|
||||||
std::vector<std::string> Check();
|
std::vector<std::string> Check();
|
||||||
std::string getHardwareID();
|
std::string getHardwareID();
|
||||||
void CheckForUpdates(const std::string& CV);
|
extern int DEFAULT_PORT;
|
||||||
|
void Discord_Main();
|
||||||
|
bool MPDEV = false;
|
||||||
void ProxyStart();
|
void ProxyStart();
|
||||||
|
|
||||||
void Download(const std::string& URL,const std::string& path){
|
void Download(const std::string& URL,const std::string& path){
|
||||||
@ -54,8 +59,9 @@ std::string CheckDir(char*dir, const std::string& ver){
|
|||||||
SetCurrentDirectoryA(AD.c_str());
|
SetCurrentDirectoryA(AD.c_str());
|
||||||
SystemExec("rename *.exe " + DN + ">nul");
|
SystemExec("rename *.exe " + DN + ">nul");
|
||||||
SystemExec(R"(powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Desktop\BeamMP-Launcher.lnk');$s.TargetPath=')"+AD+"\\"+DN+"';$s.Save()\"");
|
SystemExec(R"(powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Desktop\BeamMP-Launcher.lnk');$s.TargetPath=')"+AD+"\\"+DN+"';$s.Save()\"");
|
||||||
CreateDirectoryA("BeamNG",nullptr);
|
if(stat("BeamNG",&info))SystemExec("mkdir BeamNG>nul");
|
||||||
CreateDirectoryA("BeamNG\\mods",nullptr);
|
if(stat("BeamNG\\mods",&info))SystemExec("mkdir BeamNG\\mods>nul");
|
||||||
|
if(stat("BeamNG\\settings",&info))SystemExec("mkdir BeamNG\\settings>nul");
|
||||||
SetFileAttributesA("BeamNG",2|4);
|
SetFileAttributesA("BeamNG",2|4);
|
||||||
return AD + "\\BeamNG";
|
return AD + "\\BeamNG";
|
||||||
}
|
}
|
||||||
@ -76,43 +82,97 @@ std::string CheckVer(const std::string &path){
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
void SyncResources(const std::string&IP,int Port);
|
void SyncResources(const std::string&IP,int Port);
|
||||||
|
|
||||||
|
std::string Write(const std::string&Path){
|
||||||
|
HKEY hKey;
|
||||||
|
LPCTSTR sk = TEXT("Software\\BeamNG\\BeamNG.drive");
|
||||||
|
|
||||||
|
LONG openRes = RegOpenKeyEx(HKEY_CURRENT_USER, sk, 0, KEY_ALL_ACCESS, &hKey);
|
||||||
|
|
||||||
|
if (openRes != ERROR_SUCCESS) {
|
||||||
|
Exit("Error! Please launch the game at least once");
|
||||||
|
}
|
||||||
|
std::string Query = QueryKey(hKey,4);
|
||||||
|
LPCTSTR value = TEXT("userpath_override");
|
||||||
|
LONG setRes = RegSetValueEx(hKey, value, 0, REG_SZ, (LPBYTE)Path.c_str(), Path.size());
|
||||||
|
|
||||||
|
if (setRes != ERROR_SUCCESS) {
|
||||||
|
Exit("Error! Failed to launch the game code 1");
|
||||||
|
}
|
||||||
|
RegCloseKey(hKey);
|
||||||
|
return Query;
|
||||||
|
}
|
||||||
|
void RollBack(const std::string&Val){
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||||
|
Write(Val);
|
||||||
|
}
|
||||||
|
void StartGame(const std::string&ExeDir,const std::string&Current){
|
||||||
|
std::cout << "Game Launched!\n";
|
||||||
|
std::thread RB(RollBack,Current);
|
||||||
|
RB.detach();
|
||||||
|
SystemExec(ExeDir + " -nocrashreport");
|
||||||
|
Exit("Game Closed!");
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
std::string ver = "0.735", Path = CheckDir(argv[0],ver),HTTP_Result;
|
const unsigned long long NPos = std::string::npos;
|
||||||
CheckForUpdates(ver); //Update Check
|
struct stat info{};
|
||||||
|
if(argc > 1){
|
||||||
|
std::string Port = argv[1];
|
||||||
|
if(Port.find_first_not_of("0123456789") == NPos){
|
||||||
|
DEFAULT_PORT = std::stoi(Port);
|
||||||
|
std::cout << "Running on custom port : " << DEFAULT_PORT << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::string ver = "0.89", Path = CheckDir(argv[0],ver),HTTP_Result;
|
||||||
|
CheckForUpdates(ver);
|
||||||
|
|
||||||
|
std::thread t1(Discord_Main);
|
||||||
|
t1.detach();
|
||||||
|
|
||||||
|
|
||||||
|
std::cout << "Connecting to discord client..." << std::endl;
|
||||||
|
while(GlobalInfo.empty()){
|
||||||
|
GlobalInfo = GetDiscordInfo();
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
HTTP_Result = HTTP_REQUEST("https://beamng-mp.com/entitlement?did="+GlobalInfo.at(2),443);
|
||||||
|
if(HTTP_Result.find("\"MDEV\"") == NPos){
|
||||||
|
if (HTTP_Result.find("\"MOD\"") == NPos && HTTP_Result.find("\"EA\"") == NPos){
|
||||||
|
if (HTTP_Result.find("\"SUPPORT\"") == NPos && HTTP_Result.find("\"YT\"") == NPos){
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else MPDEV = true;
|
||||||
//Security
|
//Security
|
||||||
std::vector<std::string> Data = Check();
|
std::vector<std::string> Data = Check();
|
||||||
std::string GamePath = Data.at(2);
|
std::string GamePath = Data.at(2);
|
||||||
std::cout << "You own BeamNG on this machine!" << std::endl;
|
if(MPDEV)std::cout << "You own BeamNG on this machine!" << std::endl;
|
||||||
std::cout << "Game Ver : " << CheckVer(GamePath) << std::endl;
|
std::cout << "Game Version : " << CheckVer(GamePath) << std::endl;
|
||||||
|
std::string ExeDir = "\""+GamePath.substr(0,GamePath.find_last_of('\\')) + R"(\Bin64\BeamNG.drive.x64.exe")";
|
||||||
|
std::string Settings = Path + "\\settings\\uiapps-layouts.json";
|
||||||
|
if(stat(Settings.c_str(),&info)!=0){
|
||||||
|
Download("https://beamng-mp.com/client-data",Settings);
|
||||||
|
std::cout << "Downloaded default config!" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
GlobalInfo = Discord_Main();
|
Download("https://beamng-mp.com/builds/client?did="+GlobalInfo.at(2),Path + R"(\mods\BeamMP.zip)");
|
||||||
std::cout << "Name : " << GlobalInfo.at(0) << std::endl;
|
|
||||||
std::cout << "Discriminator : " << GlobalInfo.at(1) << std::endl;
|
|
||||||
std::cout << "Unique ID : " << GlobalInfo.at(2) << std::endl;
|
|
||||||
|
|
||||||
std::cout << "HWID : " << getHardwareID() << std::endl;
|
if(!MPDEV){
|
||||||
|
std::thread Game(StartGame,ExeDir,Write(Path + "\\"));
|
||||||
HTTP_Result = HTTP_REQUEST("https://beamng-mp.com/entitlement?did="+Discord_Main().at(2),443);
|
Game.detach();
|
||||||
std::cout << "you have : " << HTTP_Result << std::endl;
|
|
||||||
if(HTTP_Result.find("[\"MDEV\"]") == std::string::npos && HTTP_Result.find("[\"MOD\"]") == std::string::npos)exit(-1); ///Remove later
|
|
||||||
|
|
||||||
|
|
||||||
std::string ExeDir = GamePath.substr(0,GamePath.find_last_of('\\')) + "\\Bin64\\BeamNG.drive.x64.exe";
|
|
||||||
Download("https://beamng-mp.com/builds/client?did="+Discord_Main().at(2),Path + R"(\mods\BeamMP.zip)");
|
|
||||||
|
|
||||||
|
|
||||||
/*if(HTTP_Result.find("[\"MDEV\"]") != std::string::npos){
|
|
||||||
WinExec(ExeDir + " -cefdev -console -nocrashreport -userpath " + Path);
|
|
||||||
}else{
|
}else{
|
||||||
WinExec(ExeDir + " -nocrashreport -userpath " + Path);
|
std::cout << "Name : " << GlobalInfo.at(0) << std::endl;
|
||||||
}*/
|
std::cout << "Discriminator : " << GlobalInfo.at(1) << std::endl;
|
||||||
//std::cout << "Game Launched!\n";
|
std::cout << "Unique ID : " << GlobalInfo.at(2) << std::endl;
|
||||||
|
std::cout << "HWID : " << getHardwareID() << std::endl;
|
||||||
|
std::cout << "you have : " << HTTP_Result << std::endl;
|
||||||
ProxyStart(); //Proxy main start
|
}
|
||||||
|
|
||||||
|
ProxyStart();
|
||||||
Exit("");
|
Exit("");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
112
proxy.cpp
112
proxy.cpp
@ -12,7 +12,8 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
#define DEFAULT_PORT "4445"
|
int DEFAULT_PORT = 4445;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
ENetHost *host;
|
ENetHost *host;
|
||||||
ENetPeer *peer;
|
ENetPeer *peer;
|
||||||
@ -20,15 +21,25 @@ typedef struct {
|
|||||||
std::vector<std::string> Split(const std::string& String,const std::string& delimiter);
|
std::vector<std::string> Split(const std::string& String,const std::string& delimiter);
|
||||||
std::chrono::time_point<std::chrono::steady_clock> PingStart,PingEnd;
|
std::chrono::time_point<std::chrono::steady_clock> PingStart,PingEnd;
|
||||||
extern std::vector<std::string> GlobalInfo;
|
extern std::vector<std::string> GlobalInfo;
|
||||||
|
std::condition_variable RUDPLOCK,TCPLOCK;
|
||||||
std::queue<std::string> RUDPToSend;
|
std::queue<std::string> RUDPToSend;
|
||||||
std::queue<std::string> RUDPData;
|
std::queue<std::string> RUDPData;
|
||||||
bool TCPTerminate = false;
|
bool TCPTerminate = false;
|
||||||
bool Terminate = false;
|
bool Terminate = false;
|
||||||
bool CServer = true;
|
bool CServer = true;
|
||||||
int ping = 0;
|
int ping = 0;
|
||||||
|
extern bool MPDEV;
|
||||||
|
|
||||||
[[noreturn]] void CoreNetworkThread();
|
[[noreturn]] void CoreNetworkThread();
|
||||||
|
|
||||||
|
void TCPSEND(const std::string&Data){
|
||||||
|
RUDPData.push(Data);
|
||||||
|
TCPLOCK.notify_all();
|
||||||
|
}
|
||||||
|
void RUDPSEND(const std::string&Data){
|
||||||
|
RUDPToSend.push(Data);
|
||||||
|
RUDPLOCK.notify_all();
|
||||||
|
}
|
||||||
void AutoPing(ENetPeer*peer){
|
void AutoPing(ENetPeer*peer){
|
||||||
while(!Terminate && peer != nullptr){
|
while(!Terminate && peer != nullptr){
|
||||||
enet_peer_send(peer, 0, enet_packet_create("p", 2, ENET_PACKET_FLAG_RELIABLE));
|
enet_peer_send(peer, 0, enet_packet_create("p", 2, ENET_PACKET_FLAG_RELIABLE));
|
||||||
@ -54,13 +65,13 @@ void RUDPParser(const std::string& Data,ENetPeer*peer){
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
///std::cout << "Received: " << Data << std::endl;
|
///std::cout << "Received: " << Data << std::endl;
|
||||||
RUDPData.push(Data);
|
TCPSEND(Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandleEvent(ENetEvent event,Client client){
|
void HandleEvent(ENetEvent event,Client client){
|
||||||
switch (event.type){
|
switch (event.type){
|
||||||
case ENET_EVENT_TYPE_CONNECT:
|
case ENET_EVENT_TYPE_CONNECT:
|
||||||
std::cout << "(Launcher->Server) Client Connected!" << std::endl;
|
std::cout << "Connected to server!" << std::endl;
|
||||||
//printf("Client Connected port : %u.\n",event.peer->address.port);
|
//printf("Client Connected port : %u.\n",event.peer->address.port);
|
||||||
event.peer->data = (void*)"Connected Server";
|
event.peer->data = (void*)"Connected Server";
|
||||||
break;
|
break;
|
||||||
@ -70,6 +81,8 @@ void HandleEvent(ENetEvent event,Client client){
|
|||||||
break;
|
break;
|
||||||
case ENET_EVENT_TYPE_DISCONNECT:
|
case ENET_EVENT_TYPE_DISCONNECT:
|
||||||
printf ("%s disconnected.\n", (char *)event.peer->data);
|
printf ("%s disconnected.\n", (char *)event.peer->data);
|
||||||
|
CServer = true;
|
||||||
|
Terminate = true;
|
||||||
event.peer->data = nullptr;
|
event.peer->data = nullptr;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -77,28 +90,29 @@ void HandleEvent(ENetEvent event,Client client){
|
|||||||
printf ("%s timeout.\n", (char *)event.peer->data);
|
printf ("%s timeout.\n", (char *)event.peer->data);
|
||||||
CServer = true;
|
CServer = true;
|
||||||
Terminate = true;
|
Terminate = true;
|
||||||
|
TCPSEND("TTimeout");
|
||||||
event.peer->data = nullptr;
|
event.peer->data = nullptr;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ENET_EVENT_TYPE_NONE: break;
|
case ENET_EVENT_TYPE_NONE: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void RUDPClientThread(const std::string& IP, int Port){
|
void RUDPClientThread(const std::string& IP, int Port){
|
||||||
if (enet_initialize() != 0) {
|
if (enet_initialize() != 0) {
|
||||||
std::cout << "An error occurred while initializing RUDP.\n";
|
std::cout << "An error occurred while initializing!\n";
|
||||||
}
|
}
|
||||||
|
std::mutex Lock;
|
||||||
Client client;
|
Client client;
|
||||||
ENetAddress address = {0};
|
ENetAddress address = {0};
|
||||||
|
|
||||||
address.host = ENET_HOST_ANY;
|
address.host = ENET_HOST_ANY;
|
||||||
address.port = Port;
|
address.port = Port;
|
||||||
std::cout << "(Launcher->Server) Connecting...\n";
|
if(MPDEV)std::cout << "(Launcher->Server) Connecting...\n";
|
||||||
|
|
||||||
enet_address_set_host(&address, IP.c_str());
|
enet_address_set_host(&address, IP.c_str());
|
||||||
client.host = enet_host_create(nullptr, 1, 2, 0, 0);
|
client.host = enet_host_create(nullptr, 1, 2, 0, 0);
|
||||||
client.peer = enet_host_connect(client.host, &address, 2, 0);
|
client.peer = enet_host_connect(client.host, &address, 2, 0);
|
||||||
if (client.peer == nullptr) {
|
if (client.peer == nullptr) {
|
||||||
std::cout << "could not connect\n";
|
if(MPDEV)std::cout << "could not connect\n";
|
||||||
}
|
}
|
||||||
std::thread Ping(AutoPing,client.peer);
|
std::thread Ping(AutoPing,client.peer);
|
||||||
Ping.detach();
|
Ping.detach();
|
||||||
@ -107,56 +121,67 @@ void RUDPClientThread(const std::string& IP, int Port){
|
|||||||
enet_host_service(client.host, &event, 0);
|
enet_host_service(client.host, &event, 0);
|
||||||
HandleEvent(event,client);
|
HandleEvent(event,client);
|
||||||
while (!RUDPToSend.empty()){
|
while (!RUDPToSend.empty()){
|
||||||
|
if(RUDPToSend.front().length() > 3) {
|
||||||
int Rel = 8;
|
int Rel = 8;
|
||||||
char C = RUDPToSend.front().at(0);
|
char C = RUDPToSend.front().at(0);
|
||||||
if (C == 's' || C == 'd' || C == 'm' || C == 'r')Rel = 1;
|
if (C == 'O' || C == 'T')Rel = 1;
|
||||||
ENetPacket *packet = enet_packet_create(RUDPToSend.front().c_str(),
|
ENetPacket *packet = enet_packet_create(RUDPToSend.front().c_str(),
|
||||||
RUDPToSend.front().length() + 1,
|
RUDPToSend.front().length() + 1,
|
||||||
Rel);
|
Rel);
|
||||||
enet_peer_send(client.peer, 0, packet);
|
enet_peer_send(client.peer, 0, packet);
|
||||||
if (RUDPToSend.front().length() > 1000) {
|
if (RUDPToSend.front().length() > 1000) {
|
||||||
std::cout << "(Launcher->Server) Bytes sent: " << RUDPToSend.front().length() << " : "
|
if(MPDEV){std::cout << "(Launcher->Server) Bytes sent: " << RUDPToSend.front().length() << " : "
|
||||||
<< RUDPToSend.front().substr(0, 10)
|
<< RUDPToSend.front().substr(0, 10)
|
||||||
<< RUDPToSend.front().substr(RUDPToSend.front().length() - 10) << std::endl;
|
<< RUDPToSend.front().substr(RUDPToSend.front().length() - 10) << std::endl;}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
RUDPToSend.pop();
|
RUDPToSend.pop();
|
||||||
}
|
}
|
||||||
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
|
std::unique_lock<std::mutex> lk(Lock);
|
||||||
|
std::chrono::high_resolution_clock::time_point tp = std::chrono::high_resolution_clock::now() + std::chrono::nanoseconds (1);
|
||||||
|
RUDPLOCK.wait_until(lk, tp, [](){return !RUDPToSend.empty();});
|
||||||
|
|
||||||
} while (!Terminate);
|
} while (!Terminate);
|
||||||
enet_peer_disconnect(client.peer,0);
|
enet_peer_disconnect(client.peer,0);
|
||||||
enet_host_service(client.host, &event, 0);
|
enet_host_service(client.host, &event, 0);
|
||||||
HandleEvent(event,client);
|
HandleEvent(event,client);
|
||||||
CServer = true;
|
CServer = true;
|
||||||
std::cout << "(Launcher->Server) Terminated!" << std::endl;
|
std::cout << "Connection Terminated!" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPRespond(const SOCKET *CS){
|
void TCPRespond(const SOCKET *CS){
|
||||||
SOCKET ClientSocket = *CS;
|
SOCKET ClientSocket = *CS;
|
||||||
int iSendResult;
|
int iSendResult;
|
||||||
std::mutex m;
|
std::mutex Lock;
|
||||||
while(!TCPTerminate){
|
while(!TCPTerminate){
|
||||||
while (!RUDPData.empty()) {
|
while (!RUDPData.empty()) {
|
||||||
std::string Data = RUDPData.front() + "\n";
|
std::string Data = RUDPData.front() + "\n";
|
||||||
iSendResult = send(ClientSocket, Data.c_str(), Data.length(), 0);
|
iSendResult = send(ClientSocket, Data.c_str(), Data.length(), 0);
|
||||||
if (iSendResult == SOCKET_ERROR) {
|
if (iSendResult == SOCKET_ERROR) {
|
||||||
std::cout << "(Proxy) send failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Proxy) send failed with error: " << WSAGetLastError() << std::endl;
|
||||||
|
TCPTerminate = true;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
if(iSendResult > 1000){
|
if(iSendResult > 1000){
|
||||||
std::cout << "(Launcher->Game) Bytes sent: " << iSendResult << " : " << RUDPData.front().substr(0,10)
|
std::cout << "(Launcher->Game) Bytes sent: " << iSendResult << " : " << RUDPData.front().substr(0,10)
|
||||||
<< RUDPData.front().substr(RUDPData.front().length()-10) << std::endl;
|
<< RUDPData.front().substr(RUDPData.front().length()-10) << std::endl;
|
||||||
}
|
}
|
||||||
|
//std::cout << "(Launcher->Game) Bytes sent: " << iSendResult << " : " << RUDPData.front()<< std::endl;
|
||||||
RUDPData.pop();
|
RUDPData.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
|
std::unique_lock<std::mutex> lk(Lock);
|
||||||
|
std::chrono::high_resolution_clock::time_point tp = std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(1);
|
||||||
|
TCPLOCK.wait_until(lk, tp, [](){return !RUDPData.empty();});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Compress(const std::string&Data);
|
||||||
|
std::string Decompress(const std::string&Data);
|
||||||
void TCPServerThread(const std::string& IP, int Port){
|
void TCPServerThread(const std::string& IP, int Port){
|
||||||
std::cout << "Proxy Started! " << IP << ":" << Port << std::endl;
|
if(MPDEV)std::cout << "Proxy Started! " << IP << ":" << Port << std::endl;
|
||||||
do {
|
do {
|
||||||
std::cout << "Proxy on Start" << std::endl;
|
if(MPDEV)std::cout << "Proxy on Start" << std::endl;
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
int iResult;
|
int iResult;
|
||||||
SOCKET ListenSocket = INVALID_SOCKET;
|
SOCKET ListenSocket = INVALID_SOCKET;
|
||||||
@ -170,7 +195,7 @@ void TCPServerThread(const std::string& IP, int Port){
|
|||||||
// Initialize Winsock
|
// Initialize Winsock
|
||||||
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||||
if (iResult != 0) {
|
if (iResult != 0) {
|
||||||
std::cout << "(Proxy) WSAStartup failed with error: " << iResult << std::endl;
|
if(MPDEV)std::cout << "(Proxy) WSAStartup failed with error: " << iResult << std::endl;
|
||||||
std::cin.get();
|
std::cin.get();
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
@ -180,11 +205,10 @@ void TCPServerThread(const std::string& IP, int Port){
|
|||||||
hints.ai_socktype = SOCK_STREAM;
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
hints.ai_protocol = IPPROTO_TCP;
|
hints.ai_protocol = IPPROTO_TCP;
|
||||||
hints.ai_flags = AI_PASSIVE;
|
hints.ai_flags = AI_PASSIVE;
|
||||||
|
|
||||||
// Resolve the server address and port
|
// Resolve the server address and port
|
||||||
iResult = getaddrinfo(nullptr, DEFAULT_PORT, &hints, &result);
|
iResult = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT).c_str(), &hints, &result);
|
||||||
if (iResult != 0) {
|
if (iResult != 0) {
|
||||||
std::cout << "(Proxy) getaddrinfo failed with error: " << iResult << std::endl;
|
if(MPDEV)std::cout << "(Proxy) getaddrinfo failed with error: " << iResult << std::endl;
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -192,7 +216,7 @@ void TCPServerThread(const std::string& IP, int Port){
|
|||||||
// Create a socket for connecting to server
|
// Create a socket for connecting to server
|
||||||
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
||||||
if (ListenSocket == INVALID_SOCKET) {
|
if (ListenSocket == INVALID_SOCKET) {
|
||||||
std::cout << "(Proxy) socket failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Proxy) socket failed with error: " << WSAGetLastError() << std::endl;
|
||||||
freeaddrinfo(result);
|
freeaddrinfo(result);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
break;
|
break;
|
||||||
@ -201,7 +225,7 @@ void TCPServerThread(const std::string& IP, int Port){
|
|||||||
// Setup the TCP listening socket
|
// Setup the TCP listening socket
|
||||||
iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen);
|
iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen);
|
||||||
if (iResult == SOCKET_ERROR) {
|
if (iResult == SOCKET_ERROR) {
|
||||||
std::cout << "(Proxy) bind failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Proxy) bind failed with error: " << WSAGetLastError() << std::endl;
|
||||||
freeaddrinfo(result);
|
freeaddrinfo(result);
|
||||||
closesocket(ListenSocket);
|
closesocket(ListenSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
@ -212,20 +236,20 @@ void TCPServerThread(const std::string& IP, int Port){
|
|||||||
|
|
||||||
iResult = listen(ListenSocket, SOMAXCONN);
|
iResult = listen(ListenSocket, SOMAXCONN);
|
||||||
if (iResult == SOCKET_ERROR) {
|
if (iResult == SOCKET_ERROR) {
|
||||||
std::cout << "(Proxy) listen failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Proxy) listen failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ListenSocket);
|
closesocket(ListenSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ClientSocket = accept(ListenSocket, NULL, NULL);
|
ClientSocket = accept(ListenSocket, NULL, NULL);
|
||||||
if (ClientSocket == INVALID_SOCKET) {
|
if (ClientSocket == INVALID_SOCKET) {
|
||||||
std::cout << "(Proxy) accept failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Proxy) accept failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ListenSocket);
|
closesocket(ListenSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
closesocket(ListenSocket);
|
closesocket(ListenSocket);
|
||||||
std::cout << "(Proxy) Game Connected!" << std::endl;
|
if(MPDEV)std::cout << "(Proxy) Game Connected!" << std::endl;
|
||||||
if(CServer){
|
if(CServer){
|
||||||
std::thread t1(RUDPClientThread, IP, Port);
|
std::thread t1(RUDPClientThread, IP, Port);
|
||||||
t1.detach();
|
t1.detach();
|
||||||
@ -241,18 +265,26 @@ void TCPServerThread(const std::string& IP, int Port){
|
|||||||
buff.resize(iResult*2);
|
buff.resize(iResult*2);
|
||||||
memcpy(&buff[0],recvbuf,iResult);
|
memcpy(&buff[0],recvbuf,iResult);
|
||||||
buff.resize(iResult);
|
buff.resize(iResult);
|
||||||
|
if(MPDEV && buff.length() > 1000) {
|
||||||
if(buff.length() > 3)RUDPToSend.push(buff);
|
std::string cmp = Compress(buff), dcm = Decompress(cmp);
|
||||||
|
std::cout << "Compressed Size : " << cmp.length() << std::endl;
|
||||||
|
std::cout << "Decompressed Size : " << dcm.length() << std::endl;
|
||||||
|
if (cmp == dcm) {
|
||||||
|
std::cout << "Success!" << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << "Fail!" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RUDPSEND(buff);
|
||||||
//std::cout << "(Game->Launcher) Data : " << buff.length() << std::endl;
|
//std::cout << "(Game->Launcher) Data : " << buff.length() << std::endl;
|
||||||
} else if (iResult == 0) {
|
} else if (iResult == 0) {
|
||||||
std::cout << "(Proxy) Connection closing...\n";
|
if(MPDEV)std::cout << "(Proxy) Connection closing...\n";
|
||||||
closesocket(ClientSocket);
|
closesocket(ClientSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
std::cout << "(Proxy) recv failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Proxy) recv failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ClientSocket);
|
closesocket(ClientSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
continue;
|
continue;
|
||||||
@ -261,7 +293,7 @@ void TCPServerThread(const std::string& IP, int Port){
|
|||||||
|
|
||||||
iResult = shutdown(ClientSocket, SD_SEND);
|
iResult = shutdown(ClientSocket, SD_SEND);
|
||||||
if (iResult == SOCKET_ERROR) {
|
if (iResult == SOCKET_ERROR) {
|
||||||
std::cout << "(Proxy) shutdown failed with error: " << WSAGetLastError() << std::endl;
|
if(MPDEV)std::cout << "(Proxy) shutdown failed with error: " << WSAGetLastError() << std::endl;
|
||||||
closesocket(ClientSocket);
|
closesocket(ClientSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
continue;
|
continue;
|
||||||
@ -271,19 +303,23 @@ void TCPServerThread(const std::string& IP, int Port){
|
|||||||
}while (!TCPTerminate);
|
}while (!TCPTerminate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VehicleNetworkStart();
|
||||||
|
|
||||||
void ProxyStart(){
|
void ProxyStart(){
|
||||||
std::thread t1(CoreNetworkThread);
|
std::thread t1(CoreNetworkThread);
|
||||||
std::cout << "Core Network Started!\n";
|
if(MPDEV)std::cout << "Core Network Started!\n";
|
||||||
t1.join();
|
t1.join();
|
||||||
}
|
}
|
||||||
void VehicleNetworkStart();
|
void Reset(){
|
||||||
void ProxyThread(const std::string& IP, int Port){
|
|
||||||
Terminate = false;
|
Terminate = false;
|
||||||
TCPTerminate = false;
|
TCPTerminate = false;
|
||||||
|
while(!RUDPToSend.empty()) RUDPToSend.pop();
|
||||||
|
while(!RUDPData.empty()) RUDPData.pop();
|
||||||
|
}
|
||||||
|
void ProxyThread(const std::string& IP, int Port){
|
||||||
|
Reset();
|
||||||
std::thread t1(TCPServerThread,IP,Port);
|
std::thread t1(TCPServerThread,IP,Port);
|
||||||
t1.detach();
|
t1.detach();
|
||||||
std::thread t2(VehicleNetworkStart);
|
/*std::thread t2(VehicleNetworkStart);
|
||||||
t2.detach();
|
t2.detach();*/
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user