mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-02-18 07:14:44 +00:00
Minor fixes
This commit is contained in:
parent
9a8918cb9e
commit
6b4824ffba
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
@ -36,6 +37,13 @@ func (c *PacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) er
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *PacketConn) Close() error {
|
||||||
|
return common.Close(
|
||||||
|
c.PacketConn,
|
||||||
|
c.instance,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *PacketConn) Upstream() any {
|
func (c *PacketConn) Upstream() any {
|
||||||
return c.PacketConn
|
return c.PacketConn
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,20 +35,21 @@ type Server struct {
|
||||||
httpServer *http.Server
|
httpServer *http.Server
|
||||||
trafficManager *trafficontrol.Manager
|
trafficManager *trafficontrol.Manager
|
||||||
urlTestHistory *urltest.HistoryStorage
|
urlTestHistory *urltest.HistoryStorage
|
||||||
|
tcpListener net.Listener
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) *Server {
|
func NewServer(router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) *Server {
|
||||||
trafficManager := trafficontrol.NewManager()
|
trafficManager := trafficontrol.NewManager()
|
||||||
chiRouter := chi.NewRouter()
|
chiRouter := chi.NewRouter()
|
||||||
server := &Server{
|
server := &Server{
|
||||||
router,
|
router: router,
|
||||||
logFactory.NewLogger("clash-api"),
|
logger: logFactory.NewLogger("clash-api"),
|
||||||
&http.Server{
|
httpServer: &http.Server{
|
||||||
Addr: options.ExternalController,
|
Addr: options.ExternalController,
|
||||||
Handler: chiRouter,
|
Handler: chiRouter,
|
||||||
},
|
},
|
||||||
trafficManager,
|
trafficManager: trafficManager,
|
||||||
urltest.NewHistoryStorage(),
|
urlTestHistory: urltest.NewHistoryStorage(),
|
||||||
}
|
}
|
||||||
cors := cors.New(cors.Options{
|
cors := cors.New(cors.Options{
|
||||||
AllowedOrigins: []string{"*"},
|
AllowedOrigins: []string{"*"},
|
||||||
|
@ -91,6 +92,7 @@ func (s *Server) Start() error {
|
||||||
return E.Cause(err, "external controller listen error")
|
return E.Cause(err, "external controller listen error")
|
||||||
}
|
}
|
||||||
s.logger.Info("restful api listening at ", listener.Addr())
|
s.logger.Info("restful api listening at ", listener.Addr())
|
||||||
|
s.tcpListener = listener
|
||||||
go func() {
|
go func() {
|
||||||
err = s.httpServer.Serve(listener)
|
err = s.httpServer.Serve(listener)
|
||||||
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
@ -102,6 +104,7 @@ func (s *Server) Start() error {
|
||||||
|
|
||||||
func (s *Server) Close() error {
|
func (s *Server) Close() error {
|
||||||
s.httpServer.Close()
|
s.httpServer.Close()
|
||||||
|
s.tcpListener.Close()
|
||||||
s.trafficManager.Close()
|
s.trafficManager.Close()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,16 +81,18 @@ func (m *Manager) ResetStatistic() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) handle() {
|
func (m *Manager) handle() {
|
||||||
|
var uploadTemp int64
|
||||||
|
var downloadTemp int64
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-m.done:
|
case <-m.done:
|
||||||
return
|
return
|
||||||
case <-m.ticker.C:
|
case <-m.ticker.C:
|
||||||
}
|
}
|
||||||
m.uploadBlip.Store(m.uploadTemp.Load())
|
uploadTemp = m.uploadTemp.Swap(0)
|
||||||
m.uploadTemp.Store(0)
|
downloadTemp = m.downloadTemp.Swap(0)
|
||||||
m.downloadBlip.Store(m.downloadTemp.Load())
|
m.uploadBlip.Store(uploadTemp)
|
||||||
m.downloadTemp.Store(0)
|
m.downloadBlip.Store(downloadTemp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue