| 36 | } |
| 37 | |
| 38 | func (rl *RegLimit) CheckIPIsAllowed(ip string) (allowHour, allowDaily bool) { |
| 39 | now := time.Now() |
| 40 | o := orm.NewOrm() |
| 41 | if rl.HourRegNum > 0 { |
| 42 | hourBefore := now.Add(-1 * time.Hour) |
| 43 | cnt, _ := o.QueryTable(rl).Filter("ip", ip).Filter("created_at__gt", hourBefore).Filter("created_at__lt", now).Count() |
| 44 | if int(cnt) >= rl.HourRegNum { |
| 45 | return false, true |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | DayBefore := now.Add(-24 * time.Hour) |
| 50 | if rl.DailyRegNum > 0 { |
| 51 | cnt, _ := o.QueryTable(rl).Filter("ip", ip).Filter("created_at__gt", DayBefore).Filter("created_at__lt", now).Count() |
| 52 | if int(cnt) >= rl.DailyRegNum { |
| 53 | return true, false |
| 54 | } |
| 55 | } |
| 56 | o.QueryTable(rl).Filter("created_at__lt", DayBefore).Delete() |
| 57 | return true, true |
| 58 | } |
| 59 | |
| 60 | func (rl *RegLimit) Insert(ip string) (err error) { |
| 61 | rl.Ip = ip |