1
1
const Keyv = require ( 'keyv' ) ;
2
2
const uap = require ( 'ua-parser-js' ) ;
3
- const { getLogStores } = require ( '../../cache' ) ;
4
3
const denyRequest = require ( './denyRequest' ) ;
4
+ const { getLogStores } = require ( '../../cache' ) ;
5
5
const { isEnabled, removePorts } = require ( '../utils' ) ;
6
6
const keyvRedis = require ( '../../cache/keyvRedis' ) ;
7
+ const User = require ( '../../models/User' ) ;
7
8
8
9
const banCache = isEnabled ( process . env . USE_REDIS )
9
10
? new Keyv ( { store : keyvRedis } )
@@ -52,12 +53,33 @@ const checkBan = async (req, res, next = () => {}) => {
52
53
}
53
54
54
55
req . ip = removePorts ( req ) ;
55
- const userId = req . user ?. id ?? req . user ?. _id ?? null ;
56
- const ipKey = isEnabled ( process . env . USE_REDIS ) ? `ban_cache:ip:${ req . ip } ` : req . ip ;
57
- const userKey = isEnabled ( process . env . USE_REDIS ) ? `ban_cache:user:${ userId } ` : userId ;
56
+ let userId = req . user ?. id ?? req . user ?. _id ?? null ;
57
+
58
+ if ( ! userId && req ?. body ?. email ) {
59
+ const user = await User . findOne ( { email : req . body . email } , '_id' ) . lean ( ) ;
60
+ userId = user ?. _id ? user . _id . toString ( ) : userId ;
61
+ }
62
+
63
+ if ( ! userId && ! req . ip ) {
64
+ return next ( ) ;
65
+ }
66
+
67
+ let cachedIPBan ;
68
+ let cachedUserBan ;
69
+
70
+ let ipKey = '' ;
71
+ let userKey = '' ;
72
+
73
+ if ( req . ip ) {
74
+ ipKey = isEnabled ( process . env . USE_REDIS ) ? `ban_cache:ip:${ req . ip } ` : req . ip ;
75
+ cachedIPBan = await banCache . get ( ipKey ) ;
76
+ }
77
+
78
+ if ( userId ) {
79
+ userKey = isEnabled ( process . env . USE_REDIS ) ? `ban_cache:user:${ userId } ` : userId ;
80
+ cachedUserBan = await banCache . get ( userKey ) ;
81
+ }
58
82
59
- const cachedIPBan = await banCache . get ( ipKey ) ;
60
- const cachedUserBan = await banCache . get ( userKey ) ;
61
83
const cachedBan = cachedIPBan || cachedUserBan ;
62
84
63
85
if ( cachedBan ) {
@@ -72,24 +94,42 @@ const checkBan = async (req, res, next = () => {}) => {
72
94
return next ( ) ;
73
95
}
74
96
75
- const ipBan = await banLogs . get ( req . ip ) ;
76
- const userBan = await banLogs . get ( userId ) ;
77
- const isBanned = ipBan || userBan ;
97
+ let ipBan ;
98
+ let userBan ;
99
+
100
+ if ( req . ip ) {
101
+ ipBan = await banLogs . get ( req . ip ) ;
102
+ }
103
+
104
+ if ( userId ) {
105
+ userBan = await banLogs . get ( userId ) ;
106
+ }
107
+
108
+ const isBanned = ! ! ( ipBan || userBan ) ;
78
109
79
110
if ( ! isBanned ) {
80
111
return next ( ) ;
81
112
}
82
113
83
114
const timeLeft = Number ( isBanned . expiresAt ) - Date . now ( ) ;
84
115
85
- if ( timeLeft <= 0 ) {
116
+ if ( timeLeft <= 0 && ipKey ) {
86
117
await banLogs . delete ( ipKey ) ;
118
+ }
119
+
120
+ if ( timeLeft <= 0 && userKey ) {
87
121
await banLogs . delete ( userKey ) ;
88
122
return next ( ) ;
89
123
}
90
124
91
- banCache . set ( ipKey , isBanned , timeLeft ) ;
92
- banCache . set ( userKey , isBanned , timeLeft ) ;
125
+ if ( ipKey ) {
126
+ banCache . set ( ipKey , isBanned , timeLeft ) ;
127
+ }
128
+
129
+ if ( userKey ) {
130
+ banCache . set ( userKey , isBanned , timeLeft ) ;
131
+ }
132
+
93
133
req . banned = true ;
94
134
return await banResponse ( req , res ) ;
95
135
} ;
0 commit comments