14 Commits

Author SHA1 Message Date
SaltySnail
169b14490c Added IPv6 support 2024-08-21 01:38:52 +02:00
Lion
a60ff48c08 Merge pull request #105 from WiserTixx/id-from-auth
Send id from auth to game
2024-08-17 20:34:19 +02:00
Lion
da3b49aa12 Merge pull request #106 from WiserTixx/fix-http-proxy-ub
Fix UB which was causing the http proxy to crash
2024-08-17 20:32:59 +02:00
Tixx
e505874af9 Send id from auth to game 2024-08-11 11:39:14 +02:00
Tixx
2f0a9fba99 move macro definition to cmakelist 2024-08-10 23:22:17 +02:00
Lion Kortlepel
b034072027 fix potential UB in decompression 2024-06-23 23:04:55 +02:00
Lion Kortlepel
f94b9adf7a print game's U S E R path 2024-06-22 23:26:10 +02:00
Lion Kortlepel
c95178ea59 dont auto-update in dev mode 2024-06-22 23:20:46 +02:00
Lion Kortlepel
1f7c498bd9 fix compiler error in decomp 2024-06-22 23:05:01 +02:00
Lion Kortlepel
e46d4b2f0e Merge branch 'performance-improvements' 2024-06-22 23:01:15 +02:00
Lion Kortlepel
d32da036bc fix mod name bug 2024-06-21 17:30:47 +02:00
Lion Kortlepel
8b0f4f99f6 use thread_local static buffer to receive into, null term manually 2024-06-19 16:53:17 +02:00
Lion Kortlepel
17e887442c avoid a substr() which costs us ~20% of runtime performance 2024-06-19 16:18:11 +02:00
Lion Kortlepel
fc454cd11e avoid creating a thread every packet 2024-06-19 15:53:49 +02:00
16 changed files with 101 additions and 76 deletions

View File

@@ -12,6 +12,8 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
add_compile_definitions(CPPHTTPLIB_OPENSSL_SUPPORT)
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(nlohmann_json CONFIG REQUIRED)

View File

@@ -41,12 +41,12 @@ int KillSocket(uint64_t Dead);
void UUl(const std::string& R);
void UDPSend(std::string Data);
bool CheckBytes(int32_t Bytes);
void GameSend(std::string Data);
void GameSend(std::string_view Data);
void SendLarge(std::string Data);
std::string TCPRcv(uint64_t Sock);
void SyncResources(uint64_t TCPSock);
std::string GetAddr(const std::string& IP);
void ServerParser(const std::string& Data);
void ServerParser(std::string_view Data);
std::string Login(const std::string& fields);
void TCPSend(const std::string& Data, uint64_t Sock);
void TCPClientMain(const std::string& IP, int Port);

View File

@@ -19,10 +19,3 @@ void StartProxy();
void ConfigInit();
extern bool Dev;
struct VersionParser {
explicit VersionParser(const std::string& from_string);
std::strong_ordering operator<=>(VersionParser const& rhs) const noexcept;
bool operator==(VersionParser const& rhs) const noexcept;
std::vector<std::string> split;
std::vector<size_t> data;
};

View File

@@ -9,5 +9,5 @@
#include <span>
#include <vector>
std::vector<char> Comp(std::span<char> input);
std::vector<char> DeComp(std::span<char> input);
std::vector<char> Comp(std::span<const char> input);
std::vector<char> DeComp(std::span<const char> input);

View File

@@ -15,14 +15,14 @@
#include <cstring>
#endif
std::vector<char> Comp(std::span<char> input) {
std::vector<char> Comp(std::span<const char> input) {
auto max_size = compressBound(input.size());
std::vector<char> output(max_size);
uLongf output_size = output.size();
int res = compress(
reinterpret_cast<Bytef*>(output.data()),
&output_size,
reinterpret_cast<Bytef*>(input.data()),
reinterpret_cast<const Bytef*>(input.data()),
static_cast<uLongf>(input.size()));
if (res != Z_OK) {
error("zlib compress() failed: " + std::to_string(res));
@@ -33,7 +33,7 @@ std::vector<char> Comp(std::span<char> input) {
return output;
}
std::vector<char> DeComp(std::span<char> input) {
std::vector<char> DeComp(std::span<const char> input) {
std::vector<char> output_buffer(std::min<size_t>(input.size() * 5, 15 * 1024 * 1024));
uLongf output_size = output_buffer.size();

View File

@@ -48,8 +48,8 @@ void ConfigInit() {
if (cfg.is_open()) {
cfg <<
R"({
"Port": 4444,
"Build": "Default"
"Port": 4444,
"Build": "Default"
})";
cfg.close();
} else {

View File

@@ -51,6 +51,7 @@ std::string GetGamePath() {
std::string Ver = CheckVer(GetGameDir());
Ver = Ver.substr(0, Ver.find('.', Ver.find('.') + 1));
Path += Ver + "\\";
info("Game user path: '" + Path + "'");
return Path;
}
#elif defined(__linux__)
@@ -63,6 +64,7 @@ std::string GetGamePath() {
std::string Ver = CheckVer(GetGameDir());
Ver = Ver.substr(0, Ver.find('.', Ver.find('.') + 1));
Path += Ver + "/";
info("Game user path: '" + Path + "'");
return Path;
}
#endif

View File

@@ -39,6 +39,7 @@ bool Terminate = false;
bool LoginAuth = false;
std::string Username = "";
std::string UserRole = "";
int UserID = -1;
std::string UlStatus;
std::string MStatus;
bool ModLoaded;
@@ -180,6 +181,9 @@ void Parse(std::string Data, SOCKET CSocket) {
if (!UserRole.empty()) {
Auth["role"] = UserRole;
}
if (UserID != -1) {
Auth["id"] = UserID;
}
Data = "N" + Auth.dump();
} else {
Data = "N" + Login(Data.substr(Data.find(':') + 1));
@@ -230,8 +234,7 @@ void GameHandler(SOCKET Client) {
if (Temp < 1)
break;
std::thread Respond(Parse, Ret, Client);
Respond.detach();
Parse(Ret, Client);
} while (Temp > 0);
if (Temp == 0) {
debug("(Core) Connection closing");
@@ -266,7 +269,7 @@ void CoreMain() {
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;

View File

@@ -7,6 +7,7 @@
///
#include <string>
#include "IPRegex.h"
#if defined(_WIN32)
#include <winsock2.h>
@@ -19,8 +20,9 @@
#include "Logger.h"
std::string GetAddr(const std::string& IP) {
if (IP.find_first_not_of("0123456789.") == -1)
if (!std::regex_match(IP, IP_REGEX)) {
return IP;
}
hostent* host;
#ifdef _WIN32
WSADATA wsaData;
@@ -40,4 +42,4 @@ std::string GetAddr(const std::string& IP) {
std::string Ret = inet_ntoa(*((struct in_addr*)host->h_addr));
WSACleanup();
return Ret;
}
}

View File

@@ -56,13 +56,12 @@ bool CheckBytes(uint32_t Bytes) {
return true;
}
void GameSend(std::string Data) {
void GameSend(std::string_view Data) {
static std::mutex Lock;
std::scoped_lock Guard(Lock);
if (TCPTerminate || !GConnected || CSocket == -1)
return;
int32_t Size, Temp, Sent;
Data += '\n';
Size = int32_t(Data.size());
Sent = 0;
#ifdef DEBUG
@@ -78,6 +77,11 @@ void GameSend(std::string Data) {
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) {
if (Terminate || Data.empty())
@@ -152,7 +156,7 @@ SOCKET SetupListener() {
#endif
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
@@ -194,7 +198,7 @@ void AutoPing() {
}
}
int ClientID = -1;
void ParserAsync(const std::string& Data) {
void ParserAsync(std::string_view Data) {
if (Data.empty())
return;
char Code = Data.at(0), SubCode = 0;
@@ -217,7 +221,7 @@ void ParserAsync(const std::string& Data) {
}
GameSend(Data);
}
void ServerParser(const std::string& Data) {
void ServerParser(std::string_view Data) {
ParserAsync(Data);
}
void NetMain(const std::string& IP, int Port) {

View File

@@ -5,7 +5,6 @@
///
/// Created by Anonymous275 on 7/18/2020
///
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "Http.h"
#include <Logger.h>

View File

@@ -169,16 +169,16 @@ void MultiKill(SOCKET Sock, SOCKET Sock1) {
Terminate = true;
}
SOCKET InitDSock() {
SOCKET DSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKET DSock = socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN ServerAddr;
if (DSock < 1) {
KillSocket(DSock);
Terminate = true;
return 0;
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_family = AF_UNSPEC;
ServerAddr.sin_port = htons(LastPort);
inet_pton(AF_INET, LastIP.c_str(), &ServerAddr.sin_addr);
inet_pton(AF_UNSPEC, LastIP.c_str(), &ServerAddr.sin_addr);
if (connect(DSock, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr)) != 0) {
KillSocket(DSock);
Terminate = true;
@@ -296,13 +296,14 @@ void SyncResources(SOCKET Sock) {
if (!fs::exists(GetGamePath() + "mods/multiplayer")) {
fs::create_directories(GetGamePath() + "mods/multiplayer");
}
auto name = GetGamePath() + "mods/multiplayer" + a.substr(a.find_last_of('/'));
auto modname = a.substr(a.find_last_of('/'));
#if defined(__linux__)
// Linux version of the game doesnt support uppercase letters in mod names
for (char& c : name) {
for (char& c : modname) {
c = ::tolower(c);
}
#endif
auto name = GetGamePath() + "mods/multiplayer" + modname;
auto tmp_name = name + ".tmp";
fs::copy_file(a, tmp_name, fs::copy_options::overwrite_existing);
fs::rename(tmp_name, name);

View File

@@ -14,14 +14,13 @@
#include "linuxfixes.h"
#include <arpa/inet.h>
#include <cstring>
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#endif
#include "Logger.h"
#include <set>
#include <array>
#include <string>
SOCKET UDPSock = -1;
@@ -48,13 +47,15 @@ void SendLarge(std::string Data) {
TCPSend(Data, TCPSock);
}
void UDPParser(std::string Packet) {
void UDPParser(std::string_view Packet) {
if (Packet.substr(0, 4) == "ABG:") {
auto substr = Packet.substr(4);
auto res = DeComp(std::span<char>(substr.data(), substr.size()));
Packet = std::string(res.data(), res.size());
auto res = DeComp(std::span<const char>(substr.data(), substr.size()));
std::string DeCompPacket = std::string(res.data(), res.size());
ServerParser(DeCompPacket);
} else {
ServerParser(Packet);
}
ServerParser(Packet);
}
void UDPRcv() {
sockaddr_in FromServer {};
@@ -64,13 +65,14 @@ void UDPRcv() {
socklen_t clientLength = sizeof(FromServer);
#endif
ZeroMemory(&FromServer, clientLength);
std::string Ret(10240, 0);
static thread_local std::array<char, 10240> Ret {};
if (UDPSock == -1)
return;
int32_t Rcv = recvfrom(UDPSock, &Ret[0], 10240, 0, (sockaddr*)&FromServer, &clientLength);
int32_t Rcv = recvfrom(UDPSock, Ret.data(), Ret.size() - 1, 0, (sockaddr*)&FromServer, &clientLength);
if (Rcv == SOCKET_ERROR)
return;
UDPParser(Ret.substr(0, Rcv));
Ret[Rcv] = 0;
UDPParser(std::string_view(Ret.data(), Rcv));
}
void UDPClientMain(const std::string& IP, int Port) {
#ifdef _WIN32
@@ -83,10 +85,10 @@ void UDPClientMain(const std::string& IP, int Port) {
delete ToServer;
ToServer = new sockaddr_in;
ToServer->sin_family = AF_INET;
ToServer->sin_family = AF_UNSPEC;
ToServer->sin_port = htons(Port);
inet_pton(AF_INET, IP.c_str(), &ToServer->sin_addr);
UDPSock = socket(AF_INET, SOCK_DGRAM, 0);
inet_pton(AF_UNSPEC, IP.c_str(), &ToServer->sin_addr);
UDPSock = socket(AF_UNSPEC, SOCK_DGRAM, 0);
GameSend("P" + std::to_string(ClientID));
TCPSend("H", TCPSock);
UDPSend("p");

View File

@@ -134,7 +134,7 @@ void TCPClientMain(const std::string& IP, int Port) {
WSADATA wsaData;
WSAStartup(514, &wsaData); // 2.2
#endif
TCPSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
TCPSock = socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
if (TCPSock == -1) {
printf("Client: socket failed! Error code: %d\n", WSAGetLastError());
@@ -142,9 +142,9 @@ void TCPClientMain(const std::string& IP, int Port) {
return;
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_family = AF_UNSPEC;
ServerAddr.sin_port = htons(Port);
inet_pton(AF_INET, IP.c_str(), &ServerAddr.sin_addr);
inet_pton(AF_UNSPEC, IP.c_str(), &ServerAddr.sin_addr);
RetCode = connect(TCPSock, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr));
if (RetCode != 0) {
UlStatus = "UlConnection Failed!";

View File

@@ -18,6 +18,7 @@ std::string PrivateKey;
extern bool LoginAuth;
extern std::string Username;
extern std::string UserRole;
extern int UserID;
void UpdateKey(const char* newKey) {
if (newKey && std::isalnum(newKey[0])) {
@@ -48,6 +49,7 @@ std::string Login(const std::string& fields) {
if (fields == "LO") {
Username = "";
UserRole = "";
UserID = -1;
LoginAuth = false;
UpdateKey(nullptr);
return "";
@@ -74,6 +76,9 @@ std::string Login(const std::string& fields) {
if (d.contains("role")) {
UserRole = d["role"].get<std::string>();
}
if (d.contains("id")) {
UserID = d["id"].get<int>();
}
if (d.contains("private_key")) {
UpdateKey(d["private_key"].get<std::string>().c_str());
}
@@ -129,6 +134,9 @@ void CheckLocalKey() {
if (d.contains("role")) {
UserRole = d["role"].get<std::string>();
}
if (d.contains("id")) {
UserID = d["id"].get<int>();
}
// info(Role);
} else {
info("Auto-Authentication unsuccessful please re-login!");

View File

@@ -7,6 +7,7 @@
///
#include "zip_file.h"
#include <charconv>
#include <httplib.h>
#include <nlohmann/json.hpp>
#include <string>
@@ -31,36 +32,44 @@ int ProxyPort = 0;
namespace fs = std::filesystem;
VersionParser::VersionParser(const std::string& from_string) {
std::string token;
std::istringstream tokenStream(from_string);
while (std::getline(tokenStream, token, '.')) {
data.emplace_back(std::stol(token));
split.emplace_back(token);
struct Version {
uint8_t major;
uint8_t minor;
uint8_t patch;
Version(uint8_t major, uint8_t minor, uint8_t patch);
Version(const std::array<uint8_t, 3>& v);
};
std::array<uint8_t, 3> VersionStrToInts(const std::string& str) {
std::array<uint8_t, 3> Version;
std::stringstream ss(str);
for (uint8_t& i : Version) {
std::string Part;
std::getline(ss, Part, '.');
std::from_chars(&*Part.begin(), &*Part.begin() + Part.size(), i);
}
return Version;
}
bool IsOutdated(const Version& Current, const Version& Newest) {
if (Newest.major > Current.major) {
return true;
} else if (Newest.major == Current.major && Newest.minor > Current.minor) {
return true;
} else if (Newest.major == Current.major && Newest.minor == Current.minor && Newest.patch > Current.patch) {
return true;
} else {
return false;
}
}
std::strong_ordering VersionParser::operator<=>(
const VersionParser& rhs) const noexcept {
size_t const fields = std::min(data.size(), rhs.data.size());
for (size_t i = 0; i != fields; ++i) {
if (data[i] == rhs.data[i])
continue;
else if (data[i] < rhs.data[i])
return std::strong_ordering::less;
else
return std::strong_ordering::greater;
}
if (data.size() == rhs.data.size())
return std::strong_ordering::equal;
else if (data.size() > rhs.data.size())
return std::strong_ordering::greater;
else
return std::strong_ordering::less;
}
Version::Version(uint8_t major, uint8_t minor, uint8_t patch)
: major(major)
, minor(minor)
, patch(patch) { }
bool VersionParser::operator==(const VersionParser& rhs) const noexcept {
return std::is_eq(*this <=> rhs);
Version::Version(const std::array<uint8_t, 3>& v)
: Version(v[0], v[1], v[2]) {
}
std::string GetEN() {
@@ -75,7 +84,7 @@ std::string GetVer() {
return "2.0";
}
std::string GetPatch() {
return ".85";
return ".99";
}
std::string GetEP(char* P) {
@@ -163,7 +172,7 @@ void CheckForUpdates(int argc, char* args[], const std::string& CV) {
system("clear");
#endif
if (FileHash != LatestHash && VersionParser(LatestVersion) > VersionParser(GetVer() + GetPatch())) {
if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion))) && !Dev) {
info("Launcher update found!");
#if defined(__linux__)
error("Auto update is NOT implemented for the Linux version. Please update manually ASAP as updates contain security patches.");
@@ -177,7 +186,7 @@ void CheckForUpdates(int argc, char* args[], const std::string& CV) {
+ PublicKey + "&branch=" + Branch,
EP);
URelaunch(argc, args);
#endif
#endif
} else
info("Launcher version is up to date");
TraceBack++;