mirror of
https://github.com/karl0ss/homepage.git
synced 2025-05-03 05:53:40 +01:00
Merge remote-tracking branch 'origin/wg-easy-service-widget' into LocalMain
This commit is contained in:
commit
a3b0acdff4
@ -662,5 +662,8 @@
|
||||
"nextpvr": {
|
||||
"upcoming": "Upcoming",
|
||||
"ready": "Recent"
|
||||
},
|
||||
"wgeasy": {
|
||||
"clients": "Total Clients"
|
||||
}
|
||||
}
|
@ -90,6 +90,7 @@ const components = {
|
||||
uptimekuma: dynamic(() => import("./uptimekuma/component")),
|
||||
watchtower: dynamic(() => import("./watchtower/component")),
|
||||
whatsupdocker: dynamic(() => import("./whatsupdocker/component")),
|
||||
wgeasy: dynamic(() => import("./wgeasy/component")),
|
||||
xteve: dynamic(() => import("./xteve/component")),
|
||||
};
|
||||
|
||||
|
33
src/widgets/wgeasy/component.jsx
Normal file
33
src/widgets/wgeasy/component.jsx
Normal file
@ -0,0 +1,33 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Block from "components/services/widget/block";
|
||||
import Container from "components/services/widget/container";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: wgeasyData, error: wgeasyAPIError } = useWidgetAPI(widget, "unified", {
|
||||
refreshInterval: 5000,
|
||||
});
|
||||
|
||||
if (wgeasyAPIError) {
|
||||
return <Container service={service} error={wgeasyAPIError} />;
|
||||
}
|
||||
|
||||
if (!wgeasyData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="wgeasy.clients" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="wgeasy.clients" value={t("common.number", { value: wgeasyData.clientCount })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
117
src/widgets/wgeasy/proxy.js
Normal file
117
src/widgets/wgeasy/proxy.js
Normal file
@ -0,0 +1,117 @@
|
||||
/* eslint-disable no-underscore-dangle */
|
||||
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";
|
||||
import widgets from "widgets/widgets";
|
||||
|
||||
|
||||
const proxyName = "wgeasyProxyHandler";
|
||||
|
||||
const logger = createLogger(proxyName);
|
||||
let globalSid = null;
|
||||
|
||||
async function getWidget(req) {
|
||||
const { group, service } = req.query;
|
||||
if (!group || !service) {
|
||||
logger.debug("Invalid or missing service '%s' or group '%s'", service, group);
|
||||
return null;
|
||||
}
|
||||
const widget = await getServiceWidget(group, service);
|
||||
if (!widget) {
|
||||
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group);
|
||||
return null;
|
||||
}
|
||||
|
||||
return widget;
|
||||
}
|
||||
|
||||
|
||||
async function loginToWGEasy(endpoint, widget) {
|
||||
const api = widgets?.[widget.type]?.api;
|
||||
if (!api) {
|
||||
return [403, null];
|
||||
}
|
||||
// Create new session on WgEasy
|
||||
const url = new URL(formatApiCall(api, { endpoint, ...widget }));
|
||||
|
||||
const [status, data, , responseHeaders] = await httpProxy(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
password: widget.password,
|
||||
})
|
||||
});
|
||||
|
||||
if (status !== 204) {
|
||||
logger.error("HTTP %d communicating with NextPVR. Data: %s", status, data.toString());
|
||||
return [status, data, responseHeaders];
|
||||
}
|
||||
try {
|
||||
[ globalSid ] = responseHeaders["set-cookie"]
|
||||
} catch (e) {
|
||||
logger.error("Error decoding NextPVR API data. Data: %s", data.toString());
|
||||
return [status, null];
|
||||
}
|
||||
logger.info('gettingSID')
|
||||
return [status, true];
|
||||
}
|
||||
|
||||
|
||||
async function fetchDataFromWGeasy(endpoint, widget, sid) {
|
||||
const api = widgets?.[widget.type]?.api;
|
||||
if (!api) {
|
||||
return [403, null];
|
||||
}
|
||||
const url = `${new URL(formatApiCall(api, { endpoint, ...widget }))}`
|
||||
const [status, contentType, data] = await httpProxy(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Cookie': sid
|
||||
},
|
||||
});
|
||||
|
||||
if (status !== 200) {
|
||||
logger.error("HTTP %d communicating with WGeasy. Data: %s", status, data.toString());
|
||||
return [status, data];
|
||||
}
|
||||
|
||||
try {
|
||||
return [status, JSON.parse(data), contentType];
|
||||
} catch (e) {
|
||||
logger.error("Error decoding WGeasy API data. Data: %s", data.toString());
|
||||
return [status, null];
|
||||
}
|
||||
}
|
||||
|
||||
export default async function WGeasyProxyHandler(req, res) {
|
||||
const widget = await getWidget(req);
|
||||
|
||||
if (!globalSid) {
|
||||
await loginToWGEasy('session', widget);
|
||||
}
|
||||
if (!widget) {
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
||||
logger.debug("Getting data from WGeasy API");
|
||||
// Calculate the number of clients
|
||||
const [status, apiData] = await fetchDataFromWGeasy('wireguard/client', widget, globalSid);
|
||||
|
||||
if (status !== 200) {
|
||||
return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } });
|
||||
}
|
||||
let clientCount = 0;
|
||||
clientCount = apiData.length;
|
||||
|
||||
const data = {
|
||||
clientCount
|
||||
};
|
||||
|
||||
return res.status(status).send(data);
|
||||
|
||||
}
|
||||
|
14
src/widgets/wgeasy/widget.js
Normal file
14
src/widgets/wgeasy/widget.js
Normal file
@ -0,0 +1,14 @@
|
||||
import nextpvrProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/{endpoint}",
|
||||
proxyHandler: nextpvrProxyHandler,
|
||||
|
||||
mappings: {
|
||||
unified: {
|
||||
endpoint: "/",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
@ -84,6 +84,7 @@ import unmanic from "./unmanic/widget";
|
||||
import uptimekuma from "./uptimekuma/widget";
|
||||
import watchtower from "./watchtower/widget";
|
||||
import whatsupdocker from "./whatsupdocker/widget";
|
||||
import wgeasy from "./wgeasy/widget";
|
||||
import xteve from "./xteve/widget";
|
||||
|
||||
const widgets = {
|
||||
@ -175,6 +176,7 @@ const widgets = {
|
||||
uptimekuma,
|
||||
watchtower,
|
||||
whatsupdocker,
|
||||
wgeasy,
|
||||
xteve,
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user