mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 04:30:08 +00:00
test: 增加MinReportTraffic最低流量上报阈值
This commit is contained in:
@@ -27,13 +27,6 @@ func (h *HookServer) ModeList() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewHookServer() *HookServer {
|
||||
server := &HookServer{
|
||||
counter: sync.Map{},
|
||||
}
|
||||
return server
|
||||
}
|
||||
|
||||
func (h *HookServer) RoutedConnection(_ context.Context, conn net.Conn, m adapter.InboundContext, _ adapter.Rule, _ adapter.Outbound) net.Conn {
|
||||
l, err := limiter.GetLimiter(m.Inbound)
|
||||
if err != nil {
|
||||
|
||||
@@ -394,6 +394,7 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
|
||||
}
|
||||
|
||||
func (b *Sing) AddNode(tag string, info *panel.NodeInfo, config *conf.Options) error {
|
||||
b.nodeReportMinTrafficBytes[tag] = config.ReportMinTraffic * 1024
|
||||
c, err := getInboundOptions(tag, info, config)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/sagernet/sing-box/include"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
@@ -24,11 +25,18 @@ type DNSConfig struct {
|
||||
}
|
||||
|
||||
type Sing struct {
|
||||
box *box.Box
|
||||
ctx context.Context
|
||||
hookServer *HookServer
|
||||
router adapter.Router
|
||||
logFactory log.Factory
|
||||
box *box.Box
|
||||
ctx context.Context
|
||||
hookServer *HookServer
|
||||
router adapter.Router
|
||||
logFactory log.Factory
|
||||
users *UserMap
|
||||
nodeReportMinTrafficBytes map[string]int64
|
||||
}
|
||||
|
||||
type UserMap struct {
|
||||
uidMap map[string]int
|
||||
mapLock sync.RWMutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -71,7 +79,9 @@ func New(c *conf.CoreConfig) (vCore.Core, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hs := NewHookServer()
|
||||
hs := &HookServer{
|
||||
counter: sync.Map{},
|
||||
}
|
||||
b.Router().AppendTracker(hs)
|
||||
return &Sing{
|
||||
ctx: b.Router().GetCtx(),
|
||||
@@ -79,6 +89,10 @@ func New(c *conf.CoreConfig) (vCore.Core, error) {
|
||||
hookServer: hs,
|
||||
router: b.Router(),
|
||||
logFactory: b.LogFactory(),
|
||||
users: &UserMap{
|
||||
uidMap: make(map[string]int),
|
||||
},
|
||||
nodeReportMinTrafficBytes: make(map[string]int64),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,11 @@ func (b *Sing) AddUsers(p *core.AddUsersParams) (added int, err error) {
|
||||
if !found {
|
||||
return 0, errors.New("the inbound not found")
|
||||
}
|
||||
b.users.mapLock.Lock()
|
||||
defer b.users.mapLock.Unlock()
|
||||
for i := range p.Users {
|
||||
b.users.uidMap[p.Users[i].Uuid] = p.Users[i].Id
|
||||
}
|
||||
switch p.NodeInfo.Type {
|
||||
case "vless":
|
||||
us := make([]option.VLESSUser, len(p.Users))
|
||||
@@ -129,6 +134,39 @@ func (b *Sing) GetUserTraffic(tag, uuid string, reset bool) (up int64, down int6
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
func (b *Sing) GetUserTrafficSlice(tag string, reset bool) ([]panel.UserTraffic, error) {
|
||||
trafficSlice := make([]panel.UserTraffic, 0)
|
||||
hook := b.hookServer
|
||||
b.users.mapLock.RLock()
|
||||
defer b.users.mapLock.RUnlock()
|
||||
if v, ok := hook.counter.Load(tag); ok {
|
||||
c := v.(*counter.TrafficCounter)
|
||||
c.Counters.Range(func(key, value interface{}) bool {
|
||||
uuid := key.(string)
|
||||
traffic := value.(*counter.TrafficStorage)
|
||||
up := traffic.UpCounter.Load()
|
||||
down := traffic.DownCounter.Load()
|
||||
if up+down >= b.nodeReportMinTrafficBytes[tag] {
|
||||
if reset {
|
||||
traffic.UpCounter.Store(0)
|
||||
traffic.DownCounter.Store(0)
|
||||
}
|
||||
trafficSlice = append(trafficSlice, panel.UserTraffic{
|
||||
UID: b.users.uidMap[uuid],
|
||||
Upload: up,
|
||||
Download: down,
|
||||
})
|
||||
}
|
||||
return true
|
||||
})
|
||||
if len(trafficSlice) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return trafficSlice, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type UserDeleter interface {
|
||||
DelUsers(uuid []string) error
|
||||
}
|
||||
@@ -158,7 +196,10 @@ func (b *Sing) DelUsers(users []panel.UserInfo, tag string, info *panel.NodeInfo
|
||||
return errors.New("the inbound not found")
|
||||
}
|
||||
uuids := make([]string, len(users))
|
||||
b.users.mapLock.Lock()
|
||||
defer b.users.mapLock.Unlock()
|
||||
for i := range users {
|
||||
delete(b.users.uidMap, users[i].Uuid)
|
||||
uuids[i] = users[i].Uuid
|
||||
}
|
||||
err := del.DelUsers(uuids)
|
||||
|
||||
Reference in New Issue
Block a user