invidious-mod-JP/src/invidious/helpers/signatures.cr

54 lines
1.9 KiB
Crystal
Raw Normal View History

2020-01-24 22:02:28 +00:00
alias SigProc = Proc(Array(String), Int32, Array(String))
2018-09-15 00:50:11 +00:00
def fetch_decrypt_function(id = "CvFH_6DNRCY")
2020-06-15 22:33:23 +00:00
document = YT_POOL.client &.get("/watch?v=#{id}&gl=US&hl=en").body
2020-06-17 00:20:58 +00:00
url = document.match(/src="(?<url>\/s\/player\/[^\/]+\/player_ias[^\/]+\/en_US\/base.js)"/).not_nil!["url"]
2019-10-25 16:58:16 +00:00
player = YT_POOL.client &.get(url).body
2018-08-04 20:30:44 +00:00
2020-01-24 22:02:28 +00:00
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"]
2018-08-04 20:30:44 +00:00
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"]
2018-08-04 20:30:44 +00:00
2020-01-24 22:02:28 +00:00
operations = {} of String => SigProc
2018-08-28 14:51:59 +00:00
var_body.split("},").each do |operation|
op_name = operation.match(/^[^:]+/).not_nil![0]
op_body = operation.match(/\{[^}]+/).not_nil![0]
2018-08-04 20:30:44 +00:00
case op_body
2018-08-28 14:51:59 +00:00
when "{a.reverse()"
2020-01-24 22:02:28 +00:00
operations[op_name] = ->(a : Array(String), b : Int32) { a.reverse }
2018-08-28 14:51:59 +00:00
when "{a.splice(0,b)"
2020-01-24 22:02:28 +00:00
operations[op_name] = ->(a : Array(String), b : Int32) { a.delete_at(0..(b - 1)); a }
2018-08-04 20:30:44 +00:00
else
2020-01-24 22:02:28 +00:00
operations[op_name] = ->(a : Array(String), b : Int32) { c = a[0]; a[0] = a[b % a.size]; a[b % a.size] = c; a }
2018-08-04 20:30:44 +00:00
end
end
2020-01-24 22:02:28 +00:00
decrypt_function = [] of {SigProc, Int32}
2018-08-04 20:30:44 +00:00
function_body.each do |function|
2018-08-28 14:51:59 +00:00
function = function.lchop(var_name).delete("[].")
2018-08-04 20:30:44 +00:00
2018-08-28 14:51:59 +00:00
op_name = function.match(/[^\(]+/).not_nil![0]
2020-01-24 22:02:28 +00:00
value = function.match(/\(\w,(?<value>[\d]+)\)/).not_nil!["value"].to_i
2018-08-04 20:30:44 +00:00
2020-01-24 22:02:28 +00:00
decrypt_function << {operations[op_name], value}
2018-08-04 20:30:44 +00:00
end
return decrypt_function
end
2020-06-15 22:10:30 +00:00
def decrypt_signature(fmt : Hash(String, JSON::Any))
2020-01-24 22:02:28 +00:00
return "" if !fmt["s"]? || !fmt["sp"]?
2018-08-04 20:30:44 +00:00
2020-06-15 22:10:30 +00:00
sp = fmt["sp"].as_s
sig = fmt["s"].as_s.split("")
DECRYPT_FUNCTION.each do |proc, value|
2020-01-24 22:02:28 +00:00
sig = proc.call(sig, value)
2018-08-04 20:30:44 +00:00
end
2020-01-24 22:02:28 +00:00
return "&#{sp}=#{sig.join("")}"
2018-08-04 20:30:44 +00:00
end