Skip to content

Commit c4c894c

Browse files
committed
Bump version v3.9.0
1 parent 039a44b commit c4c894c

File tree

5 files changed

+66
-16
lines changed

5 files changed

+66
-16
lines changed

include/Client.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <queue>
2525
#include <string>
2626
#include <unordered_set>
27+
#include <utility>
2728

2829
#include "BoostAliases.h"
2930
#include "Common.h"
@@ -101,6 +102,8 @@ class TClient final {
101102
[[nodiscard]] TServer& Server() const;
102103
void UpdatePingTime();
103104
int SecondsSinceLastPing();
105+
void SetMagic(std::vector<uint8_t> magic) { mMagic = std::move(magic); }
106+
[[nodiscard]] const std::vector<uint8_t>& GetMagic() const { return mMagic; }
104107

105108
private:
106109
void InsertVehicle(int ID, const std::string& Data);
@@ -125,6 +128,7 @@ class TClient final {
125128
std::string mDID;
126129
int mID = -1;
127130
std::chrono::time_point<std::chrono::high_resolution_clock> mLastPingTime = std::chrono::high_resolution_clock::now();
131+
std::vector<uint8_t> mMagic;
128132
};
129133

130134
std::optional<std::weak_ptr<TClient>> GetClient(class TServer& Server, int ID);

include/Common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Application final {
7474
static TConsole& Console() { return mConsole; }
7575
static std::string ServerVersionString();
7676
static const Version& ServerVersion() { return mVersion; }
77-
static Version ClientMinimumVersion() { return Version { 2, 2, 0 }; }
77+
static Version ClientMinimumVersion() { return Version { 2, 7, 0 }; }
7878
static std::string PPS() { return mPPS; }
7979
static void SetPPS(const std::string& NewPPS) { mPPS = NewPPS; }
8080

@@ -129,7 +129,7 @@ class Application final {
129129
static inline std::mutex mShutdownHandlersMutex {};
130130
static inline std::deque<TShutdownHandler> mShutdownHandlers {};
131131

132-
static inline Version mVersion { 3, 8, 5 };
132+
static inline Version mVersion { 3, 9, 0 };
133133
};
134134

135135
void SplitString(std::string const& str, const char delim, std::vector<std::string>& out);

include/TServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class TServer final {
4444
void ForEachClient(const std::function<bool(std::weak_ptr<TClient>)>& Fn);
4545
size_t ClientCount() const;
4646

47-
void GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uint8_t>&& Packet, TPPSMonitor& PPSMonitor, TNetwork& Network);
47+
void GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uint8_t>&& Packet, TPPSMonitor& PPSMonitor, TNetwork& Network, bool udp);
4848
static void HandleEvent(TClient& c, const std::string& Data);
4949
RWMutex& GetClientMutex() const { return mClientsMutex; }
5050

src/TNetwork.cpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include <boost/asio/ip/address_v6.hpp>
3333
#include <boost/asio/ip/v6_only.hpp>
3434
#include <cstring>
35+
#include <openssl/err.h>
36+
#include <openssl/rand.h>
3537
#include <zlib.h>
3638

3739
typedef boost::asio::detail::socket_option::integer<SOL_SOCKET, SO_RCVTIMEO> rcv_timeout_option;
@@ -146,23 +148,30 @@ void TNetwork::UDPServerMain() {
146148
}
147149

148150
if (Client->GetID() == ID) {
149-
// not initialized yet
150-
if (Client->GetUDPAddr() == ip::udp::endpoint {} || !Client->IsUDPConnected()) {
151-
// same IP (just a sanity check)
152-
if (remote_client_ep.address() == Client->GetTCPSock().remote_endpoint().address()) {
153-
Client->SetUDPAddr(remote_client_ep);
154-
Client->SetIsUDPConnected(true);
155-
beammp_debugf("UDP connected for client {}", ID);
156-
} else {
157-
beammp_debugf("Denied initial UDP packet due to IP mismatch");
151+
if (Client->GetUDPAddr() == ip::udp::endpoint {} && !Client->IsUDPConnected() && !Client->GetMagic().empty()) {
152+
if (Data.size() != 66) {
153+
beammp_debugf("Invalid size for UDP value. IP: {} ID: {}", remote_client_ep.address().to_string(), ID);
154+
return false;
155+
}
156+
157+
const std::vector Magic(Data.begin() + 2, Data.end());
158+
159+
if (Magic != Client->GetMagic()) {
160+
beammp_debugf("Invalid value for UDP IP: {} ID: {}", remote_client_ep.address().to_string(), ID);
158161
return false;
159162
}
163+
164+
Client->SetMagic({});
165+
Client->SetUDPAddr(remote_client_ep);
166+
Client->SetIsUDPConnected(true);
167+
return false;
160168
}
169+
161170
if (Client->GetUDPAddr() == remote_client_ep) {
162171
Data.erase(Data.begin(), Data.begin() + 2);
163-
mServer.GlobalParser(ClientPtr, std::move(Data), mPPSMonitor, *this);
172+
mServer.GlobalParser(ClientPtr, std::move(Data), mPPSMonitor, *this, true);
164173
} else {
165-
beammp_debugf("Ignored UDP packet due to remote address mismatch");
174+
beammp_debugf("Ignored UDP packet for Client {} due to remote address mismatch. Source: {}, Client: {}", ID, remote_client_ep.address().to_string(), Client->GetUDPAddr().address().to_string());
166175
return false;
167176
}
168177
}
@@ -660,7 +669,7 @@ void TNetwork::TCPClient(const std::weak_ptr<TClient>& c) {
660669
Client->Disconnect("TCPRcv failed");
661670
break;
662671
}
663-
mServer.GlobalParser(c, std::move(res), mPPSMonitor, *this);
672+
mServer.GlobalParser(c, std::move(res), mPPSMonitor, *this, false);
664673
}
665674

666675
if (QueueSync.joinable())
@@ -751,6 +760,18 @@ void TNetwork::OnConnect(const std::weak_ptr<TClient>& c) {
751760
SyncResources(*LockedClient);
752761
if (LockedClient->IsDisconnected())
753762
return;
763+
std::vector<unsigned char> buf(64);
764+
int ret = RAND_bytes(buf.data(), buf.size());
765+
if (ret != 1) {
766+
unsigned long error = ERR_get_error();
767+
beammp_errorf("RAND_bytes failed with error code {}", error);
768+
beammp_assert(ret != 1);
769+
return;
770+
}
771+
772+
LockedClient->SetMagic(buf);
773+
buf.insert(buf.begin(), 'U');
774+
(void)Respond(*LockedClient, buf, true);
754775
(void)Respond(*LockedClient, StringToVector("M" + Application::Settings.getAsString(Settings::Key::General_Map)), true); // Send the Map on connect
755776
beammp_info(LockedClient->GetName() + " : Connected");
756777
LuaAPI::MP::Engine->ReportErrors(LuaAPI::MP::Engine->TriggerEvent("onPlayerJoining", "", LockedClient->GetID()));

src/TServer.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ size_t TServer::ClientCount() const {
161161
return mClients.size();
162162
}
163163

164-
void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uint8_t>&& Packet, TPPSMonitor& PPSMonitor, TNetwork& Network) {
164+
void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uint8_t>&& Packet, TPPSMonitor& PPSMonitor, TNetwork& Network, bool udp) {
165165
constexpr std::string_view ABG = "ABG:";
166166
if (Packet.size() >= ABG.size() && std::equal(Packet.begin(), Packet.begin() + ABG.size(), ABG.begin(), ABG.end())) {
167167
Packet.erase(Packet.begin(), Packet.begin() + ABG.size());
@@ -213,6 +213,10 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
213213
}
214214
switch (Code) {
215215
case 'H': // initial connection
216+
if (udp) {
217+
beammp_debugf("Received 'H' packet over UDP from client '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID());
218+
return;
219+
}
216220
if (!Network.SyncClient(Client)) {
217221
// TODO handle
218222
}
@@ -226,12 +230,20 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
226230
}
227231
return;
228232
case 'O':
233+
if (udp) {
234+
beammp_debugf("Received 'O' packet over UDP from client '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID());
235+
return;
236+
}
229237
if (Packet.size() > 1000) {
230238
beammp_debug(("Received data from: ") + LockedClient->GetName() + (" Size: ") + std::to_string(Packet.size()));
231239
}
232240
ParseVehicle(*LockedClient, StringPacket, Network);
233241
return;
234242
case 'C': {
243+
if (udp) {
244+
beammp_debugf("Received 'C' packet over UDP from client '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID());
245+
return;
246+
}
235247
if (Packet.size() < 4 || std::find(Packet.begin() + 3, Packet.end(), ':') == Packet.end())
236248
break;
237249
const auto PacketAsString = std::string(reinterpret_cast<const char*>(Packet.data()), Packet.size());
@@ -262,6 +274,10 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
262274
return;
263275
}
264276
case 'E':
277+
if (udp) {
278+
beammp_debugf("Received 'E' packet over UDP from client '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID());
279+
return;
280+
}
265281
HandleEvent(*LockedClient, StringPacket);
266282
return;
267283
case 'N':
@@ -304,6 +320,15 @@ void TServer::HandleEvent(TClient& c, const std::string& RawData) {
304320
}
305321
std::string Name = RawData.substr(2, NameDataSep - 2);
306322
std::string Data = RawData.substr(NameDataSep + 1);
323+
324+
std::vector<std::string> exclude = {"onInit", "onFileChanged","onVehicleDeleted","onConsoleInput","onPlayerAuth","postPlayerAuth", "onPlayerDisconnect",
325+
"onPlayerConnecting","onPlayerJoining","onPlayerJoin","onChatMessage","postChatMessage","onVehicleSpawn","postVehicleSpawn","onVehicleEdited", "postVehicleEdited",
326+
"onVehicleReset","onVehiclePaintChanged","onShutdown"};
327+
328+
if (std::ranges::find(exclude, Name) != exclude.end()) {
329+
beammp_debugf("Excluded event triggered by client '{}' ({}): '{}', ignoring.", c.GetName(), c.GetID(), Name);
330+
return;
331+
}
307332
LuaAPI::MP::Engine->ReportErrors(LuaAPI::MP::Engine->TriggerEvent(Name, "", c.GetID(), Data));
308333
}
309334

0 commit comments

Comments
 (0)