Added Proxy

This commit is contained in:
Anonymous275 2020-03-04 00:37:54 +02:00
parent 1b95a23b88
commit f37808e84f
4 changed files with 141 additions and 3 deletions

View File

@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.15)
project(BeamNG-MP-Launcher)
set(CMAKE_CXX_STANDARD 14)
add_executable(BeamNG-MP-Launcher main.cpp enet.h)
add_executable(BeamNG-MP-Launcher main.cpp enet.h proxy.cpp)

1
enet.h
View File

@ -93,7 +93,6 @@
#include <WS2tcpip.h>
#include <mmsystem.h>
#include <intrin.h>
#if defined(_WIN32) && defined(_MSC_VER)
#if _MSC_VER < 1900

View File

@ -80,7 +80,7 @@ std::string QueryKey(HKEY hKey)
return "";
}
void Start();
int main()
{
HKEY hKey;
@ -93,6 +93,10 @@ int main()
}
RegCloseKey(hKey);
/// Update, Mods ect...
Start(); //Proxy main start
system("pause");
return 0;
}

134
proxy.cpp Normal file
View File

@ -0,0 +1,134 @@
////
//// Created by Anonymous275 on 3/3/2020.
////
#include "enet.h"
#include <WinSock2.h>
#include <Windows.h>
#include <WS2tcpip.h>
#include <cstdio>
#include <string>
#define DEFAULT_BUFLEN 64000
#define DEFAULT_PORT "4444"
std::string TCPData;
std::string RUDPData;
std::string TCPToSend;
std::string RUDPToSend;
void TCPServerThread(){
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
}
// Create a socket for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
}
freeaddrinfo(result);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
}
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
}
closesocket(ListenSocket);
do {
if(!RUDPData.empty()){
iSendResult = send( ClientSocket, RUDPData.c_str(), int(RUDPData.length())+1, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
}else{
RUDPData.clear();
printf("Bytes sent: %d\n", iSendResult);
}
}
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("Bytes received: %d\n", iResult);
printf("Data : %s\n",recvbuf);
RUDPToSend = recvbuf;
}
else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
}
} while (iResult > 0);
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
}
closesocket(ClientSocket);
WSACleanup();
}
void RUDPClientThread(){
}
void Start(){
TCPServerThread();
RUDPClientThread();
}