Feature(restapi): add netstats

This commit is contained in:
xjasonlyu
2022-03-30 23:38:31 +08:00
parent abdbaa6b83
commit e6fc4adccd
4 changed files with 106 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package engine
import (
"errors"
"sync"
"github.com/xjasonlyu/tun2socks/v2/component/dialer"
"github.com/xjasonlyu/tun2socks/v2/core"
@@ -16,6 +17,8 @@ import (
)
var (
_engineMu sync.Mutex
// _defaultKey holds the default key for the engine.
_defaultKey *Key
@@ -45,10 +48,13 @@ func Stop() {
// Insert loads *Key to the default engine.
func Insert(k *Key) {
_engineMu.Lock()
_defaultKey = k
_engineMu.Unlock()
}
func start() error {
_engineMu.Lock()
if _defaultKey == nil {
return errors.New("empty key")
}
@@ -62,10 +68,12 @@ func start() error {
return err
}
}
_engineMu.Unlock()
return nil
}
func stop() (err error) {
_engineMu.Lock()
if _defaultDevice != nil {
err = _defaultDevice.Close()
}
@@ -73,6 +81,7 @@ func stop() (err error) {
_defaultStack.Close()
_defaultStack.Wait()
}
_engineMu.Unlock()
return err
}
@@ -107,6 +116,17 @@ func restAPI(k *Key) error {
}
host, token := u.Host, u.User.String()
restapi.SetStatsFunc(func() tcpip.Stats {
_engineMu.Lock()
defer _engineMu.Unlock()
// default stack is not initialized.
if _defaultStack == nil {
return tcpip.Stats{}
}
return _defaultStack.Stats()
})
go func() {
if err := restapi.Start(host, token); err != nil {
log.Warnf("[RESTAPI] failed to start: %v", err)