commit
2d5294804c
@ -0,0 +1,69 @@
|
|||||||
|
import useSWR from "swr";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
|
import Widget from "../widget";
|
||||||
|
import Block from "../block";
|
||||||
|
|
||||||
|
import { formatApiUrl } from "utils/api-helpers";
|
||||||
|
|
||||||
|
export default function QBittorrent ({ service }) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
const config = service.widget;
|
||||||
|
|
||||||
|
const { data: torrentData, error: torrentError } = useSWR(formatApiUrl(config, "torrents/info"));
|
||||||
|
|
||||||
|
if (torrentError) {
|
||||||
|
return <Widget error={t("widget.api_error")} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!torrentData) {
|
||||||
|
return (
|
||||||
|
<Widget>
|
||||||
|
<Block label={t("qbittorrent.leech")} />
|
||||||
|
<Block label={t("qbittorrent.download")} />
|
||||||
|
<Block label={t("qbittorrent.seed")} />
|
||||||
|
<Block label={t("qbittorrent.upload")} />
|
||||||
|
</Widget>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let rateDl = 0;
|
||||||
|
let rateUl = 0;
|
||||||
|
let completed = 0;
|
||||||
|
|
||||||
|
for (let i = 0; i < torrentData.length; i += 1) {
|
||||||
|
const torrent = torrentData[i];
|
||||||
|
rateDl += torrent.dlspeed;
|
||||||
|
rateUl += torrent.upspeed;
|
||||||
|
if (torrent.progress === 1) {
|
||||||
|
completed += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const leech = torrentData.length - completed;
|
||||||
|
|
||||||
|
let unitsDl = "KB/s";
|
||||||
|
let unitsUl = "KB/s";
|
||||||
|
rateDl /= 1024;
|
||||||
|
rateUl /= 1024;
|
||||||
|
|
||||||
|
if (rateDl > 1024) {
|
||||||
|
rateDl /= 1024;
|
||||||
|
unitsDl = "MB/s";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rateUl > 1024) {
|
||||||
|
rateUl /= 1024;
|
||||||
|
unitsUl = "MB/s";
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Widget>
|
||||||
|
<Block label={t("qbittorrent.leech")} value={t("common.number", { value: leech })} />
|
||||||
|
<Block label={t("qbittorrent.download")} value={`${rateDl.toFixed(2)} ${unitsDl}`} />
|
||||||
|
<Block label={t("qbittorrent.seed")} value={t("common.number", { value: completed })} />
|
||||||
|
<Block label={t("qbittorrent.upload")} value={`${rateUl.toFixed(2)} ${unitsUl}`} />
|
||||||
|
</Widget>
|
||||||
|
);
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
/* eslint-disable no-param-reassign */
|
||||||
|
import { Cookie, CookieJar } from 'tough-cookie';
|
||||||
|
|
||||||
|
const cookieJar = new CookieJar();
|
||||||
|
|
||||||
|
export function setCookieHeader(url, params) {
|
||||||
|
// add cookie header, if we have one in the jar
|
||||||
|
const existingCookie = cookieJar.getCookieStringSync(url.toString());
|
||||||
|
if (existingCookie) {
|
||||||
|
params.headers = params.headers ?? {};
|
||||||
|
params.headers.Cookie = existingCookie;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function addCookieToJar(url, headers) {
|
||||||
|
let cookieHeader = headers['set-cookie'];
|
||||||
|
if (headers instanceof Headers) {
|
||||||
|
cookieHeader = headers.get('set-cookie');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cookieHeader || cookieHeader.length === 0) return;
|
||||||
|
|
||||||
|
let cookies = null;
|
||||||
|
if (cookieHeader instanceof Array) {
|
||||||
|
cookies = cookieHeader.map(Cookie.parse);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cookies = [Cookie.parse(cookieHeader)];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < cookies.length; i += 1) {
|
||||||
|
cookieJar.setCookieSync(cookies[i], url.toString());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
import { formatApiCall } from "utils/api-helpers";
|
||||||
|
import { addCookieToJar, setCookieHeader } from "utils/cookie-jar";
|
||||||
|
import { httpProxy } from "utils/http";
|
||||||
|
import getServiceWidget from "utils/service-helpers";
|
||||||
|
|
||||||
|
async function login(widget, params) {
|
||||||
|
const loginUrl = new URL(`${widget.url}/api/v2/auth/login`);
|
||||||
|
const loginBody = `username=${encodeURI(widget.username)}&password=${encodeURI(widget.password)}`;
|
||||||
|
|
||||||
|
// using fetch intentionally, for login only, as the httpProxy method causes qBittorrent to
|
||||||
|
// complain about header encoding
|
||||||
|
return fetch(loginUrl, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||||
|
body: loginBody
|
||||||
|
})
|
||||||
|
.then(async response => {
|
||||||
|
addCookieToJar(loginUrl, response.headers);
|
||||||
|
setCookieHeader(loginUrl, params);
|
||||||
|
const data = await response.text();
|
||||||
|
return ([response.status, data]);
|
||||||
|
})
|
||||||
|
.catch(err => ([500, err]));
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function qbittorrentProxyHandler(req, res) {
|
||||||
|
const { group, service, endpoint } = req.query;
|
||||||
|
|
||||||
|
if (!group || !service) {
|
||||||
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const widget = await getServiceWidget(group, service);
|
||||||
|
|
||||||
|
if (!widget) {
|
||||||
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = new URL(formatApiCall(widget.type, { endpoint, ...widget }));
|
||||||
|
const params = { method: "GET", headers: {} };
|
||||||
|
setCookieHeader(url, params);
|
||||||
|
|
||||||
|
if (!params.headers.Cookie) {
|
||||||
|
const [status, data] = await login(widget, params);
|
||||||
|
|
||||||
|
if (status !== 200) {
|
||||||
|
return res.status(status).end(data);
|
||||||
|
}
|
||||||
|
if (data.toString() !== 'Ok.') {
|
||||||
|
return res.status(401).end(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const [status, contentType, data] = await httpProxy(url, params);
|
||||||
|
|
||||||
|
if (contentType) res.setHeader("Content-Type", contentType);
|
||||||
|
return res.status(status).send(data);
|
||||||
|
}
|
Loading…
Reference in new issue