mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-04-03 06:16:15 +00:00
Compare commits
13 Commits
fix-resour
...
ipv6-suppo
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f65fbf60fe | ||
|
|
188a31c69e | ||
|
|
caab92375d | ||
|
|
b76930b0bd | ||
|
|
96d579f64b | ||
|
|
ba35d039ae | ||
|
|
3535923f40 | ||
|
|
3ca6e7fd3d | ||
|
|
7c24020124 | ||
|
|
88a9d4a1b1 | ||
|
|
4a5728d421 | ||
|
|
137d9dd1e2 | ||
|
|
7b733bf8eb |
@@ -15,6 +15,8 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
|
|||||||
file(GLOB source_files "src/*.cpp" "src/*/*.cpp" "src/*/*.hpp" "include/*.h" "include/*/*.h" "include/*/*/*.h" "include/*.hpp" "include/*/*.hpp" "include/*/*/*.hpp")
|
file(GLOB source_files "src/*.cpp" "src/*/*.cpp" "src/*/*.hpp" "include/*.h" "include/*/*.h" "include/*/*/*.h" "include/*.hpp" "include/*/*.hpp" "include/*/*/*.hpp")
|
||||||
find_package(httplib CONFIG REQUIRED)
|
find_package(httplib CONFIG REQUIRED)
|
||||||
find_package(nlohmann_json CONFIG REQUIRED)
|
find_package(nlohmann_json CONFIG REQUIRED)
|
||||||
|
find_package(asio CONFIG REQUIRED)
|
||||||
|
find_package(fmt CONFIG REQUIRED)
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME} ${source_files})
|
add_executable(${PROJECT_NAME} ${source_files})
|
||||||
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "BeamMP-Launcher")
|
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "BeamMP-Launcher")
|
||||||
@@ -23,15 +25,15 @@ if (WIN32)
|
|||||||
find_package(ZLIB REQUIRED)
|
find_package(ZLIB REQUIRED)
|
||||||
find_package(OpenSSL REQUIRED)
|
find_package(OpenSSL REQUIRED)
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||||
ZLIB::ZLIB OpenSSL::SSL OpenSSL::Crypto ws2_32 httplib::httplib nlohmann_json::nlohmann_json)
|
ZLIB::ZLIB OpenSSL::SSL OpenSSL::Crypto ws2_32 httplib::httplib nlohmann_json::nlohmann_json asio::asio fmt::fmt)
|
||||||
elseif (LINUX)
|
elseif (LINUX)
|
||||||
find_package(ZLIB REQUIRED)
|
find_package(ZLIB REQUIRED)
|
||||||
find_package(OpenSSL REQUIRED)
|
find_package(OpenSSL REQUIRED)
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||||
ZLIB::ZLIB OpenSSL::SSL OpenSSL::Crypto)
|
ZLIB::ZLIB OpenSSL::SSL OpenSSL::Crypto asio::asio fmt::fmt)
|
||||||
else(WIN32) #MINGW
|
elseif (WIN32) #MINGW
|
||||||
add_definitions("-D_WIN32_WINNT=0x0600")
|
add_definitions("-D_WIN32_WINNT=0x0600")
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -s --static")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -s --static")
|
||||||
target_link_libraries(${PROJECT_NAME} ssl crypto ws2_32 ssp crypt32 z)
|
target_link_libraries(${PROJECT_NAME} ssl crypto ws2_32 ssp crypt32 z asio::asio fmt::fmt)
|
||||||
endif(WIN32)
|
endif(WIN32)
|
||||||
target_include_directories(${PROJECT_NAME} PRIVATE "include")
|
target_include_directories(${PROJECT_NAME} PRIVATE "include")
|
||||||
|
|||||||
11
include/Helpers.h
Normal file
11
include/Helpers.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#include <span>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
using ByteSpan = std::span<const char>;
|
||||||
|
|
||||||
|
std::string bytespan_to_string(ByteSpan span);
|
||||||
|
|
||||||
|
std::vector<char> strtovec(std::string_view str);
|
||||||
@@ -7,6 +7,10 @@
|
|||||||
///
|
///
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Helpers.h"
|
||||||
|
#include "asio/io_context.hpp"
|
||||||
|
#include "asio/ip/address.hpp"
|
||||||
|
#include <span>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
@@ -14,8 +18,13 @@
|
|||||||
#include <bits/types/siginfo_t.h>
|
#include <bits/types/siginfo_t.h>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <sys/ucontext.h>
|
#include <sys/ucontext.h>
|
||||||
|
#include <vector>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <asio.hpp>
|
||||||
|
|
||||||
|
extern asio::io_context io;
|
||||||
|
|
||||||
void NetReset();
|
void NetReset();
|
||||||
extern bool Dev;
|
extern bool Dev;
|
||||||
extern int ping;
|
extern int ping;
|
||||||
@@ -27,8 +36,8 @@ extern int LastPort;
|
|||||||
extern bool ModLoaded;
|
extern bool ModLoaded;
|
||||||
extern bool Terminate;
|
extern bool Terminate;
|
||||||
extern int DEFAULT_PORT;
|
extern int DEFAULT_PORT;
|
||||||
extern uint64_t UDPSock;
|
extern std::shared_ptr<asio::ip::udp::socket> UDPSock;
|
||||||
extern uint64_t TCPSock;
|
extern std::shared_ptr<asio::ip::tcp::socket> TCPSock;
|
||||||
extern std::string Branch;
|
extern std::string Branch;
|
||||||
extern bool TCPTerminate;
|
extern bool TCPTerminate;
|
||||||
extern std::string LastIP;
|
extern std::string LastIP;
|
||||||
@@ -37,18 +46,21 @@ extern std::string UlStatus;
|
|||||||
extern std::string PublicKey;
|
extern std::string PublicKey;
|
||||||
extern std::string PrivateKey;
|
extern std::string PrivateKey;
|
||||||
extern std::string ListOfMods;
|
extern std::string ListOfMods;
|
||||||
int KillSocket(uint64_t Dead);
|
void KillSocket(std::shared_ptr<asio::ip::tcp::socket>& Dead);
|
||||||
|
void KillSocket(std::shared_ptr<asio::ip::udp::socket>& Dead);
|
||||||
|
void KillSocket(asio::ip::tcp::socket& Dead);
|
||||||
|
void KillSocket(asio::ip::udp::socket& Dead);
|
||||||
void UUl(const std::string& R);
|
void UUl(const std::string& R);
|
||||||
void UDPSend(std::string Data);
|
void UDPSend(const std::vector<char>& Data);
|
||||||
bool CheckBytes(int32_t Bytes);
|
bool CheckBytes(int32_t Bytes);
|
||||||
void GameSend(std::string_view Data);
|
void GameSend(std::string_view Data);
|
||||||
void SendLarge(std::string Data);
|
void SendLarge(const std::vector<char>& Data);
|
||||||
std::string TCPRcv(uint64_t Sock);
|
std::string TCPRcv(asio::ip::tcp::socket& Sock);
|
||||||
void SyncResources(uint64_t TCPSock);
|
void SyncResources(asio::ip::tcp::socket& TCPSock);
|
||||||
std::string GetAddr(const std::string& IP);
|
std::string GetAddr(const std::string& IP);
|
||||||
void ServerParser(std::string_view Data);
|
void ServerParser(std::string_view Data);
|
||||||
std::string Login(const std::string& fields);
|
std::string Login(const std::string& fields);
|
||||||
void TCPSend(const std::string& Data, uint64_t Sock);
|
void TCPSend(const std::vector<char>& Data, asio::ip::tcp::socket& Sock);
|
||||||
void TCPClientMain(const std::string& IP, int Port);
|
void TCPClientMain(asio::ip::tcp::socket&& Socket);
|
||||||
void UDPClientMain(const std::string& IP, int Port);
|
void UDPClientMain(asio::ip::address addr, uint16_t port);
|
||||||
void TCPGameServer(const std::string& IP, int Port);
|
void TCPGameServer(asio::ip::tcp::socket&& Socket);
|
||||||
|
|||||||
6
include/NetworkHelpers.h
Normal file
6
include/NetworkHelpers.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <asio.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void ReceiveFromGame(asio::ip::tcp::socket& socket, std::vector<char>& out_data);
|
||||||
@@ -19,11 +19,12 @@ std::vector<char> Comp(std::span<const char> input) {
|
|||||||
auto max_size = compressBound(input.size());
|
auto max_size = compressBound(input.size());
|
||||||
std::vector<char> output(max_size);
|
std::vector<char> output(max_size);
|
||||||
uLongf output_size = output.size();
|
uLongf output_size = output.size();
|
||||||
int res = compress(
|
int res = compress2(
|
||||||
reinterpret_cast<Bytef*>(output.data()),
|
reinterpret_cast<Bytef*>(output.data()),
|
||||||
&output_size,
|
&output_size,
|
||||||
reinterpret_cast<const Bytef*>(input.data()),
|
reinterpret_cast<const Bytef*>(input.data()),
|
||||||
static_cast<uLongf>(input.size()));
|
static_cast<uLongf>(input.size()),
|
||||||
|
3);
|
||||||
if (res != Z_OK) {
|
if (res != Z_OK) {
|
||||||
error("zlib compress() failed: " + std::to_string(res));
|
error("zlib compress() failed: " + std::to_string(res));
|
||||||
throw std::runtime_error("zlib compress() failed");
|
throw std::runtime_error("zlib compress() failed");
|
||||||
|
|||||||
@@ -10,6 +10,8 @@
|
|||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
#include "vdf_parser.hpp"
|
#include "vdf_parser.hpp"
|
||||||
|
#include <cerrno>
|
||||||
|
#include <cstring>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <spawn.h>
|
#include <spawn.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@@ -51,7 +53,6 @@ std::string GetGamePath() {
|
|||||||
std::string Ver = CheckVer(GetGameDir());
|
std::string Ver = CheckVer(GetGameDir());
|
||||||
Ver = Ver.substr(0, Ver.find('.', Ver.find('.') + 1));
|
Ver = Ver.substr(0, Ver.find('.', Ver.find('.') + 1));
|
||||||
Path += Ver + "\\";
|
Path += Ver + "\\";
|
||||||
info("Game user path: '" + Path + "'");
|
|
||||||
return Path;
|
return Path;
|
||||||
}
|
}
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
@@ -64,7 +65,6 @@ std::string GetGamePath() {
|
|||||||
std::string Ver = CheckVer(GetGameDir());
|
std::string Ver = CheckVer(GetGameDir());
|
||||||
Ver = Ver.substr(0, Ver.find('.', Ver.find('.') + 1));
|
Ver = Ver.substr(0, Ver.find('.', Ver.find('.') + 1));
|
||||||
Path += Ver + "/";
|
Path += Ver + "/";
|
||||||
info("Game user path: '" + Path + "'");
|
|
||||||
return Path;
|
return Path;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -92,11 +92,27 @@ void StartGame(std::string Dir) {
|
|||||||
}
|
}
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
void StartGame(std::string Dir) {
|
void StartGame(std::string Dir) {
|
||||||
int status;
|
|
||||||
std::string filename = (Dir + "/BinLinux/BeamNG.drive.x64");
|
std::string filename = (Dir + "/BinLinux/BeamNG.drive.x64");
|
||||||
char* argv[] = { filename.data(), NULL };
|
char* argv[] = { filename.data(), NULL };
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int result = posix_spawn(&pid, filename.c_str(), NULL, NULL, argv, environ);
|
|
||||||
|
posix_spawn_file_actions_t file_actions;
|
||||||
|
auto status = posix_spawn_file_actions_init(&file_actions);
|
||||||
|
// disable stdout
|
||||||
|
if (status != 0) {
|
||||||
|
error(std::string("posix_spawn_file_actions_init failed: ") + std::strerror(errno));
|
||||||
|
}
|
||||||
|
status = posix_spawn_file_actions_addclose(&file_actions, STDOUT_FILENO);
|
||||||
|
if (status != 0) {
|
||||||
|
error(std::string("posix_spawn_file_actions_addclose for STDOUT failed: ") + std::strerror(errno));
|
||||||
|
}
|
||||||
|
status = posix_spawn_file_actions_addclose(&file_actions, STDERR_FILENO);
|
||||||
|
if (status != 0) {
|
||||||
|
error(std::string("posix_spawn_file_actions_addclose for STDERR failed: ") + std::strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
// launch the game
|
||||||
|
int result = posix_spawn(&pid, filename.c_str(), &file_actions, NULL, argv, environ);
|
||||||
|
|
||||||
if (result != 0) {
|
if (result != 0) {
|
||||||
error("Failed to Launch the game! launcher closing soon");
|
error("Failed to Launch the game! launcher closing soon");
|
||||||
@@ -106,6 +122,11 @@ void StartGame(std::string Dir) {
|
|||||||
error("Game Closed! launcher closing soon");
|
error("Game Closed! launcher closing soon");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
status = posix_spawn_file_actions_destroy(&file_actions);
|
||||||
|
if (status != 0) {
|
||||||
|
warn(std::string("posix_spawn_file_actions_destroy failed: ") + std::strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
|
|||||||
9
src/Helpers.cpp
Normal file
9
src/Helpers.cpp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#include "Helpers.h"
|
||||||
|
|
||||||
|
std::string bytespan_to_string(ByteSpan span) {
|
||||||
|
return std::string(span.data(), span.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<char> strtovec(std::string_view str) {
|
||||||
|
return std::vector<char>(str.begin(), str.end());
|
||||||
|
}
|
||||||
@@ -50,35 +50,35 @@ void addToLog(const std::string& Line) {
|
|||||||
}
|
}
|
||||||
void info(const std::string& toPrint) {
|
void info(const std::string& toPrint) {
|
||||||
std::string Print = getDate() + "[INFO] " + toPrint + "\n";
|
std::string Print = getDate() + "[INFO] " + toPrint + "\n";
|
||||||
std::cout << Print;
|
std::cout << Print << std::flush;
|
||||||
addToLog(Print);
|
addToLog(Print);
|
||||||
}
|
}
|
||||||
void debug(const std::string& toPrint) {
|
void debug(const std::string& toPrint) {
|
||||||
if (!Dev)
|
if (!Dev)
|
||||||
return;
|
return;
|
||||||
std::string Print = getDate() + "[DEBUG] " + toPrint + "\n";
|
std::string Print = getDate() + "[DEBUG] " + toPrint + "\n";
|
||||||
std::cout << Print;
|
std::cout << Print << std::flush;
|
||||||
addToLog(Print);
|
addToLog(Print);
|
||||||
}
|
}
|
||||||
void warn(const std::string& toPrint) {
|
void warn(const std::string& toPrint) {
|
||||||
std::string Print = getDate() + "[WARN] " + toPrint + "\n";
|
std::string Print = getDate() + "[WARN] " + toPrint + "\n";
|
||||||
std::cout << Print;
|
std::cout << Print << std::flush;
|
||||||
addToLog(Print);
|
addToLog(Print);
|
||||||
}
|
}
|
||||||
void error(const std::string& toPrint) {
|
void error(const std::string& toPrint) {
|
||||||
std::string Print = getDate() + "[ERROR] " + toPrint + "\n";
|
std::string Print = getDate() + "[ERROR] " + toPrint + "\n";
|
||||||
std::cout << Print;
|
std::cout << Print << std::flush;
|
||||||
addToLog(Print);
|
addToLog(Print);
|
||||||
}
|
}
|
||||||
void fatal(const std::string& toPrint) {
|
void fatal(const std::string& toPrint) {
|
||||||
std::string Print = getDate() + "[FATAL] " + toPrint + "\n";
|
std::string Print = getDate() + "[FATAL] " + toPrint + "\n";
|
||||||
std::cout << Print;
|
std::cout << Print << std::flush;
|
||||||
addToLog(Print);
|
addToLog(Print);
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||||
_Exit(-1);
|
_Exit(-1);
|
||||||
}
|
}
|
||||||
void except(const std::string& toPrint) {
|
void except(const std::string& toPrint) {
|
||||||
std::string Print = getDate() + "[EXCEP] " + toPrint + "\n";
|
std::string Print = getDate() + "[EXCEP] " + toPrint + "\n";
|
||||||
std::cout << Print;
|
std::cout << Print << std::flush;
|
||||||
addToLog(Print);
|
addToLog(Print);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,18 +7,17 @@
|
|||||||
///
|
///
|
||||||
#include "Http.h"
|
#include "Http.h"
|
||||||
#include "Network/network.hpp"
|
#include "Network/network.hpp"
|
||||||
|
#include "NetworkHelpers.h"
|
||||||
#include "Security/Init.h"
|
#include "Security/Init.h"
|
||||||
|
#include <asio/io_context.hpp>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <optional>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#if defined(_WIN32)
|
#if defined(__linux__)
|
||||||
#include <winsock2.h>
|
|
||||||
#include <ws2tcpip.h>
|
|
||||||
#elif defined(__linux__)
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <spawn.h>
|
#include <spawn.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -27,6 +26,7 @@
|
|||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
#include "Startup.h"
|
#include "Startup.h"
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
|
#include <fmt/format.h>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@@ -44,65 +44,117 @@ std::string MStatus;
|
|||||||
bool ModLoaded;
|
bool ModLoaded;
|
||||||
int ping = -1;
|
int ping = -1;
|
||||||
|
|
||||||
void StartSync(const std::string& Data) {
|
asio::io_context io {};
|
||||||
std::string IP = GetAddr(Data.substr(1, Data.find(':') - 1));
|
|
||||||
if (IP.find('.') == -1) {
|
static asio::ip::tcp::socket ResolveAndConnect(const std::string& host_port_string) {
|
||||||
if (IP == "DNS")
|
|
||||||
UlStatus = "UlConnection Failed! (DNS Lookup Failed)";
|
using namespace asio;
|
||||||
else
|
ip::tcp::resolver resolver(io);
|
||||||
UlStatus = "UlConnection Failed! (WSA failed to start)";
|
asio::error_code ec;
|
||||||
ListOfMods = "-";
|
auto port = host_port_string.substr(host_port_string.find_last_of(':') + 1);
|
||||||
Terminate = true;
|
auto host = host_port_string.substr(0, host_port_string.find_last_of(':'));
|
||||||
return;
|
auto resolved = resolver.resolve(host, port, ec);
|
||||||
|
if (ec) {
|
||||||
|
::error(fmt::format("Failed to resolve '[{}]:{}': {}", host, port, ec.message()));
|
||||||
|
throw std::runtime_error(fmt::format("Failed to resolve '{}': {}", host_port_string, ec.message()));
|
||||||
}
|
}
|
||||||
CheckLocalKey();
|
bool connected = false;
|
||||||
|
|
||||||
UlStatus = "UlLoading...";
|
UlStatus = "UlLoading...";
|
||||||
TCPTerminate = false;
|
|
||||||
Terminate = false;
|
for (const auto& addr : resolved) {
|
||||||
ConfList->clear();
|
try {
|
||||||
ping = -1;
|
info(fmt::format("Resolved and connected to '[{}]:{}'",
|
||||||
std::thread GS(TCPGameServer, IP, std::stoi(Data.substr(Data.find(':') + 1)));
|
addr.endpoint().address().to_string(),
|
||||||
GS.detach();
|
addr.endpoint().port()));
|
||||||
info("Connecting to server");
|
ip::tcp::socket socket(io);
|
||||||
|
socket.connect(addr);
|
||||||
|
// done, connected fine
|
||||||
|
return socket;
|
||||||
|
} catch (...) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw std::runtime_error(fmt::format("Failed to connect to '{}'; connection refused", host_port_string));
|
||||||
|
}
|
||||||
|
|
||||||
|
void StartSync(const std::string& Data) {
|
||||||
|
try {
|
||||||
|
auto Socket = ResolveAndConnect(Data.substr(1));
|
||||||
|
ListOfMods = "-";
|
||||||
|
CheckLocalKey();
|
||||||
|
TCPTerminate = false;
|
||||||
|
Terminate = false;
|
||||||
|
ConfList->clear();
|
||||||
|
ping = -1;
|
||||||
|
std::thread GS(TCPGameServer, std::move(Socket));
|
||||||
|
GS.detach();
|
||||||
|
info("Connecting to server");
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
UlStatus = "UlConnection Failed!";
|
||||||
|
error(fmt::format("Client: connect failed! Error: {}", e.what()));
|
||||||
|
WSACleanup();
|
||||||
|
Terminate = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsAllowedLink(const std::string& Link) {
|
bool IsAllowedLink(const std::string& Link) {
|
||||||
std::regex link_pattern(R"(https:\/\/(?:\w+)?(?:\.)?(?:beammp\.com|discord\.gg))");
|
std::vector<std::string> allowed_links = {
|
||||||
std::smatch link_match;
|
R"(patreon\.com\/beammp$)",
|
||||||
return std::regex_search(Link, link_match, link_pattern) && link_match.position() == 0;
|
R"(discord\.gg\/beammp$)",
|
||||||
|
R"(forum\.beammp\.com$)",
|
||||||
|
R"(beammp\.com$)",
|
||||||
|
R"(patreon\.com\/beammp\/$)",
|
||||||
|
R"(discord\.gg\/beammp\/$)",
|
||||||
|
R"(forum\.beammp\.com\/$)",
|
||||||
|
R"(beammp\.com\/$)",
|
||||||
|
R"(docs\.beammp\.com$)",
|
||||||
|
R"(wiki\.beammp\.com$)",
|
||||||
|
R"(docs\.beammp\.com\/$)",
|
||||||
|
R"(wiki\.beammp\.com\/$)",
|
||||||
|
R"(docs\.beammp\.com\/.*$)",
|
||||||
|
R"(wiki\.beammp\.com\/.*$)",
|
||||||
|
};
|
||||||
|
for (const auto& allowed_link : allowed_links) {
|
||||||
|
if (std::regex_match(Link, std::regex(std::string(R"(^http(s)?:\/\/)") + allowed_link))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Parse(std::string Data, SOCKET CSocket) {
|
void Parse(std::span<char> InData, asio::ip::tcp::socket& CSocket) {
|
||||||
char Code = Data.at(0), SubCode = 0;
|
std::string OutData;
|
||||||
if (Data.length() > 1)
|
char Code = InData[0], SubCode = 0;
|
||||||
SubCode = Data.at(1);
|
if (InData.size() > 1)
|
||||||
|
SubCode = InData[1];
|
||||||
switch (Code) {
|
switch (Code) {
|
||||||
case 'A':
|
case 'A':
|
||||||
Data = Data.substr(0, 1);
|
OutData = "A";
|
||||||
break;
|
break;
|
||||||
case 'B':
|
case 'B':
|
||||||
NetReset();
|
NetReset();
|
||||||
Terminate = true;
|
Terminate = true;
|
||||||
TCPTerminate = true;
|
TCPTerminate = true;
|
||||||
Data = Code + HTTP::Get("https://backend.beammp.com/servers-info");
|
OutData = Code + HTTP::Get("https://backend.beammp.com/servers-info");
|
||||||
break;
|
break;
|
||||||
case 'C':
|
case 'C':
|
||||||
ListOfMods.clear();
|
ListOfMods.clear();
|
||||||
StartSync(Data);
|
StartSync(std::string(InData.data(), InData.size()));
|
||||||
while (ListOfMods.empty() && !Terminate) {
|
while (ListOfMods.empty() && !Terminate) {
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
}
|
}
|
||||||
if (ListOfMods == "-")
|
if (ListOfMods == "-")
|
||||||
Data = "L";
|
OutData = "L";
|
||||||
else
|
else
|
||||||
Data = "L" + ListOfMods;
|
OutData = "L" + ListOfMods;
|
||||||
break;
|
break;
|
||||||
case 'O': // open default browser with URL
|
case 'O': // open default browser with URL
|
||||||
if (IsAllowedLink(Data.substr(1))) {
|
if (IsAllowedLink(bytespan_to_string(InData.subspan(1)))) {
|
||||||
#if defined(__linux)
|
#if defined(__linux)
|
||||||
if (char* browser = getenv("BROWSER"); browser != nullptr && !std::string_view(browser).empty()) {
|
if (char* browser = getenv("BROWSER"); browser != nullptr && !std::string_view(browser).empty()) {
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
auto arg = Data.substr(1);
|
auto arg = bytespan_to_string(InData.subspan(1));
|
||||||
char* argv[] = { browser, arg.data() };
|
char* argv[] = { browser, arg.data() };
|
||||||
auto status = posix_spawn(&pid, browser, nullptr, nullptr, argv, environ);
|
auto status = posix_spawn(&pid, browser, nullptr, nullptr, argv, environ);
|
||||||
if (status == 0) {
|
if (status == 0) {
|
||||||
@@ -114,27 +166,27 @@ void Parse(std::string Data, SOCKET CSocket) {
|
|||||||
error(std::string("posix_spawn: ") + strerror(status));
|
error(std::string("posix_spawn: ") + strerror(status));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
error("Failed to open the following link in the browser because the $BROWSER environment variable is not set: " + Data.substr(1));
|
error("Failed to open the following link in the browser because the $BROWSER environment variable is not set: " + bytespan_to_string(InData.subspan(1)));
|
||||||
}
|
}
|
||||||
#elif defined(WIN32)
|
#elif defined(WIN32)
|
||||||
ShellExecuteA(nullptr, "open", Data.substr(1).c_str(), nullptr, nullptr, SW_SHOW); /// TODO: Look at when working on linux port
|
ShellExecuteA(nullptr, "open", InData.subspan(1).data(), nullptr, nullptr, SW_SHOW); /// TODO: Look at when working on linux port
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
info("Opening Link \"" + Data.substr(1) + "\"");
|
info("Opening Link \"" + bytespan_to_string(InData.subspan(1)) + "\"");
|
||||||
}
|
}
|
||||||
Data.clear();
|
OutData.clear();
|
||||||
break;
|
break;
|
||||||
case 'P':
|
case 'P':
|
||||||
Data = Code + std::to_string(ProxyPort);
|
OutData = Code + std::to_string(ProxyPort);
|
||||||
break;
|
break;
|
||||||
case 'U':
|
case 'U':
|
||||||
if (SubCode == 'l')
|
if (SubCode == 'l')
|
||||||
Data = UlStatus;
|
OutData = UlStatus;
|
||||||
if (SubCode == 'p') {
|
if (SubCode == 'p') {
|
||||||
if (ping > 800) {
|
if (ping > 800) {
|
||||||
Data = "Up-2";
|
OutData = "Up-2";
|
||||||
} else
|
} else
|
||||||
Data = "Up" + std::to_string(ping);
|
OutData = "Up" + std::to_string(ping);
|
||||||
}
|
}
|
||||||
if (!SubCode) {
|
if (!SubCode) {
|
||||||
std::string Ping;
|
std::string Ping;
|
||||||
@@ -142,11 +194,11 @@ void Parse(std::string Data, SOCKET CSocket) {
|
|||||||
Ping = "-2";
|
Ping = "-2";
|
||||||
else
|
else
|
||||||
Ping = std::to_string(ping);
|
Ping = std::to_string(ping);
|
||||||
Data = std::string(UlStatus) + "\n" + "Up" + Ping;
|
OutData = std::string(UlStatus) + "\n" + "Up" + Ping;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'M':
|
case 'M':
|
||||||
Data = MStatus;
|
OutData = MStatus;
|
||||||
break;
|
break;
|
||||||
case 'Q':
|
case 'Q':
|
||||||
if (SubCode == 'S') {
|
if (SubCode == 'S') {
|
||||||
@@ -157,17 +209,19 @@ void Parse(std::string Data, SOCKET CSocket) {
|
|||||||
}
|
}
|
||||||
if (SubCode == 'G')
|
if (SubCode == 'G')
|
||||||
exit(2);
|
exit(2);
|
||||||
Data.clear();
|
OutData.clear();
|
||||||
break;
|
break;
|
||||||
case 'R': // will send mod name
|
case 'R': // will send mod name
|
||||||
if (ConfList->find(Data) == ConfList->end()) {
|
{
|
||||||
ConfList->insert(Data);
|
auto str = bytespan_to_string(InData);
|
||||||
|
if (ConfList->find(str) == ConfList->end()) {
|
||||||
|
ConfList->insert(str);
|
||||||
ModLoaded = true;
|
ModLoaded = true;
|
||||||
}
|
}
|
||||||
Data.clear();
|
OutData.clear();
|
||||||
break;
|
} break;
|
||||||
case 'Z':
|
case 'Z':
|
||||||
Data = "Z" + GetVer();
|
OutData = "Z" + GetVer();
|
||||||
break;
|
break;
|
||||||
case 'N':
|
case 'N':
|
||||||
if (SubCode == 'c') {
|
if (SubCode == 'c') {
|
||||||
@@ -180,63 +234,39 @@ void Parse(std::string Data, SOCKET CSocket) {
|
|||||||
if (!UserRole.empty()) {
|
if (!UserRole.empty()) {
|
||||||
Auth["role"] = UserRole;
|
Auth["role"] = UserRole;
|
||||||
}
|
}
|
||||||
Data = "N" + Auth.dump();
|
OutData = "N" + Auth.dump();
|
||||||
} else {
|
} else {
|
||||||
Data = "N" + Login(Data.substr(Data.find(':') + 1));
|
auto indata_str = bytespan_to_string(InData);
|
||||||
|
OutData = "N" + Login(indata_str.substr(indata_str.find(':') + 1));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Data.clear();
|
OutData.clear();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!Data.empty() && CSocket != -1) {
|
if (!OutData.empty() && CSocket.is_open()) {
|
||||||
int res = send(CSocket, (Data + "\n").c_str(), int(Data.size()) + 1, 0);
|
uint32_t DataSize = OutData.size();
|
||||||
if (res < 0) {
|
std::vector<char> ToSend(sizeof(DataSize) + OutData.size());
|
||||||
debug("(Core) send failed with error: " + std::to_string(WSAGetLastError()));
|
std::copy_n(reinterpret_cast<char*>(&DataSize), sizeof(DataSize), ToSend.begin());
|
||||||
|
std::copy_n(OutData.data(), OutData.size(), ToSend.begin() + sizeof(DataSize));
|
||||||
|
asio::error_code ec;
|
||||||
|
asio::write(CSocket, asio::buffer(ToSend), ec);
|
||||||
|
if (ec) {
|
||||||
|
debug(fmt::format("(Core) send failed with error: {}", ec.message()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void GameHandler(SOCKET Client) {
|
void GameHandler(asio::ip::tcp::socket& Client) {
|
||||||
|
std::vector<char> data {};
|
||||||
int32_t Size, Temp, Rcv;
|
|
||||||
char Header[10] = { 0 };
|
|
||||||
do {
|
do {
|
||||||
Rcv = 0;
|
try {
|
||||||
do {
|
ReceiveFromGame(Client, data);
|
||||||
Temp = recv(Client, &Header[Rcv], 1, 0);
|
Parse(data, Client);
|
||||||
if (Temp < 1)
|
} catch (const std::exception& e) {
|
||||||
break;
|
error(std::string("Error while receiving from game: ") + e.what());
|
||||||
if (!isdigit(Header[Rcv]) && Header[Rcv] != '>') {
|
|
||||||
error("(Core) Invalid lua communication");
|
|
||||||
KillSocket(Client);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} while (Header[Rcv++] != '>');
|
|
||||||
if (Temp < 1)
|
|
||||||
break;
|
|
||||||
if (std::from_chars(Header, &Header[Rcv], Size).ptr[0] != '>') {
|
|
||||||
debug("(Core) Invalid lua Header -> " + std::string(Header, Rcv));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
std::string Ret(Size, 0);
|
} while (true);
|
||||||
Rcv = 0;
|
|
||||||
|
|
||||||
do {
|
|
||||||
Temp = recv(Client, &Ret[Rcv], Size - Rcv, 0);
|
|
||||||
if (Temp < 1)
|
|
||||||
break;
|
|
||||||
Rcv += Temp;
|
|
||||||
} while (Rcv < Size);
|
|
||||||
if (Temp < 1)
|
|
||||||
break;
|
|
||||||
|
|
||||||
Parse(Ret, Client);
|
|
||||||
} while (Temp > 0);
|
|
||||||
if (Temp == 0) {
|
|
||||||
debug("(Core) Connection closing");
|
|
||||||
} else {
|
|
||||||
debug("(Core) recv failed with error: " + std::to_string(WSAGetLastError()));
|
|
||||||
}
|
|
||||||
NetReset();
|
NetReset();
|
||||||
KillSocket(Client);
|
KillSocket(Client);
|
||||||
}
|
}
|
||||||
@@ -252,63 +282,52 @@ void localRes() {
|
|||||||
}
|
}
|
||||||
void CoreMain() {
|
void CoreMain() {
|
||||||
debug("Core Network on start!");
|
debug("Core Network on start!");
|
||||||
SOCKET LSocket, CSocket;
|
|
||||||
struct addrinfo* res = nullptr;
|
|
||||||
struct addrinfo hints { };
|
|
||||||
int iRes;
|
|
||||||
#ifdef _WIN32
|
|
||||||
WSADATA wsaData;
|
|
||||||
iRes = WSAStartup(514, &wsaData); // 2.2
|
|
||||||
if (iRes)
|
|
||||||
debug("WSAStartup failed with error: " + std::to_string(iRes));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ZeroMemory(&hints, sizeof(hints));
|
asio::ip::tcp::endpoint listen_ep(asio::ip::address::from_string("0.0.0.0"), static_cast<uint16_t>(DEFAULT_PORT));
|
||||||
|
asio::ip::tcp::socket LSocket(io);
|
||||||
|
asio::error_code ec;
|
||||||
|
LSocket.open(listen_ep.protocol(), ec);
|
||||||
|
if (ec) {
|
||||||
|
::error(fmt::format("Failed to open core socket: {}", ec.message()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
asio::ip::tcp::socket::linger linger_opt {};
|
||||||
|
linger_opt.enabled(false);
|
||||||
|
LSocket.set_option(linger_opt, ec);
|
||||||
|
if (ec) {
|
||||||
|
::error(fmt::format("Failed to set up listening core socket to not linger / reuse address. "
|
||||||
|
"This may cause the core socket to refuse to bind(). Error: {}",
|
||||||
|
ec.message()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
asio::ip::tcp::socket::reuse_address reuse_opt { true };
|
||||||
|
LSocket.set_option(reuse_opt, ec);
|
||||||
|
if (ec) {
|
||||||
|
::error(fmt::format("Failed to set up listening core socket to not linger / reuse address. "
|
||||||
|
"This may cause the core socket to refuse to bind(). Error: {}",
|
||||||
|
ec.message()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
hints.ai_family = AF_INET;
|
auto acceptor = asio::ip::tcp::acceptor(io, listen_ep);
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
acceptor.listen(asio::ip::tcp::socket::max_listen_connections, ec);
|
||||||
hints.ai_protocol = IPPROTO_TCP;
|
if (ec) {
|
||||||
hints.ai_flags = AI_PASSIVE;
|
::error(fmt::format("listen() failed, which is needed for the launcher to operate. Error: {}",
|
||||||
iRes = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT).c_str(), &hints, &res);
|
ec.message()));
|
||||||
if (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("(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("(Core) bind failed with error: " + std::to_string(WSAGetLastError()));
|
|
||||||
freeaddrinfo(res);
|
|
||||||
KillSocket(LSocket);
|
|
||||||
WSACleanup();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
iRes = listen(LSocket, SOMAXCONN);
|
|
||||||
if (iRes == SOCKET_ERROR) {
|
|
||||||
debug("(Core) listen failed with error: " + std::to_string(WSAGetLastError()));
|
|
||||||
freeaddrinfo(res);
|
|
||||||
KillSocket(LSocket);
|
|
||||||
WSACleanup();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
CSocket = accept(LSocket, nullptr, nullptr);
|
auto CSocket = acceptor.accept(ec);
|
||||||
if (CSocket == -1) {
|
if (ec) {
|
||||||
error("(Core) accept failed with error: " + std::to_string(WSAGetLastError()));
|
error(fmt::format("(Core) accept failed with error: {}", ec.message()));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
localRes();
|
localRes();
|
||||||
info("Game Connected!");
|
info("Game Connected!");
|
||||||
GameHandler(CSocket);
|
GameHandler(CSocket);
|
||||||
warn("Game Reconnecting...");
|
warn("Game Reconnecting...");
|
||||||
} while (CSocket);
|
} while (LSocket.is_open());
|
||||||
KillSocket(LSocket);
|
KillSocket(LSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,13 @@
|
|||||||
///
|
///
|
||||||
/// Created by Anonymous275 on 7/25/2020
|
/// Created by Anonymous275 on 7/25/2020
|
||||||
///
|
///
|
||||||
|
#include "Helpers.h"
|
||||||
#include "Network/network.hpp"
|
#include "Network/network.hpp"
|
||||||
|
#include "NetworkHelpers.h"
|
||||||
|
#include "asio/socket_base.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <span>
|
||||||
|
#include <vector>
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
@@ -22,6 +28,7 @@
|
|||||||
|
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
|
#include <fmt/format.h>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@@ -29,20 +36,31 @@
|
|||||||
std::chrono::time_point<std::chrono::high_resolution_clock> PingStart, PingEnd;
|
std::chrono::time_point<std::chrono::high_resolution_clock> PingStart, PingEnd;
|
||||||
bool GConnected = false;
|
bool GConnected = false;
|
||||||
bool CServer = true;
|
bool CServer = true;
|
||||||
SOCKET CSocket = -1;
|
std::shared_ptr<asio::ip::tcp::socket> CSocket = nullptr;
|
||||||
SOCKET GSocket = -1;
|
std::shared_ptr<asio::ip::tcp::socket> GSocket = nullptr;
|
||||||
|
|
||||||
int KillSocket(uint64_t Dead) {
|
void KillSocket(std::shared_ptr<asio::ip::tcp::socket>& Dead) {
|
||||||
if (Dead == (SOCKET)-1) {
|
if (!Dead)
|
||||||
debug("Kill socket got -1 returning...");
|
return;
|
||||||
return 0;
|
asio::error_code ec;
|
||||||
}
|
Dead->shutdown(asio::socket_base::shutdown_both, ec);
|
||||||
shutdown(Dead, SD_BOTH);
|
}
|
||||||
int a = closesocket(Dead);
|
|
||||||
if (a != 0) {
|
void KillSocket(std::shared_ptr<asio::ip::udp::socket>& Dead) {
|
||||||
warn("Failed to close socket!");
|
if (!Dead)
|
||||||
}
|
return;
|
||||||
return a;
|
asio::error_code ec;
|
||||||
|
Dead->shutdown(asio::socket_base::shutdown_both, ec);
|
||||||
|
}
|
||||||
|
|
||||||
|
void KillSocket(asio::ip::tcp::socket& Dead) {
|
||||||
|
asio::error_code ec;
|
||||||
|
Dead.shutdown(asio::socket_base::shutdown_both, ec);
|
||||||
|
}
|
||||||
|
|
||||||
|
void KillSocket(asio::ip::udp::socket& Dead) {
|
||||||
|
asio::error_code ec;
|
||||||
|
Dead.shutdown(asio::socket_base::shutdown_both, ec);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CheckBytes(uint32_t Bytes) {
|
bool CheckBytes(uint32_t Bytes) {
|
||||||
@@ -56,42 +74,34 @@ bool CheckBytes(uint32_t Bytes) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameSend(std::string_view Data) {
|
void GameSend(std::string_view RawData) {
|
||||||
static std::mutex Lock;
|
static std::mutex Lock;
|
||||||
std::scoped_lock Guard(Lock);
|
std::scoped_lock Guard(Lock);
|
||||||
if (TCPTerminate || !GConnected || CSocket == -1)
|
if (TCPTerminate || !GConnected || CSocket == nullptr)
|
||||||
return;
|
return;
|
||||||
int32_t Size, Temp, Sent;
|
int32_t Size, Temp, Sent;
|
||||||
Size = int32_t(Data.size());
|
uint32_t DataSize = RawData.size();
|
||||||
|
std::vector<char> Data(sizeof(DataSize) + RawData.size());
|
||||||
|
std::copy_n(reinterpret_cast<char*>(&DataSize), sizeof(DataSize), Data.begin());
|
||||||
|
std::copy_n(RawData.data(), RawData.size(), Data.begin() + sizeof(DataSize));
|
||||||
|
Size = Data.size();
|
||||||
Sent = 0;
|
Sent = 0;
|
||||||
#ifdef DEBUG
|
|
||||||
if (Size > 1000) {
|
asio::error_code ec;
|
||||||
debug("Launcher -> game (" + std::to_string(Size) + ")");
|
asio::write(*CSocket, asio::buffer(Data), ec);
|
||||||
}
|
if (ec) {
|
||||||
#endif
|
debug(fmt::format("(TCP CB) recv failed with error: {}", ec.message()));
|
||||||
do {
|
KillSocket(TCPSock);
|
||||||
if (Sent > -1) {
|
Terminate = true;
|
||||||
Temp = send(CSocket, &Data[Sent], Size - Sent, 0);
|
|
||||||
}
|
|
||||||
if (!CheckBytes(Temp))
|
|
||||||
return;
|
|
||||||
Sent += Temp;
|
|
||||||
} while (Sent < Size);
|
|
||||||
// send separately to avoid an allocation for += "\n"
|
|
||||||
Temp = send(CSocket, "\n", 1, 0);
|
|
||||||
if (!CheckBytes(Temp)) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void ServerSend(std::string Data, bool Rel) {
|
|
||||||
|
void ServerSend(const std::vector<char>& Data, bool Rel) {
|
||||||
if (Terminate || Data.empty())
|
if (Terminate || Data.empty())
|
||||||
return;
|
return;
|
||||||
if (Data.find("Zp") != std::string::npos && Data.size() > 500) {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
char C = 0;
|
char C = 0;
|
||||||
bool Ack = false;
|
bool Ack = false;
|
||||||
int DLen = int(Data.length());
|
int DLen = int(Data.size());
|
||||||
if (DLen > 3)
|
if (DLen > 3)
|
||||||
C = Data.at(0);
|
C = Data.at(0);
|
||||||
if (C == 'O' || C == 'T')
|
if (C == 'O' || C == 'T')
|
||||||
@@ -103,18 +113,10 @@ void ServerSend(std::string Data, bool Rel) {
|
|||||||
if (Ack || Rel) {
|
if (Ack || Rel) {
|
||||||
if (Ack || DLen > 1000)
|
if (Ack || DLen > 1000)
|
||||||
SendLarge(Data);
|
SendLarge(Data);
|
||||||
else
|
else if (TCPSock)
|
||||||
TCPSend(Data, TCPSock);
|
TCPSend(Data, *TCPSock);
|
||||||
} else
|
} else
|
||||||
UDPSend(Data);
|
UDPSend(Data);
|
||||||
|
|
||||||
if (DLen > 1000) {
|
|
||||||
debug("(Launcher->Server) Bytes sent: " + std::to_string(Data.length()) + " : "
|
|
||||||
+ Data.substr(0, 10)
|
|
||||||
+ Data.substr(Data.length() - 10));
|
|
||||||
} else if (C == 'Z') {
|
|
||||||
// debug("(Game->Launcher) : " + Data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetReset() {
|
void NetReset() {
|
||||||
@@ -123,76 +125,23 @@ void NetReset() {
|
|||||||
Terminate = false;
|
Terminate = false;
|
||||||
UlStatus = "Ulstart";
|
UlStatus = "Ulstart";
|
||||||
MStatus = " ";
|
MStatus = " ";
|
||||||
if (UDPSock != (SOCKET)(-1)) {
|
if (UDPSock != nullptr) {
|
||||||
debug("Terminating UDP Socket : " + std::to_string(TCPSock));
|
KillSocket(*UDPSock);
|
||||||
KillSocket(UDPSock);
|
|
||||||
}
|
}
|
||||||
UDPSock = -1;
|
UDPSock = nullptr;
|
||||||
if (TCPSock != (SOCKET)(-1)) {
|
if (TCPSock != nullptr) {
|
||||||
debug("Terminating TCP Socket : " + std::to_string(TCPSock));
|
KillSocket(*TCPSock);
|
||||||
KillSocket(TCPSock);
|
|
||||||
}
|
}
|
||||||
TCPSock = -1;
|
TCPSock = nullptr;
|
||||||
if (GSocket != (SOCKET)(-1)) {
|
if (GSocket != nullptr) {
|
||||||
debug("Terminating GTCP Socket : " + std::to_string(GSocket));
|
KillSocket(*GSocket);
|
||||||
KillSocket(GSocket);
|
|
||||||
}
|
}
|
||||||
GSocket = -1;
|
GSocket = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
SOCKET SetupListener() {
|
|
||||||
if (GSocket != -1)
|
|
||||||
return GSocket;
|
|
||||||
struct addrinfo* result = nullptr;
|
|
||||||
struct addrinfo hints { };
|
|
||||||
int iRes;
|
|
||||||
#ifdef _WIN32
|
|
||||||
WSADATA wsaData;
|
|
||||||
iRes = WSAStartup(514, &wsaData); // 2.2
|
|
||||||
if (iRes != 0) {
|
|
||||||
error("(Proxy) WSAStartup failed with error: " + std::to_string(iRes));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ZeroMemory(&hints, sizeof(hints));
|
|
||||||
hints.ai_family = AF_INET;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
hints.ai_protocol = IPPROTO_TCP;
|
|
||||||
hints.ai_flags = AI_PASSIVE;
|
|
||||||
iRes = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT + 1).c_str(), &hints, &result);
|
|
||||||
if (iRes != 0) {
|
|
||||||
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("(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("(Proxy) bind failed with error: " + std::to_string(WSAGetLastError()));
|
|
||||||
freeaddrinfo(result);
|
|
||||||
KillSocket(GSocket);
|
|
||||||
WSACleanup();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
freeaddrinfo(result);
|
|
||||||
iRes = listen(GSocket, SOMAXCONN);
|
|
||||||
if (iRes == SOCKET_ERROR) {
|
|
||||||
error("(Proxy) listen failed with error: " + std::to_string(WSAGetLastError()));
|
|
||||||
KillSocket(GSocket);
|
|
||||||
WSACleanup();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return GSocket;
|
|
||||||
}
|
|
||||||
void AutoPing() {
|
void AutoPing() {
|
||||||
while (!Terminate) {
|
while (!Terminate) {
|
||||||
ServerSend("p", false);
|
ServerSend(strtovec("p"), false);
|
||||||
PingStart = std::chrono::high_resolution_clock::now();
|
PingStart = std::chrono::high_resolution_clock::now();
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
}
|
}
|
||||||
@@ -224,17 +173,47 @@ void ParserAsync(std::string_view Data) {
|
|||||||
void ServerParser(std::string_view Data) {
|
void ServerParser(std::string_view Data) {
|
||||||
ParserAsync(Data);
|
ParserAsync(Data);
|
||||||
}
|
}
|
||||||
void NetMain(const std::string& IP, int Port) {
|
void NetMain(asio::ip::address addr, uint16_t port) {
|
||||||
std::thread Ping(AutoPing);
|
std::thread Ping(AutoPing);
|
||||||
Ping.detach();
|
Ping.detach();
|
||||||
UDPClientMain(IP, Port);
|
UDPClientMain(addr, port);
|
||||||
CServer = true;
|
CServer = true;
|
||||||
Terminate = true;
|
Terminate = true;
|
||||||
info("Connection Terminated!");
|
info("Connection Terminated!");
|
||||||
}
|
}
|
||||||
void TCPGameServer(const std::string& IP, int Port) {
|
void TCPGameServer(asio::ip::tcp::socket&& Socket) {
|
||||||
GSocket = SetupListener();
|
asio::ip::tcp::endpoint listen_ep(asio::ip::address::from_string("0.0.0.0"), static_cast<uint16_t>(DEFAULT_PORT + 1));
|
||||||
while (!TCPTerminate && GSocket != -1) {
|
asio::ip::tcp::socket listener(io);
|
||||||
|
asio::error_code ec;
|
||||||
|
listener.open(listen_ep.protocol(), ec);
|
||||||
|
if (ec) {
|
||||||
|
::error(fmt::format("Failed to open game socket: {}", ec.message()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
asio::ip::tcp::socket::linger linger_opt {};
|
||||||
|
linger_opt.enabled(false);
|
||||||
|
listener.set_option(linger_opt, ec);
|
||||||
|
if (ec) {
|
||||||
|
::error(fmt::format("Failed to set up listening game socket to not linger / reuse address. "
|
||||||
|
"This may cause the game socket to refuse to bind(). Error: {}",
|
||||||
|
ec.message()));
|
||||||
|
}
|
||||||
|
asio::ip::tcp::socket::reuse_address reuse_opt { true };
|
||||||
|
listener.set_option(reuse_opt, ec);
|
||||||
|
if (ec) {
|
||||||
|
::error(fmt::format("Failed to set up listening core socket to not linger / reuse address. "
|
||||||
|
"This may cause the core socket to refuse to bind(). Error: {}",
|
||||||
|
ec.message()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto acceptor = asio::ip::tcp::acceptor(io, listen_ep);
|
||||||
|
acceptor.listen(asio::ip::tcp::socket::max_listen_connections, ec);
|
||||||
|
if (ec) {
|
||||||
|
debug(fmt::format("Proxy accept failed: {}", ec.message()));
|
||||||
|
TCPTerminate = true; // skip the loop
|
||||||
|
}
|
||||||
|
debug(fmt::format("Game server listening on {}:{}", acceptor.local_endpoint().address().to_string(), acceptor.local_endpoint().port()));
|
||||||
|
while (!TCPTerminate && acceptor.is_open()) {
|
||||||
debug("MAIN LOOP OF GAME SERVER");
|
debug("MAIN LOOP OF GAME SERVER");
|
||||||
GConnected = false;
|
GConnected = false;
|
||||||
if (!CServer) {
|
if (!CServer) {
|
||||||
@@ -245,62 +224,35 @@ void TCPGameServer(const std::string& IP, int Port) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (CServer) {
|
if (CServer) {
|
||||||
std::thread Client(TCPClientMain, IP, Port);
|
std::thread Client(TCPClientMain, std::move(Socket));
|
||||||
Client.detach();
|
Client.detach();
|
||||||
}
|
}
|
||||||
CSocket = accept(GSocket, nullptr, nullptr);
|
|
||||||
if (CSocket == -1) {
|
CSocket = std::make_shared<asio::ip::tcp::socket>(acceptor.accept());
|
||||||
debug("(Proxy) accept failed with error: " + std::to_string(WSAGetLastError()));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
debug("(Proxy) Game Connected!");
|
debug("(Proxy) Game Connected!");
|
||||||
GConnected = true;
|
GConnected = true;
|
||||||
if (CServer) {
|
if (CServer) {
|
||||||
std::thread t1(NetMain, IP, Port);
|
std::thread t1(NetMain, CSocket->remote_endpoint().address(), CSocket->remote_endpoint().port());
|
||||||
t1.detach();
|
t1.detach();
|
||||||
CServer = false;
|
CServer = false;
|
||||||
}
|
}
|
||||||
int32_t Size, Temp, Rcv;
|
std::vector<char> data {};
|
||||||
char Header[10] = { 0 };
|
|
||||||
|
|
||||||
// Read byte by byte until '>' is rcved then get the size and read based on it
|
// Read byte by byte until '>' is rcved then get the size and read based on it
|
||||||
do {
|
while (!TCPTerminate && !CSocket) {
|
||||||
Rcv = 0;
|
try {
|
||||||
|
ReceiveFromGame(*CSocket, data);
|
||||||
do {
|
ServerSend(data, false);
|
||||||
Temp = recv(CSocket, &Header[Rcv], 1, 0);
|
} catch (const std::exception& e) {
|
||||||
if (Temp < 1 || TCPTerminate)
|
error(std::string("Error while receiving from game: ") + e.what());
|
||||||
break;
|
|
||||||
} while (Header[Rcv++] != '>');
|
|
||||||
if (Temp < 1 || TCPTerminate)
|
|
||||||
break;
|
|
||||||
if (std::from_chars(Header, &Header[Rcv], Size).ptr[0] != '>') {
|
|
||||||
debug("(Game) Invalid lua Header -> " + std::string(Header, Rcv));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
std::string Ret(Size, 0);
|
}
|
||||||
Rcv = 0;
|
|
||||||
do {
|
|
||||||
Temp = recv(CSocket, &Ret[Rcv], Size - Rcv, 0);
|
|
||||||
if (Temp < 1)
|
|
||||||
break;
|
|
||||||
Rcv += Temp;
|
|
||||||
} while (Rcv < Size && !TCPTerminate);
|
|
||||||
if (Temp < 1 || TCPTerminate)
|
|
||||||
break;
|
|
||||||
|
|
||||||
ServerSend(Ret, false);
|
|
||||||
|
|
||||||
} while (Temp > 0 && !TCPTerminate);
|
|
||||||
if (Temp == 0)
|
|
||||||
debug("(Proxy) Connection closing");
|
|
||||||
else
|
|
||||||
debug("(Proxy) recv failed error : " + std::to_string(WSAGetLastError()));
|
|
||||||
}
|
}
|
||||||
TCPTerminate = true;
|
TCPTerminate = true;
|
||||||
GConnected = false;
|
GConnected = false;
|
||||||
Terminate = true;
|
Terminate = true;
|
||||||
if (CSocket != SOCKET_ERROR)
|
if (CSocket != nullptr)
|
||||||
KillSocket(CSocket);
|
KillSocket(CSocket);
|
||||||
debug("END OF GAME SERVER");
|
debug("END OF GAME SERVER");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
///
|
///
|
||||||
|
|
||||||
#include "Network/network.hpp"
|
#include "Network/network.hpp"
|
||||||
|
#include "fmt/core.h"
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
@@ -27,6 +28,7 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <future>
|
#include <future>
|
||||||
|
#include <asio.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@@ -72,8 +74,8 @@ void Abord() {
|
|||||||
info("Terminated!");
|
info("Terminated!");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Auth(SOCKET Sock) {
|
std::string Auth(asio::ip::tcp::socket& Sock) {
|
||||||
TCPSend("VC" + GetVer(), Sock);
|
TCPSend(strtovec("VC" + GetVer()), Sock);
|
||||||
|
|
||||||
auto Res = TCPRcv(Sock);
|
auto Res = TCPRcv(Sock);
|
||||||
|
|
||||||
@@ -82,7 +84,7 @@ std::string Auth(SOCKET Sock) {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
TCPSend(PublicKey, Sock);
|
TCPSend(strtovec(PublicKey), Sock);
|
||||||
if (Terminate)
|
if (Terminate)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
@@ -100,7 +102,7 @@ std::string Auth(SOCKET Sock) {
|
|||||||
UUl("Authentication failed!");
|
UUl("Authentication failed!");
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
TCPSend("SR", Sock);
|
TCPSend(strtovec("SR"), Sock);
|
||||||
if (Terminate)
|
if (Terminate)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
@@ -114,7 +116,7 @@ std::string Auth(SOCKET Sock) {
|
|||||||
if (Res.empty() || Res == "-") {
|
if (Res.empty() || Res == "-") {
|
||||||
info("Didn't Receive any mods...");
|
info("Didn't Receive any mods...");
|
||||||
ListOfMods = "-";
|
ListOfMods = "-";
|
||||||
TCPSend("Done", Sock);
|
TCPSend(strtovec("Done"), Sock);
|
||||||
info("Done!");
|
info("Done!");
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@@ -137,22 +139,18 @@ void AsyncUpdate(uint64_t& Rcv, uint64_t Size, const std::string& Name) {
|
|||||||
} while (!Terminate && Rcv < Size);
|
} while (!Terminate && Rcv < Size);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* TCPRcvRaw(SOCKET Sock, uint64_t& GRcv, uint64_t Size) {
|
char* TCPRcvRaw(asio::ip::tcp::socket& Sock, uint64_t& GRcv, uint64_t Size) {
|
||||||
if (Sock == -1) {
|
|
||||||
Terminate = true;
|
|
||||||
UUl("Invalid Socket");
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
char* File = new char[Size];
|
char* File = new char[Size];
|
||||||
uint64_t Rcv = 0;
|
uint64_t Rcv = 0;
|
||||||
|
asio::error_code ec;
|
||||||
do {
|
do {
|
||||||
int Len = int(Size - Rcv);
|
int Len = int(Size - Rcv);
|
||||||
if (Len > 1000000)
|
if (Len > 1000000)
|
||||||
Len = 1000000;
|
Len = 1000000;
|
||||||
int32_t Temp = recv(Sock, &File[Rcv], Len, MSG_WAITALL);
|
int32_t Temp = asio::read(Sock, asio::buffer(&File[Rcv], Len), ec);
|
||||||
if (Temp < 1) {
|
if (ec) {
|
||||||
info(std::to_string(Temp));
|
::error(fmt::format("Failed to receive data from server: {}", ec.message()));
|
||||||
UUl("Socket Closed Code 1");
|
UUl("Failed to receive data from server, connection closed (Code 1)");
|
||||||
KillSocket(Sock);
|
KillSocket(Sock);
|
||||||
Terminate = true;
|
Terminate = true;
|
||||||
delete[] File;
|
delete[] File;
|
||||||
@@ -163,29 +161,23 @@ char* TCPRcvRaw(SOCKET Sock, uint64_t& GRcv, uint64_t Size) {
|
|||||||
} while (Rcv < Size && !Terminate);
|
} while (Rcv < Size && !Terminate);
|
||||||
return File;
|
return File;
|
||||||
}
|
}
|
||||||
void MultiKill(SOCKET Sock, SOCKET Sock1) {
|
void MultiKill(asio::ip::tcp::socket& Sock, asio::ip::tcp::socket& Sock1) {
|
||||||
KillSocket(Sock1);
|
KillSocket(Sock1);
|
||||||
KillSocket(Sock);
|
KillSocket(Sock);
|
||||||
Terminate = true;
|
Terminate = true;
|
||||||
}
|
}
|
||||||
SOCKET InitDSock() {
|
std::shared_ptr<asio::ip::tcp::socket> InitDSock(asio::ip::tcp::endpoint ep) {
|
||||||
SOCKET DSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
auto DSock = std::make_shared<asio::ip::tcp::socket>(io);
|
||||||
SOCKADDR_IN ServerAddr;
|
asio::error_code ec;
|
||||||
if (DSock < 1) {
|
DSock->connect(ep, ec);
|
||||||
|
if (ec) {
|
||||||
KillSocket(DSock);
|
KillSocket(DSock);
|
||||||
Terminate = true;
|
Terminate = true;
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
|
||||||
ServerAddr.sin_family = AF_INET;
|
|
||||||
ServerAddr.sin_port = htons(LastPort);
|
|
||||||
inet_pton(AF_INET, LastIP.c_str(), &ServerAddr.sin_addr);
|
|
||||||
if (connect(DSock, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr)) != 0) {
|
|
||||||
KillSocket(DSock);
|
|
||||||
Terminate = true;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
char Code[2] = { 'D', char(ClientID) };
|
char Code[2] = { 'D', char(ClientID) };
|
||||||
if (send(DSock, Code, 2, 0) != 2) {
|
asio::write(*DSock, asio::buffer(Code, 2), ec);
|
||||||
|
if (ec) {
|
||||||
KillSocket(DSock);
|
KillSocket(DSock);
|
||||||
Terminate = true;
|
Terminate = true;
|
||||||
return 0;
|
return 0;
|
||||||
@@ -193,7 +185,7 @@ SOCKET InitDSock() {
|
|||||||
return DSock;
|
return DSock;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string MultiDownload(SOCKET MSock, SOCKET DSock, uint64_t Size, const std::string& Name) {
|
std::string MultiDownload(asio::ip::tcp::socket& MSock, asio::ip::tcp::socket& DSock, uint64_t Size, const std::string& Name) {
|
||||||
|
|
||||||
uint64_t GRcv = 0, MSize = Size / 2, DSize = Size - MSize;
|
uint64_t GRcv = 0, MSize = Size / 2, DSize = Size - MSize;
|
||||||
|
|
||||||
@@ -239,7 +231,7 @@ void InvalidResource(const std::string& File) {
|
|||||||
Terminate = true;
|
Terminate = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SyncResources(SOCKET Sock) {
|
void SyncResources(asio::ip::tcp::socket& Sock) {
|
||||||
std::string Ret = Auth(Sock);
|
std::string Ret = Auth(Sock);
|
||||||
if (Ret.empty())
|
if (Ret.empty())
|
||||||
return;
|
return;
|
||||||
@@ -278,7 +270,7 @@ void SyncResources(SOCKET Sock) {
|
|||||||
}
|
}
|
||||||
if (!FNames.empty())
|
if (!FNames.empty())
|
||||||
info("Syncing...");
|
info("Syncing...");
|
||||||
SOCKET DSock = InitDSock();
|
auto DSock = InitDSock(Sock.remote_endpoint());
|
||||||
for (auto FN = FNames.begin(), FS = FSizes.begin(); FN != FNames.end() && !Terminate; ++FN, ++FS) {
|
for (auto FN = FNames.begin(), FS = FSizes.begin(); FN != FNames.end() && !Terminate; ++FN, ++FS) {
|
||||||
auto pos = FN->find_last_of('/');
|
auto pos = FN->find_last_of('/');
|
||||||
if (pos != std::string::npos) {
|
if (pos != std::string::npos) {
|
||||||
@@ -320,7 +312,7 @@ void SyncResources(SOCKET Sock) {
|
|||||||
CheckForDir();
|
CheckForDir();
|
||||||
std::string FName = a.substr(a.find_last_of('/'));
|
std::string FName = a.substr(a.find_last_of('/'));
|
||||||
do {
|
do {
|
||||||
TCPSend("f" + *FN, Sock);
|
TCPSend(strtovec("f" + *FN), Sock);
|
||||||
|
|
||||||
std::string Data = TCPRcv(Sock);
|
std::string Data = TCPRcv(Sock);
|
||||||
if (Data == "CO" || Terminate) {
|
if (Data == "CO" || Terminate) {
|
||||||
@@ -331,7 +323,7 @@ void SyncResources(SOCKET Sock) {
|
|||||||
|
|
||||||
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);
|
Data = MultiDownload(Sock, *DSock, std::stoull(*FS), Name);
|
||||||
|
|
||||||
if (Terminate)
|
if (Terminate)
|
||||||
break;
|
break;
|
||||||
@@ -362,7 +354,7 @@ void SyncResources(SOCKET Sock) {
|
|||||||
}
|
}
|
||||||
KillSocket(DSock);
|
KillSocket(DSock);
|
||||||
if (!Terminate) {
|
if (!Terminate) {
|
||||||
TCPSend("Done", Sock);
|
TCPSend(strtovec("Done"), Sock);
|
||||||
info("Done!");
|
info("Done!");
|
||||||
} else {
|
} else {
|
||||||
UlStatus = "Ulstart";
|
UlStatus = "Ulstart";
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
///
|
///
|
||||||
#include "Network/network.hpp"
|
#include "Network/network.hpp"
|
||||||
#include "Zlib/Compressor.h"
|
#include "Zlib/Compressor.h"
|
||||||
|
#include "asio/ip/address.hpp"
|
||||||
|
#include "fmt/format.h"
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
@@ -23,28 +25,40 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
SOCKET UDPSock = -1;
|
std::shared_ptr<asio::ip::udp::socket> UDPSock = nullptr;
|
||||||
sockaddr_in* ToServer = nullptr;
|
|
||||||
|
|
||||||
void UDPSend(std::string Data) {
|
void UDPSend(const std::vector<char>& RawData) {
|
||||||
if (ClientID == -1 || UDPSock == -1)
|
if (ClientID == -1 || UDPSock == nullptr)
|
||||||
return;
|
return;
|
||||||
if (Data.length() > 400) {
|
std::string Data;
|
||||||
auto res = Comp(std::span<char>(Data.data(), Data.size()));
|
if (Data.size() > 400) {
|
||||||
|
auto res = Comp(RawData);
|
||||||
Data = "ABG:" + std::string(res.data(), res.size());
|
Data = "ABG:" + std::string(res.data(), res.size());
|
||||||
|
} else {
|
||||||
|
Data = std::string(RawData.data(), RawData.size());
|
||||||
}
|
}
|
||||||
std::string Packet = char(ClientID + 1) + 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));
|
int sendOk = UDPSock->send(asio::buffer(Packet));
|
||||||
if (sendOk == SOCKET_ERROR)
|
if (sendOk == SOCKET_ERROR)
|
||||||
error("Error Code : " + std::to_string(WSAGetLastError()));
|
error("Error Code : " + std::to_string(WSAGetLastError()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendLarge(std::string Data) {
|
void SendLarge(const std::vector<char>& Data) {
|
||||||
if (Data.length() > 400) {
|
if (Data.size() > 400) {
|
||||||
auto res = Comp(std::span<char>(Data.data(), Data.size()));
|
auto res = Comp(Data);
|
||||||
Data = "ABG:" + std::string(res.data(), res.size());
|
res.insert(res.begin(), { 'A', 'B', 'G', ':' });
|
||||||
|
if (!TCPSock) {
|
||||||
|
::debug("TCPSock is null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
TCPSend(res, *TCPSock);
|
||||||
|
} else {
|
||||||
|
if (!TCPSock) {
|
||||||
|
::debug("TCPSock is null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
TCPSend(Data, *TCPSock);
|
||||||
}
|
}
|
||||||
TCPSend(Data, TCPSock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UDPParser(std::string_view Packet) {
|
void UDPParser(std::string_view Packet) {
|
||||||
@@ -58,40 +72,29 @@ void UDPParser(std::string_view Packet) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
void UDPRcv() {
|
void UDPRcv() {
|
||||||
sockaddr_in FromServer {};
|
|
||||||
#if defined(_WIN32)
|
|
||||||
int clientLength = sizeof(FromServer);
|
|
||||||
#elif defined(__linux__)
|
|
||||||
socklen_t clientLength = sizeof(FromServer);
|
|
||||||
#endif
|
|
||||||
ZeroMemory(&FromServer, clientLength);
|
|
||||||
static thread_local std::array<char, 10240> Ret {};
|
static thread_local std::array<char, 10240> Ret {};
|
||||||
if (UDPSock == -1)
|
if (UDPSock == nullptr) {
|
||||||
|
::debug("UDPSock is null");
|
||||||
return;
|
return;
|
||||||
int32_t Rcv = recvfrom(UDPSock, Ret.data(), Ret.size() - 1, 0, (sockaddr*)&FromServer, &clientLength);
|
}
|
||||||
if (Rcv == SOCKET_ERROR)
|
asio::error_code ec;
|
||||||
|
int32_t Rcv = UDPSock->receive(asio::buffer(Ret.data(), Ret.size() - 1), 0, ec);
|
||||||
|
if (ec)
|
||||||
return;
|
return;
|
||||||
Ret[Rcv] = 0;
|
Ret[Rcv] = 0;
|
||||||
UDPParser(std::string_view(Ret.data(), Rcv));
|
UDPParser(std::string_view(Ret.data(), Rcv));
|
||||||
}
|
}
|
||||||
void UDPClientMain(const std::string& IP, int Port) {
|
void UDPClientMain(asio::ip::address addr, uint16_t port) {
|
||||||
#ifdef _WIN32
|
UDPSock = std::make_shared<asio::ip::udp::socket>(io);
|
||||||
WSADATA data;
|
asio::error_code ec;
|
||||||
if (WSAStartup(514, &data)) {
|
UDPSock->connect(asio::ip::udp::endpoint(addr, port), ec);
|
||||||
error("Can't start Winsock!");
|
if (ec) {
|
||||||
return;
|
::error(fmt::format("Failed to connect UDP to server: {}", ec.message()));
|
||||||
|
Terminate = true;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
delete ToServer;
|
|
||||||
ToServer = new sockaddr_in;
|
|
||||||
ToServer->sin_family = AF_INET;
|
|
||||||
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));
|
GameSend("P" + std::to_string(ClientID));
|
||||||
TCPSend("H", TCPSock);
|
TCPSend(strtovec("H"), *TCPSock);
|
||||||
UDPSend("p");
|
UDPSend(strtovec("p"));
|
||||||
while (!Terminate)
|
while (!Terminate)
|
||||||
UDPRcv();
|
UDPRcv();
|
||||||
KillSocket(UDPSock);
|
KillSocket(UDPSock);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
///
|
///
|
||||||
|
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
#include "fmt/format.h"
|
||||||
#include <Zlib/Compressor.h>
|
#include <Zlib/Compressor.h>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -27,7 +28,7 @@
|
|||||||
|
|
||||||
int LastPort;
|
int LastPort;
|
||||||
std::string LastIP;
|
std::string LastIP;
|
||||||
SOCKET TCPSock = -1;
|
std::shared_ptr<asio::ip::tcp::socket> TCPSock = nullptr;
|
||||||
|
|
||||||
bool CheckBytes(int32_t Bytes) {
|
bool CheckBytes(int32_t Bytes) {
|
||||||
if (Bytes == 0) {
|
if (Bytes == 0) {
|
||||||
@@ -46,8 +47,8 @@ void UUl(const std::string& R) {
|
|||||||
UlStatus = "UlDisconnected: " + R;
|
UlStatus = "UlDisconnected: " + R;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPSend(const std::string& Data, uint64_t Sock) {
|
void TCPSend(const std::vector<char>& Data, asio::ip::tcp::socket& Sock) {
|
||||||
if (Sock == -1) {
|
if (!Sock.is_open()) {
|
||||||
Terminate = true;
|
Terminate = true;
|
||||||
UUl("Invalid Socket");
|
UUl("Invalid Socket");
|
||||||
return;
|
return;
|
||||||
@@ -57,63 +58,43 @@ void TCPSend(const std::string& Data, uint64_t Sock) {
|
|||||||
std::string Send(4, 0);
|
std::string Send(4, 0);
|
||||||
Size = int32_t(Data.size());
|
Size = int32_t(Data.size());
|
||||||
memcpy(&Send[0], &Size, sizeof(Size));
|
memcpy(&Send[0], &Size, sizeof(Size));
|
||||||
Send += Data;
|
Send += std::string(Data.data(), Data.size());
|
||||||
// Do not use Size before this point for anything but the header
|
// Do not use Size before this point for anything but the header
|
||||||
Sent = 0;
|
Sent = 0;
|
||||||
Size += 4;
|
Size += 4;
|
||||||
do {
|
asio::error_code ec;
|
||||||
if (size_t(Sent) >= Send.size()) {
|
asio::write(Sock, asio::buffer(Send), ec);
|
||||||
error("string OOB in " + std::string(__func__));
|
if (ec) {
|
||||||
UUl("TCP Send OOB");
|
UUl(fmt::format("Failed to send data: {}", ec.message()));
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
Temp = send(Sock, &Send[Sent], Size - Sent, 0);
|
|
||||||
if (!CheckBytes(Temp)) {
|
|
||||||
UUl("Socket Closed Code 2");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Sent += Temp;
|
|
||||||
} while (Sent < Size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string TCPRcv(SOCKET Sock) {
|
std::string TCPRcv(asio::ip::tcp::socket& Sock) {
|
||||||
if (Sock == -1) {
|
if (!Sock.is_open()) {
|
||||||
Terminate = true;
|
Terminate = true;
|
||||||
UUl("Invalid Socket");
|
UUl("Invalid Socket");
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
int32_t Header, BytesRcv = 0, Temp;
|
int32_t Header, BytesRcv = 0, Temp;
|
||||||
std::vector<char> Data(sizeof(Header));
|
std::vector<char> Data(sizeof(Header));
|
||||||
do {
|
asio::error_code ec;
|
||||||
Temp = recv(Sock, &Data[BytesRcv], 4 - BytesRcv, 0);
|
asio::read(Sock, asio::buffer(Data), ec);
|
||||||
if (!CheckBytes(Temp)) {
|
if (ec) {
|
||||||
UUl("Socket Closed Code 3");
|
UUl(fmt::format("Failed to receive header: {}", ec.message()));
|
||||||
return "";
|
}
|
||||||
}
|
|
||||||
BytesRcv += Temp;
|
|
||||||
} while (BytesRcv < 4);
|
|
||||||
memcpy(&Header, &Data[0], sizeof(Header));
|
memcpy(&Header, &Data[0], sizeof(Header));
|
||||||
|
|
||||||
if (!CheckBytes(BytesRcv)) {
|
|
||||||
UUl("Socket Closed Code 4");
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
Data.resize(Header);
|
Data.resize(Header);
|
||||||
BytesRcv = 0;
|
asio::read(Sock, asio::buffer(Data), ec);
|
||||||
do {
|
if (ec) {
|
||||||
Temp = recv(Sock, &Data[BytesRcv], Header - BytesRcv, 0);
|
UUl(fmt::format("Failed to receive data: {}", ec.message()));
|
||||||
if (!CheckBytes(Temp)) {
|
}
|
||||||
UUl("Socket Closed Code 5");
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
BytesRcv += Temp;
|
|
||||||
} while (BytesRcv < Header);
|
|
||||||
|
|
||||||
std::string Ret(Data.data(), Header);
|
std::string Ret(Data.data(), Header);
|
||||||
|
|
||||||
if (Ret.substr(0, 4) == "ABG:") {
|
if (Ret.substr(0, 4) == "ABG:") {
|
||||||
auto substr = Ret.substr(4);
|
auto substr = Ret.substr(4);
|
||||||
auto res = DeComp(std::span<char>(substr.data(), substr.size()));
|
auto res = DeComp(strtovec(substr));
|
||||||
Ret = std::string(res.data(), res.size());
|
Ret = std::string(res.data(), res.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,50 +106,24 @@ std::string TCPRcv(SOCKET Sock) {
|
|||||||
return Ret;
|
return Ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPClientMain(const std::string& IP, int Port) {
|
void TCPClientMain(asio::ip::tcp::socket&& socket) {
|
||||||
LastIP = IP;
|
if (!TCPSock) {
|
||||||
LastPort = Port;
|
return;
|
||||||
|
}
|
||||||
|
LastIP = socket.remote_endpoint().address().to_string();
|
||||||
|
LastPort = socket.remote_endpoint().port();
|
||||||
SOCKADDR_IN ServerAddr;
|
SOCKADDR_IN ServerAddr;
|
||||||
int RetCode;
|
int RetCode;
|
||||||
#ifdef _WIN32
|
TCPSock = std::make_shared<asio::ip::tcp::socket>(std::move(socket));
|
||||||
WSADATA wsaData;
|
|
||||||
WSAStartup(514, &wsaData); // 2.2
|
|
||||||
#endif
|
|
||||||
TCPSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
||||||
|
|
||||||
if (TCPSock == -1) {
|
|
||||||
printf("Client: socket failed! Error code: %d\n", WSAGetLastError());
|
|
||||||
WSACleanup();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ServerAddr.sin_family = AF_INET;
|
|
||||||
ServerAddr.sin_port = htons(Port);
|
|
||||||
inet_pton(AF_INET, IP.c_str(), &ServerAddr.sin_addr);
|
|
||||||
RetCode = connect(TCPSock, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr));
|
|
||||||
if (RetCode != 0) {
|
|
||||||
UlStatus = "UlConnection Failed!";
|
|
||||||
error("Client: connect failed! Error code: " + std::to_string(WSAGetLastError()));
|
|
||||||
KillSocket(TCPSock);
|
|
||||||
WSACleanup();
|
|
||||||
Terminate = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
info("Connected!");
|
info("Connected!");
|
||||||
|
|
||||||
char Code = 'C';
|
char Code = 'C';
|
||||||
send(TCPSock, &Code, 1, 0);
|
asio::write(*TCPSock, asio::buffer(&Code, 1));
|
||||||
SyncResources(TCPSock);
|
SyncResources(*TCPSock);
|
||||||
while (!Terminate) {
|
while (!Terminate && TCPSock) {
|
||||||
ServerParser(TCPRcv(TCPSock));
|
ServerParser(TCPRcv(*TCPSock));
|
||||||
}
|
}
|
||||||
GameSend("T");
|
GameSend("T");
|
||||||
////Game Send Terminate
|
KillSocket(TCPSock);
|
||||||
if (KillSocket(TCPSock) != 0)
|
|
||||||
debug("(TCP) Cannot close socket. Error code: " + std::to_string(WSAGetLastError()));
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
if (WSACleanup() != 0)
|
|
||||||
debug("(TCP) Client: WSACleanup() failed!...");
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|||||||
35
src/NetworkHelpers.cpp
Normal file
35
src/NetworkHelpers.cpp
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#include "NetworkHelpers.h"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cerrno>
|
||||||
|
#include <cstring>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
using asio::ip::tcp;
|
||||||
|
|
||||||
|
static uint32_t RecvHeader(tcp::socket& socket) {
|
||||||
|
std::array<uint8_t, sizeof(uint32_t)> header_buffer {};
|
||||||
|
asio::error_code ec;
|
||||||
|
auto n = asio::read(socket, asio::buffer(header_buffer), ec);
|
||||||
|
if (ec) {
|
||||||
|
throw std::runtime_error(std::string("recv() of header failed: ") + ec.message());
|
||||||
|
}
|
||||||
|
if (n == 0) {
|
||||||
|
throw std::runtime_error("Game disconnected");
|
||||||
|
}
|
||||||
|
return *reinterpret_cast<uint32_t*>(header_buffer.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Throws!!!
|
||||||
|
void ReceiveFromGame(tcp::socket& socket, std::vector<char>& out_data) {
|
||||||
|
auto header = RecvHeader(socket);
|
||||||
|
out_data.resize(header);
|
||||||
|
asio::error_code ec;
|
||||||
|
auto n = asio::read(socket, asio::buffer(out_data), ec);
|
||||||
|
if (ec) {
|
||||||
|
throw std::runtime_error(std::string("recv() of data failed: ") + ec.message());
|
||||||
|
}
|
||||||
|
if (n == 0) {
|
||||||
|
throw std::runtime_error("Game disconnected");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -81,10 +81,10 @@ std::string GetEN() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string GetVer() {
|
std::string GetVer() {
|
||||||
return "2.0";
|
return "2.1";
|
||||||
}
|
}
|
||||||
std::string GetPatch() {
|
std::string GetPatch() {
|
||||||
return ".99";
|
return ".0";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string GetEP(char* P) {
|
std::string GetEP(char* P) {
|
||||||
@@ -172,7 +172,7 @@ void CheckForUpdates(int argc, char* args[], const std::string& CV) {
|
|||||||
system("clear");
|
system("clear");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion))) && !Dev) {
|
if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion)))) {
|
||||||
info("Launcher update found!");
|
info("Launcher update found!");
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
error("Auto update is NOT implemented for the Linux version. Please update manually ASAP as updates contain security patches.");
|
error("Auto update is NOT implemented for the Linux version. Please update manually ASAP as updates contain security patches.");
|
||||||
@@ -204,6 +204,13 @@ void CustomPort(int argc, char* argv[]) {
|
|||||||
if (argc > 2)
|
if (argc > 2)
|
||||||
Dev = true;
|
Dev = true;
|
||||||
}
|
}
|
||||||
|
for (int i = 1; i < argc; ++i) {
|
||||||
|
if (std::string_view(argv[i]) == "--dev") {
|
||||||
|
Dev = true;
|
||||||
|
} else if (std::string_view(argv[i]) == "--no-dev") {
|
||||||
|
Dev = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -255,7 +262,15 @@ void InitLauncher(int argc, char* argv[]) {
|
|||||||
CheckLocalKey();
|
CheckLocalKey();
|
||||||
ConfigInit();
|
ConfigInit();
|
||||||
CustomPort(argc, argv);
|
CustomPort(argc, argv);
|
||||||
CheckForUpdates(argc, argv, std::string(GetVer()) + GetPatch());
|
bool update = true;
|
||||||
|
for (int i = 1; i < argc; ++i) {
|
||||||
|
if (std::string_view(argv[i]) == "--no-update") {
|
||||||
|
update = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (update) {
|
||||||
|
CheckForUpdates(argc, argv, std::string(GetVer()) + GetPatch());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -318,6 +333,8 @@ void PreGame(const std::string& GamePath) {
|
|||||||
|
|
||||||
CheckMP(GetGamePath() + "mods/multiplayer");
|
CheckMP(GetGamePath() + "mods/multiplayer");
|
||||||
|
|
||||||
|
info("Game user path: '" + GetGamePath() + "'");
|
||||||
|
|
||||||
if (!Dev) {
|
if (!Dev) {
|
||||||
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
|
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
|
||||||
transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower);
|
transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower);
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
"cpp-httplib",
|
"cpp-httplib",
|
||||||
"nlohmann-json",
|
"nlohmann-json",
|
||||||
"zlib",
|
"zlib",
|
||||||
"openssl"
|
"openssl",
|
||||||
|
"asio",
|
||||||
|
"fmt"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user