Skip to content

Commit 2858e19

Browse files
updated
1 parent 0cbf188 commit 2858e19

File tree

3 files changed

+176
-1
lines changed

3 files changed

+176
-1
lines changed

modules/utils.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
const path = require("path");
2+
const {
3+
fopen,
4+
fwrite,
5+
freadBin,
6+
fwriteBin,
7+
} = require("./autoFileSysModule.js");
8+
const routesDir = __dirname;
9+
const files2 = __dirname + "/src/";
10+
const path_pages = files2 + "pages/";
11+
const forbiddenFilePath = path.join(path_pages, "forbidden.html");
12+
13+
// Função para ordenar bases por usuario
14+
function ordenarUsuario(file) {
15+
const data = freadBin(file);
16+
17+
// Ordena o array de usuarios com base no usuario, do maior para o menor
18+
data.sort((a, b) => b.usuario - a.usuario);
19+
20+
// Salva o array ordenado de volta no arquivo
21+
fwriteBin(file, data);
22+
}
23+
24+
25+
function pesqUsuarioByEmail(file,email) {
26+
const data = freadBin(file);
27+
let pos = 0;
28+
29+
for (pos = 0; pos < data.length; pos++) {
30+
var currentDB = data[pos];
31+
const currentEmail = currentDB.email;
32+
33+
// Verifica se e o email
34+
const authEmail = currentEmail == email;
35+
36+
console.log("-----SISTEMA----");
37+
console.log("TAMANHO: " + data.length);
38+
console.log("POSIÇÃO: " + pos);
39+
console.log("Pesquisando...");
40+
console.log("e-mail : " + email + " == " + currentEmail);
41+
42+
// Verifica se o nome,usuário e email são verdadeiros
43+
if (authEmail) {
44+
return pos;
45+
}
46+
}
47+
return -1; // Retorna -1 se não foram encontrados usuarios no vetor
48+
}
49+
50+
function pesqUsuario(file,username) {
51+
const data = freadBin(file);
52+
let pos = 0;
53+
54+
for (pos = 0; pos < data.length; pos++) {
55+
var currentDB = data[pos];
56+
const currentUser = currentDB.usuario;
57+
58+
// Verifica se e o usuario
59+
const authNome = currentUser == username;
60+
61+
console.log("-----SISTEMA----");
62+
console.log("TAMANHO: " + data.length);
63+
console.log("POSIÇÃO: " + pos);
64+
console.log("Pesquisando...");
65+
console.log("User: " + username + " == " + currentUser);
66+
67+
// Verifica se o nome,usuário e email são verdadeiros
68+
if (authNome) {
69+
return pos;
70+
}
71+
}
72+
return -1; // Retorna -1 se não foram encontrados usuarios no vetor
73+
}
74+
75+
function getRandomInt(max) {
76+
return Math.floor(Math.random() * max);
77+
}
78+
79+
function getRandomBin(max) {
80+
return Math.floor(Math.random() * max).toString(2);
81+
}
82+
83+
function getRandomHex(max) {
84+
return Math.floor(Math.random() * max).toString(16);
85+
}
86+
87+
function generateToken() {
88+
let token = '';
89+
const maxLength = 32; // Precisamos de exatamente 32 caracteres
90+
91+
while (token.length < maxLength) {
92+
let hex = getRandomHex(256); // Gera um valor hexadecimal
93+
token += hex; // Adiciona o valor ao token
94+
}
95+
96+
return token.substring(0, maxLength); // Garante que o token tenha exatamente 32 caracteres
97+
}
98+
99+
function formatDate(dateString) {
100+
const options = {
101+
year: 'numeric',
102+
month: '2-digit',
103+
day: '2-digit',
104+
hour: '2-digit',
105+
minute: '2-digit',
106+
second: '2-digit',
107+
hour12: false
108+
};
109+
const date = new Date(dateString);
110+
return date.toLocaleString('pt-BR', options);
111+
}
112+
113+
function validadeApiKey(req,res,key){
114+
const keyHeader = req.headers["authorization"];
115+
const authApi = keyHeader == key;
116+
117+
if(!authApi){
118+
forbidden(res);
119+
}
120+
}
121+
122+
function conversorSimEnao(value) {
123+
if (value) {
124+
return "✔Voce foi autorizado, esta tudo correto";
125+
}
126+
return "⚠Esta faltando algo ou não foi autorizado!";
127+
}
128+
129+
130+
function forbidden(res) {
131+
console.error(403);
132+
res.status(403);
133+
res.sendFile(forbiddenFilePath);
134+
}
135+
136+
function unauthorized(res) {
137+
console.error(401);
138+
res.status(401);
139+
res.sendStatus(401);
140+
}
141+
142+
module.exports = {
143+
getRandomInt,
144+
getRandomBin,
145+
getRandomHex,
146+
generateToken,
147+
ordenarUsuario,
148+
pesqUsuario,
149+
validadeApiKey,
150+
unauthorized,
151+
forbidden,
152+
formatDate,
153+
conversorSimEnao,
154+
};

pages.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require("express");
22
const router = express.Router();
33
const fs = require("fs");
4+
const os = require("os");
45
const path = require("path");
56

67
const files = __dirname + "/src/";
@@ -21,6 +22,23 @@ router.get("/", (req, res) => {
2122
res.sendFile(indexFilePath);
2223
});
2324

25+
router.get('/status', (req, res) => {
26+
const healthCheck = {
27+
uptime: process.uptime(),
28+
message: 'OK',
29+
timestamp: Date.now(),
30+
cpuUsage: os.loadavg(),
31+
memoryUsage: process.memoryUsage(),
32+
};
33+
34+
try {
35+
res.json(healthCheck);
36+
} catch (e) {
37+
healthCheck.message = e;
38+
res.status(503).send();
39+
}
40+
});
41+
2442
router.get("/host", (req, res) => {
2543
console.log("SISTEMA <OBTER> <SITE>: " + req.url);
2644
res.sendFile(hostFilePath);

server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ const checkHeaderMiddleware = require("./modules/checkHeaderMiddleware.js");
1212
const ddosModule = require("./modules/ddosModule.js");
1313
const { fetchGet, fetchPost, discordLogs } = require("./modules/fetchModule.js");
1414
const { fopen, fwrite, freadBin, fwriteBin } = require("./modules/autoFileSysModule.js");
15-
const hostname = "localhost"
15+
// const hostname = "127.0.0.1"; só local
16+
// const hostname = "0.0.0.0"; Bind na placa de rede
17+
// const hostname = "::"; bind ipv4 e ipv6 pra fora
18+
const hostname = "::";
1619
const porta = process.env.PORTA;
1720
const dinamicPort = (porta || 8080);
1821

0 commit comments

Comments
 (0)