|
| 1 | +/***************************************************************************** |
| 2 | + * |
| 3 | + * PROJECT: Multi Theft Auto v1.0 |
| 4 | + * LICENSE: See LICENSE in the top level directory |
| 5 | + * FILE: cefweb/CWebAppAuth.h |
| 6 | + * PURPOSE: IPC authentication code generation for browser process |
| 7 | + * |
| 8 | + *****************************************************************************/ |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <cef3/cef/include/cef_command_line.h> |
| 12 | +#include <array> |
| 13 | +#include <chrono> |
| 14 | +#include <mutex> |
| 15 | +#include <random> |
| 16 | +#include <string> |
| 17 | + |
| 18 | +// Forward declarations |
| 19 | +class CWebCore; |
| 20 | +class CCoreInterface; |
| 21 | +extern CCoreInterface* g_pCore; |
| 22 | + |
| 23 | +// Note: IsReadablePointer is available via StdInc.h which includes SharedUtil.h |
| 24 | + |
| 25 | +namespace WebAppAuth |
| 26 | +{ |
| 27 | + // Shared auth code storage (used by both generation and validation) |
| 28 | + inline std::string& GetSharedAuthCode() |
| 29 | + { |
| 30 | + static std::string sharedAuthCode; |
| 31 | + return sharedAuthCode; |
| 32 | + } |
| 33 | + |
| 34 | + inline std::mutex& GetSharedAuthMutex() |
| 35 | + { |
| 36 | + static std::mutex sharedMutex; |
| 37 | + return sharedMutex; |
| 38 | + } |
| 39 | + |
| 40 | + inline bool& GetSyncedToWebCoreFlag() |
| 41 | + { |
| 42 | + static bool syncedToWebCore = false; |
| 43 | + return syncedToWebCore; |
| 44 | + } |
| 45 | + |
| 46 | + // Thread-safe auth code generation |
| 47 | + inline std::mutex& GetAuthCodeMutex() |
| 48 | + { |
| 49 | + static std::mutex mutex; |
| 50 | + return mutex; |
| 51 | + } |
| 52 | + |
| 53 | + // Auth code configuration |
| 54 | + inline constexpr std::size_t AUTH_CODE_LENGTH = 30; |
| 55 | + inline constexpr char AUTH_CODE_MIN_CHAR = 'A'; |
| 56 | + inline constexpr char AUTH_CODE_MAX_CHAR = 'Z'; |
| 57 | + |
| 58 | + // Generates random 30-character auth code (A-Z) |
| 59 | + [[nodiscard]] inline std::string GenerateAuthCode() |
| 60 | + { |
| 61 | + std::array<char, AUTH_CODE_LENGTH> buffer{}; |
| 62 | + |
| 63 | + // Use mt19937 with time-based seed (fast, cryptographic strength not needed for DoS prevention) |
| 64 | + static std::mt19937 rng(static_cast<unsigned int>(std::chrono::high_resolution_clock::now().time_since_epoch().count())); |
| 65 | + std::uniform_int_distribution<int> dist(0, AUTH_CODE_MAX_CHAR - AUTH_CODE_MIN_CHAR); |
| 66 | + |
| 67 | + for (auto& ch : buffer) |
| 68 | + ch = static_cast<char>(AUTH_CODE_MIN_CHAR + dist(rng)); |
| 69 | + |
| 70 | + return std::string(buffer.data(), buffer.size()); |
| 71 | + } |
| 72 | + |
| 73 | + // Generates and attaches auth code to child process command line |
| 74 | + inline void AttachAuthCodeToCommandLine(CefRefPtr<CefCommandLine> commandLine) |
| 75 | + { |
| 76 | + if (!commandLine) [[unlikely]] |
| 77 | + return; |
| 78 | + |
| 79 | + const std::lock_guard<std::mutex> lock{GetSharedAuthMutex()}; |
| 80 | + |
| 81 | + // Always use webCore->m_AuthCode (already populated in CWebCore constructor) |
| 82 | + // No need for fallback - webCore is guaranteed to exist before this is called |
| 83 | + if (!::g_pCore || !IsReadablePointer(::g_pCore, sizeof(void*))) [[unlikely]] |
| 84 | + return; |
| 85 | + |
| 86 | + auto* const webCore = static_cast<CWebCore*>(::g_pCore->GetWebCore()); |
| 87 | + if (!webCore || !IsReadablePointer(webCore, sizeof(void*))) [[unlikely]] |
| 88 | + return; |
| 89 | + |
| 90 | + // webCore->m_AuthCode already populated in CWebCore::CWebCore() |
| 91 | + if (webCore->m_AuthCode.empty()) [[unlikely]] |
| 92 | + return; |
| 93 | + |
| 94 | + commandLine->AppendSwitchWithValue("kgfiv8n", webCore->m_AuthCode); |
| 95 | + } |
| 96 | +} |
0 commit comments