mirror of https://github.com/Aelita4/sshmon.git
Adapt NextJS/modules
This commit is contained in:
parent
f795c986a8
commit
d55e9c3d45
|
@ -9,6 +9,7 @@
|
|||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"bcrypt": "^5.0.1",
|
||||
"cookie-parser": "^1.4.6",
|
||||
|
|
41
src/index.ts
41
src/index.ts
|
@ -1,10 +1,14 @@
|
|||
import express, { Request, Response } from 'express';
|
||||
import * as Ping from 'ping';
|
||||
import Ping from 'ping';
|
||||
import { existsSync, readdirSync, writeFileSync, readFileSync } from 'fs';
|
||||
import sessions from "express-session"
|
||||
import cookieParser from "cookie-parser"
|
||||
import mysql from 'mysql'
|
||||
import bcrypt from 'bcrypt'
|
||||
import url from 'url';
|
||||
|
||||
//const __filename = url.fileURLToPath(import.meta.url);
|
||||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
|
||||
|
||||
const ping = async (host: string) => {
|
||||
const result = await Ping.promise.probe(host, {
|
||||
|
@ -14,6 +18,17 @@ const ping = async (host: string) => {
|
|||
return result;
|
||||
}
|
||||
|
||||
const getIP = () : Promise<string[]> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query("SELECT * FROM ips", async (err, results, fields) => {
|
||||
if(err) reject(err);
|
||||
const addresses = [...results.slice()];
|
||||
|
||||
resolve(addresses);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const connection = mysql.createConnection({
|
||||
host: "mysql",
|
||||
user: "root",
|
||||
|
@ -38,32 +53,34 @@ app.use(express.urlencoded());
|
|||
|
||||
const pings = new Map();
|
||||
|
||||
if(!existsSync("/usr/src/app/data.json")) writeFileSync("/usr/src/app/data.json", "[]");
|
||||
let addresses = JSON.parse(readFileSync("/usr/src/app/data.json", {encoding:'utf8', flag:'r'}))
|
||||
let addresses: Array<string> = await getIP();
|
||||
|
||||
//if(!existsSync("/usr/src/app/data.json")) writeFileSync("/usr/src/app/data.json", "[]");
|
||||
//let addresses = JSON.parse(readFileSync("/usr/src/app/data.json", {encoding:'utf8', flag:'r'}))
|
||||
addresses.forEach((a: string) => pings.set(a, -1));
|
||||
const timeoutDelay = 60000;
|
||||
|
||||
const routes = readdirSync(__dirname + "/routes");
|
||||
routes.forEach(route => {
|
||||
const file = require(`${__dirname}/routes/${route}`);
|
||||
console.log(`[${file.method}] ${file.url}`);
|
||||
switch(file.method) {
|
||||
case "GET": app.get(file.url, file.callback.bind(null, { addresses, ping, pings, timeoutDelay })); break;
|
||||
case "POST": app.post(file.url, file.callback.bind(null, { addresses, ping, pings, timeoutDelay })); break;
|
||||
routes.forEach(async route => {
|
||||
const file = await import(`${__dirname}/routes/${route}`);
|
||||
console.log(`[${file.default.method}] ${file.default.url}`);
|
||||
switch(file.default.method) {
|
||||
case "GET": app.get(file.default.url, file.default.callback.bind(null, { addresses, ping, pings, timeoutDelay })); break;
|
||||
case "POST": app.post(file.default.url, file.default.callback.bind(null, { addresses, ping, pings, timeoutDelay })); break;
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(8080, () => console.log('rdy'));
|
||||
|
||||
interface Data {
|
||||
export interface Data {
|
||||
ping: Function,
|
||||
pings: Map<string, number>,
|
||||
addresses: Array<string>,
|
||||
timeoutDelay: number
|
||||
};
|
||||
|
||||
interface User {
|
||||
export interface User {
|
||||
username: string
|
||||
}
|
||||
|
||||
export { connection, bcrypt, User, Data };
|
||||
export { connection, bcrypt, getIP };
|
|
@ -1,7 +1,7 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { existsSync, writeFileSync, readFileSync } from 'fs'
|
||||
|
||||
module.exports = {
|
||||
export default {
|
||||
method: "GET",
|
||||
url: "/addIP/:ip",
|
||||
callback: async (data: any, req: Request, res: Response) => {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Request, Response } from 'express';
|
||||
|
||||
module.exports = {
|
||||
export default {
|
||||
method: "GET",
|
||||
url: "/downtime/:ip",
|
||||
callback: async (data: any, req: Request, res: Response) => {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Request, Response } from 'express';
|
||||
|
||||
module.exports = {
|
||||
export default {
|
||||
method: "GET",
|
||||
url: "/login",
|
||||
callback: async (data: any, req: Request, res: Response) => {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { connection, bcrypt } from '../index'
|
||||
import { connection, bcrypt } from '../index.js'
|
||||
|
||||
module.exports = {
|
||||
export default {
|
||||
method: "POST",
|
||||
url: "/loginUser",
|
||||
callback: async (data: any, req: Request, res: Response) => {
|
||||
|
@ -17,6 +17,8 @@ module.exports = {
|
|||
session.user = {}
|
||||
//@ts-ignore
|
||||
session.user.username = req.body.username;
|
||||
|
||||
session.save();
|
||||
|
||||
res.redirect("/");
|
||||
} else res.render("pages/login.ejs", { invalid: "baduserorpass" });
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Request, Response } from 'express';
|
||||
|
||||
module.exports = {
|
||||
export default {
|
||||
method: "GET",
|
||||
url: "/logout",
|
||||
callback: async (data: any, req: Request, res: Response) => {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Request, Response } from 'express';
|
||||
|
||||
module.exports = {
|
||||
export default {
|
||||
method: "GET",
|
||||
url: "/ping/:ip",
|
||||
callback: async (data: any, req: Request, res: Response) => {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Request, Response } from 'express';
|
||||
|
||||
module.exports = {
|
||||
export default {
|
||||
method: "GET",
|
||||
url: "/register",
|
||||
callback: async (data: any, req: Request, res: Response) => {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import e, { Request, Response } from 'express';
|
||||
import { connection, bcrypt } from '../index'
|
||||
import { Request, Response } from 'express';
|
||||
import { connection, bcrypt } from '../index.js'
|
||||
|
||||
module.exports = {
|
||||
export default {
|
||||
method: "POST",
|
||||
url: "/registerUser",
|
||||
callback: async (data: any, req: Request, res: Response) => {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { existsSync, writeFileSync, readFileSync } from 'fs'
|
||||
|
||||
module.exports = {
|
||||
export default {
|
||||
method: "GET",
|
||||
url: "/removeIP/:ip",
|
||||
callback: async (data: any, req: Request, res: Response) => {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { readFileSync } from 'fs';
|
||||
|
||||
module.exports = {
|
||||
export default {
|
||||
method: "GET",
|
||||
url: "/",
|
||||
callback: async (data: any, req: Request, res: Response) => {
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
||||
|
||||
/* Modules */
|
||||
"module": "commonjs", /* Specify what module code is generated. */
|
||||
"module": "ESNext", /* Specify what module code is generated. */
|
||||
"rootDir": "./src", /* Specify the root folder within your source files. */
|
||||
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
||||
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
||||
|
@ -33,7 +33,7 @@
|
|||
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
|
||||
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||
"resolveJsonModule": true, /* Enable importing .json files */
|
||||
// "resolveJsonModule": true, /* Enable importing .json files */
|
||||
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
|
||||
|
||||
/* JavaScript Support */
|
||||
|
|
Loading…
Reference in New Issue