From 4fa7aa65bdfa34f4aba07bcc0f9e7d6e068eca1e Mon Sep 17 00:00:00 2001 From: mooleshacat <43627985+mooleshacat@users.noreply.github.com> Date: Tue, 15 Oct 2024 13:32:29 -0700 Subject: [PATCH] cherry picked commits for dev-token-updater --- README.md | 3 + src/invidious.cr | 4 ++ src/invidious/jobs/token_monitor.cr | 17 +++++ src/invidious/reloadpotoken.cr | 83 +++++++++++++++++++++++++ src/invidious/yt_backend/youtube_api.cr | 10 +-- 5 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 src/invidious/jobs/token_monitor.cr create mode 100644 src/invidious/reloadpotoken.cr diff --git a/README.md b/README.md index b139c5f6..5677b937 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,9 @@ ## Features +**Patches** +- token updater patch (mooleshacat) + **User features** - Lightweight - No ads diff --git a/src/invidious.cr b/src/invidious.cr index 63f2a9cc..0b420ddf 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -189,6 +189,10 @@ Invidious::Jobs.register Invidious::Jobs::NotificationJob.new(CONNECTION_CHANNEL Invidious::Jobs.register Invidious::Jobs::ClearExpiredItemsJob.new +ReloadPOToken.get_tokens #init + +Invidious::Jobs.register Invidious::Jobs::MonitorCfgPotokensJob.new() + Invidious::Jobs.register Invidious::Jobs::InstanceListRefreshJob.new Invidious::Jobs.start_all diff --git a/src/invidious/jobs/token_monitor.cr b/src/invidious/jobs/token_monitor.cr new file mode 100644 index 00000000..a94f7ed1 --- /dev/null +++ b/src/invidious/jobs/token_monitor.cr @@ -0,0 +1,17 @@ + +class Invidious::Jobs::MonitorCfgPotokensJob < Invidious::Jobs::BaseJob + include Invidious + def begin + loop do + + LOGGER.info("jobs: running MonitorCfgPotokens job") + + ReloadPOToken.get_tokens + + LOGGER.info("jobs: MonitorCfgPotokens: pot: " + ReloadPOToken.pot.as(String)) + LOGGER.info("jobs: MonitorCfgPotokens: vdata: " + ReloadPOToken.vdata.as(String)) + + sleep 15.seconds + end + end +end diff --git a/src/invidious/reloadpotoken.cr b/src/invidious/reloadpotoken.cr new file mode 100644 index 00000000..2b874ad8 --- /dev/null +++ b/src/invidious/reloadpotoken.cr @@ -0,0 +1,83 @@ +class ReloadPOToken + + @@instance = new + + def self.pot + @@pot + end + + def self.vdata + @@vdata + end + + def initialize + + @@pot = "error" + @@vdata = "error" + + end + + def self.get_tokens + + # Load config from file or YAML string env var + env_config_file = "INVIDIOUS_CONFIG_FILE" + env_config_yaml = "INVIDIOUS_CONFIG" + + config_file = ENV.has_key?(env_config_file) ? ENV.fetch(env_config_file) : "config/config.yml" + config_yaml = ENV.has_key?(env_config_yaml) ? ENV.fetch(env_config_yaml) : File.read(config_file) + + config = Config.from_yaml(config_yaml) + + + + + # Update config from env vars (upcased and prefixed with "INVIDIOUS_") + {% for ivar in Config.instance_vars %} + {% env_id = "INVIDIOUS_#{ivar.id.upcase}" %} + + if ENV.has_key?({{env_id}}) + env_value = ENV.fetch({{env_id}}) + success = false + + # Use YAML converter if specified + {% ann = ivar.annotation(::YAML::Field) %} + {% if ann && ann[:converter] %} + config.{{ivar.id}} = {{ann[:converter]}}.from_yaml(YAML::ParseContext.new, YAML::Nodes.parse(ENV.fetch({{env_id}})).nodes[0]) + success = true + + # Use regular YAML parser otherwise + {% else %} + {% ivar_types = ivar.type.union? ? ivar.type.union_types : [ivar.type] %} + # Sort types to avoid parsing nulls and numbers as strings + {% ivar_types = ivar_types.sort_by { |ivar_type| ivar_type == Nil ? 0 : ivar_type == Int32 ? 1 : 2 } %} + {{ivar_types}}.each do |ivar_type| + if !success + begin + config.{{ivar.id}} = ivar_type.from_yaml(env_value) + success = true + rescue + # nop + end + end + end + {% end %} + + # Exit on fail + if !success + puts %(Config.{{ivar.id}} failed to parse #{env_value} as {{ivar.type}}) + exit(1) + end + end + {% end %} + + + @@pot = config.po_token + @@vdata = config.visitor_data + + end + + def self.get_instance + return @@instance + end + +end \ No newline at end of file diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr index baa3cd92..be817d78 100644 --- a/src/invidious/yt_backend/youtube_api.cr +++ b/src/invidious/yt_backend/youtube_api.cr @@ -330,8 +330,8 @@ module YoutubeAPI client_context["client"]["platform"] = platform end - if CONFIG.visitor_data.is_a?(String) - client_context["client"]["visitorData"] = CONFIG.visitor_data.as(String) + if ReloadPOToken.vdata.is_a?(String) + client_context["client"]["visitorData"] = ReloadPOToken.vdata.as(String) end return client_context @@ -492,7 +492,7 @@ module YoutubeAPI "contentPlaybackContext" => playback_ctx, }, "serviceIntegrityDimensions" => { - "poToken" => CONFIG.po_token, + "poToken" => ReloadPOToken.pot.as(String), }, } @@ -626,8 +626,8 @@ module YoutubeAPI headers["User-Agent"] = user_agent end - if CONFIG.visitor_data.is_a?(String) - headers["X-Goog-Visitor-Id"] = CONFIG.visitor_data.as(String) + if ReloadPOToken.vdata.is_a?(String) + headers["X-Goog-Visitor-Id"] = ReloadPOToken.vdata.as(String) end # Logging