forked from TruthHun/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreg_limit.go
67 lines (59 loc) · 1.69 KB
/
reg_limit.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
57
58
59
60
61
62
63
64
65
66
67
package models
import (
"strconv"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
)
type RegLimit struct {
Id int
Ip string `orm:"size(15);index"`
CreatedAt time.Time `orm:"index"`
DailyRegNum int `orm:"-"`
HourRegNum int `orm:"-"`
RealIPField string `orm:"-"`
}
func NewRegLimit() (rl *RegLimit) {
rl = &RegLimit{}
var options []Option
orm.NewOrm().QueryTable(NewOption()).Filter("option_name__in", "REAL_IP_FIELD", "HOUR_REG_NUM", "DAILY_REG_NUM").All(&options, "option_name", "option_value")
for _, item := range options {
switch item.OptionName {
case "REAL_IP_FIELD":
rl.RealIPField = item.OptionValue
case "HOUR_REG_NUM":
rl.HourRegNum, _ = strconv.Atoi(item.OptionValue)
case "DAILY_REG_NUM":
rl.DailyRegNum, _ = strconv.Atoi(item.OptionValue)
}
}
return rl
}
func (rl *RegLimit) CheckIPIsAllowed(ip string) (allowHour, allowDaily bool) {
now := time.Now()
o := orm.NewOrm()
if rl.HourRegNum > 0 {
hourBefore := now.Add(-1 * time.Hour)
cnt, _ := o.QueryTable(rl).Filter("ip", ip).Filter("created_at__gt", hourBefore).Filter("created_at__lt", now).Count()
if int(cnt) >= rl.HourRegNum {
return false, true
}
}
DayBefore := now.Add(-24 * time.Hour)
if rl.DailyRegNum > 0 {
cnt, _ := o.QueryTable(rl).Filter("ip", ip).Filter("created_at__gt", DayBefore).Filter("created_at__lt", now).Count()
if int(cnt) >= rl.DailyRegNum {
return true, false
}
}
o.QueryTable(rl).Filter("created_at__lt", DayBefore).Delete()
return true, true
}
func (rl *RegLimit) Insert(ip string) (err error) {
rl.Ip = ip
rl.CreatedAt = time.Now()
if _, err = orm.NewOrm().Insert(rl); err != nil {
beego.Error(err)
}
return
}