#pragma once #include #include #include #include #include #include #include // Opaque quickjs handles — only the .cpp needs , and keeping it // out of this header keeps the quickjs include dir private to EchoionCore. struct JSRuntime; struct JSContext; namespace Echoion::Core { /** * Signature cipher for YouTube stream URLs * Uses QuickJS (when available) to execute the JavaScript decipher * functions from player JS * * Based on yt-dlp's signature extraction logic */ class SignatureCipher { public: SignatureCipher(); ~SignatureCipher(); // Disable copy, enable move SignatureCipher(const SignatureCipher&) = delete; SignatureCipher& operator=(const SignatureCipher&) = delete; SignatureCipher(SignatureCipher&&) noexcept; SignatureCipher& operator=(SignatureCipher&&) noexcept; /** * Initialize the QuickJS solver with core libraries * Must be called before using the cipher */ void initialize(); /** * Set the player JS code and extract decipher functions * @param playerJsCode The raw player JavaScript code from YouTube * @return true if extraction succeeded */ bool setPlayerJs(std::string_view playerJsCode); /** * Check if the cipher is ready (has valid decipher function) */ [[nodiscard]] bool isReady() const; /** * Decipher a signature cipher string * Format: s=ENCODED_SIG&sp=SIG_PARAM&url=ENCODED_URL * @param signatureCipher The signature cipher string * @return The full deciphered URL or empty string on failure */ [[nodiscard]] std::string decipher(std::string_view signatureCipher); /** * Solve a challenge (signature or n-param) using the preprocessed * player * @param challenge The challenge string to solve * @param type "sig" for signature, "n" for n-parameter * @return The solved challenge or empty string on failure */ [[nodiscard]] std::string solveChallenge(const std::string& challenge, const std::string& type); /** * Decipher the n-parameter for throttling * @param nParam The encoded n parameter * @return The deciphered n parameter */ [[nodiscard]] std::string decipherNParam(std::string_view nParam); /** * Clear the cipher state */ void clear(); /** * Check if cipher has been initialized */ [[nodiscard]] bool isInitialized() const; private: /** * Initialize QuickJS runtime and context with solver libraries */ void initSolver(); /** * Download solver libraries from remote URL * @param url The URL to download from * @return The downloaded JS code or empty string on failure */ [[nodiscard]] std::optional downloadSolverLibrary(std::string_view url); /** * Get cache directory for storing solver libraries */ [[nodiscard]] std::filesystem::path getCacheDir(); /** * Execute JavaScript code in QuickJS and return result */ [[nodiscard]] std::optional executeJS(std::string_view code, std::string_view filename = "