Minor fixes

This commit is contained in:
世界 2022-08-02 18:47:23 +08:00
parent 9a8918cb9e
commit 6b4824ffba
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 22 additions and 9 deletions

View file

@ -4,6 +4,7 @@ import (
"context"
"time"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
@ -36,6 +37,13 @@ func (c *PacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) er
return err
}
func (c *PacketConn) Close() error {
return common.Close(
c.PacketConn,
c.instance,
)
}
func (c *PacketConn) Upstream() any {
return c.PacketConn
}

View file

@ -35,20 +35,21 @@ type Server struct {
httpServer *http.Server
trafficManager *trafficontrol.Manager
urlTestHistory *urltest.HistoryStorage
tcpListener net.Listener
}
func NewServer(router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) *Server {
trafficManager := trafficontrol.NewManager()
chiRouter := chi.NewRouter()
server := &Server{
router,
logFactory.NewLogger("clash-api"),
&http.Server{
router: router,
logger: logFactory.NewLogger("clash-api"),
httpServer: &http.Server{
Addr: options.ExternalController,
Handler: chiRouter,
},
trafficManager,
urltest.NewHistoryStorage(),
trafficManager: trafficManager,
urlTestHistory: urltest.NewHistoryStorage(),
}
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
@ -91,6 +92,7 @@ func (s *Server) Start() error {
return E.Cause(err, "external controller listen error")
}
s.logger.Info("restful api listening at ", listener.Addr())
s.tcpListener = listener
go func() {
err = s.httpServer.Serve(listener)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
@ -102,6 +104,7 @@ func (s *Server) Start() error {
func (s *Server) Close() error {
s.httpServer.Close()
s.tcpListener.Close()
s.trafficManager.Close()
return nil
}

View file

@ -81,16 +81,18 @@ func (m *Manager) ResetStatistic() {
}
func (m *Manager) handle() {
var uploadTemp int64
var downloadTemp int64
for {
select {
case <-m.done:
return
case <-m.ticker.C:
}
m.uploadBlip.Store(m.uploadTemp.Load())
m.uploadTemp.Store(0)
m.downloadBlip.Store(m.downloadTemp.Load())
m.downloadTemp.Store(0)
uploadTemp = m.uploadTemp.Swap(0)
downloadTemp = m.downloadTemp.Swap(0)
m.uploadBlip.Store(uploadTemp)
m.downloadBlip.Store(downloadTemp)
}
}