mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 20:50:09 +00:00
update sing-box core
This commit is contained in:
@@ -5,11 +5,9 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/InazumaV/V2bX/common/format"
|
||||
"github.com/InazumaV/V2bX/common/rate"
|
||||
"github.com/InazumaV/V2bX/common/task"
|
||||
|
||||
"github.com/InazumaV/V2bX/limiter"
|
||||
|
||||
@@ -21,15 +19,8 @@ import (
|
||||
|
||||
var _ adapter.ConnectionTracker = (*HookServer)(nil)
|
||||
|
||||
type ConnEntry struct {
|
||||
Conn net.Conn
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
type HookServer struct {
|
||||
counter sync.Map //map[string]*counter.TrafficCounter
|
||||
userconn sync.Map //map[string][]*ConnEntry
|
||||
Cleanup *task.Task
|
||||
counter sync.Map //map[string]*counter.TrafficCounter
|
||||
}
|
||||
|
||||
func (h *HookServer) ModeList() []string {
|
||||
@@ -38,12 +29,7 @@ func (h *HookServer) ModeList() []string {
|
||||
|
||||
func NewHookServer() *HookServer {
|
||||
server := &HookServer{
|
||||
counter: sync.Map{},
|
||||
userconn: sync.Map{},
|
||||
}
|
||||
server.Cleanup = &task.Task{
|
||||
Interval: 5 * time.Minute,
|
||||
Execute: server.CleanupOldConnections,
|
||||
counter: sync.Map{},
|
||||
}
|
||||
return server
|
||||
}
|
||||
@@ -92,23 +78,7 @@ func (h *HookServer) RoutedConnection(_ context.Context, conn net.Conn, m adapte
|
||||
} else {
|
||||
t = c.(*counter.TrafficCounter)
|
||||
}
|
||||
|
||||
conn = counter.NewConnCounter(conn, t.GetCounter(m.User))
|
||||
entry := &ConnEntry{
|
||||
Conn: conn,
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
if conns, exist := h.userconn.Load(taguuid); exist {
|
||||
if connList, ok := conns.([]*ConnEntry); ok {
|
||||
h.userconn.Store(taguuid, append(connList, entry))
|
||||
} else {
|
||||
h.userconn.Delete(taguuid)
|
||||
h.userconn.Store(taguuid, []*ConnEntry{entry})
|
||||
}
|
||||
} else {
|
||||
h.userconn.Store(taguuid, []*ConnEntry{entry})
|
||||
}
|
||||
|
||||
return conn
|
||||
}
|
||||
|
||||
@@ -159,53 +129,3 @@ func (h *HookServer) RoutedPacketConnection(_ context.Context, conn N.PacketConn
|
||||
conn = counter.NewPacketConnCounter(conn, t.GetCounter(m.User))
|
||||
return conn
|
||||
}
|
||||
|
||||
func (h *HookServer) CloseConnections(tag string, uuids []string) error {
|
||||
for _, uuid := range uuids {
|
||||
taguuid := format.UserTag(tag, uuid)
|
||||
v, ok := h.userconn.Load(taguuid)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
connList, ok := v.([]*ConnEntry)
|
||||
if !ok {
|
||||
h.userconn.Delete(taguuid)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, entry := range connList {
|
||||
err := entry.Conn.Close()
|
||||
if err != nil {
|
||||
log.Error("close conn error: ", err)
|
||||
}
|
||||
}
|
||||
h.userconn.Delete(taguuid)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *HookServer) CleanupOldConnections() error {
|
||||
expiredTime := time.Now().Add(-time.Minute * 30)
|
||||
h.userconn.Range(func(key, value interface{}) bool {
|
||||
connList, ok := value.([]*ConnEntry)
|
||||
if !ok {
|
||||
h.userconn.Delete(key)
|
||||
return true
|
||||
}
|
||||
|
||||
var activeConns []*ConnEntry
|
||||
for _, entry := range connList {
|
||||
if entry.Timestamp.After(expiredTime) {
|
||||
activeConns = append(activeConns, entry)
|
||||
}
|
||||
}
|
||||
|
||||
if len(activeConns) == 0 {
|
||||
h.userconn.Delete(key)
|
||||
} else {
|
||||
h.userconn.Store(key, activeConns)
|
||||
}
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -83,7 +83,6 @@ func New(c *conf.CoreConfig) (vCore.Core, error) {
|
||||
}
|
||||
|
||||
func (b *Sing) Start() error {
|
||||
b.hookServer.Cleanup.Start(false)
|
||||
return b.box.Start()
|
||||
}
|
||||
|
||||
|
||||
@@ -138,9 +138,5 @@ func (b *Sing) DelUsers(users []panel.UserInfo, tag string, info *panel.NodeInfo
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = b.hookServer.CloseConnections(tag, uuids)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user