测试:尝试增加hysteria2协议,尝试增加设备数限制功能

This commit is contained in:
wyx2685
2023-11-18 07:05:28 +09:00
parent f928b4f8f3
commit cf8016f405
18 changed files with 380 additions and 323 deletions

View File

@@ -35,9 +35,11 @@ type Limiter struct {
DomainRules []*regexp.Regexp
ProtocolRules []string
SpeedLimit int
UserLimitInfo *sync.Map // Key: Uid value: UserLimitInfo
ConnLimiter *ConnLimiter // Key: Uid value: ConnLimiter
SpeedLimiter *sync.Map // key: Uid, value: *ratelimit.Bucket
UserOnlineIP *sync.Map // Key: Name, value: {Key: Ip, value: Uid}
UUIDtoUID map[string]int // Key: UUID, value: UID
UserLimitInfo *sync.Map // Key: Uid value: UserLimitInfo
ConnLimiter *ConnLimiter // Key: Uid value: ConnLimiter
SpeedLimiter *sync.Map // key: Uid, value: *ratelimit.Bucket
}
type UserLimitInfo struct {
@@ -50,11 +52,14 @@ type UserLimitInfo struct {
func AddLimiter(tag string, l *conf.LimitConfig, users []panel.UserInfo) *Limiter {
info := &Limiter{
SpeedLimit: l.SpeedLimit,
UserOnlineIP: new(sync.Map),
UserLimitInfo: new(sync.Map),
ConnLimiter: NewConnLimiter(l.ConnLimit, l.IPLimit, l.EnableRealtime),
SpeedLimiter: new(sync.Map),
}
uuidmap := make(map[string]int)
for i := range users {
uuidmap[users[i].Uuid] = users[i].Id
if users[i].SpeedLimit != 0 {
userLimit := &UserLimitInfo{
UID: users[i].Id,
@@ -63,6 +68,7 @@ func AddLimiter(tag string, l *conf.LimitConfig, users []panel.UserInfo) *Limite
info.UserLimitInfo.Store(format.UserTag(tag, users[i].Uuid), userLimit)
}
}
info.UUIDtoUID = uuidmap
limitLock.Lock()
limiter[tag] = info
limitLock.Unlock()
@@ -76,7 +82,7 @@ func GetLimiter(tag string) (info *Limiter, err error) {
if !ok {
return nil, errors.New("not found")
}
return
return info, nil
}
func DeleteLimiter(tag string) {
@@ -88,6 +94,7 @@ func DeleteLimiter(tag string) {
func (l *Limiter) UpdateUser(tag string, added []panel.UserInfo, deleted []panel.UserInfo) {
for i := range deleted {
l.UserLimitInfo.Delete(format.UserTag(tag, deleted[i].Uuid))
delete(l.UUIDtoUID, deleted[i].Uuid)
}
for i := range added {
if added[i].SpeedLimit != 0 {
@@ -98,6 +105,7 @@ func (l *Limiter) UpdateUser(tag string, added []panel.UserInfo, deleted []panel
}
l.UserLimitInfo.Store(format.UserTag(tag, added[i].Uuid), userLimit)
}
l.UUIDtoUID[added[i].Uuid] = added[i].Id
}
}
@@ -134,6 +142,24 @@ func (l *Limiter) CheckLimit(email string, ip string, isTcp bool) (Bucket *ratel
userLimit = determineSpeedLimit(u.SpeedLimit, u.DynamicSpeedLimit)
}
}
// Store online user for device limit
ipMap := new(sync.Map)
uid := l.UUIDtoUID[email]
ipMap.Store(ip, uid)
// If any device is online
if v, ok := l.UserOnlineIP.LoadOrStore(email, ipMap); ok {
ipMap := v.(*sync.Map)
// If this is a new ip
if _, ok := ipMap.LoadOrStore(ip, uid); !ok {
counter := 0
ipMap.Range(func(key, value interface{}) bool {
counter++
return true
})
}
}
limit := int64(determineSpeedLimit(nodeLimit, userLimit)) * 1000000 / 8 // If you need the Speed limit
if limit > 0 {
Bucket = ratelimit.NewBucketWithQuantum(time.Second, limit, limit) // Byte/s
@@ -148,6 +174,25 @@ func (l *Limiter) CheckLimit(email string, ip string, isTcp bool) (Bucket *ratel
}
}
func (l *Limiter) GetOnlineDevice() (*[]panel.OnlineUser, error) {
var onlineUser []panel.OnlineUser
l.UserOnlineIP.Range(func(key, value interface{}) bool {
email := key.(string)
ipMap := value.(*sync.Map)
ipMap.Range(func(key, value interface{}) bool {
uid := value.(int)
ip := key.(string)
onlineUser = append(onlineUser, panel.OnlineUser{UID: uid, IP: ip})
return true
})
l.UserOnlineIP.Delete(email) // Reset online device
return true
})
return &onlineUser, nil
}
type UserIpList struct {
Uid int `json:"Uid"`
IpList []string `json:"Ips"`