commit
ff31b36b46
@ -0,0 +1,53 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: torrentData, error: torrentError } = useWidgetAPI(widget, "torrents");
|
||||
|
||||
if (torrentError) {
|
||||
return <Container error={torrentError} />;
|
||||
}
|
||||
|
||||
if (!torrentData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="flood.leech" />
|
||||
<Block label="flood.download" />
|
||||
<Block label="flood.seed" />
|
||||
<Block label="flood.upload" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
let rateDl = 0;
|
||||
let rateUl = 0;
|
||||
let completed = 0;
|
||||
let leech = 0;
|
||||
|
||||
for (var torrent in torrentData.torrents) {
|
||||
rateDl += torrentData.torrents[torrent].downRate;
|
||||
rateUl += torrentData.torrents[torrent].upRate;
|
||||
if(torrentData.torrents[torrent].status.includes('complete')){
|
||||
completed += 1;
|
||||
}
|
||||
if(torrentData.torrents[torrent].status.includes('downloading')){
|
||||
leech += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="flood.leech" value={t("common.number", { value: leech })} />
|
||||
<Block label="flood.download" value={t("common.bitrate", { value: rateDl })} />
|
||||
<Block label="flood.seed" value={t("common.number", { value: completed })} />
|
||||
<Block label="flood.upload" value={t("common.bitrate", { value: rateUl })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import createLogger from "utils/logger";
|
||||
|
||||
const logger = createLogger("floodProxyHandler");
|
||||
|
||||
async function login(widget) {
|
||||
logger.debug("flood is rejecting the request, logging in.");
|
||||
const loginUrl = new URL(`${widget.url}/api/auth/authenticate`).toString();
|
||||
|
||||
const loginParams = {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: null
|
||||
};
|
||||
|
||||
if (widget.username && widget.password) {
|
||||
loginParams.body = JSON.stringify({
|
||||
"username": widget.username,
|
||||
"password": widget.password
|
||||
});
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const [status, contentType, data] = await httpProxy(loginUrl, loginParams);
|
||||
return [status, data];
|
||||
}
|
||||
|
||||
export default async function floodProxyHandler(req, res) {
|
||||
const { group, service, endpoint } = req.query;
|
||||
|
||||
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" });
|
||||
}
|
||||
|
||||
const widget = await getServiceWidget(group, service);
|
||||
|
||||
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" });
|
||||
}
|
||||
|
||||
const url = new URL(formatApiCall("{url}/api/{endpoint}", { endpoint, ...widget }));
|
||||
const params = { method: "GET", headers: {} };
|
||||
|
||||
let [status, contentType, data] = await httpProxy(url, params);
|
||||
if (status === 401) {
|
||||
[status, data] = await login(widget);
|
||||
|
||||
if (status !== 200) {
|
||||
logger.error("HTTP %d logging in to flood. Data: %s", status, data);
|
||||
return res.status(status).end(data);
|
||||
}
|
||||
|
||||
[status, contentType, data] = await httpProxy(url, params);
|
||||
}
|
||||
|
||||
if (status !== 200) {
|
||||
logger.error("HTTP %d getting data from flood. Data: %s", status, data);
|
||||
}
|
||||
|
||||
if (contentType) res.setHeader("Content-Type", contentType);
|
||||
return res.status(status).send(data);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
import floodProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
proxyHandler: floodProxyHandler,
|
||||
};
|
||||
|
||||
export default widget;
|
Loading…
Reference in new issue