From 30865c6bd0a1e3c8540c016d6dcd4e1a08884f7d Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Mon, 22 Aug 2022 09:47:11 +0200 Subject: [PATCH] Move IP storage to database --- .gitignore | 1 - Dockerfile | 2 -- src/index.ts | 3 --- src/routes/addIP.ts | 27 ++++++++++++++++----------- src/routes/removeIP.ts | 25 +++++++++++++++++-------- src/routes/root.ts | 5 ++++- 6 files changed, 37 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 591c02a..252089d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ node_modules/ -data.json out/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index ce9732a..873f28f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,8 +6,6 @@ COPY package*.json ./ RUN npm ci -COPY data.json . - COPY views views COPY out out diff --git a/src/index.ts b/src/index.ts index 8389078..0148454 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 = 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; diff --git a/src/routes/addIP.ts b/src/routes/addIP.ts index 9b0fd9c..1a912bf 100644 --- a/src/routes/addIP.ts +++ b/src/routes/addIP.ts @@ -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" })); + }); + } + }); } } \ No newline at end of file diff --git a/src/routes/removeIP.ts b/src/routes/removeIP.ts index 32ee41d..4b30f4d 100644 --- a/src/routes/removeIP.ts +++ b/src/routes/removeIP.ts @@ -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" }));*/ } } \ No newline at end of file diff --git a/src/routes/root.ts b/src/routes/root.ts index 210c482..caf7182 100644 --- a/src/routes/root.ts +++ b/src/routes/root.ts @@ -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', {