Skip to content

Commit 81612b5

Browse files
updated server, security, modules, add: autofilesystem module, add: ddos module, add: autoloaders scripts
1 parent e493db5 commit 81612b5

9 files changed

+108
-48
lines changed

config.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"porta": null,
3+
"autoporta": true,
34
"wsSystem":{
45
"portWS":null,
56
"enabled":true,

data/users.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
3+
]

modules/autoFileSysModule.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const fs = require("fs");
2+
3+
// example: const data = fopen("./data.json")
4+
function fopen(filePath) {
5+
const database = fs.readFileSync(filePath, "utf8");
6+
const data = JSON.parse(database);
7+
8+
return data;
9+
}
10+
11+
// example: fwrite("./data.json", data)
12+
function fwrite(filePath, data) {
13+
const formatData = JSON.stringify(data, null, 2)
14+
fs.writeFileSync(filePath, formatData, "utf8");
15+
return true;
16+
}
17+
18+
module.exports = { fopen, fwrite };

modules/ddosModule.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const ddos = require("ddos");
2+
3+
function configureDdosMiddleware() {
4+
const params = {
5+
limit: 150,
6+
maxcount: 250,
7+
trustProxy: true,
8+
includeUserAgent: true,
9+
whitelist: [],
10+
testmode: false,
11+
};
12+
const limiter = new ddos(params);
13+
return limiter;
14+
}
15+
16+
module.exports = configureDdosMiddleware;

modules/emailModule.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const nodemailer = require("nodemailer");
2-
const fs = require("fs");
3-
const configs = JSON.parse(fs.readFileSync("config.json", "utf8"));
2+
const { fopen, fwrite } = require("../modules/autoFileSysModule.js");
3+
const configs = fopen("config.json");
44
const configMail = configs.emailSystem
55
let transporter;
66

modules/fetchModule.js

+29-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fetch = require("node-fetch");
2+
23
const headersDefault = {
34
"x-forwarded-proto": "https,http,http",
45
"x-forwarded-port": "443,80,80",
@@ -11,14 +12,21 @@ function fetchGet(url, header, callback) {
1112
method: "GET",
1213
headers: newHeaders,
1314
};
14-
console.log("FETCH GET", url);
15+
16+
console.log("FETCH GET:", url);
1517
fetch(url, requestOptions)
1618
.then((response) => {
19+
console.log("Status da resposta:", response.status, response.statusText);
1720
const contentType = response.headers.get("content-type");
21+
console.log("Tipo de conteúdo:", contentType);
22+
1823
// Verifica se houve erro na resposta
1924
if (!response.ok) {
20-
throw new Error(`${response.status} ${response.statusText}`);
25+
return response.json().then((errorData) => {
26+
throw new Error(`Erro na resposta do servidor: ${JSON.stringify(errorData, null, 2)}`);
27+
});
2128
}
29+
2230
// Verifica o tipo de conteúdo retornado
2331
if (contentType && contentType.includes("application/json")) {
2432
// Se for JSON, retorna o JSON
@@ -30,6 +38,7 @@ function fetchGet(url, header, callback) {
3038
})
3139
.then((data) => {
3240
console.log("FETCH GET RECEBIDO! OK 200");
41+
console.log("Dados recebidos:", data);
3342
callback(null, data);
3443
})
3544
.catch((error) => {
@@ -43,28 +52,34 @@ function fetchPost(url, payload, header, callback) {
4352
"content-type": `application/json; charset=UTF-8`,
4453
};
4554
var newHeaders = headersDefault;
46-
newHeaders = Object.assign(headersDefault,header || defaultContentType);
55+
newHeaders = Object.assign(headersDefault, header || defaultContentType);
4756
const requestOptions = {
4857
method: "POST",
4958
headers: newHeaders,
5059
body: payload,
5160
};
52-
53-
if(newHeaders['content-type'] == "application/json; charset=UTF-8"){
54-
console.log("Convertendo payload para json!");
55-
payload = JSON.stringify(payload);
56-
}
57-
61+
62+
if (newHeaders["content-type"] == "application/json; charset=UTF-8") {
63+
console.log("Convertendo payload para JSON!");
64+
requestOptions.body = JSON.stringify(payload);
65+
}
66+
5867
console.log("FETCH POST", url);
68+
console.log("FETCH POST PAYLOAD: ", requestOptions.body);
5969
fetch(url, requestOptions)
6070
.then((response) => {
71+
console.log("Status da resposta:", response.status, response.statusText);
6172
const contentType = response.headers.get("content-type");
73+
console.log("Tipo de conteúdo:", contentType);
74+
6275
// Verifica se houve erro na resposta
6376
if (!response.ok) {
64-
console.error(response);
65-
throw new Error(`${response.status} ${response.statusText}`);
77+
return response.json().then((errorData) => {
78+
throw new Error(JSON.stringify(errorData, null, 2));
79+
});
6680
}
67-
// Verifica o tipo de conteúdo retornado
81+
82+
// // Verifica o tipo de conteúdo retornado
6883
if (contentType && contentType.includes("application/json")) {
6984
// Se for JSON, retorna o JSON
7085
return response.json();
@@ -75,6 +90,7 @@ function fetchPost(url, payload, header, callback) {
7590
})
7691
.then((data) => {
7792
console.log("FETCH POST ENVIADO! OK 200");
93+
console.log("Dados recebidos:", data);
7894
callback(null, data);
7995
})
8096
.catch((error) => {
@@ -83,4 +99,4 @@ function fetchPost(url, payload, header, callback) {
8399
});
84100
}
85101

86-
module.exports = { fetchGet, fetchPost };
102+
module.exports = { fetchGet, fetchPost };

modules/socket.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const express = require("express");
22
const expressWs = require('express-ws');
33
const path = require("path");
4-
const fs = require("fs");
4+
const { fopen, fwrite } = require("../modules/autoFileSysModule.js");
55
const app = express();
66
const router = express.Router();
77
const wsInstance = expressWs(app);
8-
const configs = JSON.parse(fs.readFileSync("config.json", "utf8"));
8+
const configs = fopen("config.json");
99
const wsConfig = configs.wsSystem;
1010

1111
const port = wsConfig.portWS || 2255;

server.js

+34-28
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,51 @@ const { File } = require("buffer");
77
const wsModule = require("./modules/socket.js")
88
const httpsSecurityMiddleware = require("./modules/httpsSecurityMiddleware.js");
99
const checkHeaderMiddleware = require("./modules/checkHeaderMiddleware.js");
10-
const {fetchGet,fetchPost} = require("./modules/fetchModule.js");
10+
const { fetchGet, fetchPost } = require("./modules/fetchModule.js");
11+
const ddosModule = require("./modules/ddosModule.js");
1112
const sendMail = require("./modules/emailModule.js");
1213

1314
const configs = JSON.parse(fs.readFileSync("config.json", "utf8"));
15+
const routesDir = __dirname;
1416
const porta = configs.porta
17+
const autoporta = configs.autoporta;
1518
const hostname = "localhost"
1619
const dinamicPort = (porta || 8080);
17-
const params = {
18-
limit: 100,
19-
maxcount: 200,
20-
trustProxy: true,
21-
includeUserAgent: true,
22-
whitelist: [],
23-
testmode: false
24-
};
25-
const limiter = new ddos(params)
26-
const rotas = require("./rotas");
27-
const pages = require("./pages");
28-
const emailSys = require("./sys-email");
29-
app.use(wsModule)
30-
app.use(limiter.express);
3120

32-
app.use(httpsSecurityMiddleware)
33-
app.use((req,res,next) =>{checkHeaderMiddleware(req,res,next)});
34-
app.use(pages);
35-
app.use(emailSys);
21+
app.use(wsModule);
22+
app.use(httpsSecurityMiddleware);
23+
app.use(ddosModule().express);
24+
app.use((req, res, next) => {
25+
checkHeaderMiddleware(req, res, next);
26+
});
3627
autoPages();
37-
//add here others files to load / adicione aqui outros arquivos para carregar
3828

39-
app.use(rotas);
40-
41-
var server = app.listen(dinamicPort, function () {
29+
// Carrega dinamicamente todos os módulos de rota
30+
fs.readdirSync(routesDir).forEach(file => {
31+
const filePath = path.join(routesDir, file);
32+
if (file.endsWith('.js') && file !== 'server.js') {
33+
const route = require(filePath);
34+
app.use(route);
35+
console.log(`Carregando arquivo ${file} automaticamente!`)
36+
}
37+
});
38+
if (autoporta) {
39+
var server = app.listen(() => {
40+
getServerAddress();
41+
});
42+
} else {
43+
var server = app.listen(dinamicPort, () => {
44+
getServerAddress();
45+
});
46+
}
4247

43-
var host = server.address().address
44-
var port = server.address().port
48+
function getServerAddress() {
49+
var host = server.address().address;
50+
var port = server.address().port;
4551

46-
console.log("Servidor rodando em http://%s:%s",hostname, port);
47-
console.log("IP Obtido: http://%s:%s",host, port);
48-
})
52+
console.log("Servidor rodando em http://%s:%s", hostname, port);
53+
console.log("IP Obtido: http://%s:%s", host, port);
54+
}
4955

5056
//auto page loader
5157
function autoPages() {

sys-email.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const express = require("express");
22
const router = express.Router();
3-
const fs = require("fs");
3+
const { fopen, fwrite } = require("./modules/autoFileSysModule.js");
44
const path = require("path");
55
const sendMail = require("./modules/emailModule.js");
66

@@ -77,7 +77,7 @@ function recuperarSenha(email, novaSenha) {
7777
\n
7878
\n
7979
\n
80-
Atenciosamente, PINGOBRAS S.A`
80+
Atenciosamente, Equipe Administrativa.`
8181

8282
sendMail(email, "SISTEMA: RECUPERAR SENHA", text, (error, info) => {
8383
if (error) {
@@ -131,7 +131,7 @@ function restaurarConta(email) {
131131
\n
132132
\n
133133
\n
134-
Atenciosamente, PINGOBRAS S.A`
134+
Atenciosamente, Equipe Administrativa.`
135135

136136
sendMail(email, "SISTEMA: RESTAURAR CONTA", text, (error, info) => {
137137
if (error) {

0 commit comments

Comments
 (0)