mirror of https://github.com/Aelita4/sshmon.git
				
				
				
			Move IP storage to database
This commit is contained in:
		
							parent
							
								
									d55e9c3d45
								
							
						
					
					
						commit
						30865c6bd0
					
				| 
						 | 
				
			
			@ -1,3 +1,2 @@
 | 
			
		|||
node_modules/
 | 
			
		||||
data.json
 | 
			
		||||
out/
 | 
			
		||||
| 
						 | 
				
			
			@ -6,8 +6,6 @@ COPY package*.json ./
 | 
			
		|||
 | 
			
		||||
RUN npm ci
 | 
			
		||||
 | 
			
		||||
COPY data.json .
 | 
			
		||||
 | 
			
		||||
COPY views views
 | 
			
		||||
 | 
			
		||||
COPY out out
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,6 @@ 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) => {
 | 
			
		||||
| 
						 | 
				
			
			@ -55,8 +54,6 @@ const pings = new Map();
 | 
			
		|||
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,19 +1,24 @@
 | 
			
		|||
import { Request, Response } from 'express';
 | 
			
		||||
import { existsSync, writeFileSync, readFileSync } from 'fs'
 | 
			
		||||
import { connection } from '../index.js';
 | 
			
		||||
 | 
			
		||||
export default {
 | 
			
		||||
    method: "GET",
 | 
			
		||||
    url: "/addIP/:ip",
 | 
			
		||||
    callback: async (data: any, req: Request, res: Response) => {
 | 
			
		||||
        if(!existsSync("/usr/src/app/data.json")) writeFileSync("/usr/src/app/data.json", "[]");
 | 
			
		||||
        const json = JSON.parse(readFileSync("/usr/src/app/data.json", {encoding:'utf8', flag:'r'}));
 | 
			
		||||
    
 | 
			
		||||
        json.push(req.params.ip);
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
        writeFileSync("/usr/src/app/data.json", JSON.stringify(json))
 | 
			
		||||
    
 | 
			
		||||
        res.setHeader('Content-Type', 'application/json');
 | 
			
		||||
        res.end(JSON.stringify({ status: "OK" }));
 | 
			
		||||
    callback: async (data: any, req: Request, res: Response) => {  
 | 
			
		||||
        connection.query("SELECT * FROM ips WHERE ip = ?", req.params.ip, (err, results, fields) => {
 | 
			
		||||
            if(err) throw err;
 | 
			
		||||
 | 
			
		||||
            if(results.length > 0) {
 | 
			
		||||
                res.setHeader('Content-Type', 'application/json');
 | 
			
		||||
                res.end(JSON.stringify({ status: "OK" }));
 | 
			
		||||
            } else {
 | 
			
		||||
                connection.query("INSERT INTO ips VALUES (NULL, ?)", [req.params.ip], (errr, resultss, fieldss) => {
 | 
			
		||||
                    if(errr) throw errr
 | 
			
		||||
                    res.setHeader('Content-Type', 'application/json');
 | 
			
		||||
                    res.end(JSON.stringify({ status: "OK" }));
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
        });        
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,18 +1,27 @@
 | 
			
		|||
import { Request, Response } from 'express';
 | 
			
		||||
import { existsSync, writeFileSync, readFileSync } from 'fs'
 | 
			
		||||
import { connection } from '../index.js';
 | 
			
		||||
 | 
			
		||||
export default {
 | 
			
		||||
    method: "GET",
 | 
			
		||||
    url: "/removeIP/:ip",
 | 
			
		||||
    callback: async (data: any, req: Request, res: Response) => {
 | 
			
		||||
        if(!existsSync("/usr/src/app/data.json")) writeFileSync("/usr/src/app/data.json", "[]");
 | 
			
		||||
        const json = JSON.parse(readFileSync("/usr/src/app/data.json", {encoding:'utf8', flag:'r'}));
 | 
			
		||||
        connection.query("SELECT * FROM ips WHERE ip = ?", req.params.ip, (err, results, fields) => {
 | 
			
		||||
            if(err) throw err;
 | 
			
		||||
 | 
			
		||||
            if(results.length == 0) {
 | 
			
		||||
                res.setHeader('Content-Type', 'application/json');
 | 
			
		||||
                res.end(JSON.stringify({ status: "OK" }));
 | 
			
		||||
            } else {
 | 
			
		||||
                connection.query("DELETE FROM ips WHERE ip = ?", req.params.ip, (errr, resultss, fieldss) => {
 | 
			
		||||
                    if(errr) throw errr
 | 
			
		||||
                    res.setHeader('Content-Type', 'application/json');
 | 
			
		||||
                    res.end(JSON.stringify({ status: "OK" }));
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    
 | 
			
		||||
        json.splice(json.indexOf(req.params.ip.toString()), 1)
 | 
			
		||||
    
 | 
			
		||||
        writeFileSync("/usr/src/app/data.json", JSON.stringify(json))
 | 
			
		||||
    
 | 
			
		||||
        res.setHeader('Content-Type', 'application/json');
 | 
			
		||||
        res.end(JSON.stringify({ status: "OK" }));
 | 
			
		||||
        /*res.setHeader('Content-Type', 'application/json');
 | 
			
		||||
        res.end(JSON.stringify({ status: "OK" }));*/
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,11 +1,14 @@
 | 
			
		|||
import { Request, Response } from 'express';
 | 
			
		||||
import { readFileSync } from 'fs';
 | 
			
		||||
import { getIP } from '../index.js';
 | 
			
		||||
 | 
			
		||||
export default {
 | 
			
		||||
    method: "GET",
 | 
			
		||||
    url: "/",
 | 
			
		||||
    callback: async (data: any, req: Request, res: Response) => {
 | 
			
		||||
        data.addresses = JSON.parse(readFileSync("/usr/src/app/data.json", {encoding:'utf8', flag:'r'}));
 | 
			
		||||
        const tmp = await getIP();
 | 
			
		||||
        //@ts-ignore
 | 
			
		||||
        data.addresses = tmp.map(a => a.ip);
 | 
			
		||||
        //@ts-ignore
 | 
			
		||||
        if(!req.session.user) res.redirect("/login");
 | 
			
		||||
        else res.render('pages/index.ejs', {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue