From 13ef4440d01b606e07c5e96f0f0117ead2ba9f1c Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Thu, 22 Feb 2018 13:01:37 -0600 Subject: [PATCH] Replace signature function with generic catchall --- src/helpers.cr | 50 ++++++++++-------------------------------------- src/invidious.cr | 39 +++++++++---------------------------- 2 files changed, 19 insertions(+), 70 deletions(-) diff --git a/src/helpers.cr b/src/helpers.cr index 9ce527a7..d3c89e03 100644 --- a/src/helpers.cr +++ b/src/helpers.cr @@ -175,54 +175,24 @@ def search(query, client) end end -def swap(a, b) +def splice(a, b) c = a[0] a[0] = a[b % a.size] a[b % a.size] = c return a end -def decrypt_signature(a, base) +def decrypt_signature(a) a = a.split("") - case base - when "vflG9lb96" - a = swap(a, 26) - a.reverse! - a = swap(a, 8) - a = swap(a, 61) - when "vflxuxnEY" - a.delete_at(0..2) - a.reverse! - c = a[0] - a[0] = a[49 % a.size] - a[49] = c - when "vflAXQwEj" - a = swap(a, 26) - a.reverse! - a = swap(a, 8) - a = swap(a, 61) - when "vflNpPGQq" - a = swap(a, 26) - a.reverse! - a = swap(a, 8) - a = swap(a, 61) - when "vflCCoUi2" - a = swap(a, 26) - a.reverse! - a = swap(a, 8) - a = swap(a, 61) - when "vflg4IfMn" - a = swap(a, 6) - a = swap(a, 64) - a = swap(a, 25) - a = swap(a, 53) - a.delete_at(0..1) - a.reverse! - a.delete_at(0..2) - else - raise "Could not decrypt signature for player #{base}" - end + a.delete_at(0..1) + a = splice(a, 2) + a = splice(a, 51) + a = splice(a, 9) + a.delete_at(0..1) + a.reverse! + a = splice(a, 15) + a.delete_at(0..2) return a.join("") end diff --git a/src/invidious.cr b/src/invidious.cr index cb95647c..ae9e0978 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -177,34 +177,9 @@ get "/watch" do |env| fmt_stream << HTTP::Params.parse(string) end - base = nil + signature = false if fmt_stream[0]? && fmt_stream[0]["s"]? - base = video.html.xpath_node(%q(//script[@name="player/base"])) - - if !base - base = video.html.xpath_node(%q(//script[@name="player_ias/base"])) - end - - if !base - error_message = "Could not find signature for #{video.id}" - next templated "error" - end - - base = base["src"] - base = base.split("/")[3].split("-")[1] - - begin - decrypt_signature(fmt_stream[0]["s"], base) - rescue ex - error_message = ex.message - next templated "error" - end - end - - fmt_stream.each do |fmt| - if base - fmt["url"] += "&signature=" + decrypt_signature(fmt["s"], base) - end + signature = true end # We want lowest quality first @@ -217,9 +192,13 @@ get "/watch" do |env| end end - adaptive_fmts.each do |fmt| - if base - fmt["url"] += "&signature=" + decrypt_signature(fmt["s"], base) + if signature + adaptive_fmts.each do |fmt| + fmt["url"] += "&signature=" + decrypt_signature(fmt["s"]) + end + + fmt_stream.each do |fmt| + fmt["url"] += "&signature=" + decrypt_signature(fmt["s"]) end end