mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-12-27 16:55:39 +08:00
clashapi: Remove traffic loop
This commit is contained in:
parent
3cc0e87cfb
commit
5a661cde67
|
@ -316,27 +316,31 @@ func traffic(trafficManager *trafficontrol.Manager) func(w http.ResponseWriter,
|
||||||
tick := time.NewTicker(time.Second)
|
tick := time.NewTicker(time.Second)
|
||||||
defer tick.Stop()
|
defer tick.Stop()
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
var err error
|
var (
|
||||||
|
uploadTotal int64
|
||||||
|
doanloadTotal int64
|
||||||
|
err error
|
||||||
|
)
|
||||||
for range tick.C {
|
for range tick.C {
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
up, down := trafficManager.Now()
|
uploadTotalNew, downloadTotalNew := trafficManager.Total()
|
||||||
if err := json.NewEncoder(buf).Encode(Traffic{
|
if err := json.NewEncoder(buf).Encode(Traffic{
|
||||||
Up: up,
|
Up: uploadTotalNew - uploadTotal,
|
||||||
Down: down,
|
Down: downloadTotalNew - doanloadTotal,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
_, err = w.Write(buf.Bytes())
|
_, err = w.Write(buf.Bytes())
|
||||||
w.(http.Flusher).Flush()
|
w.(http.Flusher).Flush()
|
||||||
} else {
|
} else {
|
||||||
err = wsutil.WriteServerText(conn, buf.Bytes())
|
err = wsutil.WriteServerText(conn, buf.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
uploadTotal = uploadTotalNew
|
||||||
|
doanloadTotal = downloadTotalNew
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,30 +16,18 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Manager struct {
|
type Manager struct {
|
||||||
uploadTemp atomic.Int64
|
|
||||||
downloadTemp atomic.Int64
|
|
||||||
uploadBlip atomic.Int64
|
|
||||||
downloadBlip atomic.Int64
|
|
||||||
uploadTotal atomic.Int64
|
uploadTotal atomic.Int64
|
||||||
downloadTotal atomic.Int64
|
downloadTotal atomic.Int64
|
||||||
|
|
||||||
connections compatible.Map[uuid.UUID, Tracker]
|
connections compatible.Map[uuid.UUID, Tracker]
|
||||||
closedConnectionsAccess sync.Mutex
|
closedConnectionsAccess sync.Mutex
|
||||||
closedConnections list.List[TrackerMetadata]
|
closedConnections list.List[TrackerMetadata]
|
||||||
ticker *time.Ticker
|
|
||||||
done chan struct{}
|
|
||||||
// process *process.Process
|
// process *process.Process
|
||||||
memory uint64
|
memory uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewManager() *Manager {
|
func NewManager() *Manager {
|
||||||
manager := &Manager{
|
return &Manager{}
|
||||||
ticker: time.NewTicker(time.Second),
|
|
||||||
done: make(chan struct{}),
|
|
||||||
// process: &process.Process{Pid: int32(os.Getpid())},
|
|
||||||
}
|
|
||||||
go manager.handle()
|
|
||||||
return manager
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) Join(c Tracker) {
|
func (m *Manager) Join(c Tracker) {
|
||||||
|
@ -61,19 +49,13 @@ func (m *Manager) Leave(c Tracker) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) PushUploaded(size int64) {
|
func (m *Manager) PushUploaded(size int64) {
|
||||||
m.uploadTemp.Add(size)
|
|
||||||
m.uploadTotal.Add(size)
|
m.uploadTotal.Add(size)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) PushDownloaded(size int64) {
|
func (m *Manager) PushDownloaded(size int64) {
|
||||||
m.downloadTemp.Add(size)
|
|
||||||
m.downloadTotal.Add(size)
|
m.downloadTotal.Add(size)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) Now() (up int64, down int64) {
|
|
||||||
return m.uploadBlip.Load(), m.downloadBlip.Load()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Manager) Total() (up int64, down int64) {
|
func (m *Manager) Total() (up int64, down int64) {
|
||||||
return m.uploadTotal.Load(), m.downloadTotal.Load()
|
return m.uploadTotal.Load(), m.downloadTotal.Load()
|
||||||
}
|
}
|
||||||
|
@ -127,36 +109,10 @@ func (m *Manager) Snapshot() *Snapshot {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) ResetStatistic() {
|
func (m *Manager) ResetStatistic() {
|
||||||
m.uploadTemp.Store(0)
|
|
||||||
m.uploadBlip.Store(0)
|
|
||||||
m.uploadTotal.Store(0)
|
m.uploadTotal.Store(0)
|
||||||
m.downloadTemp.Store(0)
|
|
||||||
m.downloadBlip.Store(0)
|
|
||||||
m.downloadTotal.Store(0)
|
m.downloadTotal.Store(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) handle() {
|
|
||||||
var uploadTemp int64
|
|
||||||
var downloadTemp int64
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-m.done:
|
|
||||||
return
|
|
||||||
case <-m.ticker.C:
|
|
||||||
}
|
|
||||||
uploadTemp = m.uploadTemp.Swap(0)
|
|
||||||
downloadTemp = m.downloadTemp.Swap(0)
|
|
||||||
m.uploadBlip.Store(uploadTemp)
|
|
||||||
m.downloadBlip.Store(downloadTemp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Manager) Close() error {
|
|
||||||
m.ticker.Stop()
|
|
||||||
close(m.done)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Snapshot struct {
|
type Snapshot struct {
|
||||||
Download int64
|
Download int64
|
||||||
Upload int64
|
Upload int64
|
||||||
|
|
|
@ -34,7 +34,6 @@ func (s *CommandServer) readStatus() StatusMessage {
|
||||||
if clashServer := s.service.instance.Router().ClashServer(); clashServer != nil {
|
if clashServer := s.service.instance.Router().ClashServer(); clashServer != nil {
|
||||||
message.TrafficAvailable = true
|
message.TrafficAvailable = true
|
||||||
trafficManager := clashServer.(*clashapi.Server).TrafficManager()
|
trafficManager := clashServer.(*clashapi.Server).TrafficManager()
|
||||||
message.Uplink, message.Downlink = trafficManager.Now()
|
|
||||||
message.UplinkTotal, message.DownlinkTotal = trafficManager.Total()
|
message.UplinkTotal, message.DownlinkTotal = trafficManager.Total()
|
||||||
message.ConnectionsIn = int32(trafficManager.ConnectionsLen())
|
message.ConnectionsIn = int32(trafficManager.ConnectionsLen())
|
||||||
}
|
}
|
||||||
|
@ -52,8 +51,20 @@ func (s *CommandServer) handleStatusConn(conn net.Conn) error {
|
||||||
ticker := time.NewTicker(time.Duration(interval))
|
ticker := time.NewTicker(time.Duration(interval))
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
ctx := connKeepAlive(conn)
|
ctx := connKeepAlive(conn)
|
||||||
|
var (
|
||||||
|
status StatusMessage
|
||||||
|
uploadTotal int64
|
||||||
|
downloadTotal int64
|
||||||
|
)
|
||||||
for {
|
for {
|
||||||
err = binary.Write(conn, binary.BigEndian, s.readStatus())
|
status = s.readStatus()
|
||||||
|
upload := status.UplinkTotal - uploadTotal
|
||||||
|
download := status.DownlinkTotal - downloadTotal
|
||||||
|
uploadTotal = status.UplinkTotal
|
||||||
|
downloadTotal = status.DownlinkTotal
|
||||||
|
status.Uplink = upload
|
||||||
|
status.Downlink = download
|
||||||
|
err = binary.Write(conn, binary.BigEndian, status)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user