pass host header to proxy_video_url

This commit is contained in:
Fijxu 2025-05-18 18:28:26 -04:00
parent 53d5efaa72
commit c9c69e850d
No known key found for this signature in database
GPG Key ID: 32C1DDF333EDA6A4
5 changed files with 15 additions and 10 deletions

View File

@ -4,7 +4,7 @@ module Invidious::HttpServer
module Utils module Utils
extend self extend self
def proxy_video_url(raw_url : String, *, region : String? = nil, absolute : Bool = false) def proxy_video_url(raw_url : String, *, region : String? = nil, absolute : Bool = false, host : String? = "")
url = URI.parse(raw_url) url = URI.parse(raw_url)
# Add some URL parameters # Add some URL parameters
@ -14,7 +14,7 @@ module Invidious::HttpServer
url.query_params = params url.query_params = params
if absolute if absolute
return "#{HOST_URL}#{url.request_target}" return "#{host}#{url.request_target}"
else else
return url.request_target return url.request_target
end end

View File

@ -1,6 +1,8 @@
module Invidious::Routes::API::Manifest module Invidious::Routes::API::Manifest
# /api/manifest/dash/id/:id # /api/manifest/dash/id/:id
def self.get_dash_video_id(env) def self.get_dash_video_id(env)
host = env.request.headers["Host"]
env.response.headers.add("Access-Control-Allow-Origin", "*") env.response.headers.add("Access-Control-Allow-Origin", "*")
env.response.content_type = "application/dash+xml" env.response.content_type = "application/dash+xml"
@ -36,7 +38,7 @@ module Invidious::Routes::API::Manifest
# Other API clients can get the original URLs by omiting `local=true`. # Other API clients can get the original URLs by omiting `local=true`.
manifest = response.body.gsub(/<BaseURL>[^<]+<\/BaseURL>/) do |baseurl| manifest = response.body.gsub(/<BaseURL>[^<]+<\/BaseURL>/) do |baseurl|
url = baseurl.lchop("<BaseURL>").rchop("</BaseURL>") url = baseurl.lchop("<BaseURL>").rchop("</BaseURL>")
url = HttpServer::Utils.proxy_video_url(url, absolute: true) if local url = HttpServer::Utils.proxy_video_url(url, absolute: true, host: host) if local
"<BaseURL>#{url}</BaseURL>" "<BaseURL>#{url}</BaseURL>"
end end
@ -46,7 +48,7 @@ module Invidious::Routes::API::Manifest
# Ditto, only proxify URLs if `local=true` is used # Ditto, only proxify URLs if `local=true` is used
if local if local
video.adaptive_fmts.each do |fmt| video.adaptive_fmts.each do |fmt|
fmt["url"] = JSON::Any.new(HttpServer::Utils.proxy_video_url(fmt["url"].as_s, absolute: true)) fmt["url"] = JSON::Any.new(HttpServer::Utils.proxy_video_url(fmt["url"].as_s, absolute: true, host: host))
end end
end end

View File

@ -34,6 +34,7 @@ module Invidious::Routes::Embed
def self.show(env) def self.show(env)
locale = env.get("preferences").as(Preferences).locale locale = env.get("preferences").as(Preferences).locale
host = env.request.headers["Host"]
id = env.params.url["id"] id = env.params.url["id"]
plid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "") plid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "")
@ -161,11 +162,11 @@ module Invidious::Routes::Embed
adaptive_fmts = video.adaptive_fmts adaptive_fmts = video.adaptive_fmts
if params.local if params.local
fmt_stream.each { |fmt| fmt["url"] = JSON::Any.new(HttpServer::Utils.proxy_video_url(fmt["url"].as_s)) } fmt_stream.each { |fmt| fmt["url"] = JSON::Any.new(HttpServer::Utils.proxy_video_url(fmt["url"].as_s, host: host)) }
end end
# Always proxy DASH streams, otherwise youtube CORS headers will prevent playback # Always proxy DASH streams, otherwise youtube CORS headers will prevent playback
adaptive_fmts.each { |fmt| fmt["url"] = JSON::Any.new(HttpServer::Utils.proxy_video_url(fmt["url"].as_s)) } adaptive_fmts.each { |fmt| fmt["url"] = JSON::Any.new(HttpServer::Utils.proxy_video_url(fmt["url"].as_s, host: host)) }
video_streams = video.video_streams video_streams = video.video_streams
audio_streams = video.audio_streams audio_streams = video.audio_streams

View File

@ -2,6 +2,7 @@ module Invidious::Routes::VideoPlayback
# /videoplayback # /videoplayback
def self.get_video_playback(env) def self.get_video_playback(env)
locale = env.get("preferences").as(Preferences).locale locale = env.get("preferences").as(Preferences).locale
host_ = env.request.headers["Host"]
query_params = env.params.query query_params = env.params.query
fvip = query_params["fvip"]? || "3" fvip = query_params["fvip"]? || "3"
@ -107,7 +108,7 @@ module Invidious::Routes::VideoPlayback
env.response.headers["Access-Control-Allow-Origin"] = "*" env.response.headers["Access-Control-Allow-Origin"] = "*"
if location = resp.headers["Location"]? if location = resp.headers["Location"]?
url = Invidious::HttpServer::Utils.proxy_video_url(location, region: region) url = Invidious::HttpServer::Utils.proxy_video_url(location, region: region, host: host_)
return env.redirect url return env.redirect url
end end
@ -165,7 +166,7 @@ module Invidious::Routes::VideoPlayback
env.response.headers["Access-Control-Allow-Origin"] = "*" env.response.headers["Access-Control-Allow-Origin"] = "*"
if location = resp.headers["Location"]? if location = resp.headers["Location"]?
url = Invidious::HttpServer::Utils.proxy_video_url(location, region: region) url = Invidious::HttpServer::Utils.proxy_video_url(location, region: region, host: host_)
if title = query_params["title"]? if title = query_params["title"]?
url = "#{url}&title=#{URI.encode_www_form(title)}" url = "#{url}&title=#{URI.encode_www_form(title)}"

View File

@ -4,6 +4,7 @@ module Invidious::Routes::Watch
def self.handle(env) def self.handle(env)
locale = env.get("preferences").as(Preferences).locale locale = env.get("preferences").as(Preferences).locale
region = env.params.query["region"]? region = env.params.query["region"]?
host = env.request.headers["Host"]
if env.params.query.to_s.includes?("%20") || env.params.query.to_s.includes?("+") if env.params.query.to_s.includes?("%20") || env.params.query.to_s.includes?("+")
url = "/watch?" + env.params.query.to_s.gsub("%20", "").delete("+") url = "/watch?" + env.params.query.to_s.gsub("%20", "").delete("+")
@ -121,11 +122,11 @@ module Invidious::Routes::Watch
adaptive_fmts = video.adaptive_fmts adaptive_fmts = video.adaptive_fmts
if params.local if params.local
fmt_stream.each { |fmt| fmt["url"] = JSON::Any.new(HttpServer::Utils.proxy_video_url(fmt["url"].as_s)) } fmt_stream.each { |fmt| fmt["url"] = JSON::Any.new(HttpServer::Utils.proxy_video_url(fmt["url"].as_s, host: host)) }
end end
# Always proxy DASH streams, otherwise youtube CORS headers will prevent playback # Always proxy DASH streams, otherwise youtube CORS headers will prevent playback
adaptive_fmts.each { |fmt| fmt["url"] = JSON::Any.new(HttpServer::Utils.proxy_video_url(fmt["url"].as_s)) } adaptive_fmts.each { |fmt| fmt["url"] = JSON::Any.new(HttpServer::Utils.proxy_video_url(fmt["url"].as_s, host: host)) }
video_streams = video.video_streams video_streams = video.video_streams
audio_streams = video.audio_streams audio_streams = video.audio_streams