Skip to content

Commit

Permalink
尝试修复在线设备数上报后有概率被限制链接
Browse files Browse the repository at this point in the history
  • Loading branch information
wyx2685 committed Aug 31, 2024
1 parent 173c48a commit 8d7168c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
1 change: 0 additions & 1 deletion api/panel/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Client struct {
nodeEtag string
userEtag string
responseBodyHash string
LastReportOnline map[int]int
UserList *UserListBody
AliveMap *AliveMap
}
Expand Down
3 changes: 1 addition & 2 deletions api/panel/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ func (c *Client) ReportUserTraffic(userTraffic []UserTraffic) error {
return nil
}

func (c *Client) ReportNodeOnlineUsers(data *map[int][]string, reportOnline *map[int]int) error {
c.LastReportOnline = *reportOnline
func (c *Client) ReportNodeOnlineUsers(data *map[int][]string) error {
const path = "/api/v1/server/UniProxy/alive"
r, err := c.client.R().
SetBody(data).
Expand Down
13 changes: 8 additions & 5 deletions limiter/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ type Limiter struct {
ProtocolRules []string
SpeedLimit int
UserOnlineIP *sync.Map // Key: Name, value: {Key: Ip, value: Uid}
UUIDtoUID map[string]int // Key: UUID, value: UID
OldUserOnline map[string]int // 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
AliveList map[int]int // Key:Id, value:alive_ip
AliveList map[int]int // Key: Uid, value: alive_ip
}

type UserLimitInfo struct {
Expand Down Expand Up @@ -178,6 +179,8 @@ func (l *Limiter) CheckLimit(taguuid string, ip string, isTcp bool, noSSUDP bool
}
}
}
} else if l.OldUserOnline[ip] == uid {
delete(l.OldUserOnline, ip)
} else {
if deviceLimit > 0 {
if deviceLimit <= aliveIp {
Expand All @@ -204,17 +207,17 @@ func (l *Limiter) CheckLimit(taguuid string, ip string, isTcp bool, noSSUDP bool

func (l *Limiter) GetOnlineDevice() (*[]panel.OnlineUser, error) {
var onlineUser []panel.OnlineUser

l.UserOnlineIP.Range(func(key, value interface{}) bool {
email := key.(string)
taguuid := key.(string)
ipMap := value.(*sync.Map)
ipMap.Range(func(key, value interface{}) bool {
uid := value.(int)
ip := key.(string)
l.OldUserOnline[ip] = uid
onlineUser = append(onlineUser, panel.OnlineUser{UID: uid, IP: ip})
return true
})
l.UserOnlineIP.Delete(email) // Reset online device
l.UserOnlineIP.Delete(taguuid) // Reset online device
return true
})

Expand Down
14 changes: 2 additions & 12 deletions node/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ func (c *Controller) reportUserTrafficTask() (err error) {
up, down := c.server.GetUserTraffic(c.tag, c.userList[i].Uuid, true)
if up > 0 || down > 0 {
if c.LimitConfig.EnableDynamicSpeedLimit {
if _, ok := c.traffic[c.userList[i].Uuid]; ok {
c.traffic[c.userList[i].Uuid] += up + down
} else {
c.traffic[c.userList[i].Uuid] = up + down
}
c.traffic[c.userList[i].Uuid] += up + down
}
userTraffic = append(userTraffic, panel.UserTraffic{
UID: (c.userList)[i].Id,
Expand Down Expand Up @@ -55,18 +51,12 @@ func (c *Controller) reportUserTrafficTask() (err error) {
result = append(result, online)
}
}
reportOnline := make(map[int]int)
data := make(map[int][]string)
for _, onlineuser := range result {
// json structure: { UID1:["ip1","ip2"],UID2:["ip3","ip4"] }
data[onlineuser.UID] = append(data[onlineuser.UID], onlineuser.IP)
if _, ok := reportOnline[onlineuser.UID]; ok {
reportOnline[onlineuser.UID]++
} else {
reportOnline[onlineuser.UID] = 1
}
}
if err = c.apiClient.ReportNodeOnlineUsers(&data, &reportOnline); err != nil {
if err = c.apiClient.ReportNodeOnlineUsers(&data); err != nil {
log.WithFields(log.Fields{
"tag": c.tag,
"err": err,
Expand Down

0 comments on commit 8d7168c

Please sign in to comment.