💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › ff94e9ce2743cf197566262448… captured on 2022-07-16 at 17:09:21. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
0 package auth
1
2 import (
3 "errors"
4 "gemigit/config"
5 "gemigit/db"
6 "time"
7 )
8
9 var userAttempts = make(map[string]int)
10 var clientAttempts = make(map[string]int)
11
12 func Decrease() {
13 for {
14 for k, v := range userAttempts {
15 if v > 0 {
16 userAttempts[k]--
17 }
18 }
19 for k, v := range clientAttempts {
20 if v > 0 {
21 clientAttempts[k]--
22 }
23 }
24 time.Sleep(30 * time.Second)
25 db.DisconnectTimeout()
26 }
27 }
28
29 func Connect(username string, password string, signature string, ip string) error {
30 attempts, b := userAttempts[username]
31 if b {
32 if attempts < config.Cfg.Gemigit.MaxAttemptsForAccount {
33 userAttempts[username]++
34 } else {
35 return errors.New("the account is locked, too many connections attempts")
36 }
37 } else {
38 userAttempts[username] = 1
39 }
40 attempts, b = clientAttempts[ip]
41 if b {
42 if attempts < config.Cfg.Gemigit.MaxAttemptsForIP {
43 clientAttempts[ip]++
44 } else {
45 return errors.New("too many connections attempts")
46 }
47 } else {
48 clientAttempts[ip] = 1
49 }
50 b, err := db.Login(username, password, signature)
51 if err != nil {
52 return err
53 }
54 if !b {
55 return errors.New("wrong username or password")
56 }
57 return nil
58 }
59