forked from mmdzov/luIP-marzban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
239 lines (181 loc) · 5.17 KB
/
utils.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const fs = require("fs");
const { spawn } = require("child_process");
function banIP(ip, email) {
const scriptPath = "./ipban.sh";
const args = [
scriptPath,
ip,
`${process.env.BAN_TIME}`,
`${process.env.SSH_PORT}`,
];
const childProcess = spawn("bash", args);
childProcess.on("close", (code) => {
if (code === 0) {
if (process.env.TG_ENABLE === "true")
globalThis.bot.api.sendMessage(
process.env.TG_ADMIN,
`${email}: IP ${ip} banned successfully.
Duration: ${process.env.BAN_TIME} minutes
`,
);
console.log(`IP ${ip} banned successfully.`);
} else {
console.error(`Failed to ban IP ${ip}.`);
}
});
}
class User {
/**
* @param {string} data Raw websocket data
* @returns {NewUserIpType[]}
*/
GetNewUserIP = (data) => {
let lines = data
.split(/\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}/g)
.map((item) => item.trim())
.filter((item) => item);
lines = lines.filter((item) => item.includes("accepted"));
if (!lines.length === 0) return [];
const getIp = (params) => {
const chunks = params.split(":");
if (/[a-zA-Z]/g.test(params)) chunks.shift();
return { ip: chunks[0], port: chunks[1] };
};
lines = lines.map((item) => ({
...getIp(item.split(" ")[0]),
email: item.split(" ").slice(-1)[0].replace(/\d\./g, ""),
}));
return lines.reduce((prev, curr) => {
const index = prev.findIndex((item) => item.ip === curr.ip);
if (index !== -1) prev[index] = curr;
else prev.push(curr);
return prev;
}, []);
};
/**
*
* @param {string} data Raw websocket data
* @returns {string}
*/
GetEmail = (data) => {
let returnData = "";
const chunks = data.split(" ");
if (!chunks.includes("email:")) return returnData;
const index = chunks.findIndex((item) => item === "email:");
const email = chunks[index + 1].split(".")[1].split("\n")[0];
return email;
};
/**
*
* @param {string} data Raw websocket data
* @returns {number}
*/
GetConnectionId(data) {
const chunks = data.split(" ");
const index = chunks.findIndex((item) => item === "[Info]");
if (index === -1) return 0;
const d = chunks[index + 1];
return JSON.parse(d)[0];
}
/**
*
* @param {string} data Raw websocket data
* @returns {number}
*/
Closed = (data) => {
const chunks = data.split(" ");
const ends =
chunks.includes("connection") &&
chunks.includes("ends") &&
chunks.includes("websocket:");
if (!ends) return 0;
return this.GetConnectionId(data);
};
}
class Server {
/**
* @param {string} address address with port. Like: example.com:443
* @param {boolean} api Return address with api
* @returns {string}
*/
CleanAddress(address, api = true, showHttp = true) {
const [ADDRESS, port] = address.split(":");
let _address = address;
if (+port === 443) _address = ADDRESS;
if (showHttp)
if (process.env.SSL == "true") _address = `https://${_address}`;
else _address = `http://${_address}`;
if (api) _address += "/api";
return _address;
}
}
class File {
constructor() {}
ForceExistsFile(path, data = undefined) {
if (!fs.existsSync(path)) fs.writeFileSync(path, data);
return;
}
GetJsonFile(path) {
this.ForceExistsFile(path);
return JSON.parse(fs.readFileSync(path));
}
GetCsvFile(path) {
this.ForceExistsFile(path, "");
return fs.readFileSync(path);
}
}
/**
* @description IP Guard
*/
class IPGuard {
constructor(banDB) {
this.banDB = banDB;
}
/**
*
* @param {IPSDataType} record A user's record includes email, ips array
* @param {function[]} callback Return function to allow ip usage
*
* @returns {void | Promise<Function>}
*/
async use(ip, ...callback) {
const data = await callback[0]();
if (!data) return await callback[1]();
const indexOfIp = data.ips.findIndex((item) => item.ip === `${ip}`);
const users = new File().GetJsonFile("users.json");
let usersCsv = new File().GetCsvFile("users.csv").toString();
if (usersCsv.trim()) {
usersCsv = usersCsv.split("\r\n").map((item) => item.split(","));
}
if (usersCsv && usersCsv.some((item) => item[0] === data.email) === false)
usersCsv = null;
let userCsv = null;
if (usersCsv.trim())
userCsv = usersCsv.filter((item) => item[0] === data.email)[0] || null;
const user = users.filter((item) => item[0] === data.email)[0] || null;
const maxAllowConnection = userCsv
? +userCsv[1]
: user
? +user[1]
: +process.env.MAX_ALLOW_USERS;
const limited = data.ips.length > maxAllowConnection;
// Remove last user from db
if (indexOfIp !== -1 && limited) {
return callback[2]();
}
//
if (data.ips.length >= maxAllowConnection && indexOfIp === -1) {
this.ban({ ip, email: data.email });
return;
}
return await callback[1]();
}
/**
* @param {BanIpConfigAddType} params
*/
ban(params) {
banIP(`${params.ip}`, params.email);
// console.log("ban", params);
}
}
module.exports = { User, Server, File, IPGuard };