-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.js
105 lines (86 loc) · 2.28 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const { Server } = require("./utils");
const { Ws, Api } = require("./config");
const DBSqlite3 = require("./db/DBSqlite3");
const def = require("./def");
const express = require("express");
const nodeCron = require("node-cron");
const { DBAdapter } = require("./db/Adapter");
const { exec } = require("child_process");
const { join } = require("path");
const router = require("./api/controller");
const bodyParser = require("body-parser");
const tg = require("./telegram/tg");
require("dotenv").config();
const app = express();
const api = new Api();
const DBType = new DBSqlite3();
def();
(async () => {
tg();
api.create();
await api.token();
const url = new Server().CleanAddress(
`${process.env.ADDRESS}:${process.env.PORT_ADDRESS}`,
false,
false,
);
const nodes = await api.getNodes();
const ws = new Ws({ url, accessToken: api.accessToken, DB: DBType, api });
ws.logs();
for (let i in nodes) {
const node = nodes[i];
const ws = new Ws({
url,
accessToken: `${api.accessToken}d`,
DB: DBType,
node,
api,
});
ws.logs();
}
})();
if (process.env.NODE_ENV.includes("production")) {
nodeCron.schedule(
`*/${process.env.CHECK_INACTIVE_USERS_DURATION} * * * *`,
() => {
const db = new DBAdapter(DBType);
db.deleteInactiveUsers();
},
);
nodeCron.schedule(
`*/${process.env.CHECK_IPS_FOR_UNBAN_USERS} * * * *`,
() => {
exec("bash ./ipunban.sh", (error, stdout, stderr) => {
if (error) {
console.error(`Error executing ipunban.sh: ${error.message}`);
return;
}
if (stderr) {
console.error(`ipunban.sh stderr: ${stderr}`);
return;
}
// console.log(`ipunban.sh stdout: ${stdout}`);
});
},
);
}
// Api server
if (process.env?.API_ENABLE === "true") {
const PORT = process.env?.API_PORT;
let address = new Server().CleanAddress(
`${process.env.ADDRESS}:${process.env.API_PORT}`,
false,
true,
);
address = `${address}${process.env.API_PATH}`;
app.use(
bodyParser.urlencoded({
extended: true,
}),
);
app.use(bodyParser.json());
app.use(`${process.env.API_PATH}`, router);
const server = app.listen(PORT, () => {
console.log(`Server running: ${address}`);
});
}