mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 10:25:52 +02:00
41 lines
932 B
Go
41 lines
932 B
Go
package user
|
|
|
|
import (
|
|
"github.com/0xJacky/Nginx-UI/model"
|
|
"github.com/0xJacky/Nginx-UI/query"
|
|
"github.com/0xJacky/Nginx-UI/settings"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"time"
|
|
)
|
|
|
|
func Login(name string, password string) (user *model.User, err error) {
|
|
u := query.User
|
|
|
|
user, err = u.Where(u.Name.Eq(name)).First()
|
|
if err != nil {
|
|
return nil, ErrPasswordIncorrect
|
|
}
|
|
|
|
if err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
|
|
return nil, ErrPasswordIncorrect
|
|
}
|
|
|
|
if !user.Status {
|
|
return nil, ErrUserBanned
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func BanIP(ip string) {
|
|
b := query.BanIP
|
|
banIP, err := b.Where(b.IP.Eq(ip)).First()
|
|
if err != nil || banIP.ExpiredAt <= time.Now().Unix() {
|
|
_ = b.Create(&model.BanIP{
|
|
IP: ip,
|
|
Attempts: 1,
|
|
ExpiredAt: time.Now().Unix() + int64(settings.AuthSettings.BanThresholdMinutes*60),
|
|
})
|
|
}
|
|
_, _ = b.Where(b.IP.Eq(ip)).UpdateSimple(b.Attempts.Add(1))
|
|
}
|