2022-11-23 11:51:53 -08:00
|
|
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
|
|
|
import { sendJsonRpcRequest } from "utils/proxy/handlers/jsonrpc";
|
|
|
|
import getServiceWidget from "utils/config/service-helpers";
|
|
|
|
import createLogger from "utils/logger";
|
|
|
|
import widgets from "widgets/widgets";
|
|
|
|
|
|
|
|
const logger = createLogger("delugeProxyHandler");
|
|
|
|
|
|
|
|
const dataMethod = "web.update_ui";
|
|
|
|
const dataParams = [
|
2023-10-17 23:26:55 -07:00
|
|
|
[
|
|
|
|
"queue",
|
|
|
|
"name",
|
|
|
|
"total_wanted",
|
|
|
|
"state",
|
|
|
|
"progress",
|
|
|
|
"download_payload_rate",
|
|
|
|
"upload_payload_rate",
|
|
|
|
"total_remaining",
|
|
|
|
],
|
|
|
|
{},
|
2022-11-23 11:51:53 -08:00
|
|
|
];
|
|
|
|
const loginMethod = "auth.login";
|
|
|
|
|
2022-11-27 22:52:57 -08:00
|
|
|
async function sendRpc(url, method, params) {
|
|
|
|
const [status, contentType, data] = await sendJsonRpcRequest(url, method, params);
|
2022-11-23 11:51:53 -08:00
|
|
|
const json = JSON.parse(data.toString());
|
|
|
|
if (json?.error) {
|
|
|
|
if (json.error.code === 1) {
|
|
|
|
return [403, contentType, data];
|
|
|
|
}
|
|
|
|
return [500, contentType, data];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [status, contentType, data];
|
|
|
|
}
|
|
|
|
|
2022-11-27 22:52:57 -08:00
|
|
|
function login(url, password) {
|
|
|
|
return sendRpc(url, loginMethod, [password]);
|
2022-11-23 11:51:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default async function delugeProxyHandler(req, res) {
|
2024-11-27 02:33:40 -08:00
|
|
|
const { group, service, index } = req.query;
|
2022-11-23 11:51:53 -08:00
|
|
|
|
|
|
|
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" });
|
|
|
|
}
|
|
|
|
|
2024-11-27 02:33:40 -08:00
|
|
|
const widget = await getServiceWidget(group, service, index);
|
2022-11-23 11:51:53 -08:00
|
|
|
|
|
|
|
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" });
|
|
|
|
}
|
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
const api = widgets?.[widget.type]?.api;
|
2022-11-23 11:51:53 -08:00
|
|
|
const url = new URL(formatApiCall(api, { ...widget }));
|
|
|
|
|
2022-11-27 22:52:57 -08:00
|
|
|
let [status, contentType, data] = await sendRpc(url, dataMethod, dataParams);
|
2022-11-23 11:51:53 -08:00
|
|
|
if (status === 403) {
|
2022-11-27 22:52:57 -08:00
|
|
|
[status, contentType, data] = await login(url, widget.password);
|
2022-11-23 11:51:53 -08:00
|
|
|
if (status !== 200) {
|
|
|
|
return res.status(status).end(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2022-11-27 22:52:57 -08:00
|
|
|
[status, contentType, data] = await sendRpc(url, dataMethod, dataParams);
|
2022-11-23 11:51:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(status).end(data);
|
|
|
|
}
|