Skip to content

Commit

Permalink
fix: fix network member api resp handling
Browse files Browse the repository at this point in the history
filter controller peer
handle undefined
write exp msg to error msg
  • Loading branch information
wongsyrone authored and dec0dOS committed Jul 31, 2023
1 parent db8f497 commit 856682b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
6 changes: 3 additions & 3 deletions backend/routes/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ router.get("/", auth.isAuthorized, async function (req, res) {
api
.get("controller/network/" + nwid + "/member")
.then(async function (controllerRes) {
const mids = Object.keys(controllerRes.data);
const mids = controllerRes.data.map((i) => Object.keys(i)[0]);

This comment has been minimized.

Copy link
@wongsyrone

wongsyrone Sep 1, 2023

Author Contributor

You can revert this line when Zerotier 1.12.2 is released. If someone uses Zerotier dev branch like me, he can fix it based on my change

const data = await member.getMembersData(nwid, mids);
res.send(data);
})
.catch(function () {
res.status(404).send({ error: "Network not found" });
.catch(function (err) {
res.status(404).send({ error: `Network not found ${err}` });
});
});

Expand Down
26 changes: 18 additions & 8 deletions backend/services/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,29 @@ async function getMemberAdditionalData(data) {
.get("members")
.find({ id: data.id });

const additionalData = member.get("additionalConfig").value();
const additionalData = member.get("additionalConfig").value() || {};
const lastOnline = member.get("lastOnline").value() || 0;

const peer = await getPeer(data.id);
let peerData = {};
if (peer) {
if (peer && !_.isEmpty(peer)) {
peerData.latency = peer.latency;
if (peer.latency !== -1) peerData.online = 1;
if (peer.latency == -1) peerData.online = 2;
peerData.clientVersion = peer.version;
if (peer.paths[0]) {
peerData.lastOnline = peer.paths[0].lastReceive;
peerData.physicalAddress = peer.paths[0].address.split("/")[0];
peerData.physicalPort = peer.paths[0].address.split("/")[1];
if (peer.paths.length > 0) {
let path = peer.paths.filter((p) => {
let ret = p.active && !p.expired;
if (typeof p.preferred !== "undefined") {
ret = ret && p.preferred;
}
return ret;
});
if (path.length > 0) {
peerData.lastOnline = path[0].lastReceive;
peerData.physicalAddress = path[0].address.split("/")[0];
peerData.physicalPort = path[0].address.split("/")[1];
}
}
} else {
peerData.online = 0;
Expand Down Expand Up @@ -76,7 +85,8 @@ async function filterDeleted(nwid, mid) {
.get("members")
.find({ id: mid });

if (!member.get("deleted").value()) return mid;
let deleted = member.get("deleted").value() || false;
if (!deleted) return mid;
else return;
}

Expand All @@ -95,7 +105,7 @@ async function getMembersData(nwid, mids) {
return res;
})
)
.catch(function () {
.catch(function (err) {
return [];
});

Expand Down

0 comments on commit 856682b

Please sign in to comment.