-
Notifications
You must be signed in to change notification settings - Fork 0
/
risk.go
56 lines (43 loc) · 1.24 KB
/
risk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2016 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author:polaris polaris@studygolang.com
package logic
import (
. "github.com/ue4-community/learnue/db"
"github.com/ue4-community/learnue/model"
"github.com/ue4-community/learnue/modules/nosql"
)
type RiskLogic struct{}
var DefaultRisk = RiskLogic{}
// AddBlackIP 加入 IP 黑名单
func (RiskLogic) AddBlackIP(ip string) error {
redisClient := nosql.NewRedisClient()
defer redisClient.Close()
key := "black:ip"
return redisClient.HSET(key, ip, "1")
}
// AddBlackIPByUID 通过用户 UID 将最后一次登录 IP 加入黑名单
func (self RiskLogic) AddBlackIPByUID(uid int) error {
userLogin := &model.UserLogin{}
_, err := MasterDB.Where("uid=?", uid).Get(userLogin)
if err != nil {
return err
}
if userLogin.LoginIp != "" {
return self.AddBlackIP(userLogin.LoginIp)
}
return nil
}
// IsBlackIP 是否是 IP 黑名单
func (RiskLogic) IsBlackIP(ip string) bool {
redisClient := nosql.NewRedisClient()
defer redisClient.Close()
key := "black:ip"
val, err := redisClient.HGET(key, ip)
if err != nil {
return false
}
return val == "1"
}