mirror of
https://github.com/karl0ss/homepage.git
synced 2025-05-03 22:13:39 +01:00
Change qBittorrent to no longer use fetch
This commit is contained in:
parent
9f03d18e49
commit
034dbb956a
@ -1,30 +1,23 @@
|
|||||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||||
import { addCookieToJar, setCookieHeader } from "utils/proxy/cookie-jar";
|
|
||||||
import { httpProxy } from "utils/proxy/http";
|
import { httpProxy } from "utils/proxy/http";
|
||||||
import getServiceWidget from "utils/config/service-helpers";
|
import getServiceWidget from "utils/config/service-helpers";
|
||||||
import createLogger from "utils/logger";
|
import createLogger from "utils/logger";
|
||||||
|
|
||||||
const logger = createLogger("qbittorrentProxyHandler");
|
const logger = createLogger("qbittorrentProxyHandler");
|
||||||
|
|
||||||
async function login(widget, params) {
|
async function login(widget) {
|
||||||
logger.debug("qBittorrent is rejecting the request, logging in.");
|
logger.debug("qBittorrent is rejecting the request, logging in.");
|
||||||
const loginUrl = new URL(`${widget.url}/api/v2/auth/login`).toString();
|
const loginUrl = new URL(`${widget.url}/api/v2/auth/login`).toString();
|
||||||
const loginBody = `username=${encodeURI(widget.username)}&password=${encodeURI(widget.password)}`;
|
const loginBody = `username=${encodeURI(widget.username)}&password=${encodeURI(widget.password)}`;
|
||||||
|
const loginParams = {
|
||||||
// using fetch intentionally, for login only, as the httpProxy method causes qBittorrent to
|
|
||||||
// complain about header encoding
|
|
||||||
return fetch(loginUrl, {
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||||
body: loginBody,
|
body: loginBody,
|
||||||
})
|
}
|
||||||
.then(async (response) => {
|
|
||||||
addCookieToJar(loginUrl, response.headers);
|
// eslint-disable-next-line no-unused-vars
|
||||||
setCookieHeader(loginUrl, params);
|
const [status, contentType, data] = await httpProxy(loginUrl, loginParams);
|
||||||
const data = await response.text();
|
return [status, data];
|
||||||
return [response.status, data];
|
|
||||||
})
|
|
||||||
.catch((err) => [500, err]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function qbittorrentProxyHandler(req, res) {
|
export default async function qbittorrentProxyHandler(req, res) {
|
||||||
@ -44,11 +37,10 @@ export default async function qbittorrentProxyHandler(req, res) {
|
|||||||
|
|
||||||
const url = new URL(formatApiCall("{url}/api/v2/{endpoint}", { endpoint, ...widget }));
|
const url = new URL(formatApiCall("{url}/api/v2/{endpoint}", { endpoint, ...widget }));
|
||||||
const params = { method: "GET", headers: {} };
|
const params = { method: "GET", headers: {} };
|
||||||
setCookieHeader(url, params);
|
|
||||||
|
|
||||||
let [status, contentType, data] = await httpProxy(url, params);
|
let [status, contentType, data] = await httpProxy(url, params);
|
||||||
if (status === 403) {
|
if (status === 403) {
|
||||||
[status, data] = await login(widget, params);
|
[status, data] = await login(widget);
|
||||||
|
|
||||||
if (status !== 200) {
|
if (status !== 200) {
|
||||||
logger.error("HTTP %d logging in to qBittorrent. Data: %s", status, data);
|
logger.error("HTTP %d logging in to qBittorrent. Data: %s", status, data);
|
||||||
@ -59,9 +51,9 @@ export default async function qbittorrentProxyHandler(req, res) {
|
|||||||
logger.error("Error logging in to qBittorrent: Data: %s", data);
|
logger.error("Error logging in to qBittorrent: Data: %s", data);
|
||||||
return res.status(401).end(data);
|
return res.status(401).end(data);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
[status, contentType, data] = await httpProxy(url, params);
|
[status, contentType, data] = await httpProxy(url, params);
|
||||||
|
}
|
||||||
|
|
||||||
if (status !== 200) {
|
if (status !== 200) {
|
||||||
logger.error("HTTP %d getting data from qBittorrent. Data: %s", status, data);
|
logger.error("HTTP %d getting data from qBittorrent. Data: %s", status, data);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user