V2bX/node/user.go

111 lines
2.9 KiB
Go

package node
import (
"fmt"
"strconv"
"github.com/InazumaV/V2bX/api/panel"
log "github.com/sirupsen/logrus"
)
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 {
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
}
}
userTraffic = append(userTraffic, panel.UserTraffic{
UID: (c.userList)[i].Id,
Upload: up,
Download: down})
}
}
if len(userTraffic) > 0 {
err = c.apiClient.ReportUserTraffic(userTraffic)
if err != nil {
log.WithFields(log.Fields{
"tag": c.tag,
"err": err,
}).Info("Report user traffic failed")
} else {
log.WithField("tag", c.tag).Infof("Report %d users traffic", len(userTraffic))
}
}
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)
}
}
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], fmt.Sprintf("%s_%d", onlineuser.IP, c.info.Id))
if _, ok := reportOnline[onlineuser.UID]; ok {
reportOnline[onlineuser.UID]++
} else {
reportOnline[onlineuser.UID] = 1
}
}
if err = c.apiClient.ReportNodeOnlineUsers(&data, &reportOnline); 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))
}
}
userTraffic = nil
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
}