Browse Source

Minor fixes

世界 3 years ago
parent
commit
2ce09b6ffd

+ 1 - 0
box.go

@@ -196,6 +196,7 @@ func (s *Box) Close() error {
 	}
 	return common.Close(
 		s.router,
+		s.logFactory,
 		s.clashServer,
 		common.PtrOrNil(s.logFile),
 	)

+ 12 - 0
cmd/sing-box/debug.go

@@ -0,0 +1,12 @@
+//go:build debug
+
+package main
+
+import (
+	"net/http"
+	_ "net/http/pprof"
+)
+
+func init() {
+	go http.ListenAndServe("0.0.0.0:8964", nil)
+}

+ 2 - 0
common/mux/service.go

@@ -39,6 +39,7 @@ func NewConnection(ctx context.Context, router adapter.Router, errorHandler E.Ha
 				logger.InfoContext(ctx, "inbound multiplex connection to ", metadata.Destination)
 				hErr := router.RouteConnection(ctx, &ServerConn{ExtendedConn: bufio.NewExtendedConn(stream)}, metadata)
 				if hErr != nil {
+					stream.Close()
 					errorHandler.NewError(ctx, hErr)
 				}
 			}()
@@ -54,6 +55,7 @@ func NewConnection(ctx context.Context, router adapter.Router, errorHandler E.Ha
 				}
 				hErr := router.RoutePacketConnection(ctx, packetConn, metadata)
 				if hErr != nil {
+					stream.Close()
 					errorHandler.NewError(ctx, hErr)
 				}
 			}()

+ 3 - 1
experimental/clashapi/server.go

@@ -101,7 +101,9 @@ func (s *Server) Start() error {
 }
 
 func (s *Server) Close() error {
-	return s.httpServer.Close()
+	s.httpServer.Close()
+	s.trafficManager.Close()
+	return nil
 }
 
 func (s *Server) RoutedConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, matchedRule adapter.Rule) (net.Conn, adapter.Tracker) {

+ 17 - 4
experimental/clashapi/trafficontrol/manager.go

@@ -16,6 +16,8 @@ type Manager struct {
 	downloadBlip  *atomic.Int64
 	uploadTotal   *atomic.Int64
 	downloadTotal *atomic.Int64
+	ticker        *time.Ticker
+	done          chan struct{}
 }
 
 func NewManager() *Manager {
@@ -26,6 +28,8 @@ func NewManager() *Manager {
 		downloadBlip:  atomic.NewInt64(0),
 		uploadTotal:   atomic.NewInt64(0),
 		downloadTotal: atomic.NewInt64(0),
+		ticker:        time.NewTicker(time.Second),
+		done:          make(chan struct{}),
 	}
 	go manager.handle()
 	return manager
@@ -54,7 +58,7 @@ func (m *Manager) Now() (up int64, down int64) {
 }
 
 func (m *Manager) Snapshot() *Snapshot {
-	connections := []tracker{}
+	var connections []tracker
 	m.connections.Range(func(_ string, value tracker) bool {
 		connections = append(connections, value)
 		return true
@@ -77,9 +81,12 @@ func (m *Manager) ResetStatistic() {
 }
 
 func (m *Manager) handle() {
-	ticker := time.NewTicker(time.Second)
-
-	for range ticker.C {
+	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())
@@ -87,6 +94,12 @@ func (m *Manager) handle() {
 	}
 }
 
+func (m *Manager) Close() error {
+	m.ticker.Stop()
+	close(m.done)
+	return nil
+}
+
 type Snapshot struct {
 	DownloadTotal int64     `json:"downloadTotal"`
 	UploadTotal   int64     `json:"uploadTotal"`

+ 3 - 3
go.mod

@@ -13,10 +13,10 @@ require (
 	github.com/hashicorp/yamux v0.1.1
 	github.com/logrusorgru/aurora v2.0.3+incompatible
 	github.com/oschwald/maxminddb-golang v1.9.0
-	github.com/sagernet/sing v0.0.0-20220729120910-4376f188c512
-	github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1
+	github.com/sagernet/sing v0.0.0-20220730061053-a21e329a2698
+	github.com/sagernet/sing-dns v0.0.0-20220730061139-c8e0fb296da9
 	github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80
-	github.com/sagernet/sing-tun v0.0.0-20220730015349-3b4e77c4c1b3
+	github.com/sagernet/sing-tun v0.0.0-20220730061211-d5dd3b3bb14a
 	github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5
 	github.com/spf13/cobra v1.5.0
 	github.com/stretchr/testify v1.8.0

+ 6 - 6
go.sum

@@ -147,14 +147,14 @@ github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7q
 github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
 github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
 github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
-github.com/sagernet/sing v0.0.0-20220729120910-4376f188c512 h1:dCWDE55LpZu//W02FccNbGObZFlv1N2NS0yUdf2i4Mc=
-github.com/sagernet/sing v0.0.0-20220729120910-4376f188c512/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
-github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1 h1:Gv9ow1IF98Qdxs+X8unPHJG4iwuEWoq0PE/jvlIqgqY=
-github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1/go.mod h1:LQJDT4IpqyWI6NugkSSqxTcFfxxNBp94n+fXtHFMboQ=
+github.com/sagernet/sing v0.0.0-20220730061053-a21e329a2698 h1:wjoF4/FOwze8cN2/EvQyyuq1tzXjxNViPIoqQ7CNIb8=
+github.com/sagernet/sing v0.0.0-20220730061053-a21e329a2698/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
+github.com/sagernet/sing-dns v0.0.0-20220730061139-c8e0fb296da9 h1:2xg2bzELWQyaD5QwS3QV90RLWqjL8d6LQmKNWOI8XWQ=
+github.com/sagernet/sing-dns v0.0.0-20220730061139-c8e0fb296da9/go.mod h1:ZSslb2fc27A1Tk3WM5yootwWLSglsxqRZv3noam5pso=
 github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80 h1:gpCPZyZJQVn6ZTBCJ/XaYbPi6j43TdyTty/MI5bXhbE=
 github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80/go.mod h1:mH6wE4b5FZp1Q/meATe4tjiPjvQO9E7Lr0FBBwFYp4I=
-github.com/sagernet/sing-tun v0.0.0-20220730015349-3b4e77c4c1b3 h1:QYKRVeyRa8bGE2ggOaroNlXQ/1cyRKGwtJOUOO/ZvXk=
-github.com/sagernet/sing-tun v0.0.0-20220730015349-3b4e77c4c1b3/go.mod h1:lOVup6Io7873/8lUpdrBy/TLjQ7PJHUqSP/yp1k0ld8=
+github.com/sagernet/sing-tun v0.0.0-20220730061211-d5dd3b3bb14a h1:ULsuxdirFkCUNmcDENQCjfWl/28G6rzgs2xiZFSBSZc=
+github.com/sagernet/sing-tun v0.0.0-20220730061211-d5dd3b3bb14a/go.mod h1:1V/Scct3DGHi925AasPCj1k+6SRWIcg0TvRHM0ZXB8I=
 github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5 h1:TNguWTPF6gxX/gR02hY3LGviUn6LGlDPofE6lpSJWeo=
 github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5/go.mod h1:Q8csko2kQZHRZTz8ztqELrJB22HV60/tztPVgACV84E=
 github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=

+ 3 - 1
inbound/default.go

@@ -150,10 +150,12 @@ func (a *myInboundAdapter) newPacketConnection(ctx context.Context, conn N.Packe
 func (a *myInboundAdapter) loopTCPIn() {
 	tcpListener := a.tcpListener
 	for {
-		conn, err := tcpListener.Accept()
+		conn, err := tcpListener.AcceptTCP()
 		if err != nil {
 			return
 		}
+		conn.SetKeepAlive(true)
+		conn.SetKeepAlivePeriod(C.TCPKeepAlivePeriod)
 		go func() {
 			ctx := log.ContextWithNewID(a.ctx)
 			var metadata adapter.InboundContext

+ 5 - 0
log/default.go

@@ -6,6 +6,7 @@ import (
 	"os"
 	"time"
 
+	"github.com/sagernet/sing/common"
 	F "github.com/sagernet/sing/common/format"
 )
 
@@ -41,6 +42,10 @@ func (f *simpleFactory) NewLogger(tag string) ContextLogger {
 	return &simpleLogger{f, tag}
 }
 
+func (f *simpleFactory) Close() error {
+	return common.Close(f.writer)
+}
+
 var _ ContextLogger = (*simpleLogger)(nil)
 
 type simpleLogger struct {

+ 8 - 0
log/observable.go

@@ -6,6 +6,7 @@ import (
 	"os"
 	"time"
 
+	"github.com/sagernet/sing/common"
 	F "github.com/sagernet/sing/common/format"
 	"github.com/sagernet/sing/common/observable"
 )
@@ -55,6 +56,13 @@ func (f *observableFactory) UnSubscribe(sub observable.Subscription[Entry]) {
 	f.observer.UnSubscribe(sub)
 }
 
+func (f *observableFactory) Close() error {
+	return common.Close(
+		f.writer,
+		f.observer,
+	)
+}
+
 var _ ContextLogger = (*observableLogger)(nil)
 
 type observableLogger struct {

+ 18 - 0
release/local/debug.sh

@@ -0,0 +1,18 @@
+#!/usr/bin/env bash
+
+set -e -o pipefail
+
+DIR=$(dirname "$0")
+PROJECT=$DIR/../..
+
+pushd $PROJECT
+git fetch
+git reset FETCH_HEAD --hard
+git clean -fdx
+go install -v -trimpath -ldflags "-s -w -buildid=" -tags "no_gvisor,debug" ./cmd/sing-box
+popd
+
+sudo systemctl stop sing-box
+sudo cp $(go env GOPATH)/bin/sing-box /usr/local/bin/
+sudo systemctl start sing-box
+sudo journalctl -u sing-box --output cat -f

+ 3 - 3
test/go.mod

@@ -10,7 +10,7 @@ require (
 	github.com/docker/docker v20.10.17+incompatible
 	github.com/docker/go-connections v0.4.0
 	github.com/gofrs/uuid v4.2.0+incompatible
-	github.com/sagernet/sing v0.0.0-20220729120910-4376f188c512
+	github.com/sagernet/sing v0.0.0-20220730061053-a21e329a2698
 	github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80
 	github.com/spyzhov/ajson v0.7.1
 	github.com/stretchr/testify v1.8.0
@@ -52,8 +52,8 @@ require (
 	github.com/oschwald/maxminddb-golang v1.9.0 // indirect
 	github.com/pkg/errors v0.9.1 // indirect
 	github.com/pmezard/go-difflib v1.0.0 // indirect
-	github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1 // indirect
-	github.com/sagernet/sing-tun v0.0.0-20220730015349-3b4e77c4c1b3 // indirect
+	github.com/sagernet/sing-dns v0.0.0-20220730061139-c8e0fb296da9 // indirect
+	github.com/sagernet/sing-tun v0.0.0-20220730061211-d5dd3b3bb14a // indirect
 	github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5 // indirect
 	github.com/sirupsen/logrus v1.8.1 // indirect
 	github.com/vishvananda/netlink v1.1.0 // indirect

+ 6 - 6
test/go.sum

@@ -172,14 +172,14 @@ github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:
 github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
 github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
 github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
-github.com/sagernet/sing v0.0.0-20220729120910-4376f188c512 h1:dCWDE55LpZu//W02FccNbGObZFlv1N2NS0yUdf2i4Mc=
-github.com/sagernet/sing v0.0.0-20220729120910-4376f188c512/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
-github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1 h1:Gv9ow1IF98Qdxs+X8unPHJG4iwuEWoq0PE/jvlIqgqY=
-github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1/go.mod h1:LQJDT4IpqyWI6NugkSSqxTcFfxxNBp94n+fXtHFMboQ=
+github.com/sagernet/sing v0.0.0-20220730061053-a21e329a2698 h1:wjoF4/FOwze8cN2/EvQyyuq1tzXjxNViPIoqQ7CNIb8=
+github.com/sagernet/sing v0.0.0-20220730061053-a21e329a2698/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
+github.com/sagernet/sing-dns v0.0.0-20220730061139-c8e0fb296da9 h1:2xg2bzELWQyaD5QwS3QV90RLWqjL8d6LQmKNWOI8XWQ=
+github.com/sagernet/sing-dns v0.0.0-20220730061139-c8e0fb296da9/go.mod h1:ZSslb2fc27A1Tk3WM5yootwWLSglsxqRZv3noam5pso=
 github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80 h1:gpCPZyZJQVn6ZTBCJ/XaYbPi6j43TdyTty/MI5bXhbE=
 github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80/go.mod h1:mH6wE4b5FZp1Q/meATe4tjiPjvQO9E7Lr0FBBwFYp4I=
-github.com/sagernet/sing-tun v0.0.0-20220730015349-3b4e77c4c1b3 h1:QYKRVeyRa8bGE2ggOaroNlXQ/1cyRKGwtJOUOO/ZvXk=
-github.com/sagernet/sing-tun v0.0.0-20220730015349-3b4e77c4c1b3/go.mod h1:lOVup6Io7873/8lUpdrBy/TLjQ7PJHUqSP/yp1k0ld8=
+github.com/sagernet/sing-tun v0.0.0-20220730061211-d5dd3b3bb14a h1:ULsuxdirFkCUNmcDENQCjfWl/28G6rzgs2xiZFSBSZc=
+github.com/sagernet/sing-tun v0.0.0-20220730061211-d5dd3b3bb14a/go.mod h1:1V/Scct3DGHi925AasPCj1k+6SRWIcg0TvRHM0ZXB8I=
 github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5 h1:TNguWTPF6gxX/gR02hY3LGviUn6LGlDPofE6lpSJWeo=
 github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5/go.mod h1:Q8csko2kQZHRZTz8ztqELrJB22HV60/tztPVgACV84E=
 github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=