diff --git a/experimental/clashapi/trafficontrol/manager.go b/experimental/clashapi/trafficontrol/manager.go index 69298788..c1472f34 100644 --- a/experimental/clashapi/trafficontrol/manager.go +++ b/experimental/clashapi/trafficontrol/manager.go @@ -4,32 +4,26 @@ import ( "time" "github.com/sagernet/sing-box/experimental/clashapi/compatible" - - "go.uber.org/atomic" + "github.com/sagernet/sing/common/atomic" ) type Manager struct { - connections compatible.Map[string, tracker] - uploadTemp *atomic.Int64 - downloadTemp *atomic.Int64 - uploadBlip *atomic.Int64 - downloadBlip *atomic.Int64 - uploadTotal *atomic.Int64 - downloadTotal *atomic.Int64 - ticker *time.Ticker - done chan struct{} + uploadTemp atomic.Int64 + downloadTemp atomic.Int64 + uploadBlip atomic.Int64 + downloadBlip atomic.Int64 + uploadTotal atomic.Int64 + downloadTotal atomic.Int64 + + connections compatible.Map[string, tracker] + ticker *time.Ticker + done chan struct{} } func NewManager() *Manager { manager := &Manager{ - uploadTemp: atomic.NewInt64(0), - downloadTemp: atomic.NewInt64(0), - uploadBlip: atomic.NewInt64(0), - downloadBlip: atomic.NewInt64(0), - uploadTotal: atomic.NewInt64(0), - downloadTotal: atomic.NewInt64(0), - ticker: time.NewTicker(time.Second), - done: make(chan struct{}), + ticker: time.NewTicker(time.Second), + done: make(chan struct{}), } go manager.handle() return manager diff --git a/experimental/clashapi/trafficontrol/tracker.go b/experimental/clashapi/trafficontrol/tracker.go index 7e16dcdd..97be411f 100644 --- a/experimental/clashapi/trafficontrol/tracker.go +++ b/experimental/clashapi/trafficontrol/tracker.go @@ -1,6 +1,7 @@ package trafficontrol import ( + "encoding/json" "net" "net/netip" "time" @@ -8,10 +9,10 @@ import ( "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/experimental/trackerconn" "github.com/sagernet/sing/common" + "github.com/sagernet/sing/common/atomic" N "github.com/sagernet/sing/common/network" - "github.com/gofrs/uuid" - "go.uber.org/atomic" + "github.com/gofrs/uuid/v5" ) type Metadata struct { @@ -43,6 +44,19 @@ type trackerInfo struct { RulePayload string `json:"rulePayload"` } +func (t trackerInfo) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]any{ + "id": t.UUID.String(), + "metadata": t.Metadata, + "upload": t.UploadTotal.Load(), + "download": t.DownloadTotal.Load(), + "start": t.Start, + "chains": t.Chain, + "rule": t.Rule, + "rulePayload": t.RulePayload, + }) +} + type tcpTracker struct { N.ExtendedConn `json:"-"` *trackerInfo @@ -97,8 +111,8 @@ func NewTCPTracker(conn net.Conn, manager *Manager, metadata Metadata, router ad next = group.Now() } - upload := atomic.NewInt64(0) - download := atomic.NewInt64(0) + upload := new(atomic.Int64) + download := new(atomic.Int64) t := &tcpTracker{ ExtendedConn: trackerconn.NewHook(conn, func(n int64) { @@ -184,8 +198,8 @@ func NewUDPTracker(conn N.PacketConn, manager *Manager, metadata Metadata, route next = group.Now() } - upload := atomic.NewInt64(0) - download := atomic.NewInt64(0) + upload := new(atomic.Int64) + download := new(atomic.Int64) ut := &udpTracker{ PacketConn: trackerconn.NewHookPacket(conn, func(n int64) { diff --git a/experimental/trackerconn/conn.go b/experimental/trackerconn/conn.go index 07efd814..f9f70e7c 100644 --- a/experimental/trackerconn/conn.go +++ b/experimental/trackerconn/conn.go @@ -3,11 +3,10 @@ package trackerconn import ( "net" + "github.com/sagernet/sing/common/atomic" "github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/bufio" N "github.com/sagernet/sing/common/network" - - "go.uber.org/atomic" ) func New(conn net.Conn, readCounter []*atomic.Int64, writeCounter []*atomic.Int64) *Conn { diff --git a/experimental/trackerconn/packet_conn.go b/experimental/trackerconn/packet_conn.go index f90f4b9b..3a84c565 100644 --- a/experimental/trackerconn/packet_conn.go +++ b/experimental/trackerconn/packet_conn.go @@ -1,11 +1,10 @@ package trackerconn import ( + "github.com/sagernet/sing/common/atomic" "github.com/sagernet/sing/common/buf" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" - - "go.uber.org/atomic" ) func NewPacket(conn N.PacketConn, readCounter []*atomic.Int64, writeCounter []*atomic.Int64) *PacketConn { diff --git a/experimental/v2rayapi/stats.go b/experimental/v2rayapi/stats.go index b4d307d9..dfb5d666 100644 --- a/experimental/v2rayapi/stats.go +++ b/experimental/v2rayapi/stats.go @@ -12,10 +12,9 @@ import ( "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/experimental/trackerconn" "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing/common/atomic" E "github.com/sagernet/sing/common/exceptions" N "github.com/sagernet/sing/common/network" - - "go.uber.org/atomic" ) func init() { @@ -211,7 +210,7 @@ func (s *StatsService) loadOrCreateCounter(name string) *atomic.Int64 { if loaded { return counter } - counter = atomic.NewInt64(0) + counter = &atomic.Int64{} s.counters[name] = counter return counter } diff --git a/inbound/default.go b/inbound/default.go index 4d64465b..28f8cee2 100644 --- a/inbound/default.go +++ b/inbound/default.go @@ -10,11 +10,10 @@ import ( "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" "github.com/sagernet/sing/common" + "github.com/sagernet/sing/common/atomic" E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" - - "go.uber.org/atomic" ) var _ adapter.Inbound = (*myInboundAdapter)(nil)