mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-23 15:10:25 +08:00
优化代码
This commit is contained in:
@@ -135,7 +135,7 @@ func (this *ClientConn) checkSYNFlood() {
|
|||||||
|
|
||||||
var ip = this.RawIP()
|
var ip = this.RawIP()
|
||||||
if len(ip) > 0 && !iplibrary.IsInWhiteList(ip) && (!synFloodConfig.IgnoreLocal || !utils.IsLocalIP(ip)) {
|
if len(ip) > 0 && !iplibrary.IsInWhiteList(ip) && (!synFloodConfig.IgnoreLocal || !utils.IsLocalIP(ip)) {
|
||||||
var timestamp = (utils.UnixTime()/60)*60 + 60
|
var timestamp = utils.NextMinuteUnixTime()
|
||||||
var result = ttlcache.SharedCache.IncreaseInt64("SYN_FLOOD:"+ip, 1, timestamp)
|
var result = ttlcache.SharedCache.IncreaseInt64("SYN_FLOOD:"+ip, 1, timestamp)
|
||||||
var minAttempts = synFloodConfig.MinAttempts
|
var minAttempts = synFloodConfig.MinAttempts
|
||||||
if minAttempts < 3 {
|
if minAttempts < 3 {
|
||||||
|
|||||||
@@ -100,8 +100,7 @@ func (this *TrafficStatManager) Add(serverId int64, domain string, bytes int64,
|
|||||||
|
|
||||||
this.totalRequests++
|
this.totalRequests++
|
||||||
|
|
||||||
timestamp := utils.UnixTime() / 300 * 300
|
timestamp := utils.FloorUnixTime(300)
|
||||||
|
|
||||||
key := strconv.FormatInt(timestamp, 10) + strconv.FormatInt(serverId, 10)
|
key := strconv.FormatInt(timestamp, 10) + strconv.FormatInt(serverId, 10)
|
||||||
this.locker.Lock()
|
this.locker.Lock()
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,21 @@ func UnixTime() int64 {
|
|||||||
return unixTime
|
return unixTime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FloorUnixTime 取整
|
||||||
|
func FloorUnixTime(seconds int) int64 {
|
||||||
|
return UnixTime() / int64(seconds) * int64(seconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CeilUnixTime 取整并加1
|
||||||
|
func CeilUnixTime(seconds int) int64 {
|
||||||
|
return UnixTime()/int64(seconds)*int64(seconds) + int64(seconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NextMinuteUnixTime 获取下一分钟开始的时间戳
|
||||||
|
func NextMinuteUnixTime() int64 {
|
||||||
|
return CeilUnixTime(60)
|
||||||
|
}
|
||||||
|
|
||||||
// UnixTimeMilli 获取时间戳,精确到毫秒
|
// UnixTimeMilli 获取时间戳,精确到毫秒
|
||||||
func UnixTimeMilli() int64 {
|
func UnixTimeMilli() int64 {
|
||||||
return unixTimeMilli
|
return unixTimeMilli
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -19,3 +20,11 @@ func TestGMTUnixTime(t *testing.T) {
|
|||||||
func TestGMTTime(t *testing.T) {
|
func TestGMTTime(t *testing.T) {
|
||||||
t.Log(GMTTime(time.Now()))
|
t.Log(GMTTime(time.Now()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFloorUnixTime(t *testing.T) {
|
||||||
|
var timestamp = time.Now().Unix()
|
||||||
|
t.Log("floor 60:", timestamp, FloorUnixTime(60), timeutil.FormatTime("Y-m-d H:i:s", FloorUnixTime(60)))
|
||||||
|
t.Log("ceil 60:", timestamp, CeilUnixTime(60), timeutil.FormatTime("Y-m-d H:i:s", CeilUnixTime(60)))
|
||||||
|
t.Log("floor 300:", timestamp, FloorUnixTime(300), timeutil.FormatTime("Y-m-d H:i:s", FloorUnixTime(300)))
|
||||||
|
t.Log("next minute:", NextMinuteUnixTime())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user