-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
165 lines (146 loc) · 4.38 KB
/
server.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const express = require("express");
const router = express.Router();
const app = express();
const compression = require("compression");
const session = require("express-session");
const path = require("path");
const cors = require("cors");
const bodyParser = require("body-parser");
const fs = require("fs");
const axios = require("axios");
const { createProxyMiddleware } = require("http-proxy-middleware");
require("dotenv").config();
const redis = require("redis");
const client = (global.client = redis.createClient({
username: process.env.Redis_Username,
password: process.env.Redis_Password,
socket: {
host: process.env.Redis_Host,
port: process.env.Redis_Port,
},
}));
client.on("error", (err) => console.log("Redis Client Error", err));
client.on("ready", () => {
logger.success("Event", "Redis client ready");
});
client.on("reconnecting", () => {
logger.warn("Event", "Redis client reconnecting");
});
client.on("end", () => {
logger.warn("Event", "Redis client connection ended");
});
client.connect();
const cluster = require("node:cluster");
const os = require("node:os");
const logger = require("./utils/logger");
if (cluster.isPrimary) {
const numCPUs = os.cpus().length;
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on("exit", (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died. Starting a new one...`);
cluster.fork();
});
createServer();
global.disableLogs = false;
} else {
global.disableLogs = true;
setTimeout(() => {
global.disableLogs = false;
logger.worker("Event", `Worker ${process.pid} started`);
}, 5000);
}
async function createServer() {
app.use(compression());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(bodyParser.json());
app.use(cors());
app.engine(".ejs", require("ejs").__express);
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "/public")));
app.set("views", __dirname + "/views");
app.set("trust proxy", true);
app.use(
session({
key: "logshield",
secret: "sg809psargae9pr8gaertgheho9ar8g",
resave: false,
saveUninitialized: true,
cookie: { maxAge: process.env.Session_Time * 60 * 1000 }, // 30 minutes
})
);
const verifyRoutes = require("./routes/verify");
const wafMiddleware = require("./middleware/wafRules");
const rateLimit = require("./middleware/rateLimiter");
const bandwidth = require("./middleware/bandwidth");
const { router: verifyRouter, generateRayId } = verifyRoutes;
fs.readdirSync("./api").forEach((file) => {
app.use("/logshield/api", router);
require(`./api/${file}`)(router, client, checkAuth);
});
///////////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////////
app.use(
rateLimit({
limit: process.env.Max_Requests,
resetInterval: 60 * 1000 * process.env.Reset_Interval,
blockDuration: process.env.BlockDuration * 60 * 1000,
})
);
app.use(runcheck);
app.use(wafMiddleware);
app.use(verifyRouter);
app.use(bandwidth);
// Proxy configuration
const Difficulty = process.env.DIFFICULTY || 1;
function checkAuth(req, res, next) {
const authCode = req.body.auth;
if (authCode === "aielgv8sgeasgryleairgearihu") {
next();
} else {
res.status(401).send("Unauthorized");
}
}
async function runcheck(req, res, next) {
const userIp = req.ip || req.headers["x-forwarded-for"];
try {
await axios.get(process.env.TARGETURL);
next();
} catch (error) {
res.render("badGateway", { userIp });
return;
}
}
app.use(async (req, res, next) => {
const userIp = req.ip || req.headers["x-forwarded-for"];
if (req.session.whitelisted) {
next();
} else {
const secret = generateRayId();
res.render("ddosProtection", {
req,
secret,
sub: secret.substring(0, 25),
Difficulty,
userIp: userIp,
});
}
});
app.use(
["**", "/"],
createProxyMiddleware({
target: process.env.TARGETURL || "http://localhost:3000",
changeOrigin: true,
ws: true,
})
);
app.listen(process.env.PORT || 7000, () => {
logger.success(
"Event",
`Proxy server listening at http://localhost:${process.env.PORT}`
);
});
}