explogin.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package explogin
  2. import (
  3. "math"
  4. "sync"
  5. )
  6. /*
  7. Explogin.go
  8. Package to handle expotential login time
  9. so as to prevent someone from brute forcing your password
  10. Author: tobychui
  11. */
  12. type UserLoginEntry struct {
  13. Username string
  14. TargetIP string
  15. Timestamp int64
  16. RetryCount int64
  17. }
  18. type ExpLoginHandler struct {
  19. LoginRecord *sync.Map //Sync map to store UserLoginEntry, username+ip as key
  20. BaseDelay int //Base delay exponent
  21. DelayCeiling int //Max delay time
  22. }
  23. //Create a new exponential login handler object
  24. func NewExponentialLoginHandler(baseDelay int) *ExpLoginHandler {
  25. recordMap := sync.Map{}
  26. return &ExpLoginHandler{
  27. LoginRecord: &recordMap,
  28. BaseDelay: baseDelay,
  29. }
  30. }
  31. //Check allow access now, if false return how many seconds till next retry
  32. func (e *ExpLoginHandler) AllowImmediateAccess(username string, ip string) (bool, int64) {
  33. }
  34. func (e *ExpLoginHandler) getDelayTimeFromRetryCount(retryCount int) int64 {
  35. delaySecs := int64(math.Floor((math.Pow(2, float64(retryCount)) - 1) * 0.5))
  36. if delaySecs > int64(e.DelayCeiling) {
  37. delaySecs = int64(e.DelayCeiling)
  38. }
  39. return delaySecs
  40. }