检查是否在最近添加过
(ip string)
| 16 | |
| 17 | // 检查是否在最近添加过 |
| 18 | func (this *BaseFirewall) checkLatestIP(ip string) bool { |
| 19 | this.locker.Lock() |
| 20 | defer this.locker.Unlock() |
| 21 | |
| 22 | var expiredIndex = -1 |
| 23 | for index, ipTime := range this.latestIPTimes { |
| 24 | var pieces = strings.Split(ipTime, "@") |
| 25 | var oldIP = pieces[0] |
| 26 | var oldTimestamp = pieces[1] |
| 27 | if types.Int64(oldTimestamp) < time.Now().Unix()-3 /** 3秒外表示过期 **/ { |
| 28 | expiredIndex = index |
| 29 | continue |
| 30 | } |
| 31 | if oldIP == ip { |
| 32 | return true |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | if expiredIndex > -1 { |
| 37 | this.latestIPTimes = this.latestIPTimes[expiredIndex+1:] |
| 38 | } |
| 39 | |
| 40 | this.latestIPTimes = append(this.latestIPTimes, ip+"@"+types.String(time.Now().Unix())) |
| 41 | const maxLen = 128 |
| 42 | if len(this.latestIPTimes) > maxLen { |
| 43 | this.latestIPTimes = this.latestIPTimes[1:] |
| 44 | } |
| 45 | |
| 46 | return false |
| 47 | } |
no test coverage detected