mirror of
https://github.com/dec0dOS/zero-ui.git
synced 2025-08-19 21:03:56 -07:00
refactor: squash commits
This commit is contained in:
parent
63ebcb5915
commit
1e6e237aa3
107 changed files with 20077 additions and 0 deletions
22
backend/routes/auth.js
Normal file
22
backend/routes/auth.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
const auth = require("../services/auth");
|
||||
|
||||
router.post("/login", async function (req, res) {
|
||||
if (req.body.username && req.body.password) {
|
||||
auth.authorize(req.body.username, req.body.password, function (err, user) {
|
||||
if (user) {
|
||||
res.send({ token: user["token"] });
|
||||
} else {
|
||||
res.status(401).send({
|
||||
error: err.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
res.status(400).send({ error: "Specify username and password" });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
13
backend/routes/controller.js
Normal file
13
backend/routes/controller.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
const auth = require("../services/auth");
|
||||
const api = require("../utils/controller-api");
|
||||
|
||||
router.get("/status", auth.isAuthorized, async function (req, res) {
|
||||
api.get("status").then(function (controllerRes) {
|
||||
res.send(controllerRes.data);
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
85
backend/routes/member.js
Normal file
85
backend/routes/member.js
Normal file
|
@ -0,0 +1,85 @@
|
|||
const express = require("express");
|
||||
const router = express.Router({ mergeParams: true });
|
||||
|
||||
const auth = require("../services/auth");
|
||||
const member = require("../services/member");
|
||||
|
||||
const api = require("../utils/controller-api");
|
||||
|
||||
// get all members
|
||||
router.get("/", auth.isAuthorized, async function (req, res) {
|
||||
const nwid = req.params.nwid;
|
||||
api
|
||||
.get("controller/network/" + nwid + "/member")
|
||||
.then(async function (controllerRes) {
|
||||
const mids = Object.keys(controllerRes.data);
|
||||
const data = await member.getMembersData(nwid, mids);
|
||||
res.send(data);
|
||||
})
|
||||
.catch(function () {
|
||||
res.status(404).send({ error: "Network not found" });
|
||||
});
|
||||
});
|
||||
|
||||
// get member
|
||||
router.get("/:mid", auth.isAuthorized, async function (req, res) {
|
||||
const nwid = req.params.nwid;
|
||||
const mid = req.params.mid;
|
||||
const data = await member.getMembersData(nwid, [mid]);
|
||||
if (data[0]) {
|
||||
res.send(data[0]);
|
||||
} else {
|
||||
res.status(404).send({ error: "Member not found" });
|
||||
}
|
||||
});
|
||||
|
||||
// update member
|
||||
router.post("/:mid", auth.isAuthorized, async function (req, res) {
|
||||
const nwid = req.params.nwid;
|
||||
const mid = req.params.mid;
|
||||
member.updateMemberAdditionalData(nwid, mid, req.body);
|
||||
if (req.body.config) {
|
||||
api
|
||||
.post("controller/network/" + nwid + "/member/" + mid, req.body.config)
|
||||
.then(async function () {
|
||||
const data = await member.getMembersData(nwid, [mid]);
|
||||
res.send(data[0]);
|
||||
})
|
||||
.catch(function (err) {
|
||||
res.status(500).send({ error: err.message });
|
||||
});
|
||||
} else {
|
||||
const data = await member.getMembersData(nwid, [mid]);
|
||||
res.send(data[0]);
|
||||
}
|
||||
});
|
||||
|
||||
// delete member
|
||||
router.delete("/:mid", auth.isAuthorized, async function (req, res) {
|
||||
const nwid = req.params.nwid;
|
||||
const mid = req.params.mid;
|
||||
member.deleteMemberAdditionalData(nwid, mid);
|
||||
api
|
||||
.delete("controller/network/" + nwid + "/member/" + mid)
|
||||
.then(function () {})
|
||||
.catch(function (err) {
|
||||
res.status(500).send({ error: err.message });
|
||||
});
|
||||
// Need this to fix ZT controller bug https://github.com/zerotier/ZeroTierOne/issues/859
|
||||
const defaultConfig = {
|
||||
authorized: false,
|
||||
ipAssignments: [],
|
||||
capabilities: [],
|
||||
tags: [],
|
||||
};
|
||||
api
|
||||
.post("controller/network/" + nwid + "/member/" + mid, defaultConfig)
|
||||
.then(function (controllerRes) {
|
||||
res.status(controllerRes.status).send("");
|
||||
})
|
||||
.catch(function (err) {
|
||||
res.status(500).send({ error: err.message });
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
90
backend/routes/network.js
Normal file
90
backend/routes/network.js
Normal file
|
@ -0,0 +1,90 @@
|
|||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
const auth = require("../services/auth");
|
||||
const network = require("../services/network");
|
||||
|
||||
const api = require("../utils/controller-api");
|
||||
const constants = require("../utils/constants");
|
||||
const getZTAddress = require("../utils/zt-address");
|
||||
|
||||
let ZT_ADDRESS = null;
|
||||
getZTAddress().then(function (address) {
|
||||
ZT_ADDRESS = address;
|
||||
});
|
||||
|
||||
// get all networks
|
||||
router.get("/", auth.isAuthorized, async function (req, res) {
|
||||
api.get("controller/network").then(async function (controllerRes) {
|
||||
const nwids = controllerRes.data;
|
||||
const data = await network.getNetworksData(nwids);
|
||||
res.send(data);
|
||||
});
|
||||
});
|
||||
|
||||
// create new network
|
||||
router.post("/", auth.isAuthorized, async function (req, res) {
|
||||
let reqData = req.body;
|
||||
if (reqData.config) {
|
||||
const config = reqData.config;
|
||||
delete reqData.config;
|
||||
reqData = config;
|
||||
reqData.rules = JSON.parse(constants.defaultRules);
|
||||
} else {
|
||||
res.status(400).send({ error: "Bad request" });
|
||||
}
|
||||
api
|
||||
.post("controller/network/" + ZT_ADDRESS + "______", reqData)
|
||||
.then(async function (controllerRes) {
|
||||
await network.createNetworkAdditionalData(controllerRes.data);
|
||||
const data = await network.getNetworksData([controllerRes.data.id]);
|
||||
res.send(data[0]);
|
||||
});
|
||||
});
|
||||
|
||||
// get network
|
||||
router.get("/:nwid", auth.isAuthorized, async function (req, res) {
|
||||
const nwid = req.params.nwid;
|
||||
const data = await network.getNetworksData([nwid]);
|
||||
if (data[0]) {
|
||||
res.send(data[0]);
|
||||
} else {
|
||||
res.status(404).send({ error: "Network not found" });
|
||||
}
|
||||
});
|
||||
|
||||
// update network
|
||||
router.post("/:nwid", auth.isAuthorized, async function (req, res) {
|
||||
const nwid = req.params.nwid;
|
||||
network.updateNetworkAdditionalData(nwid, req.body);
|
||||
if (req.body.config) {
|
||||
api
|
||||
.post("controller/network/" + nwid, req.body.config)
|
||||
.then(async function () {
|
||||
const data = await network.getNetworksData([nwid]);
|
||||
res.send(data[0]);
|
||||
})
|
||||
.catch(function (err) {
|
||||
res.status(500).send({ error: err.message });
|
||||
});
|
||||
} else {
|
||||
const data = await network.getNetworksData([nwid]);
|
||||
res.send(data[0]);
|
||||
}
|
||||
});
|
||||
|
||||
// delete network
|
||||
router.delete("/:nwid", auth.isAuthorized, async function (req, res) {
|
||||
const nwid = req.params.nwid;
|
||||
network.deleteNetworkAdditionalData(nwid);
|
||||
api
|
||||
.delete("controller/network/" + nwid)
|
||||
.then(function (controllerRes) {
|
||||
res.status(controllerRes.status).send("");
|
||||
})
|
||||
.catch(function (err) {
|
||||
res.status(500).send({ error: err.message });
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
Loading…
Add table
Add a link
Reference in a new issue