Optimize RNG and limit packet input
This commit is contained in:
parent
698961b57b
commit
0ec1029a89
@ -83,7 +83,7 @@ namespace Components
|
||||
|
||||
bool Loader::PerformingUnitTests()
|
||||
{
|
||||
#if DEBUG
|
||||
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
|
||||
return Flags::HasFlag("tests");
|
||||
#else
|
||||
return false;
|
||||
|
@ -181,6 +181,21 @@ namespace Components
|
||||
|
||||
int Network::PacketInterceptionHandler(const char* packet)
|
||||
{
|
||||
// Packet rate limit.
|
||||
static uint32_t packets = 0;
|
||||
static int lastClean = 0;
|
||||
|
||||
if ((Game::Com_Milliseconds() - lastClean) > 1'000)
|
||||
{
|
||||
packets = 0;
|
||||
lastClean = Game::Com_Milliseconds();
|
||||
}
|
||||
|
||||
if ((++packets) > NETWORK_MAX_PACKETS_PER_SECOND)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string packetCommand = packet;
|
||||
auto pos = packetCommand.find_first_of("\\\n ");
|
||||
if (pos != std::string::npos)
|
||||
|
@ -1,3 +1,5 @@
|
||||
#define NETWORK_MAX_PACKETS_PER_SECOND 100'000
|
||||
|
||||
namespace Components
|
||||
{
|
||||
class Network : public Component
|
||||
|
@ -7,9 +7,11 @@ namespace Main
|
||||
void Initialize()
|
||||
{
|
||||
Main::EntryPointHook.Uninstall();
|
||||
|
||||
Utils::Cryptography::Rand::Initialize();
|
||||
Components::Loader::Initialize();
|
||||
|
||||
#ifdef DEBUG
|
||||
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
|
||||
if (Components::Loader::PerformingUnitTests())
|
||||
{
|
||||
DWORD result = (Components::Loader::PerformUnitTests() ? 0 : -1);
|
||||
|
@ -105,3 +105,6 @@
|
||||
#define VERSION_STR "4.2." REVISION_STR
|
||||
|
||||
#define Assert_Size(x, size) static_assert(sizeof(x) == size, STRINGIZE(x) " structure has an invalid size.")
|
||||
|
||||
// Enable unit-test flag for release builds
|
||||
//#define FORCE_UNIT_TESTS
|
||||
|
@ -8,42 +8,19 @@ namespace Utils
|
||||
{
|
||||
#pragma region Rand
|
||||
|
||||
prng_state Rand::State;
|
||||
|
||||
uint32_t Rand::GenerateInt()
|
||||
{
|
||||
static int length = 0;
|
||||
static int time = 0;
|
||||
static int access = 0;
|
||||
static uint8_t randPool[2048] = { 0 };
|
||||
uint32_t number = 0;
|
||||
fortuna_read(reinterpret_cast<uint8_t*>(&number), sizeof(number), &Rand::State);
|
||||
return number;
|
||||
}
|
||||
|
||||
int mseconds = Game::Com_Milliseconds();
|
||||
|
||||
if ((mseconds - time) > (1000 * 60 * 5) || (access > (sizeof(randPool) * 30)))
|
||||
{
|
||||
access = 0;
|
||||
length = 0;
|
||||
}
|
||||
|
||||
while (length != sizeof(randPool))
|
||||
{
|
||||
length += sprng_read(randPool, sizeof(randPool), NULL);
|
||||
}
|
||||
|
||||
uint8_t numberBuf[4] = { 0 };
|
||||
|
||||
for (int i = 0; i < sizeof(numberBuf); ++i)
|
||||
{
|
||||
numberBuf[i] = randPool[(rand() + mseconds + i + numberBuf[(i > 0 ? (i - 1) : 0)]) % sizeof(randPool)];
|
||||
}
|
||||
|
||||
uint32_t num = *reinterpret_cast<uint32_t*>(numberBuf);
|
||||
|
||||
if (!(access % 100))
|
||||
{
|
||||
srand(num);
|
||||
}
|
||||
|
||||
access++;
|
||||
return num;
|
||||
void Rand::Initialize()
|
||||
{
|
||||
register_prng(&fortuna_desc);
|
||||
rng_make_prng(128, find_prng("fortuna"), &Rand::State, NULL);
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
@ -6,6 +6,10 @@ namespace Utils
|
||||
{
|
||||
public:
|
||||
static uint32_t GenerateInt();
|
||||
static void Initialize();
|
||||
|
||||
private:
|
||||
static prng_state State;
|
||||
};
|
||||
|
||||
class ECDSA
|
||||
|
Loading…
Reference in New Issue
Block a user