@@ -6,6 +6,24 @@ const sleep = require('sleep-promise');
6
6
const bcryptCompare = promisify ( bcrypt . compare ) ;
7
7
const bcryptHash = promisify ( bcrypt . hash ) ;
8
8
9
+ async function throttleAttempt ( queryHandler , ip ) {
10
+ let { attempts, lastDate } = queryHandler . getLoginAttempts ( ip ) ;
11
+
12
+ if ( attempts >= 5 ) {
13
+ if ( Date . now ( ) - lastDate >= 1000 * 60 * 5 ) {
14
+ return true ;
15
+ } else {
16
+ queryHandler . setLoginAttempts ( ip , 0 ) ;
17
+ attempts = 0 ;
18
+ }
19
+ } else {
20
+ queryHandler . setLoginAttempts ( ip , attempts + 1 ) ;
21
+ }
22
+
23
+ await sleep ( attempts * 1000 ) ;
24
+ return false ;
25
+ }
26
+
9
27
async function playerCount ( { token } ) {
10
28
const queryHandler = this . server . queryHandler ;
11
29
const playerCount = queryHandler . getPlayerCount ( ) ;
@@ -47,31 +65,22 @@ async function playerLogin({ token, username, password, ip, reconnecting }) {
47
65
48
66
const queryHandler = this . server . queryHandler ;
49
67
50
- let { attempts, lastDate } = queryHandler . getLoginAttempts ( ip ) ;
51
-
52
- if ( attempts >= 5 ) {
53
- if ( Date . now ( ) - lastDate >= 1000 * 60 * 5 ) {
54
- queryHandler . setLoginAttempts ( ip , 0 ) ;
55
- attempts = 0 ;
56
- } else {
57
- message . code = 7 ;
58
- message . success = false ;
59
- this . socket . sendMessage ( message ) ;
60
- return ;
61
- }
68
+ if ( await throttleAttempt ( queryHandler , ip ) ) {
69
+ message . code = 7 ;
70
+ message . success = false ;
71
+ return this . socket . sendMessage ( message ) ;
62
72
}
63
73
64
- await sleep ( attempts * 1000 ) ;
65
-
66
74
const hash = queryHandler . getPlayerPassword ( username ) ;
67
75
68
76
if ( ! hash || ! ( await bcryptCompare ( password , hash ) ) ) {
69
77
message . code = 3 ;
70
78
message . success = false ;
71
- queryHandler . setLoginAttempts ( ip , attempts + 1 ) ;
72
79
return this . socket . sendMessage ( message ) ;
73
80
}
74
81
82
+ queryHandler . setLoginAttempts ( ip , 0 ) ;
83
+
75
84
const rounds = bcrypt . getRounds ( hash ) ;
76
85
const passwordHashRounds = this . server . config . passwordHashRounds ;
77
86
@@ -265,10 +274,7 @@ async function playerMessage({
265
274
message
266
275
} ) ;
267
276
268
- this . socket . sendMessage ( {
269
- token,
270
- success : true
271
- } ) ;
277
+ this . socket . sendMessage ( { token, success : true } ) ;
272
278
} catch ( e ) {
273
279
this . socket . sendMessage ( {
274
280
token,
@@ -278,6 +284,28 @@ async function playerMessage({
278
284
}
279
285
}
280
286
287
+ async function webLogin ( { token, username, password, ip } ) {
288
+ username = username . trim ( ) . toLowerCase ( ) ;
289
+
290
+ const queryHandler = this . server . queryHandler ;
291
+
292
+ if ( await throttleAttempt ( queryHandler , ip ) ) {
293
+ return this . socket . sendMessage ( { token, success : false } ) ;
294
+ }
295
+
296
+ const hash = queryHandler . getPlayerPassword ( username ) ;
297
+
298
+ if ( ! hash || ! ( await bcryptCompare ( password , hash ) ) ) {
299
+ return this . socket . sendMessage ( { token, success : false } ) ;
300
+ }
301
+
302
+ queryHandler . setLoginAttempts ( ip , 0 ) ;
303
+
304
+ const player = queryHandler . getWebPlayer ( username ) ;
305
+
306
+ return this . socket . sendMessage ( { token, success : true , player } ) ;
307
+ }
308
+
281
309
module . exports = {
282
310
playerCount,
283
311
playerGetWorlds,
@@ -287,5 +315,6 @@ module.exports = {
287
315
playerOnlineCount,
288
316
playerRegister,
289
317
playerUpdate,
290
- playerMessage
318
+ playerMessage,
319
+ webLogin
291
320
} ;
0 commit comments