commitad3e664b56Author: Benoit <oupsman@oupsman.fr> Date: Tue Dec 13 19:54:54 2022 +0100 Add .idea to .gitignore commit7e51a09384Merge:93d80357dd0b0eAuthor: Benoit SERRA <oupsman@oupsman.fr> Date: Tue Dec 13 18:38:51 2022 +0100 Merge branch 'benphelps:main' into main commit93d80350b1Author: Benoit <oupsman@oupsman.fr> Date: Tue Dec 13 18:15:20 2022 +0100 Omada widget : One widget, shows only the number alerts, the number of connected AP, the number of connected devices to Wifi, the number of connected switches and gatewawys. commita1babd860cAuthor: Benoit <oupsman@oupsman.fr> Date: Tue Dec 13 09:33:50 2022 +0100 Omada widget : spliting widget between WLAN and LAN/WAN fields to have no more than 5 fields per widget. commite12cc65c77Merge:331f31f146326fAuthor: Benoit SERRA <oupsman@oupsman.fr> Date: Sun Dec 11 14:39:27 2022 +0100 Merge branch 'benphelps:main' into main commit331f31fc2bMerge:37154e3ccc1229Author: Benoit SERRA <oupsman@oupsman.fr> Date: Sat Dec 10 17:56:44 2022 +0100 Merge branch 'benphelps:main' into main commit37154e327aAuthor: Benoit <oupsman@oupsman.fr> Date: Sat Dec 10 17:11:30 2022 +0100 Omada widget : Improved error handling Omada widget: handling power as common.power in translation commit1f48491406Author: Benoit <oupsman@oupsman.fr> Date: Sat Dec 10 10:24:55 2022 +0100 Omada widget : adding stats for isolated aps, connected gateways, connected switches, available ports, power consumption commitf375f0b815Merge:467b678775b511Author: Benoit <oupsman@oupsman.fr> Date: Fri Dec 9 21:06:38 2022 +0100 Merge branch 'main' of https://github.com/Oupsman/homepage into main commit467b67802aAuthor: Benoit <oupsman@oupsman.fr> Date: Fri Dec 9 21:06:09 2022 +0100 Omada widget : v3 v4 and v5 versions don't use the same fields for the same stats, I've corrected the code to make it more reliable commit775b5111e1Merge:8d6675688c4375Author: Benoit SERRA <oupsman@oupsman.fr> Date: Thu Dec 8 15:38:20 2022 +0100 Merge branch 'benphelps:main' into main commit8d66756a7dAuthor: Benoit <oupsman@oupsman.fr> Date: Thu Dec 8 12:45:44 2022 +0100 Omada Widget : code cleanup commit282a6d0592Author: Benoit <oupsman@oupsman.fr> Date: Thu Dec 8 12:42:41 2022 +0100 Omada Widget : code cleanup commitc3e9b8f870Author: Benoit <oupsman@oupsman.fr> Date: Thu Dec 8 12:37:10 2022 +0100 Omada Widget : No more legacy variable, the code detects the controller version and adapts the requests. Logic is not duplicated anymore commiteafcc20597Author: Benoit <oupsman@oupsman.fr> Date: Wed Dec 7 15:46:00 2022 +0100 V2 API is working commitbcc2864ee2Author: Benoit <oupsman@oupsman.fr> Date: Wed Dec 7 10:01:26 2022 +0100 Code fore v2 API is not working but V1 code is. commitea8e297e84Author: Benoit <oupsman@oupsman.fr> Date: Tue Dec 6 14:28:05 2022 +0100 Errors handling commitab6d51a88cAuthor: Benoit <oupsman@oupsman.fr> Date: Tue Dec 6 09:50:14 2022 +0100 Adding alerts commit047db2cce8Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 22:53:43 2022 +0100 Fixed translation system commit42c5a3e665Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 22:34:34 2022 +0100 Translation system is still * up commitc80eac9d5bAuthor: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 22:33:50 2022 +0100 Translation system is still * up commitf8ba6b0245Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 22:32:22 2022 +0100 Translation system is still * up commitdec7eec6deAuthor: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 22:16:13 2022 +0100 Translation system is * up commitcc840cf7ccAuthor: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 21:33:00 2022 +0100 First working version commit54b65e619eAuthor: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 18:59:09 2022 +0100 Using getGlobalStat method commit7ebc8500daAuthor: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 14:33:37 2022 +0100 Working on Omada Widget : NOT WORKING FOR NOW commit04eaf28caeMerge:61065ac826fe15Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 10:32:30 2022 +0100 Merge branch 'main' of https://github.com/Oupsman/homepage into main commit61065ace28Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 10:24:57 2022 +0100 Working on Omada Widget remove idea Co-Authored-By: Benoit SERRA <11260343+oupsman@users.noreply.github.com>
273 lines
8.8 KiB
JavaScript
273 lines
8.8 KiB
JavaScript
|
|
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 = "omadaProxyHandler";
|
|
|
|
const logger = createLogger(proxyName);
|
|
|
|
|
|
async function login(loginUrl, username, password, cversion) {
|
|
let params;
|
|
if (cversion < "4.0.0") {
|
|
// change the parameters of the query string
|
|
params = JSON.stringify({
|
|
"method": "login",
|
|
"params": {
|
|
"name": username,
|
|
"password": password
|
|
}
|
|
});
|
|
} else {
|
|
params = JSON.stringify({
|
|
"username": username,
|
|
"password": password
|
|
});
|
|
}
|
|
const authResponse = await httpProxy(loginUrl, {
|
|
method: "POST",
|
|
body: params,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
const data = JSON.parse(authResponse[2]);
|
|
const status = authResponse[0];
|
|
let token;
|
|
if (data.errorCode === 0) {
|
|
token = data.result.token;
|
|
} else {
|
|
token = null;
|
|
}
|
|
return [status, token ?? data];
|
|
}
|
|
|
|
|
|
export default async function omadaProxyHandler(req, res) {
|
|
const { group, service } = req.query;
|
|
|
|
if (group && service) {
|
|
const widget = await getServiceWidget(group, service);
|
|
|
|
if (!widgets?.[widget.type]?.api) {
|
|
return res.status(403).json({ error: "Service does not support API calls" });
|
|
}
|
|
|
|
if (widget) {
|
|
let cid;
|
|
let cversion;
|
|
let connectedAp;
|
|
let activeuser;
|
|
let connectedSwitches;
|
|
let connectedGateways;
|
|
|
|
let alerts;
|
|
let loginUrl;
|
|
let siteName;
|
|
let requestresponse;
|
|
|
|
const {url} = widget;
|
|
|
|
const controllerInfoUrl = `${widget.url}/api/info`;
|
|
|
|
const cInfoResponse = await httpProxy(controllerInfoUrl, {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
|
|
if (cInfoResponse[0] === 500) {
|
|
logger.debug("Getting controller version ends with Error 500");
|
|
return res.status(cInfoResponse[0]).json({error: {message: "HTTP Error", controllerInfoUrl, data: cInfoResponse[2]}});
|
|
|
|
}
|
|
const cidresult = cInfoResponse[2];
|
|
|
|
try {
|
|
cid = JSON.parse(cidresult).result.omadacId;
|
|
cversion = JSON.parse(cidresult).result.controllerVer;
|
|
} catch (e) {
|
|
cversion = "3.2.17"
|
|
}
|
|
if (cversion < "4.0.0") {
|
|
loginUrl = `${widget.url}/api/user/login?ajax`;
|
|
} else if (cversion < "5.0.0") {
|
|
loginUrl = `${widget.url}/api/v2/login`;
|
|
} else {
|
|
loginUrl = `${widget.url}/${cid}/api/v2/login`;
|
|
}
|
|
requestresponse = await login(loginUrl, widget.username, widget.password, cversion);
|
|
|
|
if (requestresponse[1].errorCode) {
|
|
return res.status(requestresponse[0]).json({error: {message: "Error logging in", url, data: requestresponse[1]}});
|
|
}
|
|
|
|
const token = requestresponse[1];
|
|
// Switching to the site we want to gather stats from
|
|
// First, we get the list of sites
|
|
let sitesUrl;
|
|
let body;
|
|
let params;
|
|
let headers;
|
|
let method;
|
|
let sitetoswitch;
|
|
if (cversion < "4.0.0") {
|
|
sitesUrl = `${widget.url}/web/v1/controller?ajax=&token=${token}`;
|
|
body = JSON.stringify({
|
|
"method": "getUserSites",
|
|
"params": {
|
|
"userName": widget.username
|
|
}});
|
|
params = { "token": token };
|
|
headers = { };
|
|
method = "POST";
|
|
} else if (cversion < "5.0.0") {
|
|
sitesUrl = `${widget.url}/api/v2/sites?token=${token}¤tPage=1¤tPageSize=1000`;
|
|
body = {};
|
|
params = {"token": token};
|
|
headers = {"Csrf-Token": token };
|
|
method = "GET";
|
|
|
|
} else {
|
|
sitesUrl = `${widget.url}/${cid}/api/v2/sites?token=${token}¤tPage=1¤tPageSize=1000`;
|
|
body = {};
|
|
headers = { "Csrf-Token": token };
|
|
method = "GET";
|
|
params = { };
|
|
}
|
|
requestresponse = await httpProxy(sitesUrl, {
|
|
method,
|
|
params,
|
|
body: body.toString(),
|
|
headers,
|
|
});
|
|
const listresult = JSON.parse(requestresponse[2]);
|
|
if (listresult.errorCode !== 0) {
|
|
logger.debug(`HTTTP ${requestresponse[0]} getting sites list: ${requestresponse[2].msg}`);
|
|
return res.status(requestresponse[0]).json({error: {message: "Error getting sites list", url, data: requestresponse[2]}});
|
|
}
|
|
|
|
// Switching site is really needed only for Omada 3.x.x controllers
|
|
|
|
let switchUrl;
|
|
|
|
if (cversion < "4.0.0") {
|
|
sitetoswitch = listresult.result.siteList.filter(site => site.name === widget.site);
|
|
siteName = sitetoswitch[0].siteName;
|
|
switchUrl = `${widget.url}/web/v1/controller?ajax=&token=${token}`;
|
|
method = "POST";
|
|
body = JSON.stringify({
|
|
"method": "switchSite",
|
|
"params": {
|
|
"siteName": siteName,
|
|
"userName": widget.username
|
|
}
|
|
});
|
|
headers = { "Content-Type": "application/json" };
|
|
params = { "token": token };
|
|
requestresponse = await httpProxy(switchUrl, {
|
|
method,
|
|
params,
|
|
body: body.toString(),
|
|
headers,
|
|
});
|
|
const switchresult = JSON.parse(requestresponse[2]);
|
|
if (switchresult.errorCode !== 0) {
|
|
logger.debug(`HTTTP ${requestresponse[0]} getting sites list: ${requestresponse[2]}`);
|
|
return res.status(requestresponse[0]).json({error: {message: "Error switching site", url, data: requestresponse[2]}});
|
|
}
|
|
}
|
|
|
|
// OK now we are on the correct site. Let's get the stats
|
|
// on modern controller, we need to call two different endpoints
|
|
// on older controller, we can call one endpoint
|
|
if (cversion < "4.0.0") {
|
|
const statsUrl = `${widget.url}/web/v1/controller?getGlobalStat=&token=${token}`;
|
|
const statResponse = await httpProxy(statsUrl, {
|
|
method: "POST",
|
|
params: { "token": token },
|
|
body: JSON.stringify({
|
|
"method": "getGlobalStat",
|
|
}),
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
const data = JSON.parse(statResponse[2]);
|
|
|
|
if (data.errorCode !== 0) {
|
|
return res.status(statResponse[0]).json({error: {message: "Error getting stats", url, data: statResponse[2]}});
|
|
}
|
|
connectedAp = data.result.connectedAp;
|
|
activeuser = data.result.activeUser;
|
|
alerts = data.result.alerts;
|
|
|
|
} else {
|
|
let siteStatsUrl;
|
|
let response;
|
|
sitetoswitch = listresult.result.data.filter(site => site.name === widget.site);
|
|
|
|
if (sitetoswitch.length === 0) {
|
|
return res.status(requestresponse[0]).json({error: {message: `Site ${widget.site} is not found`, url, data: requestresponse[2]}});
|
|
}
|
|
|
|
// On 5.0.0, the field we need is id, on 4.x.x, it's key ...
|
|
siteName = sitetoswitch[0].id ?? sitetoswitch[0].key;
|
|
if (cversion < "5.0.0") {
|
|
siteStatsUrl = `${url}/api/v2/sites/${siteName}/dashboard/overviewDiagram?token=${token}¤tPage=1¤tPageSize=1000`;
|
|
} else {
|
|
siteStatsUrl = `${url}/${cid}/api/v2/sites/${siteName}/dashboard/overviewDiagram?token=${token}¤tPage=1¤tPageSize=1000`;
|
|
}
|
|
response = await httpProxy(siteStatsUrl, {
|
|
method: "GET",
|
|
headers: {
|
|
"Csrf-Token": token,
|
|
},
|
|
});
|
|
|
|
const clientresult = JSON.parse(response[2]);
|
|
if (clientresult.errorCode !== 0) {
|
|
logger.debug(`HTTTP ${listresult.errorCode} getting clients stats for site ${widget.site} with message ${listresult.msg}`);
|
|
return res.status(500).send(response[2]);
|
|
}
|
|
|
|
activeuser = clientresult.result.totalClientNum;
|
|
connectedAp = clientresult.result.connectedApNum;
|
|
connectedGateways = clientresult.result.connectedGatewayNum;
|
|
connectedSwitches = clientresult.result.connectedSwitchNum;
|
|
|
|
|
|
let alertUrl;
|
|
if (cversion >= "5.0.0") {
|
|
alertUrl = `${url}/${cid}/api/v2/sites/${siteName}/alerts/num?token=${token}¤tPage=1¤tPageSize=1000`;
|
|
} else {
|
|
alertUrl = `${url}/api/v2/sites/${siteName}/alerts/num?token=${token}¤tPage=1¤tPageSize=1000`;
|
|
}
|
|
response = await httpProxy(alertUrl, {
|
|
method: "GET",
|
|
headers: {
|
|
"Csrf-Token": token,
|
|
},
|
|
});
|
|
const alertresult = JSON.parse(response[2]);
|
|
alerts = alertresult.result.alertNum;
|
|
}
|
|
|
|
return res.send(JSON.stringify({
|
|
"connectedAp": connectedAp,
|
|
"activeUser": activeuser,
|
|
"alerts": alerts,
|
|
"connectedGateways": connectedGateways,
|
|
"connectedSwitches": connectedSwitches,
|
|
}));
|
|
}
|
|
}
|
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
|
}
|