summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSamantaz Fox <coding@samantaz.fr>2024-07-03 18:22:32 +0200
committerSamantaz Fox <coding@samantaz.fr>2024-07-25 22:13:08 +0200
commitec8b7916fa4b90f99a880abc6f7d7e7b2ca2919b (patch)
tree459dc721b2aea2ae375adab4af411e50476115bd /src
parent56a7488161428bb53d025246b9890f3f65edb3d4 (diff)
downloadinvidious-ec8b7916fa4b90f99a880abc6f7d7e7b2ca2919b.tar.gz
invidious-ec8b7916fa4b90f99a880abc6f7d7e7b2ca2919b.tar.bz2
invidious-ec8b7916fa4b90f99a880abc6f7d7e7b2ca2919b.zip
Videos: Make use of the video decoding
Diffstat (limited to 'src')
-rw-r--r--src/invidious.cr1
-rw-r--r--src/invidious/helpers/signatures.cr83
-rw-r--r--src/invidious/videos.cr65
3 files changed, 64 insertions, 85 deletions
diff --git a/src/invidious.cr b/src/invidious.cr
index c667ff1a..0c53197d 100644
--- a/src/invidious.cr
+++ b/src/invidious.cr
@@ -163,7 +163,6 @@ if CONFIG.feed_threads > 0
Invidious::Jobs.register Invidious::Jobs::RefreshFeedsJob.new(PG_DB)
end
-DECRYPT_FUNCTION = DecryptFunction.new(CONFIG.decrypt_polling)
if CONFIG.statistics_enabled
Invidious::Jobs.register Invidious::Jobs::StatisticsRefreshJob.new(PG_DB, SOFTWARE)
end
diff --git a/src/invidious/helpers/signatures.cr b/src/invidious/helpers/signatures.cr
index ee09415b..3b5c99eb 100644
--- a/src/invidious/helpers/signatures.cr
+++ b/src/invidious/helpers/signatures.cr
@@ -1,73 +1,28 @@
-alias SigProc = Proc(Array(String), Int32, Array(String))
+require "http/params"
+require "./sig_helper"
-struct DecryptFunction
- @decrypt_function = [] of {SigProc, Int32}
- @decrypt_time = Time.monotonic
+struct Invidious::DecryptFunction
+ @last_update = Time.monotonic - 42.days
- def initialize(@use_polling = true)
+ def initialize
+ self.check_update
end
- def update_decrypt_function
- @decrypt_function = fetch_decrypt_function
- end
-
- private def fetch_decrypt_function(id = "CvFH_6DNRCY")
- document = YT_POOL.client &.get("/watch?v=#{id}&gl=US&hl=en").body
- url = document.match(/src="(?<url>\/s\/player\/[^\/]+\/player_ias[^\/]+\/en_US\/base.js)"/).not_nil!["url"]
- player = YT_POOL.client &.get(url).body
-
- function_name = player.match(/^(?<name>[^=]+)=function\(\w\){\w=\w\.split\(""\);[^\. ]+\.[^( ]+/m).not_nil!["name"]
- function_body = player.match(/^#{Regex.escape(function_name)}=function\(\w\){(?<body>[^}]+)}/m).not_nil!["body"]
- function_body = function_body.split(";")[1..-2]
-
- var_name = function_body[0][0, 2]
- var_body = player.delete("\n").match(/var #{Regex.escape(var_name)}={(?<body>(.*?))};/).not_nil!["body"]
-
- operations = {} of String => SigProc
- var_body.split("},").each do |operation|
- op_name = operation.match(/^[^:]+/).not_nil![0]
- op_body = operation.match(/\{[^}]+/).not_nil![0]
-
- case op_body
- when "{a.reverse()"
- operations[op_name] = ->(a : Array(String), _b : Int32) { a.reverse }
- when "{a.splice(0,b)"
- operations[op_name] = ->(a : Array(String), b : Int32) { a.delete_at(0..(b - 1)); a }
- else
- operations[op_name] = ->(a : Array(String), b : Int32) { c = a[0]; a[0] = a[b % a.size]; a[b % a.size] = c; a }
- end
- end
-
- decrypt_function = [] of {SigProc, Int32}
- function_body.each do |function|
- function = function.lchop(var_name).delete("[].")
-
- op_name = function.match(/[^\(]+/).not_nil![0]
- value = function.match(/\(\w,(?<value>[\d]+)\)/).not_nil!["value"].to_i
-
- decrypt_function << {operations[op_name], value}
+ def check_update
+ now = Time.monotonic
+ if (now - @last_update) > 60.seconds
+ LOGGER.debug("Signature: Player might be outdated, updating")
+ Invidious::SigHelper::Client.force_update
+ @last_update = Time.monotonic
end
-
- return decrypt_function
end
- def decrypt_signature(fmt : Hash(String, JSON::Any))
- return "" if !fmt["s"]? || !fmt["sp"]?
-
- sp = fmt["sp"].as_s
- sig = fmt["s"].as_s.split("")
- if !@use_polling
- now = Time.monotonic
- if now - @decrypt_time > 60.seconds || @decrypt_function.size == 0
- @decrypt_function = fetch_decrypt_function
- @decrypt_time = Time.monotonic
- end
- end
-
- @decrypt_function.each do |proc, value|
- sig = proc.call(sig, value)
- end
-
- return "&#{sp}=#{sig.join("")}"
+ def decrypt_signature(str : String) : String?
+ self.check_update
+ return SigHelper::Client.decrypt_sig(str)
+ rescue ex
+ LOGGER.debug(ex.message || "Signature: Unknown error")
+ LOGGER.trace(ex.inspect_with_backtrace)
+ return nil
end
end
diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr
index cdfca02c..4e705556 100644
--- a/src/invidious/videos.cr
+++ b/src/invidious/videos.cr
@@ -1,3 +1,5 @@
+private DECRYPT_FUNCTION = IV::DecryptFunction.new
+
enum VideoType
Video
Livestream
@@ -98,20 +100,47 @@ struct Video
# Methods for parsing streaming data
+ def convert_url(fmt)
+ if cfr = fmt["signatureCipher"]?.try { |h| HTTP::Params.parse(h.as_s) }
+ sp = cfr["sp"]
+ url = URI.parse(cfr["url"])
+ params = url.query_params
+
+ LOGGER.debug("Videos: Decoding '#{cfr}'")
+
+ unsig = DECRYPT_FUNCTION.decrypt_signature(cfr["s"])
+ params[sp] = unsig if unsig
+ else
+ url = URI.parse(fmt["url"].as_s)
+ params = url.query_params
+ end
+
+ n = DECRYPT_FUNCTION.decrypt_nsig(params["n"])
+ params["n"] = n if n
+
+ params["host"] = url.host.not_nil!
+ if region = self.info["region"]?.try &.as_s
+ params["region"] = region
+ end
+
+ url.query_params = params
+ LOGGER.trace("Videos: new url is '#{url}'")
+
+ return url.to_s
+ rescue ex
+ LOGGER.debug("Videos: Error when parsing video URL")
+ LOGGER.trace(ex.inspect_with_backtrace)
+ return ""
+ end
+
def fmt_stream
return @fmt_stream.as(Array(Hash(String, JSON::Any))) if @fmt_stream
- fmt_stream = info["streamingData"]?.try &.["formats"]?.try &.as_a.map &.as_h || [] of Hash(String, JSON::Any)
- fmt_stream.each do |fmt|
- if s = (fmt["cipher"]? || fmt["signatureCipher"]?).try { |h| HTTP::Params.parse(h.as_s) }
- s.each do |k, v|
- fmt[k] = JSON::Any.new(v)
- end
- fmt["url"] = JSON::Any.new("#{fmt["url"]}#{DECRYPT_FUNCTION.decrypt_signature(fmt)}")
- end
+ fmt_stream = info.dig?("streamingData", "formats")
+ .try &.as_a.map &.as_h || [] of Hash(String, JSON::Any)
- fmt["url"] = JSON::Any.new("#{fmt["url"]}&host=#{URI.parse(fmt["url"].as_s).host}")
- fmt["url"] = JSON::Any.new("#{fmt["url"]}&region=#{self.info["region"]}") if self.info["region"]?
+ fmt_stream.each do |fmt|
+ fmt["url"] = JSON::Any.new(self.convert_url(fmt))
end
fmt_stream.sort_by! { |f| f["width"]?.try &.as_i || 0 }
@@ -121,21 +150,17 @@ struct Video
def adaptive_fmts
return @adaptive_fmts.as(Array(Hash(String, JSON::Any))) if @adaptive_fmts
- fmt_stream = info["streamingData"]?.try &.["adaptiveFormats"]?.try &.as_a.map &.as_h || [] of Hash(String, JSON::Any)
- fmt_stream.each do |fmt|
- if s = (fmt["cipher"]? || fmt["signatureCipher"]?).try { |h| HTTP::Params.parse(h.as_s) }
- s.each do |k, v|
- fmt[k] = JSON::Any.new(v)
- end
- fmt["url"] = JSON::Any.new("#{fmt["url"]}#{DECRYPT_FUNCTION.decrypt_signature(fmt)}")
- end
- fmt["url"] = JSON::Any.new("#{fmt["url"]}&host=#{URI.parse(fmt["url"].as_s).host}")
- fmt["url"] = JSON::Any.new("#{fmt["url"]}&region=#{self.info["region"]}") if self.info["region"]?
+ fmt_stream = info.dig("streamingData", "adaptiveFormats")
+ .try &.as_a.map &.as_h || [] of Hash(String, JSON::Any)
+
+ fmt_stream.each do |fmt|
+ fmt["url"] = JSON::Any.new(self.convert_url(fmt))
end
fmt_stream.sort_by! { |f| f["width"]?.try &.as_i || 0 }
@adaptive_fmts = fmt_stream
+
return @adaptive_fmts.as(Array(Hash(String, JSON::Any)))
end