diff --git a/api/panel/panel.go b/api/panel/panel.go index 9a4a0e9..61a9d69 100644 --- a/api/panel/panel.go +++ b/api/panel/panel.go @@ -24,7 +24,6 @@ type Client struct { nodeEtag string userEtag string responseBodyHash string - LastReportOnline map[int]int UserList *UserListBody AliveMap *AliveMap } diff --git a/api/panel/user.go b/api/panel/user.go index 8bbf9e7..35304f9 100644 --- a/api/panel/user.go +++ b/api/panel/user.go @@ -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). diff --git a/limiter/limiter.go b/limiter/limiter.go index 47479af..cd8b7f1 100644 --- a/limiter/limiter.go +++ b/limiter/limiter.go @@ -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 { @@ -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 { @@ -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 }) diff --git a/node/user.go b/node/user.go index 8466931..7e7f76d 100644 --- a/node/user.go +++ b/node/user.go @@ -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, @@ -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,