From 448007e5ba21114f8b82cdc1554529d0d54a7388 Mon Sep 17 00:00:00 2001 From: Fijxu Date: Thu, 17 Oct 2024 23:44:30 -0300 Subject: [PATCH] Tokens: Server side generated tokens. #18 --- crystal_formatters.py | 62 ++++++++++++++++++++++++ src/invidious/helpers/redis_tokens.cr | 24 +++++++++ src/invidious/routes/account.cr | 36 ++++++++++++++ src/invidious/routing.cr | 1 + src/invidious/views/user/preferences.ecr | 6 +++ src/invidious/views/user/tokens.ecr | 15 ++++++ 6 files changed, 144 insertions(+) create mode 100644 crystal_formatters.py create mode 100644 src/invidious/views/user/tokens.ecr diff --git a/crystal_formatters.py b/crystal_formatters.py new file mode 100644 index 00000000..2a66369a --- /dev/null +++ b/crystal_formatters.py @@ -0,0 +1,62 @@ +import lldb + +class CrystalArraySyntheticProvider: + def __init__(self, valobj, internal_dict): + self.valobj = valobj + self.buffer = None + self.size = 0 + + def update(self): + if self.valobj.type.is_pointer: + self.valobj = self.valobj.Dereference() + self.size = int(self.valobj.child[0].value) + self.type = self.valobj.type + self.buffer = self.valobj.child[3] + + def num_children(self): + size = 0 if self.size is None else self.size + return size + + def get_child_index(self, name): + try: + return int(name.lstrip('[').rstrip(']')) + except: + return -1 + + def get_child_at_index(self,index): + if index >= self.size: + return None + try: + elementType = self.buffer.type.GetPointeeType() + offset = elementType.size * index + return self.buffer.CreateChildAtOffset('[' + str(index) + ']', offset, elementType) + except Exception as e: + print('Got exception %s' % (str(e))) + return None + +def findType(name, module): + cachedTypes = module.GetTypes() + for idx in range(cachedTypes.GetSize()): + type = cachedTypes.GetTypeAtIndex(idx) + if type.name == name: + return type + return None + + +def CrystalString_SummaryProvider(value, dict): + error = lldb.SBError() + if value.TypeIsPointerType(): + value = value.Dereference() + process = value.GetTarget().GetProcess() + byteSize = int(value.child[0].value) + len = int(value.child[1].value) + len = byteSize or len + strAddr = value.child[2].load_addr + val = process.ReadCStringFromMemory(strAddr, len + 1, error) + return '"%s"' % val + + +def __lldb_init_module(debugger, dict): + debugger.HandleCommand(r'type synthetic add -l crystal_formatters.CrystalArraySyntheticProvider -x "^Array\(.+\)(\s*\**)?" -w Crystal') + debugger.HandleCommand(r'type summary add -F crystal_formatters.CrystalString_SummaryProvider -x "^(String|\(String \| Nil\))(\s*\**)?$" -w Crystal') + debugger.HandleCommand(r'type category enable Crystal') diff --git a/src/invidious/helpers/redis_tokens.cr b/src/invidious/helpers/redis_tokens.cr index 8b9289e1..7c0970e7 100644 --- a/src/invidious/helpers/redis_tokens.cr +++ b/src/invidious/helpers/redis_tokens.cr @@ -22,4 +22,28 @@ module Tokens def get_visitor_data return @@visitor_data end + + def generate_tokens(user : String) + po_token = "" + visitor_data = "" + attempts = 0 + + LOGGER.debug("Generating po_token and visitor_data for user: '#{user}'") + REDIS_DB.publish("generate-token", "#{user}") + + while REDIS_DB.get("invidious:#{user}:po_token").nil? && REDIS_DB.get("invidious:#{user}:visitor_data").nil? + if attempts > 50 + break + end + LOGGER.debug("Waiting for tokens to arrive at redis for user: '#{user}'") + attempts += 1 + sleep 250.milliseconds + end + + po_token = REDIS_DB.get("invidious:#{user}:po_token") + visitor_data = REDIS_DB.get("invidious:#{user}:visitor_data") + + LOGGER.debug("Tokens successfully generated for user: '#{user}'") + return {po_token, visitor_data} + end end diff --git a/src/invidious/routes/account.cr b/src/invidious/routes/account.cr index 8086a54e..345c96cf 100644 --- a/src/invidious/routes/account.cr +++ b/src/invidious/routes/account.cr @@ -349,4 +349,40 @@ module Invidious::Routes::Account return "{}" end end + + # ------------------- + # poToken and visitorData tokens generation + # ------------------- + + # Generates a poToken & visitorData for the user, server side + def generate_tokens(env) + locale = env.get("preferences").as(Preferences).locale + preferences = env.get("preferences").as(Preferences) + + user = env.get? "user" + sid = env.get? "sid" + referer = get_referer(env) + + if !user + return env.redirect referer + end + + user = user.as(User) + + po_token, visitor_data = Tokens.generate_tokens(user.email) + + if po_token.nil? || visitor_data.nil? + return error_template(500, "Internal server error. Please submit an issue here IF THE ISSUE PERSISTS: https://git.nadeko.net/Fijxu/invidious/issues") + end + + user.preferences.po_token = po_token + user.preferences.visitor_data = visitor_data + + Invidious::Database::Users.update_preferences(user) + + REDIS_DB.del("invidious:#{user.email}:po_token") + REDIS_DB.del("invidious:#{user.email}:visitor_data") + + templated "user/tokens" + end end diff --git a/src/invidious/routing.cr b/src/invidious/routing.cr index 620c884d..e56c670b 100644 --- a/src/invidious/routing.cr +++ b/src/invidious/routing.cr @@ -77,6 +77,7 @@ module Invidious::Routing post "/authorize_token", Routes::Account, :post_authorize_token get "/token_manager", Routes::Account, :token_manager post "/token_ajax", Routes::Account, :token_ajax + get "/generate_tokens", Routes::Account, :generate_tokens post "/subscription_ajax", Routes::Subscriptions, :toggle_subscription get "/subscription_manager", Routes::Subscriptions, :subscription_manager end diff --git a/src/invidious/views/user/preferences.ecr b/src/invidious/views/user/preferences.ecr index fcde2313..81fc1f96 100644 --- a/src/invidious/views/user/preferences.ecr +++ b/src/invidious/views/user/preferences.ecr @@ -136,6 +136,12 @@ + <% if env.get?("user") %> +
+ <%= translate(locale, "Generate po_token and visitor_data for your account") %> +
+ <% end %> + <%= translate(locale, "preferences_category_visual") %>
diff --git a/src/invidious/views/user/tokens.ecr b/src/invidious/views/user/tokens.ecr new file mode 100644 index 00000000..768a1968 --- /dev/null +++ b/src/invidious/views/user/tokens.ecr @@ -0,0 +1,15 @@ +<% content_for "header" do %> +<%= translate(locale, "Invidious token generator") %> - Invidious +<% end %> + +
+
+
+
+

po_token and visitor_data successfully generated!

+

po_token: <%= po_token %>

+

visitor_data: <%= visitor_data %>

+
+
+
+