mirror of
https://github.com/dec0dOS/zero-ui.git
synced 2025-07-05 20:41:43 -07:00
Merge pull request #170 from aruznieto/es6-visual-mods
dev: es6 + visual mods + migrate from node-cron to croner
This commit is contained in:
commit
0521c9dea0
19 changed files with 283 additions and 540 deletions
|
@ -1,19 +1,19 @@
|
||||||
const express = require("express");
|
import express from "express";
|
||||||
const path = require("path");
|
import path from "path";
|
||||||
const logger = require("morgan");
|
import logger from "morgan";
|
||||||
const compression = require("compression");
|
import compression from "compression";
|
||||||
const bearerToken = require("express-bearer-token");
|
import bearerToken from "express-bearer-token";
|
||||||
const helmet = require("helmet");
|
import helmet from "helmet";
|
||||||
const cron = require("node-cron");
|
import { Cron } from "croner";
|
||||||
|
|
||||||
const db = require("./utils/db");
|
import { db } from "./utils/db.js";
|
||||||
const initAdmin = require("./utils/init-admin");
|
import { initAdmin } from "./utils/init-admin.js";
|
||||||
const pingAll = require("./utils/ping");
|
import { pingAll } from "./utils/ping.js";
|
||||||
|
|
||||||
const authRoutes = require("./routes/auth");
|
import authRoutes from "./routes/auth.js";
|
||||||
const networkRoutes = require("./routes/network");
|
import networkRoutes from "./routes/network.js";
|
||||||
const memberRoutes = require("./routes/member");
|
import memberRoutes from "./routes/member.js";
|
||||||
const controllerRoutes = require("./routes/controller");
|
import controllerRoutes from "./routes/controller.js";
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ initAdmin().then(function (admin) {
|
||||||
|
|
||||||
if (process.env.ZU_LAST_SEEN_FETCH !== "false") {
|
if (process.env.ZU_LAST_SEEN_FETCH !== "false") {
|
||||||
let schedule = process.env.ZU_LAST_SEEN_SCHEDULE || "*/5 * * * *";
|
let schedule = process.env.ZU_LAST_SEEN_SCHEDULE || "*/5 * * * *";
|
||||||
cron.schedule(schedule, () => {
|
Cron(schedule, () => {
|
||||||
console.debug("Running scheduled job");
|
console.debug("Running scheduled job");
|
||||||
const networks = db.get("networks").value();
|
const networks = db.get("networks").value();
|
||||||
networks.forEach(async (network) => {
|
networks.forEach(async (network) => {
|
||||||
|
@ -93,4 +93,4 @@ app.use(function (err, req, res, next) {
|
||||||
res.status(500).json({ error: "500 Internal server error" });
|
res.status(500).json({ error: "500 Internal server error" });
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = app;
|
export default app;
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
require("dotenv").config();
|
import dotenv from "dotenv";
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var app = require("../app");
|
import app from "../app.js";
|
||||||
var debug = require("debug")("zero-ui:server");
|
|
||||||
var http = require("http");
|
import debug from "debug";
|
||||||
|
debug("zero-ui:server");
|
||||||
|
import http from "node:http";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get port from environment and store in Express.
|
* Get port from environment and store in Express.
|
||||||
|
@ -82,6 +85,6 @@ function onError(error) {
|
||||||
|
|
||||||
function onListening() {
|
function onListening() {
|
||||||
var addr = server.address();
|
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);
|
debug("Listening on " + bind);
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "backend",
|
"name": "backend",
|
||||||
"private": true,
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node ./bin/www",
|
"start": "node ./bin/www",
|
||||||
"lint": "eslint . --report-unused-disable-directives --max-warnings 0",
|
"lint": "eslint . --report-unused-disable-directives --max-warnings 0",
|
||||||
|
@ -9,6 +10,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^0.27.2",
|
"axios": "^0.27.2",
|
||||||
"compression": "^1.7.4",
|
"compression": "^1.7.4",
|
||||||
|
"croner": "^7.0.2",
|
||||||
"debug": "^4.3.4",
|
"debug": "^4.3.4",
|
||||||
"dotenv": "^16.3.1",
|
"dotenv": "^16.3.1",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
|
@ -17,7 +19,6 @@
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
"lowdb": "^1.0.0",
|
"lowdb": "^1.0.0",
|
||||||
"morgan": "^1.10.0",
|
"morgan": "^1.10.0",
|
||||||
"node-cron": "^3.0.2",
|
|
||||||
"pbkdf2-wrapper": "^1.3.4"
|
"pbkdf2-wrapper": "^1.3.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const express = require("express");
|
import express from "express";
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
const auth = require("../services/auth");
|
import * as auth from "../services/auth.js";
|
||||||
|
|
||||||
router.get("/login", async function (req, res) {
|
router.get("/login", async function (req, res) {
|
||||||
if (process.env.ZU_DISABLE_AUTH === "true") {
|
if (process.env.ZU_DISABLE_AUTH === "true") {
|
||||||
|
@ -27,4 +27,4 @@ router.post("/login", async function (req, res) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
export default router;
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
const express = require("express");
|
import express from "express";
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
const auth = require("../services/auth");
|
import * as auth from "../services/auth.js";
|
||||||
const api = require("../utils/controller-api");
|
import { api } from "../utils/controller-api.js";
|
||||||
|
|
||||||
router.get("/status", auth.isAuthorized, async function (req, res) {
|
router.get("/status", auth.isAuthorized, async function (req, res) {
|
||||||
api.get("status").then(function (controllerRes) {
|
api.get("status").then(function (controllerRes) {
|
||||||
|
@ -10,4 +10,4 @@ router.get("/status", auth.isAuthorized, async function (req, res) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
export default router;
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
const express = require("express");
|
import express from "express";
|
||||||
const router = express.Router({ mergeParams: true });
|
const router = express.Router({ mergeParams: true });
|
||||||
|
|
||||||
const auth = require("../services/auth");
|
import * as auth from "../services/auth.js";
|
||||||
const member = require("../services/member");
|
import * as member from "../services/member.js";
|
||||||
|
|
||||||
const api = require("../utils/controller-api");
|
import { api } from "../utils/controller-api.js";
|
||||||
|
|
||||||
// get all members
|
// get all members
|
||||||
router.get("/", auth.isAuthorized, async function (req, res) {
|
router.get("/", auth.isAuthorized, async function (req, res) {
|
||||||
|
@ -86,4 +86,4 @@ router.delete("/:mid", auth.isAuthorized, async function (req, res) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
export default router;
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
const express = require("express");
|
import express from "express";
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
const auth = require("../services/auth");
|
import * as auth from "../services/auth.js";
|
||||||
const network = require("../services/network");
|
import * as network from "../services/network.js";
|
||||||
|
|
||||||
const api = require("../utils/controller-api");
|
import { api } from "../utils/controller-api.js";
|
||||||
const constants = require("../utils/constants");
|
import { defaultRules } from "../utils/constants.js";
|
||||||
const getZTAddress = require("../utils/zt-address");
|
import { getZTAddress } from "../utils/zt-address.js";
|
||||||
|
|
||||||
let ZT_ADDRESS = null;
|
let ZT_ADDRESS = null;
|
||||||
getZTAddress().then(function (address) {
|
getZTAddress().then(function (address) {
|
||||||
|
@ -40,7 +40,7 @@ router.post("/", auth.isAuthorized, async function (req, res) {
|
||||||
const config = reqData.config;
|
const config = reqData.config;
|
||||||
delete reqData.config;
|
delete reqData.config;
|
||||||
reqData = config;
|
reqData = config;
|
||||||
reqData.rules = JSON.parse(constants.defaultRules);
|
reqData.rules = JSON.parse(defaultRules);
|
||||||
} else {
|
} else {
|
||||||
res.status(400).send({ error: "Bad request" });
|
res.status(400).send({ error: "Bad request" });
|
||||||
}
|
}
|
||||||
|
@ -87,4 +87,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");
|
import { db } from "../utils/db.js";
|
||||||
const verifyHash = require("pbkdf2-wrapper/verifyHash");
|
import verifyHash from "pbkdf2-wrapper/verifyHash.js";
|
||||||
|
|
||||||
exports.authorize = authorize;
|
export async function authorize(username, password, callback) {
|
||||||
async function authorize(username, password, callback) {
|
|
||||||
try {
|
try {
|
||||||
var users = await db.get("users");
|
var users = await db.get("users");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -18,8 +17,7 @@ async function authorize(username, password, callback) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.isAuthorized = isAuthorized;
|
export async function isAuthorized(req, res, next) {
|
||||||
async function isAuthorized(req, res, next) {
|
|
||||||
if (process.env.ZU_DISABLE_AUTH === "true") {
|
if (process.env.ZU_DISABLE_AUTH === "true") {
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
const _ = require("lodash");
|
import _ from "lodash";
|
||||||
const axios = require("axios");
|
import axios from "axios";
|
||||||
|
|
||||||
const api = require("../utils/controller-api");
|
import { api } from "../utils/controller-api.js";
|
||||||
const db = require("../utils/db");
|
import { db } from "../utils/db.js";
|
||||||
const getZTAddress = require("../utils/zt-address");
|
import { getZTAddress } from "../utils/zt-address.js";
|
||||||
|
|
||||||
let ZT_ADDRESS = null;
|
let ZT_ADDRESS = null;
|
||||||
getZTAddress().then(function (address) {
|
getZTAddress().then(function (address) {
|
||||||
|
@ -91,8 +91,7 @@ async function filterDeleted(nwid, mid) {
|
||||||
else return;
|
else return;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getMembersData = getMembersData;
|
export async function getMembersData(nwid, mids) {
|
||||||
async function getMembersData(nwid, mids) {
|
|
||||||
const prefix = "/controller/network/" + nwid + "/member/";
|
const prefix = "/controller/network/" + nwid + "/member/";
|
||||||
const filtered = (
|
const filtered = (
|
||||||
await Promise.all(mids.map(async (mid) => await filterDeleted(nwid, mid)))
|
await Promise.all(mids.map(async (mid) => await filterDeleted(nwid, mid)))
|
||||||
|
@ -119,8 +118,7 @@ async function getMembersData(nwid, mids) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.updateMemberAdditionalData = updateMemberAdditionalData;
|
export async function updateMemberAdditionalData(nwid, mid, data) {
|
||||||
async function updateMemberAdditionalData(nwid, mid, data) {
|
|
||||||
if (data.config && data.config.authorized) {
|
if (data.config && data.config.authorized) {
|
||||||
db.get("networks")
|
db.get("networks")
|
||||||
.filter({ id: nwid })
|
.filter({ id: nwid })
|
||||||
|
@ -172,8 +170,7 @@ async function updateMemberAdditionalData(nwid, mid, data) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.deleteMemberAdditionalData = deleteMemberAdditionalData;
|
export async function deleteMemberAdditionalData(nwid, mid) {
|
||||||
async function deleteMemberAdditionalData(nwid, mid) {
|
|
||||||
// ZT controller bug
|
// ZT controller bug
|
||||||
/* db.get("networks")
|
/* db.get("networks")
|
||||||
.find({ id: nwid })
|
.find({ id: nwid })
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
const _ = require("lodash");
|
import _ from "lodash";
|
||||||
const axios = require("axios");
|
import axios from "axios";
|
||||||
|
|
||||||
const api = require("../utils/controller-api");
|
import { api } from "../utils/controller-api.js";
|
||||||
const db = require("../utils/db");
|
import { db } from "../utils/db.js";
|
||||||
const constants = require("../utils/constants");
|
import { defaultRulesSource } from "../utils/constants.js";
|
||||||
|
|
||||||
async function getNetworkAdditionalData(data) {
|
export async function getNetworkAdditionalData(data) {
|
||||||
let additionalData = db
|
let additionalData = db
|
||||||
.get("networks")
|
.get("networks")
|
||||||
.find({ id: data.id })
|
.find({ id: data.id })
|
||||||
|
@ -29,8 +29,7 @@ async function getNetworkAdditionalData(data) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getNetworksData = getNetworksData;
|
export async function getNetworksData(nwids) {
|
||||||
async function getNetworksData(nwids) {
|
|
||||||
const prefix = "/controller/network/";
|
const prefix = "/controller/network/";
|
||||||
const links = nwids.map((nwid) => prefix + nwid);
|
const links = nwids.map((nwid) => prefix + nwid);
|
||||||
|
|
||||||
|
@ -54,13 +53,12 @@ async function getNetworksData(nwids) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createNetworkAdditionalData = createNetworkAdditionalData;
|
export async function createNetworkAdditionalData(nwid) {
|
||||||
async function createNetworkAdditionalData(nwid) {
|
|
||||||
const saveData = {
|
const saveData = {
|
||||||
id: nwid,
|
id: nwid,
|
||||||
additionalConfig: {
|
additionalConfig: {
|
||||||
description: "",
|
description: "",
|
||||||
rulesSource: constants.defaultRulesSource,
|
rulesSource: defaultRulesSource,
|
||||||
tagsByName: {},
|
tagsByName: {},
|
||||||
capabilitiesByName: {},
|
capabilitiesByName: {},
|
||||||
},
|
},
|
||||||
|
@ -70,8 +68,7 @@ async function createNetworkAdditionalData(nwid) {
|
||||||
db.get("networks").push(saveData).write();
|
db.get("networks").push(saveData).write();
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.updateNetworkAdditionalData = updateNetworkAdditionalData;
|
export async function updateNetworkAdditionalData(nwid, data) {
|
||||||
async function updateNetworkAdditionalData(nwid, data) {
|
|
||||||
let additionalData = {};
|
let additionalData = {};
|
||||||
|
|
||||||
if (data.hasOwnProperty("description")) {
|
if (data.hasOwnProperty("description")) {
|
||||||
|
@ -96,7 +93,6 @@ async function updateNetworkAdditionalData(nwid, data) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.deleteNetworkAdditionalData = deleteNetworkAdditionalData;
|
export async function deleteNetworkAdditionalData(nwid) {
|
||||||
async function deleteNetworkAdditionalData(nwid) {
|
|
||||||
db.get("networks").remove({ id: nwid }).write();
|
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
|
# This is a default rule set that allows IPv4 and IPv6 traffic but otherwise
|
||||||
# behaves like a standard Ethernet switch.
|
# behaves like a standard Ethernet switch.
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ drop
|
||||||
accept;
|
accept;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
exports.defaultRules = `
|
export const defaultRules = `
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"type": "MATCH_ETHERTYPE",
|
"type": "MATCH_ETHERTYPE",
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const axios = require("axios");
|
import axios from "axios";
|
||||||
const fs = require("fs");
|
import fs from "node:fs";
|
||||||
|
|
||||||
const baseURL = process.env.ZU_CONTROLLER_ENDPOINT || "http://localhost:9993/";
|
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");
|
token = fs.readFileSync("/var/lib/zerotier-one/authtoken.secret", "utf8");
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = axios.create({
|
export const api = axios.create({
|
||||||
baseURL: baseURL,
|
baseURL: baseURL,
|
||||||
responseType: "json",
|
responseType: "json",
|
||||||
headers: {
|
headers: {
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
const low = require("lowdb");
|
import low from "lowdb";
|
||||||
const FileSync = require("lowdb/adapters/FileSync");
|
import FileSync from "lowdb/adapters/FileSync.js";
|
||||||
|
|
||||||
const adapter = new FileSync(process.env.ZU_DATAPATH || "data/db.json");
|
const adapter = new FileSync(process.env.ZU_DATAPATH || "data/db.json");
|
||||||
|
|
||||||
const db = low(adapter);
|
export const db = low(adapter);
|
||||||
|
|
||||||
module.exports = db;
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const crypto = require("crypto");
|
import crypto from "crypto";
|
||||||
const hashPassword = require("pbkdf2-wrapper/hashText");
|
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) {
|
if (!process.env.ZU_DEFAULT_PASSWORD || !process.env.ZU_DEFAULT_USERNAME) {
|
||||||
console.error("ZU_DEFAULT_PASSWORD or ZU_DEFAULT_USERNAME not found!");
|
console.error("ZU_DEFAULT_PASSWORD or ZU_DEFAULT_USERNAME not found!");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
@ -13,4 +13,4 @@ module.exports = async function () {
|
||||||
password_hash: hash,
|
password_hash: hash,
|
||||||
token: crypto.randomBytes(16).toString("hex"),
|
token: crypto.randomBytes(16).toString("hex"),
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
const _ = require("lodash");
|
import _ from "lodash";
|
||||||
|
|
||||||
const api = require("./controller-api");
|
import { api } from "./controller-api.js";
|
||||||
const db = require("./db");
|
import { db } from "./db.js";
|
||||||
|
|
||||||
async function pingAll(network) {
|
export async function pingAll(network) {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
network.members.map(async (member) => {
|
network.members.map(async (member) => {
|
||||||
console.debug("Processing member " + member.id);
|
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 {
|
try {
|
||||||
const res = await api.get("status");
|
const res = await api.get("status");
|
||||||
return res.data.address;
|
return res.data.address;
|
||||||
|
@ -10,4 +10,4 @@ module.exports = async function () {
|
||||||
"Couldn't connect to the controller on " + err.config.baseURL
|
"Couldn't connect to the controller on " + err.config.baseURL
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
|
@ -39,10 +39,12 @@ function Network() {
|
||||||
if (loggedIn) {
|
if (loggedIn) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Link color="inherit" component={RouterLink} to="/" underline="none">
|
<div className={classes.breadcrumbs}>
|
||||||
<ArrowBackIcon className={classes.backIcon}></ArrowBackIcon>
|
<Link color="inherit" component={RouterLink} to="/" underline="none">
|
||||||
Networks
|
<ArrowBackIcon className={classes.backIcon}></ArrowBackIcon>
|
||||||
</Link>
|
Networks
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
<div className={classes.container}>
|
<div className={classes.container}>
|
||||||
{network["config"] && (
|
{network["config"] && (
|
||||||
<>
|
<>
|
||||||
|
|
|
@ -5,7 +5,11 @@ const useStyles = makeStyles((theme) => ({
|
||||||
fontSize: 12,
|
fontSize: 12,
|
||||||
},
|
},
|
||||||
container: {
|
container: {
|
||||||
margin: "1%",
|
margin: "3%",
|
||||||
|
},
|
||||||
|
breadcrumbs: {
|
||||||
|
paddingTop: "2%",
|
||||||
|
paddingLeft: "2%",
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue