Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions conf/limit.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package conf

type LimitConfig struct {
EnableRealtime bool `json:"EnableRealtime"`
SpeedLimit int `json:"SpeedLimit"`
IPLimit int `json:"DeviceLimit"`
ConnLimit int `json:"ConnLimit"`
EnableIpRecorder bool `json:"EnableIpRecorder"`
IpRecorderConfig *IpReportConfig `json:"IpRecorderConfig"`
EnableDynamicSpeedLimit bool `json:"EnableDynamicSpeedLimit"`
DynamicSpeedLimitConfig *DynamicSpeedLimitConfig `json:"DynamicSpeedLimitConfig"`
EnableRealtime bool `json:"EnableRealtime"`
SpeedLimit int `json:"SpeedLimit"`
IPLimit int `json:"DeviceLimit"`
ConnLimit int `json:"ConnLimit"`
EnableIpRecorder bool `json:"EnableIpRecorder"`
IpRecorderConfig *IpReportConfig `json:"IpRecorderConfig"`
}

type RecorderConfig struct {
Expand All @@ -31,10 +29,3 @@ type IpReportConfig struct {
RedisConfig *RedisConfig `json:"RedisConfig"`
EnableIpSync bool `json:"EnableIpSync"`
}

type DynamicSpeedLimitConfig struct {
Periodic int `json:"Periodic"`
Traffic int64 `json:"Traffic"`
SpeedLimit int `json:"SpeedLimit"`
ExpireTime int `json:"ExpireTime"`
}
38 changes: 0 additions & 38 deletions limiter/dynamic.go

This file was deleted.

35 changes: 5 additions & 30 deletions limiter/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ type Limiter struct {
}

type UserLimitInfo struct {
UID int
SpeedLimit int
DeviceLimit int
DynamicSpeedLimit int
ExpireTime int64
OverLimit bool
UID int
SpeedLimit int
DeviceLimit int
OverLimit bool
}

func AddLimiter(nodetype string, tag string, l *conf.LimitConfig, users []panel.UserInfo, aliveList map[int]int) *Limiter {
Expand Down Expand Up @@ -103,7 +101,6 @@ func (l *Limiter) UpdateUser(tag string, added, deleted, modified []panel.UserIn
}
if added[i].SpeedLimit != 0 {
userLimit.SpeedLimit = added[i].SpeedLimit
userLimit.ExpireTime = 0
}
if added[i].DeviceLimit != 0 {
userLimit.DeviceLimit = added[i].DeviceLimit
Expand All @@ -128,7 +125,6 @@ func (l *Limiter) UpdateUser(tag string, added, deleted, modified []panel.UserIn
}
if modified[i].SpeedLimit != 0 {
userLimit.SpeedLimit = modified[i].SpeedLimit
userLimit.ExpireTime = 0
}
if modified[i].DeviceLimit != 0 {
userLimit.DeviceLimit = modified[i].DeviceLimit
Expand All @@ -140,17 +136,6 @@ func (l *Limiter) UpdateUser(tag string, added, deleted, modified []panel.UserIn
}
}

func (l *Limiter) UpdateDynamicSpeedLimit(tag, uuid string, limit int, expire time.Time) error {
if v, ok := l.UserLimitInfo.Load(format.UserTag(tag, uuid)); ok {
info := v.(*UserLimitInfo)
info.DynamicSpeedLimit = limit
info.ExpireTime = expire.Unix()
} else {
return errors.New("not found")
}
return nil
}

func (l *Limiter) CheckLimit(taguuid string, ip string, isTcp bool, noSSUDP bool) (Bucket *ratelimit.Bucket, Reject bool) {
// check if ipv4 mapped ipv6
ip = strings.TrimPrefix(ip, "::ffff:")
Expand All @@ -164,17 +149,7 @@ func (l *Limiter) CheckLimit(taguuid string, ip string, isTcp bool, noSSUDP bool
u := v.(*UserLimitInfo)
deviceLimit = u.DeviceLimit
uid = u.UID
if u.ExpireTime < time.Now().Unix() && u.ExpireTime != 0 {
if u.SpeedLimit != 0 {
userLimit = u.SpeedLimit
u.DynamicSpeedLimit = 0
u.ExpireTime = 0
} else {
l.UserLimitInfo.Delete(taguuid)
}
} else {
userLimit = determineSpeedLimit(u.SpeedLimit, u.DynamicSpeedLimit)
}
userLimit = u.SpeedLimit
} else {
return nil, true
}
Expand Down
22 changes: 22 additions & 0 deletions limiter/speed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package limiter

// determineSpeedLimit returns the minimum non-zero rate.
func determineSpeedLimit(limit1, limit2 int) (limit int) {
if limit1 == 0 || limit2 == 0 {
if limit1 > limit2 {
return limit1
} else if limit1 < limit2 {
return limit2
} else {
return 0
}
} else {
if limit1 > limit2 {
return limit2
} else if limit1 < limit2 {
return limit1
} else {
return limit1
}
}
}
27 changes: 11 additions & 16 deletions node/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ import (
)

type Controller struct {
server vCore.Core
apiClient *panel.Client
tag string
limiter *limiter.Limiter
traffic map[string]int64
userList []panel.UserInfo
aliveMap map[int]int
info *panel.NodeInfo
nodeInfoMonitorPeriodic *task.Task
userReportPeriodic *task.Task
renewCertPeriodic *task.Task
dynamicSpeedLimitPeriodic *task.Task
onlineIpReportPeriodic *task.Task
server vCore.Core
apiClient *panel.Client
tag string
limiter *limiter.Limiter
userList []panel.UserInfo
aliveMap map[int]int
info *panel.NodeInfo
nodeInfoMonitorPeriodic *task.Task
userReportPeriodic *task.Task
renewCertPeriodic *task.Task
onlineIpReportPeriodic *task.Task
*conf.Options
}

Expand Down Expand Up @@ -109,9 +107,6 @@ func (c *Controller) Close() error {
if c.renewCertPeriodic != nil {
c.renewCertPeriodic.Close()
}
if c.dynamicSpeedLimitPeriodic != nil {
c.dynamicSpeedLimitPeriodic.Close()
}
if c.onlineIpReportPeriodic != nil {
c.onlineIpReportPeriodic.Close()
}
Expand Down
28 changes: 0 additions & 28 deletions node/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ func (c *Controller) startTasks(node *panel.NodeInfo) {
_ = c.renewCertPeriodic.Start(true)
}
}
if c.LimitConfig.EnableDynamicSpeedLimit {
c.traffic = make(map[string]int64)
c.dynamicSpeedLimitPeriodic = &task.Task{
Interval: time.Duration(c.LimitConfig.DynamicSpeedLimitConfig.Periodic) * time.Second,
Execute: c.SpeedChecker,
}
log.Printf("[%s: %d] Start dynamic speed limit", c.apiClient.NodeType, c.apiClient.NodeId)
}
}

func (c *Controller) nodeInfoMonitor() (err error) {
Expand Down Expand Up @@ -83,7 +75,6 @@ func (c *Controller) nodeInfoMonitor() (err error) {
if newU != nil {
c.userList = newU
}
c.traffic = make(map[string]int64)
// Remove old node
log.WithField("tag", c.tag).Info("Node changed, reload")
err = c.server.DelNode(c.tag)
Expand Down Expand Up @@ -216,12 +207,6 @@ func (c *Controller) nodeInfoMonitor() (err error) {
}).Error("limiter users failed")
return nil
}
// clear traffic record
if c.LimitConfig.EnableDynamicSpeedLimit {
for i := range deleted {
delete(c.traffic, deleted[i].Uuid)
}
}
}
c.userList = newU
if len(added)+len(deleted)+len(modified) != 0 {
Expand All @@ -230,16 +215,3 @@ func (c *Controller) nodeInfoMonitor() (err error) {
}
return nil
}

func (c *Controller) SpeedChecker() error {
for u, t := range c.traffic {
if t >= c.LimitConfig.DynamicSpeedLimitConfig.Traffic {
err := c.limiter.UpdateDynamicSpeedLimit(c.tag, u,
c.LimitConfig.DynamicSpeedLimitConfig.SpeedLimit,
time.Now().Add(time.Duration(c.LimitConfig.DynamicSpeedLimitConfig.ExpireTime)*time.Minute))
log.WithField("err", err).Error("Update dynamic speed limit failed")
delete(c.traffic, u)
}
}
return nil
}
Loading