mirror of
https://github.com/karl0ss/homepage.git
synced 2025-04-29 12:03:41 +01:00
Better login strategy for qbittorrent proxy
- Additional logging in httpProxy
This commit is contained in:
parent
1ea8e38372
commit
65755a08aa
@ -4,6 +4,10 @@ import { http, https } from "follow-redirects";
|
|||||||
|
|
||||||
import { addCookieToJar, setCookieHeader } from "./cookie-jar";
|
import { addCookieToJar, setCookieHeader } from "./cookie-jar";
|
||||||
|
|
||||||
|
import createLogger from "utils/logger";
|
||||||
|
|
||||||
|
const logger = createLogger("httpProxy");
|
||||||
|
|
||||||
function addCookieHandler(url, params) {
|
function addCookieHandler(url, params) {
|
||||||
setCookieHeader(url, params);
|
setCookieHeader(url, params);
|
||||||
|
|
||||||
@ -70,18 +74,30 @@ export function httpRequest(url, params) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function httpProxy(url, params = {}) {
|
export async function httpProxy(url, params = {}) {
|
||||||
const constructedUrl = new URL(url);
|
const constructedUrl = new URL(url);
|
||||||
|
|
||||||
|
let request = null;
|
||||||
if (constructedUrl.protocol === "https:") {
|
if (constructedUrl.protocol === "https:") {
|
||||||
const httpsAgent = new https.Agent({
|
const httpsAgent = new https.Agent({
|
||||||
rejectUnauthorized: false,
|
rejectUnauthorized: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
return httpsRequest(constructedUrl, {
|
request = httpsRequest(constructedUrl, {
|
||||||
agent: httpsAgent,
|
agent: httpsAgent,
|
||||||
...params,
|
...params,
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
request = httpRequest(constructedUrl, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const [status, contentType, data, responseHeaders] = await request;
|
||||||
|
return [status, contentType, data, responseHeaders];
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
logger.error("Error calling %s//%s%s...", url.protocol, url.hostname, url.pathname);
|
||||||
|
logger.error(err);
|
||||||
|
return [500, "application/json", { error: "Unexpected error" }, null];
|
||||||
}
|
}
|
||||||
return httpRequest(constructedUrl, params);
|
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,12 @@ import { formatApiCall } from "utils/proxy/api-helpers";
|
|||||||
import { addCookieToJar, setCookieHeader } from "utils/proxy/cookie-jar";
|
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";
|
||||||
|
|
||||||
|
const logger = createLogger("qbittorrentProxyHandler");
|
||||||
|
|
||||||
async function login(widget, params) {
|
async function login(widget, params) {
|
||||||
|
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)}`;
|
||||||
|
|
||||||
@ -14,46 +18,54 @@ async function login(widget, params) {
|
|||||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||||
body: loginBody,
|
body: loginBody,
|
||||||
})
|
})
|
||||||
.then(async (response) => {
|
.then(async (response) => {
|
||||||
addCookieToJar(loginUrl, response.headers);
|
addCookieToJar(loginUrl, response.headers);
|
||||||
setCookieHeader(loginUrl, params);
|
setCookieHeader(loginUrl, params);
|
||||||
const data = await response.text();
|
const data = await response.text();
|
||||||
return [response.status, data];
|
return [response.status, data];
|
||||||
})
|
})
|
||||||
.catch((err) => [500, err]);
|
.catch((err) => [500, err]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function qbittorrentProxyHandler(req, res) {
|
export default async function qbittorrentProxyHandler(req, res) {
|
||||||
const { group, service, endpoint } = req.query;
|
const { group, service, endpoint } = req.query;
|
||||||
|
|
||||||
if (!group || !service) {
|
if (!group || !service) {
|
||||||
|
logger.debug("Invalid or missing service '%s' or group '%s'", service, group);
|
||||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const widget = await getServiceWidget(group, service);
|
const widget = await getServiceWidget(group, service);
|
||||||
|
|
||||||
if (!widget) {
|
if (!widget) {
|
||||||
|
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group);
|
||||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
setCookieHeader(url, params);
|
||||||
|
|
||||||
if (!params.headers.Cookie) {
|
let [status, contentType, data] = await httpProxy(url, params);
|
||||||
const [status, data] = await login(widget, params);
|
if (status === 403) {
|
||||||
|
[status, data] = await login(widget, params);
|
||||||
|
|
||||||
if (status !== 200) {
|
if (status !== 200) {
|
||||||
|
logger.error("HTTP %d logging in to qBittorrent. Data: %s", status, data);
|
||||||
return res.status(status).end(data);
|
return res.status(status).end(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.toString() !== "Ok.") {
|
if (data.toString() !== "Ok.") {
|
||||||
|
logger.error("Error logging in to qBittorrent: Data: %s", data);
|
||||||
return res.status(401).end(data);
|
return res.status(401).end(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const [status, contentType, data] = await httpProxy(url, params);
|
[status, contentType, data] = await httpProxy(url, params);
|
||||||
|
|
||||||
|
if (status !== 200) {
|
||||||
|
logger.error("HTTP %d getting data from qBittorrent. Data: %s", status, data);
|
||||||
|
}
|
||||||
|
|
||||||
if (contentType) res.setHeader("Content-Type", contentType);
|
if (contentType) res.setHeader("Content-Type", contentType);
|
||||||
return res.status(status).send(data);
|
return res.status(status).send(data);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user