From 77c1834bc8ee24a8709ddbbd246805ebdd621cd4 Mon Sep 17 00:00:00 2001 From: Brian Witt Date: Sat, 25 Dec 2021 11:28:42 -0800 Subject: [PATCH] Re-authenticate for qbittorrent and deluge --- app/src/main/java/org/transdroid/daemon/IDaemonAdapter.java | 2 ++ .../transdroid/daemon/adapters/deluge/DelugeAdapter.java | 5 ++++- .../daemon/adapters/qBittorrent/QBittorrentAdapter.java | 6 ++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/transdroid/daemon/IDaemonAdapter.java b/app/src/main/java/org/transdroid/daemon/IDaemonAdapter.java index 5f43724a..25a67d48 100644 --- a/app/src/main/java/org/transdroid/daemon/IDaemonAdapter.java +++ b/app/src/main/java/org/transdroid/daemon/IDaemonAdapter.java @@ -35,4 +35,6 @@ public interface IDaemonAdapter { DaemonSettings getSettings(); + long MAX_SESSION_TIME = 30 * 60 * 1000; // 30 minutes + } diff --git a/app/src/main/java/org/transdroid/daemon/adapters/deluge/DelugeAdapter.java b/app/src/main/java/org/transdroid/daemon/adapters/deluge/DelugeAdapter.java index 4203dfd8..f9c40c44 100644 --- a/app/src/main/java/org/transdroid/daemon/adapters/deluge/DelugeAdapter.java +++ b/app/src/main/java/org/transdroid/daemon/adapters/deluge/DelugeAdapter.java @@ -150,6 +150,7 @@ public class DelugeAdapter implements IDaemonAdapter { private DefaultHttpClient httpclient; private Cookie sessionCookie; private int version = -1; + private long lastAuthTime = -1; public DelugeAdapter(DaemonSettings settings) { this.settings = settings; @@ -476,8 +477,9 @@ public class DelugeAdapter implements IDaemonAdapter { initialise(); // Login first? - if (sessionCookie == null) { + if (sessionCookie == null || System.currentTimeMillis() - lastAuthTime > MAX_SESSION_TIME) { + sessionCookie = null; // Build login object String extraPass = settings.getExtraPassword(); if (extraPass == null) { @@ -503,6 +505,7 @@ public class DelugeAdapter implements IDaemonAdapter { for (Cookie cookie : httpclient.getCookieStore().getCookies()) { if (cookie.getName().equals(RPC_SESSION_ID)) { sessionCookie = cookie; + lastAuthTime = System.currentTimeMillis(); break; } } diff --git a/app/src/main/java/org/transdroid/daemon/adapters/qBittorrent/QBittorrentAdapter.java b/app/src/main/java/org/transdroid/daemon/adapters/qBittorrent/QBittorrentAdapter.java index d5ccb186..d43350a3 100644 --- a/app/src/main/java/org/transdroid/daemon/adapters/qBittorrent/QBittorrentAdapter.java +++ b/app/src/main/java/org/transdroid/daemon/adapters/qBittorrent/QBittorrentAdapter.java @@ -89,6 +89,7 @@ public class QBittorrentAdapter implements IDaemonAdapter { private DaemonSettings settings; private DefaultHttpClient httpclient; private int version = -1; + private long lastAuthTime = -1; public QBittorrentAdapter(DaemonSettings settings) { this.settings = settings; @@ -171,12 +172,14 @@ public class QBittorrentAdapter implements IDaemonAdapter { // Try qBittorrent 4.1 API v2 first try { makeRequest(log, "/api/v2/auth/login", usernameParam, passwordParam); + lastAuthTime = System.currentTimeMillis(); } catch (DaemonException ignored) { } // If still not authenticated, try the qBittorrent 3.2 API v1 endpoint if (!isAuthenticated()) { try { makeRequest(log, "/login", usernameParam, passwordParam); + lastAuthTime = System.currentTimeMillis(); } catch (DaemonException ignored) { } } @@ -187,6 +190,9 @@ public class QBittorrentAdapter implements IDaemonAdapter { } private boolean isAuthenticated() { + if (System.currentTimeMillis() - lastAuthTime > MAX_SESSION_TIME) { + return false; + } List cookies = httpclient.getCookieStore().getCookies(); for (Cookie c : cookies) { if (c.getName().equals("SID")) {