mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-09 23:08:48 +00:00
widget refactoring
This commit is contained in:
parent
03fa2f86d7
commit
035dd25ece
29 changed files with 851 additions and 10 deletions
|
@ -1,58 +0,0 @@
|
|||
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);
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
import RuTorrent from "rutorrent-promise";
|
||||
|
||||
import getServiceWidget from "utils/service-helpers";
|
||||
|
||||
export default async function rutorrentProxyHandler(req, res) {
|
||||
const { group, service } = req.query;
|
||||
|
||||
if (group && service) {
|
||||
const widget = await getServiceWidget(group, service);
|
||||
|
||||
if (widget) {
|
||||
const constructedUrl = new URL(widget.url);
|
||||
|
||||
const rutorrent = new RuTorrent({
|
||||
host: constructedUrl.hostname,
|
||||
port: constructedUrl.port,
|
||||
path: constructedUrl.pathname,
|
||||
ssl: constructedUrl.protocol === "https:",
|
||||
username: widget.username,
|
||||
password: widget.password,
|
||||
});
|
||||
|
||||
const data = await rutorrent.get(["d.get_down_rate", "d.get_up_rate", "d.get_state"]);
|
||||
|
||||
return res.status(200).send(data);
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
import { httpProxy } from "utils/http";
|
||||
import { formatApiCall } from "utils/api-helpers";
|
||||
import getServiceWidget from "utils/service-helpers";
|
||||
|
||||
export default async function transmissionProxyHandler(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 csrfHeaderName = "x-transmission-session-id";
|
||||
|
||||
const method = "POST";
|
||||
const auth = `${widget.username}:${widget.password}`;
|
||||
const body = JSON.stringify({
|
||||
method: "torrent-get",
|
||||
arguments: {
|
||||
fields: ["percentDone", "status", "rateDownload", "rateUpload"]
|
||||
}
|
||||
});
|
||||
|
||||
const headers = {
|
||||
"content-type": "application/json",
|
||||
};
|
||||
|
||||
let [status, contentType, data, responseHeaders] = await httpProxy(url, {
|
||||
method,
|
||||
auth,
|
||||
body,
|
||||
headers,
|
||||
});
|
||||
|
||||
if (status === 409) {
|
||||
// Transmission is rejecting the request, but returning a CSRF token
|
||||
headers[csrfHeaderName] = responseHeaders[csrfHeaderName];
|
||||
|
||||
// retry the request, now with the CSRF token
|
||||
[status, contentType, data, responseHeaders] = await httpProxy(url, {
|
||||
method,
|
||||
auth,
|
||||
body,
|
||||
headers,
|
||||
});
|
||||
}
|
||||
|
||||
if (contentType) res.setHeader("Content-Type", contentType);
|
||||
return res.status(status).send(data);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue