Skip to content

Commit

Permalink
fix(qBittorrent): dont manually manage cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
Beastwick18 committed Jun 18, 2024
1 parent 985cb31 commit 0472a1c
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 61 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Clippy
run: cargo clippy --verbose
run: cargo clippy --verbose --release
test:

runs-on: ubuntu-latest
Expand All @@ -27,4 +27,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Run tests
run: cargo test --verbose
run: cargo test --verbose --release
13 changes: 0 additions & 13 deletions default.nix

This file was deleted.

2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ win:
cargo build --target $(WINDOWS_TARGET) --release

linux:
cargo build --target $(LINUX_TARGET) --release
cargo build --target $(LINUX_TARGET) --profile=github

fedora:
@mkdir -p "release/$(VERSION)"
Expand Down
7 changes: 0 additions & 7 deletions shell.nix

This file was deleted.

42 changes: 10 additions & 32 deletions src/client/qbit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::collections::HashMap;

use reqwest::{
header::{COOKIE, REFERER, SET_COOKIE},
Response, StatusCode,
};
use reqwest::{header::REFERER, Response, StatusCode};
use serde::{Deserialize, Serialize};

use crate::{app::Context, source::Item, util::conv::add_protocol};
Expand Down Expand Up @@ -114,43 +111,28 @@ struct QbitForm {
// rename: String // Disabled
}

async fn login(qbit: &QbitConfig, client: &reqwest::Client) -> Result<String, String> {
async fn login(qbit: &QbitConfig, client: &reqwest::Client) -> Result<(), String> {
let base_url = add_protocol(qbit.base_url.clone(), false);
let url = format!("{}/api/v2/auth/login", base_url);
let mut params = HashMap::new();
params.insert("username", qbit.username.to_owned());
params.insert("password", qbit.password.to_owned());
let res = client.post(url).form(&params).send().await;
let res = res.map_err(|e| format!("Failed to send data to qBittorrent\n{}", e))?;
let headers = res.headers().clone();
let cookie = headers.get(SET_COOKIE).ok_or(format!(
"Failed to get cookie from qBittorrent:\n{}",
res.text().await.unwrap_or_default()
))?;

let cookie = cookie
.to_str()
.map_err(|e| format!("Failed to parse cookie:\n{}", e))?;
cookie
.split(';')
.find(|c| c.split_once('=').is_some_and(|s| s.0 == "SID"))
.ok_or("No cookie returned by qBittorrent".to_owned())
.map(|s| s.to_owned())
let _ = res.map_err(|e| format!("Failed to send data to qBittorrent\n{}", e))?;
Ok(())
}

async fn logout(qbit: &QbitConfig, sid: String, client: &reqwest::Client) {
async fn logout(qbit: &QbitConfig, client: &reqwest::Client) {
let base_url = add_protocol(qbit.base_url.clone(), false);
let _ = client
.get(format!("{}/api/v2/auth/logout", base_url))
.header(REFERER, base_url)
.header(COOKIE, sid)
.send()
.await;
}

async fn add_torrent(
qbit: &QbitConfig,
sid: String,
links: String,
client: &reqwest::Client,
) -> Result<Response, reqwest::Error> {
Expand All @@ -160,7 +142,6 @@ async fn add_torrent(
client
.post(url)
.header(REFERER, base_url)
.header(COOKIE, sid)
.form(&qbit.to_form(links))
.send()
.await
Expand Down Expand Up @@ -199,12 +180,9 @@ impl DownloadClient for QbitClient {
));
}
}
let sid = match login(&qbit, &client).await {
Ok(s) => s,
Err(e) => {
return DownloadResult::error(DownloadError(format!("Failed to get SID:\n{}", e)))
}
};
if let Err(e) = login(&qbit, &client).await {
return DownloadResult::error(DownloadError(format!("Failed to get SID:\n{}", e)));
}
let links = match qbit.use_magnet.unwrap_or(true) {
true => items
.iter()
Expand All @@ -217,7 +195,7 @@ impl DownloadClient for QbitClient {
.collect::<Vec<String>>()
.join("\n"),
};
let res = match add_torrent(&qbit, sid.to_owned(), links, &client).await {
let res = match add_torrent(&qbit, links, &client).await {
Ok(res) => res,
Err(e) => {
return DownloadResult::error(DownloadError(format!(
Expand All @@ -233,7 +211,7 @@ impl DownloadClient for QbitClient {
)));
}

logout(&qbit, sid.clone(), &client).await;
logout(&qbit, &client).await;

DownloadResult::new(
format!("Successfully sent {} torrents to qBittorrent", items.len()),
Expand Down
11 changes: 5 additions & 6 deletions src/util/conv.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use crossterm::event::{KeyCode, KeyModifiers, MediaKeyCode, ModifierKeyCode};
use regex::Regex;

pub fn add_protocol<S: Into<String>>(url: S, default_https: bool) -> String {
let protocol = match default_https {
true => "https",
false => "http",
};
let url = url.into();
let re = Regex::new(r"^(https?|socks5)?://.+$").unwrap();
match re.is_match(&url) {
true => url,
// Assume http(s) if not present
false => format!("{}://{}", protocol, url),
if let Some((method, other)) = url.split_once(':') {
if matches!(method, "http" | "https" | "socks5") && matches!(other.get(..2), Some("//")) {
return url;
}
}
format!("{}://{}", protocol, url)
}

pub fn to_bytes(size: &str) -> usize {
Expand Down

0 comments on commit 0472a1c

Please sign in to comment.