V2bX/node/user.go

68 lines
1.6 KiB
Go
Raw Normal View History

package node
import (
"github.com/Yuzuki616/V2bX/api/panel"
2023-06-29 23:07:27 -04:00
log "github.com/sirupsen/logrus"
2023-06-15 22:04:39 -04:00
"runtime"
"strconv"
)
2023-06-15 22:04:39 -04:00
func (c *Controller) reportUserTrafficTask() (err error) {
// Get User traffic
userTraffic := make([]panel.UserTraffic, 0)
for i := range c.userList {
up, down := c.server.GetUserTraffic(c.Tag, c.userList[i].Uuid, true)
if up > 0 || down > 0 {
if c.LimitConfig.EnableDynamicSpeedLimit {
c.userList[i].Traffic += up + down
}
userTraffic = append(userTraffic, panel.UserTraffic{
UID: (c.userList)[i].Id,
Upload: up,
Download: down})
}
}
2023-06-18 21:33:20 -04:00
if len(userTraffic) > 0 {
2023-06-15 22:04:39 -04:00
err = c.apiClient.ReportUserTraffic(userTraffic)
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
"tag": c.Tag,
"err": err,
2023-07-12 13:19:13 -04:00
}).Info("Report user traffic failed")
2023-06-15 22:04:39 -04:00
} else {
2023-07-12 13:19:13 -04:00
log.WithField("tag", c.Tag).Infof("Report %d online users", len(userTraffic))
2023-06-15 22:04:39 -04:00
}
}
userTraffic = nil
runtime.GC()
return nil
}
func compareUserList(old, new []panel.UserInfo) (deleted, added []panel.UserInfo) {
tmp := map[string]struct{}{}
tmp2 := map[string]struct{}{}
for i := range old {
tmp[old[i].Uuid+strconv.Itoa(old[i].SpeedLimit)] = struct{}{}
}
l := len(tmp)
for i := range new {
e := new[i].Uuid + strconv.Itoa(new[i].SpeedLimit)
tmp[e] = struct{}{}
tmp2[e] = struct{}{}
if l != len(tmp) {
added = append(added, new[i])
l++
}
}
tmp = nil
l = len(tmp2)
for i := range old {
tmp2[old[i].Uuid+strconv.Itoa(old[i].SpeedLimit)] = struct{}{}
if l != len(tmp2) {
deleted = append(deleted, old[i])
l++
}
}
return deleted, added
}