From 8d7168c6a43d53183e7368708163d214c08d2005 Mon Sep 17 00:00:00 2001 From: wyx2685 Date: Sat, 31 Aug 2024 16:02:50 +0900 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E4=BF=AE=E5=A4=8D=E5=9C=A8?= =?UTF-8?q?=E7=BA=BF=E8=AE=BE=E5=A4=87=E6=95=B0=E4=B8=8A=E6=8A=A5=E5=90=8E?= =?UTF-8?q?=E6=9C=89=E6=A6=82=E7=8E=87=E8=A2=AB=E9=99=90=E5=88=B6=E9=93=BE?= =?UTF-8?q?=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/panel/panel.go | 1 - api/panel/user.go | 3 +-- limiter/limiter.go | 13 ++++++++----- node/user.go | 14 ++------------ 4 files changed, 11 insertions(+), 20 deletions(-) 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,