Files
faumaray d146f11f02 Release 1.0.0
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-04 01:49:28 +03:00

142 lines
3.5 KiB
C++

#pragma once
#include <filesystem>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
// Opaque quickjs handles — only the .cpp needs <quickjs.h>, 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<std::string>
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<std::string>
executeJS(std::string_view code,
std::string_view filename = "<script>");
/**
* Cleanup QuickJS resources
*/
void cleanupQuickJS();
JSRuntime* runtime_ = nullptr;
JSContext* context_ = nullptr;
// Player JS code and extracted functions
std::string playerJsCode_;
std::string preprocessedPlayer_;
// State
bool initialized_ = false;
bool solverInitialized_ = false;
bool hasPlayerJs_ = false;
mutable std::mutex mutex_;
};
} // namespace Echoion::Core