Invidious is an alternative front-end to YouTube
SigHelper: Add support for PLAYER_UPDATE_TIMESTAMP opcode
Samantaz Foxec1bb5db87a4
diff --git a/config/config.example.yml b/config/config.example.ymlindex 142fdfb7..2f5228a6 100644--- a/config/config.example.yml+++ b/config/config.example.yml@@ -1,6 +1,6 @@##########################################-# Database configuration+# Database and other external servers##########################################@@ -41,6 +41,19 @@ db:#check_tables: false+##+## Path to an external signature resolver, used to emulate+## the Youtube client's Javascript. If no such server is+## available, some videos will not be playable.+##+## When this setting is commented out, no external+## resolver will be used.+##+## Accepted values: a path to a UNIX socket or "<IP>:<Port>"+## Default: <none>+##+#signature_server:+##########################################diff --git a/src/invidious/helpers/sig_helper.cr b/src/invidious/helpers/sig_helper.crindex 108587ce..2239858b 100644--- a/src/invidious/helpers/sig_helper.cr+++ b/src/invidious/helpers/sig_helper.cr@@ -61,6 +61,7 @@ module Invidious::SigHelperDECRYPT_SIGNATURE = 2GET_SIGNATURE_TIMESTAMP = 3GET_PLAYER_STATUS = 4+ PLAYER_UPDATE_TIMESTAMP = 5endprivate record Request,@@ -135,7 +136,15 @@ module Invidious::SigHelperplayer_version = IO::ByteFormat::NetworkEndian.decode(UInt32, bytes[1..4])has_player ? player_version : nilend+ end++ # Return when the player was last updated+ def get_player_timestamp : UInt64?+ request = Request.new(Opcode::GET_SIGNATURE_TIMESTAMP, nil)+ return self.send_request(request) do |bytes|+ IO::ByteFormat::NetworkEndian.decode(UInt64, bytes)+ endendprivate def send_request(request : Request, &)diff --git a/src/invidious/helpers/signatures.cr b/src/invidious/helpers/signatures.crindex 8fbfaac0..cf170668 100644--- a/src/invidious/helpers/signatures.cr+++ b/src/invidious/helpers/signatures.cr@@ -2,18 +2,27 @@ require "http/params"require "./sig_helper"struct Invidious::DecryptFunction- @last_update = Time.monotonic - 42.days+ @last_update : Time = Time.utc - 42.daysdef initializeself.check_updateenddef check_update- now = Time.monotonic- if (now - @last_update) > 60.seconds+ now = Time.utc++ # If we have updated in the last 5 minutes, do nothing+ return if (now - @last_update) > 5.minutes++ # Get the time when the player was updated, in the event where+ # multiple invidious processes are run in parallel.+ player_ts = Invidious::SigHelper::Client.get_player_timestamp+ player_time = Time.unix(player_ts || 0)++ if (now - player_time) > 5.minutesLOGGER.debug("Signature: Player might be outdated, updating")Invidious::SigHelper::Client.force_update- @last_update = Time.monotonic+ @last_update = Time.utcendend