Move IP storage to database

This commit is contained in:
Aelita4 2022-08-22 09:47:11 +02:00
parent d55e9c3d45
commit 30865c6bd0
Signed by: Aelita4
GPG Key ID: F8EC95519509D1D5
6 changed files with 37 additions and 26 deletions

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
node_modules/
data.json
out/

View File

@ -6,8 +6,6 @@ COPY package*.json ./
RUN npm ci
COPY data.json .
COPY views views
COPY out out

View File

@ -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;

View File

@ -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'}));
connection.query("SELECT * FROM ips WHERE ip = ?", req.params.ip, (err, results, fields) => {
if(err) throw err;
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" }));
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" }));
});
}
});
}
}

View File

@ -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;
json.splice(json.indexOf(req.params.ip.toString()), 1)
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" }));
});
}
});
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" }));*/
}
}

View File

@ -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', {