Omada Widget : No more legacy variable, the code detects the controller version and adapts the requests. Logic is not duplicated anymore

This commit is contained in:
Benoit
2022-12-08 12:37:10 +01:00
parent eafcc20597
commit c3e9b8f870

View File

@@ -1,6 +1,3 @@
import cache from "memory-cache";
import { addCookieToJar, setCookieHeader } from "../../utils/proxy/cookie-jar";
import { httpProxy } from "utils/proxy/http"; import { httpProxy } from "utils/proxy/http";
import getServiceWidget from "utils/config/service-helpers"; import getServiceWidget from "utils/config/service-helpers";
@@ -8,42 +5,43 @@ import createLogger from "utils/logger";
import widgets from "widgets/widgets"; import widgets from "widgets/widgets";
const proxyName = "omadaProxyHandler"; const proxyName = "omadaProxyHandler";
const tokenCacheKey = `${proxyName}__token`; // const tokenCacheKey = `${proxyName}__token`;
const logger = createLogger(proxyName); const logger = createLogger(proxyName);
async function login(loginUrl, username, password, legacy) { async function login(loginUrl, username, password, cversion) {
let params;
if (legacy) { if (cversion < "4.0.0") {
// change the parameters of the query string
const authResponse = await httpProxy(loginUrl, params = JSON.stringify({
{ "method": "login",
method: "POST", "params": {
body: JSON.stringify({ "method": "login", "name": username,
"params": { "password": password
"name": username, }
"password": password });
} }), } else {
params = JSON.stringify({
"username": username,
"password": password
});
}
const authResponse = await httpProxy(loginUrl, {
method: "POST",
body: params,
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
} });
); const data = JSON.parse(authResponse[2]);
const status = data.errorCode;
const status = authResponse[0]; let token = null;
const data = JSON.parse(authResponse[2]); if (data.errorCode === 0) {
const {token} = data.result; token = data.result.token;
try { } else {
if (status === 200) { token = null;
cache.put(tokenCacheKey, token); // expiration -5 minutes
}
} catch (e) {
logger.error(`Error ${status} logging into Omada`, authResponse[2]);
}
return [status, token ?? data];
} }
return [status, token ?? data];
return null
} }
@@ -58,71 +56,137 @@ export default async function omadaProxyHandler(req, res) {
} }
if (widget) { if (widget) {
if (widget.legacy) { let status;
const loginUrl = `${widget.url}/api/user/login?ajax`; let token;
let status; let cid;
let contentType; let cversion;
let data; let connectedAp;
let result; let activeuser;
let token; let alerts;
// eslint-disable-next-line prefer-const let loginUrl;
[status, token] = await login(loginUrl, widget.username, widget.password, widget.legacy); let siteName;
if (status !== 200) { let requestresponse;
logger.debug(`HTTTP ${status} logging into Omada api: ${token}`); const {url} = widget;
return res.status(status).send(token);
} const controllerInfoUrl = `${widget.url}/api/info`;
// Switching to the site we want to gather stats from const cInfoResponse = await httpProxy(controllerInfoUrl, {
// First, we get the list of sites method: "GET",
const sitesUrl = `${widget.url}/web/v1/controller?ajax=&token=${token}`; headers: {
[status, contentType, data] = await httpProxy(sitesUrl, { "Content-Type": "application/json",
method: "POST", },
params: { "token": token }, });
body: JSON.stringify({
"method": "getUserSites", const cidresult = cInfoResponse[2];
"params": {
"userName": widget.username try {
} cid = JSON.parse(cidresult).result.omadacId;
}), cversion = JSON.parse(cidresult).result.controllerVer;
headers: { } catch (e) {
"Content-Type": "application/json", 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`;
}
[ status, token ] = await login(loginUrl, widget.username, widget.password, cversion);
if (status !== 0) {
const message = token.msg;
logger.debug(`HTTTP ${status} logging into Omada api: ${token}`);
return res.status(500).send(message);
}
// 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}&currentPage=1&currentPageSize=1000`;
body = {};
params = { };
headers = {"Csrf-Token": token };
method = "GET";
} else {
sitesUrl = `${widget.url}/${cid}/api/v2/sites?token=${token}&currentPage=1&currentPageSize=1000`;
body = {};
headers = { "Csrf-Token": token };
method = "GET";
params = { };
}
const listResponse = await httpProxy(sitesUrl, {
method: method,
params: params,
body: body.toString(),
headers: headers,
});
const listresult = JSON.parse(listResponse[2]);
if (listresult.errorCode !== 0) {
logger.debug(`HTTTP ${listresult.errorCode} getting list of sites with message ${listresult.msg}`);
return res.status(status).send(data);
}
// 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
}
}); });
const listresult = JSON.parse(data); headers = { "Content-Type": "application/json" };
if (listresult.errorCode !== 0) { params = { "token": token };
logger.debug(`HTTTP ${listresult.errorCode} getting list of sites with message ${listresult.msg}`); const switchResponse = await httpProxy(switchUrl, {
return res.status(status).send(data); method: method,
} params: params,
body: body.toString(),
const sites = JSON.parse(data); headers: headers,
const sitetoswitch = sites.result.siteList.filter(site => site.name === widget.site);
const { siteName } = sitetoswitch[0];
const switchUrl = `${widget.url}/web/v1/controller?ajax=&token=${token}`;
[status, contentType, result] = await httpProxy(switchUrl, {
method: "POST",
params: { "token": token },
body: JSON.stringify({
"method": "switchSite",
"params": { "siteName": siteName, "userName": widget.username }
}),
headers: {
"Content-Type": "application/json",
},
}); });
const switchresult = JSON.parse(result); console.log(cversion, "after switch", switchResponse[2].toString());
const switchresult = JSON.parse(switchResponse[2]);
if (switchresult.errorCode !== 0) { if (switchresult.errorCode !== 0) {
logger.debug(`HTTTP ${switchresult.errorCode} switching site with message ${switchresult.msg}`); logger.debug(`HTTTP ${switchresult.errorCode} switching site with message ${switchresult.msg}`);
return res.status(status).send(data); return res.status(500).send(switchresult);
} }
}
const url = `${widget.url}/web/v1/controller?globalStat=&token=${token}`; // OK now we are on the correct site. Let's get the stats
// on modern controller, we need to call two different endpoints
// eslint-disable-next-line prefer-const,no-unused-vars // on older controller, we can call one endpoint
[status, contentType, result] = await httpProxy(url, { if (cversion < "4.0.0") {
console.log(cversion, token, "Legacy controller");
const statsUrl = `${widget.url}/web/v1/controller?getGlobalStat=&token=${token}`;
const statResponse = await httpProxy(statsUrl, {
method: "POST", method: "POST",
params: { "token": token }, params: { "token": token },
body: JSON.stringify({ body: JSON.stringify({
@@ -133,145 +197,67 @@ export default async function omadaProxyHandler(req, res) {
}, },
}); });
data = JSON.parse(result); const data = JSON.parse(statResponse[2]);
if (data.errorCode !== 0) { if (data.errorCode !== 0) {
return res.status(status).send(data); return res.status(status).send(data);
} }
connectedAp = data.result.connectedAp;
return res.send(data.result); activeuser = data.result.activeUser;
} alerts = data.result.alerts;
// code for modern API, not working yet return res.send(JSON.stringify({
// Working on it but I can't test it "connectedAp": connectedAp,
const {url} = widget; "activeUser": activeuser,
const controllerInfoUrl = `${url}/api/info`; "alerts": alerts
const cInfoResponse = await httpProxy(controllerInfoUrl, { }));
method: "GET", } else {
headers: { let siteStatsUrl;
"Content-Type": "application/json",
},
});
const cidresult = cInfoResponse[2];
const cid = JSON.parse(cidresult).result.omadacId;
const cversion = JSON.parse(cidresult).result.controllerVer;
let loginUrl;
if (cversion >= "5.0.0"){
loginUrl = `${url}/${cid}/api/v2/login`;
} else {
loginUrl = `${url}/api/v2/login`;
}
const params = {
method: "POST",
body: JSON.stringify({ "username": widget.username, "password": widget.password }),
headers: {"Content-Type": "application/json"} };
setCookieHeader(url, params);
const authResponse = await httpProxy(loginUrl,params);
addCookieToJar(url, authResponse[3]);
setCookieHeader(url, params);
const status = authResponse[0];
const data = JSON.parse(authResponse[2]);
const {token} = data.result;
if (data.errorCode !== 0) {
logger.debug(`HTTTP ${data.errorCode} logging into Omada api: ${data.error}`);
return res.status(status).send(token);
}
let sitesUrl;
if (cversion >= "5.0.0") {
sitesUrl = `${url}/${cid}/api/v2/sites?token=${token}&currentPage=1&currentPageSize=1000`;
} else {
sitesUrl = `${url}/api/v2/sites?token=${token}&currentPage=1&currentPageSize=1000`;
}
let response; let response;
response = await httpProxy(sitesUrl, { console.log("Modern controller, getting the stats");
if (cversion < "5.0.0") {
siteStatsUrl = `${url}/api/v2/sites/${siteName}/dashboard/overviewDiagram?token=${token}&currentPage=1&currentPageSize=1000`;
} else {
siteStatsUrl = `${url}/${cid}/api/v2/sites/${siteName}/dashboard/overviewDiagram?token=${token}&currentPage=1&currentPageSize=1000`;
}
response = await httpProxy(siteStatsUrl, {
method: "GET", method: "GET",
headers: { headers: {
"Csrf-Token": token, "Csrf-Token": token,
}, },
}); });
console.log(response);
const listresult = JSON.parse(response[2]); const clientresult = JSON.parse(response[2]);
if (listresult.errorCode !== 0) { if (clientresult.errorCode !== 0) {
logger.debug(`HTTTP ${listresult.errorCode} getting list of sites with message ${listresult.msg}`); logger.debug(`HTTTP ${listresult.errorCode} getting clients stats for site ${widget.site} with message ${listresult.msg}`);
return res.status(status).send(data); return res.status(500).send(listresult);
} }
const sites = JSON.parse(response[2]); activeuser = clientresult.result.totalClientNum;
connectedAp = clientresult.result.connectedApNum;
const sitetoswitch = sites.result.data.filter(site => site.name === widget.site); let alertUrl;
const siteName = sitetoswitch[0].id; if (cversion >= "5.0.0") {
alertUrl = `${url}/${cid}/api/v2/sites/${siteName}/alerts/num?token=${token}&currentPage=1&currentPageSize=1000`;
let switchUrl; } else {
if (cversion >= "5.0.0") { alertUrl = `${url}/api/v2/sites/${siteName}/alerts/num?token=${token}&currentPage=1&currentPageSize=1000`;
switchUrl = `${url}/${cid}/api/v2/sites/${siteName}/cmd/switch?token=${token}`; }
} else { response = await httpProxy(alertUrl, {
switchUrl = `${url}/api/v2/sites/${siteName}/cmd/switch?token=${token}`; method: "GET",
} headers: {
response = await httpProxy(switchUrl, { "Csrf-Token": token,
method: "POST", },
headers: { });
"Csrf-Token": token, const alertresult = JSON.parse(response[2]);
}, alerts = alertresult.result.alertNum;
});
const switchresult = JSON.parse(response[2]);
if (switchresult.errorCode !== 0) {
logger.debug(`HTTTP ${listresult.errorCode} switching to site ${widget.site} with message ${listresult.msg}`);
return res.status(status).send(data);
} }
// get the number of devices connected to the site return res.send(JSON.stringify({
let clientUrl;
if (cversion >= "5.0.0") {
clientUrl=`${url}/${cid}/api/v2/sites/${siteName}/dashboard/overviewDiagram?token=${token}&currentPage=1&currentPageSize=1000`;
} else {
clientUrl=`${url}/api/v2/sites/${siteName}/dashboard/overviewDiagram?token=${token}&currentPage=1&currentPageSize=1000`;
}
response = await httpProxy(clientUrl, {
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(status).send(data);
}
const activeuser = clientresult.result.totalClientNum;
const connectedAp = clientresult.result.connectedApNum;
let alertUrl;
if (cversion >= "5.0.0") {
alertUrl=`${url}/${cid}/api/v2/sites/${siteName}/alerts/num?token=${token}&currentPage=1&currentPageSize=1000`;
} else {
alertUrl=`${url}/api/v2/sites/${siteName}/alerts/num?token=${token}&currentPage=1&currentPageSize=1000`;
}
response = await httpProxy(alertUrl, {
method: "GET",
headers: {
"Csrf-Token": token,
},
});
const alertresult = JSON.parse(response[2]);
const alerts = alertresult.result.alertNum;
const returnvalue = JSON.stringify({
"connectedAp": connectedAp, "connectedAp": connectedAp,
"activeUser": activeuser, "activeUser": activeuser,
"alerts": alerts "alerts": alerts
}); }));
return res.send(returnvalue);
} }
} }
return res.status(400).json({ error: "Invalid proxy service type" }); return res.status(400).json({ error: "Invalid proxy service type" });
} }