V2bX/node/user.go

95 lines
2.5 KiB
Go
Raw Permalink Normal View History

package node
import (
"strconv"
2023-07-21 14:38:07 -04:00
2023-07-29 07:27:15 -04:00
"github.com/InazumaV/V2bX/api/panel"
2023-07-21 14:38:07 -04:00
log "github.com/sirupsen/logrus"
)
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 {
2023-07-21 14:38:07 -04:00
up, down := c.server.GetUserTraffic(c.tag, c.userList[i].Uuid, true)
2023-06-15 22:04:39 -04:00
if up > 0 || down > 0 {
if c.LimitConfig.EnableDynamicSpeedLimit {
c.traffic[c.userList[i].Uuid] += up + down
2023-06-15 22:04:39 -04:00
}
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{
2023-07-21 14:38:07 -04:00
"tag": c.tag,
2023-06-29 23:07:27 -04:00
"err": err,
2023-07-12 13:19:13 -04:00
}).Info("Report user traffic failed")
2023-06-15 22:04:39 -04:00
} else {
log.WithField("tag", c.tag).Infof("Report %d users traffic", len(userTraffic))
2023-06-15 22:04:39 -04:00
}
}
if onlineDevice, err := c.limiter.GetOnlineDevice(); err != nil {
log.Print(err)
} else if len(*onlineDevice) > 0 {
// Only report user has traffic > 100kb to allow ping test
var result []panel.OnlineUser
var nocountUID = make(map[int]struct{})
for _, traffic := range userTraffic {
total := traffic.Upload + traffic.Download
if total < int64(c.Options.DeviceOnlineMinTraffic*1000) {
nocountUID[traffic.UID] = struct{}{}
}
}
for _, online := range *onlineDevice {
if _, ok := nocountUID[online.UID]; !ok {
result = append(result, online)
}
}
data := make(map[int][]string)
for _, onlineuser := range result {
// json structure: { UID1:["ip1","ip2"],UID2:["ip3","ip4"] }
2024-07-16 20:21:57 -04:00
data[onlineuser.UID] = append(data[onlineuser.UID], onlineuser.IP)
}
if err = c.apiClient.ReportNodeOnlineUsers(&data); err != nil {
log.WithFields(log.Fields{
"tag": c.tag,
"err": err,
}).Info("Report online users failed")
} else {
log.WithField("tag", c.tag).Infof("Total %d online users, %d Reported", len(*onlineDevice), len(result))
}
}
2023-06-15 22:04:39 -04:00
userTraffic = nil
return nil
}
func compareUserList(old, new []panel.UserInfo) (deleted, added []panel.UserInfo) {
2024-01-26 11:55:43 -05:00
oldMap := make(map[string]int)
for i, user := range old {
key := user.Uuid + strconv.Itoa(user.SpeedLimit)
oldMap[key] = i
}
2024-01-26 11:55:43 -05:00
for _, user := range new {
key := user.Uuid + strconv.Itoa(user.SpeedLimit)
if _, exists := oldMap[key]; !exists {
added = append(added, user)
} else {
delete(oldMap, key)
}
}
2024-01-26 11:55:43 -05:00
for _, index := range oldMap {
deleted = append(deleted, old[index])
}
2024-01-26 11:55:43 -05:00
return deleted, added
}