Skip to content

Commit fbbf09f

Browse files
committed
Update
1 parent 387c8e4 commit fbbf09f

File tree

5 files changed

+63
-15
lines changed

5 files changed

+63
-15
lines changed

config.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Ws {
5454
port: item.port,
5555
date: new Date().toISOString().toString(),
5656
}),
57+
() => this.db.deleteLastIp(item.email),
5758
);
5859
}
5960
});

db/Adapter.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,17 @@ class DBAdapter {
7373
/**
7474
* @description delete inactive users from database record
7575
* @returns {void}
76-
*/
76+
*/
7777
deleteInactiveUsers() {
78-
return this.database.deleteInactiveUsers()
78+
return this.database.deleteInactiveUsers();
79+
}
7980

81+
/**
82+
* @param {string} email
83+
* @returns {void}
84+
*/
85+
deleteLastIp(email) {
86+
return this.database.deleteLastIp(email);
8087
}
8188
}
8289

db/DBSqlite3.js

+39-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class DBSqlite3 extends DBInterface {
2727
} else {
2828
if (row) {
2929
row.ips = JSON.parse(row.ips);
30+
console.log("Read db:", row);
3031
resolve(row);
3132
} else resolve(null);
3233
}
@@ -42,7 +43,7 @@ class DBSqlite3 extends DBInterface {
4243
if (err) {
4344
reject(err);
4445
} else {
45-
console.log(row);
46+
// console.log(row);
4647
}
4748
});
4849
});
@@ -51,10 +52,15 @@ class DBSqlite3 extends DBInterface {
5152

5253
addUser(data) {
5354
db.serialize(() => {
54-
db.run("INSERT INTO users (email, ips) VALUES (?, ?)", [
55-
data.email,
56-
JSON.stringify(data.ips),
57-
]);
55+
db.run(
56+
"INSERT INTO users (email, ips) VALUES (?, ?)",
57+
[data.email, JSON.stringify(data.ips)],
58+
(err, res) => {
59+
if (err) {
60+
throw new Error(err);
61+
}
62+
},
63+
);
5864
});
5965
}
6066

@@ -152,6 +158,33 @@ class DBSqlite3 extends DBInterface {
152158
});
153159
}
154160

161+
deleteLastIp(email) {
162+
db.serialize(() => {
163+
db.get("SELECT * FROM users WHERE email = ?", [email], (err, row) => {
164+
if (err) throw new Error(err);
165+
else {
166+
if (!row) return;
167+
168+
const ips = JSON.parse(row.ips);
169+
170+
ips.pop();
171+
172+
db.run(
173+
'UPDATE users SET ips = JSON_REPLACE(ips, "$", ?) WHERE email = ?',
174+
[JSON.stringify(ips), email],
175+
(updateErr, updateRow) => {
176+
if (updateErr) {
177+
throw new Error(updateErr);
178+
} else {
179+
// console.log("Ip Successfully Added");
180+
}
181+
},
182+
);
183+
}
184+
});
185+
});
186+
}
187+
155188
deleteInactiveUsers() {
156189
const currentTime = new Date().getTime();
157190
const fewMinutesAgo = new Date(
@@ -178,7 +211,7 @@ class DBSqlite3 extends DBInterface {
178211
return idDate > fewMinutesAgo;
179212
});
180213

181-
console.log("updateIds",updatedIds);
214+
console.log("updateIds", updatedIds);
182215

183216
db.run(
184217
`UPDATE users SET ips = ? WHERE email = ?`,

interface.js

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class DBInterface {
2626
deleteInactiveUsers() {
2727
throw new Error("This method must be implemented in the class");
2828
}
29+
30+
deleteLastIp(email) {
31+
throw new Error("This method must be implemented in the class");
32+
}
2933
}
3034

3135
class LuIPInterface {

utils.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,16 @@ class IPGuard {
179179

180180
const maxAllowConnection = user ? +user[1] : +process.env.MAX_ALLOW_USERS;
181181

182-
if (
183-
indexOfIp !== -1 &&
184-
data.ips.length >= maxAllowConnection &&
185-
!data.ips[indexOfIp]?.first
186-
) {
187-
console.log("should ban ip. Full data:", data)
188-
// this.ban({ ip });
182+
const limited = data.ips.length > maxAllowConnection;
183+
184+
// Remove last user from db
185+
if (indexOfIp !== -1 && limited) {
186+
return callback[2]();
187+
}
188+
189+
//
190+
if (data.ips.length >= maxAllowConnection) {
191+
this.ban({ ip });
189192

190193
return;
191194
}

0 commit comments

Comments
 (0)