Browse Source

Merge pull request #603 from bwitt/re-auth

Re-authenticate for qbittorrent and deluge
pull/605/head
Eric Kok 3 years ago committed by GitHub
parent
commit
62659a98b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/src/main/java/org/transdroid/daemon/IDaemonAdapter.java
  2. 5
      app/src/main/java/org/transdroid/daemon/adapters/deluge/DelugeAdapter.java
  3. 6
      app/src/main/java/org/transdroid/daemon/adapters/qBittorrent/QBittorrentAdapter.java

2
app/src/main/java/org/transdroid/daemon/IDaemonAdapter.java

@ -35,4 +35,6 @@ public interface IDaemonAdapter {
DaemonSettings getSettings(); DaemonSettings getSettings();
long MAX_SESSION_TIME = 30 * 60 * 1000; // 30 minutes
} }

5
app/src/main/java/org/transdroid/daemon/adapters/deluge/DelugeAdapter.java

@ -150,6 +150,7 @@ public class DelugeAdapter implements IDaemonAdapter {
private DefaultHttpClient httpclient; private DefaultHttpClient httpclient;
private Cookie sessionCookie; private Cookie sessionCookie;
private int version = -1; private int version = -1;
private long lastAuthTime = -1;
public DelugeAdapter(DaemonSettings settings) { public DelugeAdapter(DaemonSettings settings) {
this.settings = settings; this.settings = settings;
@ -476,8 +477,9 @@ public class DelugeAdapter implements IDaemonAdapter {
initialise(); initialise();
// Login first? // Login first?
if (sessionCookie == null) { if (sessionCookie == null || System.currentTimeMillis() - lastAuthTime > MAX_SESSION_TIME) {
sessionCookie = null;
// Build login object // Build login object
String extraPass = settings.getExtraPassword(); String extraPass = settings.getExtraPassword();
if (extraPass == null) { if (extraPass == null) {
@ -503,6 +505,7 @@ public class DelugeAdapter implements IDaemonAdapter {
for (Cookie cookie : httpclient.getCookieStore().getCookies()) { for (Cookie cookie : httpclient.getCookieStore().getCookies()) {
if (cookie.getName().equals(RPC_SESSION_ID)) { if (cookie.getName().equals(RPC_SESSION_ID)) {
sessionCookie = cookie; sessionCookie = cookie;
lastAuthTime = System.currentTimeMillis();
break; break;
} }
} }

6
app/src/main/java/org/transdroid/daemon/adapters/qBittorrent/QBittorrentAdapter.java

@ -89,6 +89,7 @@ public class QBittorrentAdapter implements IDaemonAdapter {
private DaemonSettings settings; private DaemonSettings settings;
private DefaultHttpClient httpclient; private DefaultHttpClient httpclient;
private int version = -1; private int version = -1;
private long lastAuthTime = -1;
public QBittorrentAdapter(DaemonSettings settings) { public QBittorrentAdapter(DaemonSettings settings) {
this.settings = settings; this.settings = settings;
@ -171,12 +172,14 @@ public class QBittorrentAdapter implements IDaemonAdapter {
// Try qBittorrent 4.1 API v2 first // Try qBittorrent 4.1 API v2 first
try { try {
makeRequest(log, "/api/v2/auth/login", usernameParam, passwordParam); makeRequest(log, "/api/v2/auth/login", usernameParam, passwordParam);
lastAuthTime = System.currentTimeMillis();
} catch (DaemonException ignored) { } catch (DaemonException ignored) {
} }
// If still not authenticated, try the qBittorrent 3.2 API v1 endpoint // If still not authenticated, try the qBittorrent 3.2 API v1 endpoint
if (!isAuthenticated()) { if (!isAuthenticated()) {
try { try {
makeRequest(log, "/login", usernameParam, passwordParam); makeRequest(log, "/login", usernameParam, passwordParam);
lastAuthTime = System.currentTimeMillis();
} catch (DaemonException ignored) { } catch (DaemonException ignored) {
} }
} }
@ -187,6 +190,9 @@ public class QBittorrentAdapter implements IDaemonAdapter {
} }
private boolean isAuthenticated() { private boolean isAuthenticated() {
if (System.currentTimeMillis() - lastAuthTime > MAX_SESSION_TIME) {
return false;
}
List<Cookie> cookies = httpclient.getCookieStore().getCookies(); List<Cookie> cookies = httpclient.getCookieStore().getCookies();
for (Cookie c : cookies) { for (Cookie c : cookies) {
if (c.getName().equals("SID")) { if (c.getName().equals("SID")) {

Loading…
Cancel
Save