diff --git a/src/invidious.cr b/src/invidious.cr index 1f8ae251..4b5e7b94 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -213,8 +213,10 @@ if !CONFIG.external_videoplayback_proxy.empty? Invidious::Jobs.register Invidious::Jobs::CheckExternalProxy.new end -if CONFIG.refresh_tokens +if !CONFIG.tokens_server.empty? Invidious::Jobs.register Invidious::Jobs::RefreshTokens.new +else + LOGGER.info("jobs: Disabling RefreshTokens job. Invidious will use the tokens that are on the configuration file") end Invidious::Jobs.start_all diff --git a/src/invidious/config.cr b/src/invidious/config.cr index 5f3a5b94..e1d51e44 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -192,15 +192,14 @@ class Config # at the start of the URI property external_videoplayback_proxy : Array(NamedTuple(url: String, balance: Bool)) = [] of NamedTuple(url: String, balance: Bool) - # Job to refresh tokens from a Redis compatible DB - property refresh_tokens : Bool = true - property pubsub_domain : String = "" property ignore_user_tokens : Bool = false property server_id_cookie_name : String = "INVIDIOUS_SERVER_ID" + property tokens_server : String = "" + {% if flag?(:linux) %} property reload_config_automatically : Bool = true {% end %} diff --git a/src/invidious/helpers/redis_tokens.cr b/src/invidious/helpers/redis_tokens.cr deleted file mode 100644 index b79dab51..00000000 --- a/src/invidious/helpers/redis_tokens.cr +++ /dev/null @@ -1,36 +0,0 @@ -module Tokens - extend self - @@po_token : String | Nil - @@visitor_data : String | Nil - - def refresh_tokens - @@po_token = REDIS_DB.get("invidious:po_token") - @@visitor_data = REDIS_DB.get("invidious:visitor_data") - if !@@po_token.nil? && !@@visitor_data.nil? - set_tokens - LOGGER.debug("RefreshTokens: Successfully updated po_token and visitor_data") - else - LOGGER.warn("RefreshTokens: Tokens are empty!") - end - LOGGER.trace("RefreshTokens: Tokens are:") - LOGGER.trace("RefreshTokens: po_token: #{CONFIG.po_token}") - LOGGER.trace("RefreshTokens: visitor_data: #{CONFIG.visitor_data}") - end - - def set_tokens - CONFIG.po_token = @@po_token - CONFIG.visitor_data = @@visitor_data - end - - def get_tokens - return {@@po_token, @@visitor_data} - end - - def get_po_token - return @@po_token - end - - def get_visitor_data - return @@visitor_data - end -end diff --git a/src/invidious/helpers/session_tokens.cr b/src/invidious/helpers/session_tokens.cr new file mode 100644 index 00000000..812d4e4f --- /dev/null +++ b/src/invidious/helpers/session_tokens.cr @@ -0,0 +1,35 @@ +module Tokens + extend self + @@po_token : String | Nil + @@visitor_data : String | Nil + + def refresh_tokens + begin + response = HTTP::Client.get "#{CONFIG.tokens_server}/generate" + if !response.status_code == 200 + LOGGER.error("RefreshTokens: Expected response to have status code 200 but got #{response.status_code} from #{CONFIG.tokens_server}") + end + json = JSON.parse(response.body) + @@po_token = json.try &.["potoken"].as_s || nil + @@visitor_data = json.try &.["visitorData"].as_s || nil + rescue ex + LOGGER.error("RefreshTokens: Failed to fetch tokens from #{CONFIG.tokens_server}: #{ex.message}") + return + end + + if !@@po_token.nil? && !@@visitor_data.nil? + set_tokens + LOGGER.debug("RefreshTokens: Successfully updated po_token and visitor_data") + else + LOGGER.warn("RefreshTokens: Tokens are empty!. Invidious will use the tokens that are on the configuration file") + end + LOGGER.trace("RefreshTokens: Tokens are:") + LOGGER.trace("RefreshTokens: po_token: #{CONFIG.po_token}") + LOGGER.trace("RefreshTokens: visitor_data: #{CONFIG.visitor_data}") + end + + def set_tokens + CONFIG.po_token = @@po_token + CONFIG.visitor_data = @@visitor_data + end +end diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index a670ac36..a0888bfa 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -54,11 +54,6 @@ def extract_video_info(video_id : String) # Init client config for the API client_config = YoutubeAPI::ClientConfig.new - redis_po_token, redis_visitor_data = Tokens.get_tokens - - po_token = redis_po_token || CONFIG.po_token - visitor_data = redis_visitor_data || CONFIG.visitor_data - # Fetch data from the player endpoint player_response = YoutubeAPI.player(video_id: video_id, params: "2AMB", client_config: client_config) @@ -109,7 +104,7 @@ def extract_video_info(video_id : String) # Don't use Android test suite client if po_token is passed because po_token doesn't # work for Android test suite client. - if reason.nil? && po_token.nil? + if reason.nil? && CONFIG.po_token.nil? # Fetch the video streams using an Android client in order to get the # decrypted URLs and maybe fix throttling issues (#2194). See the # following issue for an explanation about decrypted URLs: