mirror of
https://github.com/dec0dOS/zero-ui.git
synced 2025-07-06 04:51:44 -07:00
feat: es6+mods
This commit is contained in:
parent
773b64ea30
commit
19c92ed244
19 changed files with 179 additions and 245 deletions
|
@ -1,19 +1,19 @@
|
|||
const express = require("express");
|
||||
const path = require("path");
|
||||
const logger = require("morgan");
|
||||
const compression = require("compression");
|
||||
const bearerToken = require("express-bearer-token");
|
||||
const helmet = require("helmet");
|
||||
const cron = require("node-cron");
|
||||
import express from "express";
|
||||
import path from "path";
|
||||
import logger from "morgan";
|
||||
import compression from "compression";
|
||||
import bearerToken from "express-bearer-token";
|
||||
import helmet from "helmet";
|
||||
import cron from "node-cron";
|
||||
|
||||
const db = require("./utils/db");
|
||||
const initAdmin = require("./utils/init-admin");
|
||||
const pingAll = require("./utils/ping");
|
||||
import { db } from "./utils/db.js";
|
||||
import { initAdmin } from "./utils/init-admin.js";
|
||||
import { pingAll } from "./utils/ping.js";
|
||||
|
||||
const authRoutes = require("./routes/auth");
|
||||
const networkRoutes = require("./routes/network");
|
||||
const memberRoutes = require("./routes/member");
|
||||
const controllerRoutes = require("./routes/controller");
|
||||
import authRoutes from "./routes/auth.js";
|
||||
import networkRoutes from "./routes/network.js";
|
||||
import memberRoutes from "./routes/member.js";
|
||||
import controllerRoutes from "./routes/controller.js";
|
||||
|
||||
const app = express();
|
||||
|
||||
|
@ -93,4 +93,4 @@ app.use(function (err, req, res, next) {
|
|||
res.status(500).json({ error: "500 Internal server error" });
|
||||
});
|
||||
|
||||
module.exports = app;
|
||||
export default app;
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
#!/usr/bin/env node
|
||||
require("dotenv").config();
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var app = require("../app");
|
||||
var debug = require("debug")("zero-ui:server");
|
||||
var http = require("http");
|
||||
import app from "../app.js";
|
||||
|
||||
import debug from "debug";
|
||||
debug("zero-ui:server");
|
||||
import http from "node:http";
|
||||
|
||||
/**
|
||||
* Get port from environment and store in Express.
|
||||
|
@ -82,6 +85,6 @@ function onError(error) {
|
|||
|
||||
function onListening() {
|
||||
var addr = server.address();
|
||||
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
|
||||
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr?.port;
|
||||
debug("Listening on " + bind);
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
{
|
||||
"name": "backend",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "node ./bin/www",
|
||||
"lint": "eslint . --report-unused-disable-directives --max-warnings 0",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const express = require("express");
|
||||
import express from "express";
|
||||
const router = express.Router();
|
||||
|
||||
const auth = require("../services/auth");
|
||||
import { authorize } from "../services/auth.js";
|
||||
|
||||
router.get("/login", async function (req, res) {
|
||||
if (process.env.ZU_DISABLE_AUTH === "true") {
|
||||
|
@ -13,7 +13,7 @@ router.get("/login", async function (req, res) {
|
|||
|
||||
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) {
|
||||
authorize(req.body.username, req.body.password, function (err, user) {
|
||||
if (user) {
|
||||
res.send({ token: user["token"] });
|
||||
} else {
|
||||
|
@ -27,4 +27,4 @@ router.post("/login", async function (req, res) {
|
|||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
export default router;
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
const express = require("express");
|
||||
import express from "express";
|
||||
const router = express.Router();
|
||||
|
||||
const auth = require("../services/auth");
|
||||
const api = require("../utils/controller-api");
|
||||
import { isAuthorized } from "../services/auth.js";
|
||||
import { api } from "../utils/controller-api.js";
|
||||
|
||||
router.get("/status", auth.isAuthorized, async function (req, res) {
|
||||
router.get("/status", isAuthorized, async function (req, res) {
|
||||
api.get("status").then(function (controllerRes) {
|
||||
res.send(controllerRes.data);
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
export default router;
|
||||
|
|
|
@ -1,20 +1,24 @@
|
|||
const express = require("express");
|
||||
import express from "express";
|
||||
const router = express.Router({ mergeParams: true });
|
||||
|
||||
const auth = require("../services/auth");
|
||||
const member = require("../services/member");
|
||||
import { isAuthorized } from "../services/auth.js";
|
||||
import {
|
||||
deleteMemberAdditionalData,
|
||||
getMembersData,
|
||||
updateMemberAdditionalData,
|
||||
} from "../services/member.js";
|
||||
|
||||
const api = require("../utils/controller-api");
|
||||
import { api } from "../utils/controller-api.js";
|
||||
|
||||
// get all members
|
||||
router.get("/", auth.isAuthorized, async function (req, res) {
|
||||
router.get("/", isAuthorized, async function (req, res) {
|
||||
// @ts-ignore
|
||||
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);
|
||||
const data = await getMembersData(nwid, mids);
|
||||
res.send(data);
|
||||
})
|
||||
.catch(function (err) {
|
||||
|
@ -23,11 +27,11 @@ router.get("/", auth.isAuthorized, async function (req, res) {
|
|||
});
|
||||
|
||||
// get member
|
||||
router.get("/:mid", auth.isAuthorized, async function (req, res) {
|
||||
router.get("/:mid", isAuthorized, async function (req, res) {
|
||||
// @ts-ignore
|
||||
const nwid = req.params.nwid;
|
||||
const mid = req.params.mid;
|
||||
const data = await member.getMembersData(nwid, [mid]);
|
||||
const data = await getMembersData(nwid, [mid]);
|
||||
if (data[0]) {
|
||||
res.send(data[0]);
|
||||
} else {
|
||||
|
@ -36,33 +40,33 @@ router.get("/:mid", auth.isAuthorized, async function (req, res) {
|
|||
});
|
||||
|
||||
// update member
|
||||
router.post("/:mid", auth.isAuthorized, async function (req, res) {
|
||||
router.post("/:mid", isAuthorized, async function (req, res) {
|
||||
// @ts-ignore
|
||||
const nwid = req.params.nwid;
|
||||
const mid = req.params.mid;
|
||||
member.updateMemberAdditionalData(nwid, mid, req.body);
|
||||
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]);
|
||||
const data = await 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]);
|
||||
const data = await getMembersData(nwid, [mid]);
|
||||
res.send(data[0]);
|
||||
}
|
||||
});
|
||||
|
||||
// delete member
|
||||
router.delete("/:mid", auth.isAuthorized, async function (req, res) {
|
||||
router.delete("/:mid", isAuthorized, async function (req, res) {
|
||||
// @ts-ignore
|
||||
const nwid = req.params.nwid;
|
||||
const mid = req.params.mid;
|
||||
member.deleteMemberAdditionalData(nwid, mid);
|
||||
deleteMemberAdditionalData(nwid, mid);
|
||||
api
|
||||
.delete("controller/network/" + nwid + "/member/" + mid)
|
||||
.then(function () {})
|
||||
|
@ -86,4 +90,4 @@ router.delete("/:mid", auth.isAuthorized, async function (req, res) {
|
|||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
export default router;
|
||||
|
|
|
@ -1,12 +1,17 @@
|
|||
const express = require("express");
|
||||
import express from "express";
|
||||
const router = express.Router();
|
||||
|
||||
const auth = require("../services/auth");
|
||||
const network = require("../services/network");
|
||||
import { isAuthorized } from "../services/auth.js";
|
||||
import {
|
||||
getNetworksData,
|
||||
createNetworkAdditionalData,
|
||||
updateNetworkAdditionalData,
|
||||
deleteNetworkAdditionalData,
|
||||
} from "../services/network.js";
|
||||
|
||||
const api = require("../utils/controller-api");
|
||||
const constants = require("../utils/constants");
|
||||
const getZTAddress = require("../utils/zt-address");
|
||||
import { api } from "../utils/controller-api.js";
|
||||
import { defaultRules } from "../utils/constants.js";
|
||||
import { getZTAddress } from "../utils/zt-address.js";
|
||||
|
||||
let ZT_ADDRESS = null;
|
||||
getZTAddress().then(function (address) {
|
||||
|
@ -14,18 +19,18 @@ getZTAddress().then(function (address) {
|
|||
});
|
||||
|
||||
// get all networks
|
||||
router.get("/", auth.isAuthorized, async function (req, res) {
|
||||
router.get("/", isAuthorized, async function (req, res) {
|
||||
api.get("controller/network").then(async function (controllerRes) {
|
||||
const nwids = controllerRes.data;
|
||||
const data = await network.getNetworksData(nwids);
|
||||
const data = await getNetworksData(nwids);
|
||||
res.send(data);
|
||||
});
|
||||
});
|
||||
|
||||
// get network
|
||||
router.get("/:nwid", auth.isAuthorized, async function (req, res) {
|
||||
router.get("/:nwid", isAuthorized, async function (req, res) {
|
||||
const nwid = req.params.nwid;
|
||||
const data = await network.getNetworksData([nwid]);
|
||||
const data = await getNetworksData([nwid]);
|
||||
if (data[0]) {
|
||||
res.send(data[0]);
|
||||
} else {
|
||||
|
@ -34,49 +39,49 @@ router.get("/:nwid", auth.isAuthorized, async function (req, res) {
|
|||
});
|
||||
|
||||
// create new network
|
||||
router.post("/", auth.isAuthorized, async function (req, res) {
|
||||
router.post("/", 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);
|
||||
reqData.rules = JSON.parse(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.id);
|
||||
const data = await network.getNetworksData([controllerRes.data.id]);
|
||||
await createNetworkAdditionalData(controllerRes.data.id);
|
||||
const data = await getNetworksData([controllerRes.data.id]);
|
||||
res.send(data[0]);
|
||||
});
|
||||
});
|
||||
|
||||
// update network
|
||||
router.post("/:nwid", auth.isAuthorized, async function (req, res) {
|
||||
router.post("/:nwid", isAuthorized, async function (req, res) {
|
||||
const nwid = req.params.nwid;
|
||||
network.updateNetworkAdditionalData(nwid, req.body);
|
||||
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]);
|
||||
const data = await getNetworksData([nwid]);
|
||||
res.send(data[0]);
|
||||
})
|
||||
.catch(function (err) {
|
||||
res.status(500).send({ error: err.message });
|
||||
});
|
||||
} else {
|
||||
const data = await network.getNetworksData([nwid]);
|
||||
const data = await getNetworksData([nwid]);
|
||||
res.send(data[0]);
|
||||
}
|
||||
});
|
||||
|
||||
// delete network
|
||||
router.delete("/:nwid", auth.isAuthorized, async function (req, res) {
|
||||
router.delete("/:nwid", isAuthorized, async function (req, res) {
|
||||
const nwid = req.params.nwid;
|
||||
network.deleteNetworkAdditionalData(nwid);
|
||||
deleteNetworkAdditionalData(nwid);
|
||||
api
|
||||
.delete("controller/network/" + nwid)
|
||||
.then(function (controllerRes) {
|
||||
|
@ -87,4 +92,4 @@ router.delete("/:nwid", auth.isAuthorized, async function (req, res) {
|
|||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
export default router;
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
const db = require("../utils/db");
|
||||
const verifyHash = require("pbkdf2-wrapper/verifyHash");
|
||||
import { db } from "../utils/db.js";
|
||||
import verifyHash from "pbkdf2-wrapper/verifyHash.js";
|
||||
|
||||
exports.authorize = authorize;
|
||||
async function authorize(username, password, callback) {
|
||||
export async function authorize(username, password, callback) {
|
||||
try {
|
||||
var users = await db.get("users");
|
||||
} catch (err) {
|
||||
|
@ -18,8 +17,7 @@ async function authorize(username, password, callback) {
|
|||
}
|
||||
}
|
||||
|
||||
exports.isAuthorized = isAuthorized;
|
||||
async function isAuthorized(req, res, next) {
|
||||
export async function isAuthorized(req, res, next) {
|
||||
if (process.env.ZU_DISABLE_AUTH === "true") {
|
||||
next();
|
||||
} else {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
const _ = require("lodash");
|
||||
const axios = require("axios");
|
||||
import _ from "lodash";
|
||||
import axios from "axios";
|
||||
|
||||
const api = require("../utils/controller-api");
|
||||
const db = require("../utils/db");
|
||||
const getZTAddress = require("../utils/zt-address");
|
||||
import { api } from "../utils/controller-api.js";
|
||||
import { db } from "../utils/db.js";
|
||||
import { getZTAddress } from "../utils/zt-address.js";
|
||||
|
||||
let ZT_ADDRESS = null;
|
||||
getZTAddress().then(function (address) {
|
||||
|
@ -91,8 +91,7 @@ async function filterDeleted(nwid, mid) {
|
|||
else return;
|
||||
}
|
||||
|
||||
exports.getMembersData = getMembersData;
|
||||
async function getMembersData(nwid, mids) {
|
||||
export async function getMembersData(nwid, mids) {
|
||||
const prefix = "/controller/network/" + nwid + "/member/";
|
||||
const filtered = (
|
||||
await Promise.all(mids.map(async (mid) => await filterDeleted(nwid, mid)))
|
||||
|
@ -119,8 +118,7 @@ async function getMembersData(nwid, mids) {
|
|||
return data;
|
||||
}
|
||||
|
||||
exports.updateMemberAdditionalData = updateMemberAdditionalData;
|
||||
async function updateMemberAdditionalData(nwid, mid, data) {
|
||||
export async function updateMemberAdditionalData(nwid, mid, data) {
|
||||
if (data.config && data.config.authorized) {
|
||||
db.get("networks")
|
||||
.filter({ id: nwid })
|
||||
|
@ -172,8 +170,7 @@ async function updateMemberAdditionalData(nwid, mid, data) {
|
|||
}
|
||||
}
|
||||
|
||||
exports.deleteMemberAdditionalData = deleteMemberAdditionalData;
|
||||
async function deleteMemberAdditionalData(nwid, mid) {
|
||||
export async function deleteMemberAdditionalData(nwid, mid) {
|
||||
// ZT controller bug
|
||||
/* db.get("networks")
|
||||
.find({ id: nwid })
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
const _ = require("lodash");
|
||||
const axios = require("axios");
|
||||
import _ from "lodash";
|
||||
import axios from "axios";
|
||||
|
||||
const api = require("../utils/controller-api");
|
||||
const db = require("../utils/db");
|
||||
const constants = require("../utils/constants");
|
||||
import { api } from "../utils/controller-api.js";
|
||||
import { db } from "../utils/db.js";
|
||||
import { defaultRulesSource } from "../utils/constants.js";
|
||||
|
||||
async function getNetworkAdditionalData(data) {
|
||||
export async function getNetworkAdditionalData(data) {
|
||||
let additionalData = db
|
||||
.get("networks")
|
||||
.find({ id: data.id })
|
||||
|
@ -29,8 +29,7 @@ async function getNetworkAdditionalData(data) {
|
|||
};
|
||||
}
|
||||
|
||||
exports.getNetworksData = getNetworksData;
|
||||
async function getNetworksData(nwids) {
|
||||
export async function getNetworksData(nwids) {
|
||||
const prefix = "/controller/network/";
|
||||
const links = nwids.map((nwid) => prefix + nwid);
|
||||
|
||||
|
@ -54,13 +53,12 @@ async function getNetworksData(nwids) {
|
|||
return data;
|
||||
}
|
||||
|
||||
exports.createNetworkAdditionalData = createNetworkAdditionalData;
|
||||
async function createNetworkAdditionalData(nwid) {
|
||||
export async function createNetworkAdditionalData(nwid) {
|
||||
const saveData = {
|
||||
id: nwid,
|
||||
additionalConfig: {
|
||||
description: "",
|
||||
rulesSource: constants.defaultRulesSource,
|
||||
rulesSource: defaultRulesSource,
|
||||
tagsByName: {},
|
||||
capabilitiesByName: {},
|
||||
},
|
||||
|
@ -70,8 +68,7 @@ async function createNetworkAdditionalData(nwid) {
|
|||
db.get("networks").push(saveData).write();
|
||||
}
|
||||
|
||||
exports.updateNetworkAdditionalData = updateNetworkAdditionalData;
|
||||
async function updateNetworkAdditionalData(nwid, data) {
|
||||
export async function updateNetworkAdditionalData(nwid, data) {
|
||||
let additionalData = {};
|
||||
|
||||
if (data.hasOwnProperty("description")) {
|
||||
|
@ -96,7 +93,6 @@ async function updateNetworkAdditionalData(nwid, data) {
|
|||
}
|
||||
}
|
||||
|
||||
exports.deleteNetworkAdditionalData = deleteNetworkAdditionalData;
|
||||
async function deleteNetworkAdditionalData(nwid) {
|
||||
export async function deleteNetworkAdditionalData(nwid) {
|
||||
db.get("networks").remove({ id: nwid }).write();
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
exports.defaultRulesSource = `
|
||||
export const defaultRulesSource = `
|
||||
# This is a default rule set that allows IPv4 and IPv6 traffic but otherwise
|
||||
# behaves like a standard Ethernet switch.
|
||||
|
||||
|
@ -26,7 +26,7 @@ drop
|
|||
accept;
|
||||
`;
|
||||
|
||||
exports.defaultRules = `
|
||||
export const defaultRules = `
|
||||
[
|
||||
{
|
||||
"type": "MATCH_ETHERTYPE",
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const axios = require("axios");
|
||||
const fs = require("fs");
|
||||
import axios from "axios";
|
||||
import fs from "node:fs";
|
||||
|
||||
const baseURL = process.env.ZU_CONTROLLER_ENDPOINT || "http://localhost:9993/";
|
||||
|
||||
|
@ -10,7 +10,7 @@ if (process.env.ZU_CONTROLLER_TOKEN) {
|
|||
token = fs.readFileSync("/var/lib/zerotier-one/authtoken.secret", "utf8");
|
||||
}
|
||||
|
||||
module.exports = axios.create({
|
||||
export const api = axios.create({
|
||||
baseURL: baseURL,
|
||||
responseType: "json",
|
||||
headers: {
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
const low = require("lowdb");
|
||||
const FileSync = require("lowdb/adapters/FileSync");
|
||||
import low from "lowdb";
|
||||
import FileSync from "lowdb/adapters/FileSync.js";
|
||||
|
||||
const adapter = new FileSync(process.env.ZU_DATAPATH || "data/db.json");
|
||||
|
||||
const db = low(adapter);
|
||||
|
||||
module.exports = db;
|
||||
export const db = low(adapter);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const crypto = require("crypto");
|
||||
const hashPassword = require("pbkdf2-wrapper/hashText");
|
||||
import crypto from "crypto";
|
||||
import hashPassword from "pbkdf2-wrapper/hashText.js";
|
||||
|
||||
module.exports = async function () {
|
||||
export async function initAdmin() {
|
||||
if (!process.env.ZU_DEFAULT_PASSWORD || !process.env.ZU_DEFAULT_USERNAME) {
|
||||
console.error("ZU_DEFAULT_PASSWORD or ZU_DEFAULT_USERNAME not found!");
|
||||
process.exit(1);
|
||||
|
@ -13,4 +13,4 @@ module.exports = async function () {
|
|||
password_hash: hash,
|
||||
token: crypto.randomBytes(16).toString("hex"),
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
const _ = require("lodash");
|
||||
import _ from "lodash";
|
||||
|
||||
const api = require("./controller-api");
|
||||
const db = require("./db");
|
||||
import { api } from "./controller-api.js";
|
||||
import { db } from "./db.js";
|
||||
|
||||
async function pingAll(network) {
|
||||
export async function pingAll(network) {
|
||||
await Promise.all(
|
||||
network.members.map(async (member) => {
|
||||
console.debug("Processing member " + member.id);
|
||||
|
@ -29,5 +29,3 @@ async function pingAll(network) {
|
|||
})
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = pingAll;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const api = require("../utils/controller-api");
|
||||
import { api } from "../utils/controller-api.js";
|
||||
|
||||
module.exports = async function () {
|
||||
export async function getZTAddress() {
|
||||
try {
|
||||
const res = await api.get("status");
|
||||
return res.data.address;
|
||||
|
@ -10,4 +10,4 @@ module.exports = async function () {
|
|||
"Couldn't connect to the controller on " + err.config.baseURL
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue