mirror of
https://github.com/karl0ss/homepage.git
synced 2025-05-03 22:13:39 +01:00
WGeasy initial commit
This commit is contained in:
parent
062e99da6f
commit
667e759ca4
@ -653,5 +653,8 @@
|
|||||||
"nextpvr": {
|
"nextpvr": {
|
||||||
"upcoming": "Upcoming",
|
"upcoming": "Upcoming",
|
||||||
"ready": "Recent"
|
"ready": "Recent"
|
||||||
|
},
|
||||||
|
"wgeasy": {
|
||||||
|
"clients": "Total Clients"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -89,6 +89,7 @@ const components = {
|
|||||||
uptimekuma: dynamic(() => import("./uptimekuma/component")),
|
uptimekuma: dynamic(() => import("./uptimekuma/component")),
|
||||||
watchtower: dynamic(() => import("./watchtower/component")),
|
watchtower: dynamic(() => import("./watchtower/component")),
|
||||||
whatsupdocker: dynamic(() => import("./whatsupdocker/component")),
|
whatsupdocker: dynamic(() => import("./whatsupdocker/component")),
|
||||||
|
wgeasy: dynamic(() => import("./wgeasy/component")),
|
||||||
xteve: dynamic(() => import("./xteve/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>
|
||||||
|
);
|
||||||
|
}
|
118
src/widgets/wgeasy/proxy.js
Normal file
118
src/widgets/wgeasy/proxy.js
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
/* 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
|
||||||
|
let url = new URL(formatApiCall(api, { endpoint, ...widget }));
|
||||||
|
|
||||||
|
let [status, contentType, 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"][0]
|
||||||
|
} 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
|
||||||
|
let [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;
|
||||||
|
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;
|
@ -83,6 +83,7 @@ import unmanic from "./unmanic/widget";
|
|||||||
import uptimekuma from "./uptimekuma/widget";
|
import uptimekuma from "./uptimekuma/widget";
|
||||||
import watchtower from "./watchtower/widget";
|
import watchtower from "./watchtower/widget";
|
||||||
import whatsupdocker from "./whatsupdocker/widget";
|
import whatsupdocker from "./whatsupdocker/widget";
|
||||||
|
import wgeasy from "./wgeasy/widget";
|
||||||
import xteve from "./xteve/widget";
|
import xteve from "./xteve/widget";
|
||||||
|
|
||||||
const widgets = {
|
const widgets = {
|
||||||
@ -173,6 +174,7 @@ const widgets = {
|
|||||||
uptimekuma,
|
uptimekuma,
|
||||||
watchtower,
|
watchtower,
|
||||||
whatsupdocker,
|
whatsupdocker,
|
||||||
|
wgeasy,
|
||||||
xteve,
|
xteve,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user