Skip to content

Commit

Permalink
Add ability to set po_token and visitordata ID (#4789)
Browse files Browse the repository at this point in the history
This PR adds two new config option, to pass a PO token (config 'po_token') and
a visitor ID (config 'visitor_data') to Youtube. These two strings are required
to play videos using the WEB client.

Warning: These strings gives much more identifiable information to Google!

If the po_token setting is filled in, then the WEB client is used. If not, the
Android client is used. TvHtml5ScreenEmbed will still be used as a fallback.

Script for generating po_token and visitor_data:
https://github.com/iv-org/youtube-trusted-session-generator

Helps with issue 4734
  • Loading branch information
SamantazFox committed Aug 13, 2024
2 parents 2d7869b + e6c39f9 commit 2d18ff1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
12 changes: 12 additions & 0 deletions config/config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ https_only: false
##
# use_innertube_for_captions: false

##
## Send Google session informations. This is useful when Invidious is blocked
## by the message "This helps protect our community."
## See https://github.com/iv-org/invidious/issues/4734.
##
## Warning: These strings gives much more identifiable information to Google!
##
## Accepted values: String
## Default: <none>
##
# po_token: ""
# visitor_data: ""

# -----------------------------
# Logging
Expand Down
5 changes: 5 additions & 0 deletions src/invidious/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ class Config
# Use Innertube's transcripts API instead of timedtext for closed captions
property use_innertube_for_captions : Bool = false

# visitor data ID for Google session
property visitor_data : String? = nil
# poToken for passing bot attestation
property po_token : String? = nil

# Saved cookies in "name1=value1; name2=value2..." format
@[YAML::Field(converter: Preferences::StringToCookies)]
property cookies : HTTP::Cookies = HTTP::Cookies.new
Expand Down
1 change: 1 addition & 0 deletions src/invidious/videos.cr
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ struct Video

n = DECRYPT_FUNCTION.try &.decrypt_nsig(params["n"])
params["n"] = n if n
params["pot"] = CONFIG.po_token if CONFIG.po_token

Check failure on line 118 in src/invidious/videos.cr

View workflow job for this annotation

GitHub Actions / build - crystal: 1.9.2, stable: true

expected argument #2 to 'URI::Params#[]=' to be (Array(String) | String), not (String | Nil)

Check failure on line 118 in src/invidious/videos.cr

View workflow job for this annotation

GitHub Actions / build - crystal: 1.10.1, stable: true

expected argument #2 to 'URI::Params#[]=' to be (Array(String) | String), not (String | Nil)

Check failure on line 118 in src/invidious/videos.cr

View workflow job for this annotation

GitHub Actions / build - crystal: 1.11.2, stable: true

expected argument #2 to 'URI::Params#[]=' to be (Array(String) | String), not (String | Nil)

Check failure on line 118 in src/invidious/videos.cr

View workflow job for this annotation

GitHub Actions / build - crystal: 1.12.1, stable: true

expected argument #2 to 'URI::Params#[]=' to be (Array(String) | String), not (String | Nil)

Check failure on line 118 in src/invidious/videos.cr

View workflow job for this annotation

GitHub Actions / build - crystal: nightly, stable: false

expected argument #2 to 'URI::Params#[]=' to be (Array(String) | String), not (String | Nil)

params["host"] = url.host.not_nil!
if region = self.info["region"]?.try &.as_s
Expand Down
11 changes: 8 additions & 3 deletions src/invidious/videos/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def extract_video_info(video_id : String)
client_config = YoutubeAPI::ClientConfig.new

# Fetch data from the player endpoint
player_response = YoutubeAPI.player(video_id: video_id, params: "", client_config: client_config)
player_response = YoutubeAPI.player(video_id: video_id, params: "2AMB", client_config: client_config)

playability_status = player_response.dig?("playabilityStatus", "status").try &.as_s

Expand Down Expand Up @@ -102,7 +102,9 @@ def extract_video_info(video_id : String)

new_player_response = nil

if reason.nil?
# Don't use Android client if po_token is passed because po_token doesn't
# work for Android client.
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:
Expand All @@ -112,7 +114,10 @@ def extract_video_info(video_id : String)
end

# Last hope
if new_player_response.nil?
# Only trigger if reason found and po_token or didn't work wth Android client.
# TvHtml5ScreenEmbed now requires sig helper for it to work but po_token is not required
# if the IP address is not blocked.
if CONFIG.po_token && reason || CONFIG.po_token.nil? && new_player_response.nil?
client_config.client_type = YoutubeAPI::ClientType::TvHtml5ScreenEmbed
new_player_response = try_fetch_streaming_data(video_id, client_config)
end
Expand Down
11 changes: 11 additions & 0 deletions src/invidious/yt_backend/youtube_api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ module YoutubeAPI
client_context["client"]["platform"] = platform
end

if CONFIG.visitor_data.is_a?(String)
client_context["client"]["visitorData"] = CONFIG.visitor_data.as(String)
end

return client_context
end

Expand Down Expand Up @@ -477,6 +481,9 @@ module YoutubeAPI
"playbackContext" => {
"contentPlaybackContext" => playback_ctx,
},
"serviceIntegrityDimensions" => {
"poToken" => CONFIG.po_token,
},
}

# Append the additional parameters if those were provided
Expand Down Expand Up @@ -609,6 +616,10 @@ 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)
end

# Logging
LOGGER.debug("YoutubeAPI: Using endpoint: \"#{endpoint}\"")
LOGGER.trace("YoutubeAPI: ClientConfig: #{client_config}")
Expand Down

0 comments on commit 2d18ff1

Please sign in to comment.