Minor fixes

This commit is contained in:
世界 2022-07-30 14:50:33 +08:00
parent d3378a575c
commit 2ce09b6ffd
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
13 changed files with 87 additions and 24 deletions

1
box.go
View file

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

12
cmd/sing-box/debug.go Normal file
View file

@ -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)
}

View file

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

View file

@ -101,7 +101,9 @@ func (s *Server) Start() error {
} }
func (s *Server) Close() 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) { func (s *Server) RoutedConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, matchedRule adapter.Rule) (net.Conn, adapter.Tracker) {

View file

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

6
go.mod
View file

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

12
go.sum
View file

@ -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/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 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/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-20220730061053-a21e329a2698 h1:wjoF4/FOwze8cN2/EvQyyuq1tzXjxNViPIoqQ7CNIb8=
github.com/sagernet/sing v0.0.0-20220729120910-4376f188c512/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM= github.com/sagernet/sing v0.0.0-20220730061053-a21e329a2698/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-20220730061139-c8e0fb296da9 h1:2xg2bzELWQyaD5QwS3QV90RLWqjL8d6LQmKNWOI8XWQ=
github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1/go.mod h1:LQJDT4IpqyWI6NugkSSqxTcFfxxNBp94n+fXtHFMboQ= 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 h1:gpCPZyZJQVn6ZTBCJ/XaYbPi6j43TdyTty/MI5bXhbE=
github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80/go.mod h1:mH6wE4b5FZp1Q/meATe4tjiPjvQO9E7Lr0FBBwFYp4I= 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-20220730061211-d5dd3b3bb14a h1:ULsuxdirFkCUNmcDENQCjfWl/28G6rzgs2xiZFSBSZc=
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/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 h1:TNguWTPF6gxX/gR02hY3LGviUn6LGlDPofE6lpSJWeo=
github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5/go.mod h1:Q8csko2kQZHRZTz8ztqELrJB22HV60/tztPVgACV84E= 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= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=

View file

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

View file

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

View file

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

18
release/local/debug.sh Executable file
View file

@ -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

View file

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

View file

@ -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/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/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 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-20220730061053-a21e329a2698 h1:wjoF4/FOwze8cN2/EvQyyuq1tzXjxNViPIoqQ7CNIb8=
github.com/sagernet/sing v0.0.0-20220729120910-4376f188c512/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM= github.com/sagernet/sing v0.0.0-20220730061053-a21e329a2698/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-20220730061139-c8e0fb296da9 h1:2xg2bzELWQyaD5QwS3QV90RLWqjL8d6LQmKNWOI8XWQ=
github.com/sagernet/sing-dns v0.0.0-20220729120941-109c0a7aabb1/go.mod h1:LQJDT4IpqyWI6NugkSSqxTcFfxxNBp94n+fXtHFMboQ= 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 h1:gpCPZyZJQVn6ZTBCJ/XaYbPi6j43TdyTty/MI5bXhbE=
github.com/sagernet/sing-shadowsocks v0.0.0-20220729155919-91d2780bfc80/go.mod h1:mH6wE4b5FZp1Q/meATe4tjiPjvQO9E7Lr0FBBwFYp4I= 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-20220730061211-d5dd3b3bb14a h1:ULsuxdirFkCUNmcDENQCjfWl/28G6rzgs2xiZFSBSZc=
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/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 h1:TNguWTPF6gxX/gR02hY3LGviUn6LGlDPofE6lpSJWeo=
github.com/sagernet/sing-vmess v0.0.0-20220726034841-4dae776653e5/go.mod h1:Q8csko2kQZHRZTz8ztqELrJB22HV60/tztPVgACV84E= 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= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=