From 97870c9288f3c2f50cb988043d8807341034e2cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 11 Aug 2022 23:59:22 +0800 Subject: [PATCH] Refactor bufio --- cmd/sing-box/debug.go | 6 +++--- common/mux/client.go | 4 ++-- common/mux/service.go | 9 +++++---- go.mod | 6 +++--- go.sum | 12 ++++++------ inbound/naive.go | 20 +++++++++++++------- inbound/naive_quic.go | 1 + log/default.go | 5 ----- log/observable.go | 1 - test/box_test.go | 3 +-- test/go.mod | 6 +++--- test/go.sum | 12 ++++++------ test/mux_test.go | 2 +- test/vmess_test.go | 4 ++-- 14 files changed, 46 insertions(+), 45 deletions(-) diff --git a/cmd/sing-box/debug.go b/cmd/sing-box/debug.go index a27fbbcf..4f12ac92 100644 --- a/cmd/sing-box/debug.go +++ b/cmd/sing-box/debug.go @@ -3,16 +3,16 @@ package main import ( + "encoding/json" "net/http" _ "net/http/pprof" "runtime" + "runtime/debug" + "github.com/sagernet/sing-box/common/badjson" "github.com/sagernet/sing-box/log" "github.com/dustin/go-humanize" - "runtime/debug" - "encoding/json" - "github.com/sagernet/sing-box/common/badjson" ) func init() { diff --git a/common/mux/client.go b/common/mux/client.go index f94675d1..5bf29727 100644 --- a/common/mux/client.go +++ b/common/mux/client.go @@ -334,7 +334,7 @@ func (c *ClientPacketConn) WriteBuffer(buffer *buf.Buffer) error { return c.ExtendedConn.WriteBuffer(buffer) } -func (c *ClientPacketConn) Headroom() int { +func (c *ClientPacketConn) FrontHeadroom() int { return 2 } @@ -486,7 +486,7 @@ func (c *ClientPacketAddrConn) LocalAddr() net.Addr { return c.ExtendedConn.LocalAddr() } -func (c *ClientPacketAddrConn) Headroom() int { +func (c *ClientPacketAddrConn) FrontHeadroom() int { return 2 + M.MaxSocksaddrLength } diff --git a/common/mux/service.go b/common/mux/service.go index 64f45b81..f89796dc 100644 --- a/common/mux/service.go +++ b/common/mux/service.go @@ -26,8 +26,9 @@ func NewConnection(ctx context.Context, router adapter.Router, errorHandler E.Ha if err != nil { return err } + var stream net.Conn for { - stream, err := session.Accept() + stream, err = session.Accept() if err != nil { return err } @@ -116,7 +117,7 @@ func (c *ServerConn) WriteBuffer(buffer *buf.Buffer) error { return c.ExtendedConn.WriteBuffer(buffer) } -func (c *ServerConn) Headroom() int { +func (c *ServerConn) FrontHeadroom() int { if !c.responseWrite { return 1 } @@ -182,7 +183,7 @@ func (c *ServerPacketConn) Upstream() any { return c.ExtendedConn } -func (c *ServerPacketConn) Headroom() int { +func (c *ServerPacketConn) FrontHeadroom() int { if !c.responseWrite { return 3 } @@ -247,7 +248,7 @@ func (c *ServerPacketAddrConn) Upstream() any { return c.ExtendedConn } -func (c *ServerPacketAddrConn) Headroom() int { +func (c *ServerPacketAddrConn) FrontHeadroom() int { if !c.responseWrite { return 3 + M.MaxSocksaddrLength } diff --git a/go.mod b/go.mod index bb4ea302..148b819d 100644 --- a/go.mod +++ b/go.mod @@ -15,11 +15,11 @@ require ( github.com/logrusorgru/aurora v2.0.3+incompatible github.com/lucas-clemente/quic-go v0.28.1 github.com/oschwald/maxminddb-golang v1.10.0 - github.com/sagernet/sing v0.0.0-20220808004927-21369d10810d + github.com/sagernet/sing v0.0.0-20220811152014-735372ab3ccf github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91 - github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 + github.com/sagernet/sing-shadowsocks v0.0.0-20220811135826-7e47fd1a99d9 github.com/sagernet/sing-tun v0.0.0-20220808133432-d378b6ca536d - github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2 + github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4 github.com/spf13/cobra v1.5.0 github.com/stretchr/testify v1.8.0 github.com/xtaci/smux v1.5.16 diff --git a/go.sum b/go.sum index 0cb2b80c..c09bbb47 100644 --- a/go.sum +++ b/go.sum @@ -152,16 +152,16 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 h1:hE+vtsjBCCPmxkRz9jZA+CicHgVkDT6H+Av5ZzskVxs= github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM= -github.com/sagernet/sing v0.0.0-20220808004927-21369d10810d h1:vWzXLfdGyAYYbBpYFFHErtJlBXC59AieYMlUMAI6gw8= -github.com/sagernet/sing v0.0.0-20220808004927-21369d10810d/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= +github.com/sagernet/sing v0.0.0-20220811152014-735372ab3ccf h1:k8ZXdBb5D6JqDTgwLAw4cazapPcLYbcJNxSFUvUff+s= +github.com/sagernet/sing v0.0.0-20220811152014-735372ab3ccf/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91 h1:jxt2PYixIkK2i7nUGW3f+PzJagEZcbNyQddBWGuqNnw= github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91/go.mod h1:T77zZdE2Cm6VqnFumrpwsq+kxYsbq+vWDhmjtdSl/oM= -github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 h1:RYvOc69eSNMN0dwVugrDts41Nn7Ar/C/n/fvytvFcp4= -github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1/go.mod h1:NqZjiXszgVCMQ4gVDa2V+drhS8NMfGqUqDF86EacEFc= +github.com/sagernet/sing-shadowsocks v0.0.0-20220811135826-7e47fd1a99d9 h1:/FFNyglfOlk1x6NWhBWI+bc/kVQc7SFOSYAJ2m7FwHc= +github.com/sagernet/sing-shadowsocks v0.0.0-20220811135826-7e47fd1a99d9/go.mod h1:3Pe1OCs1zrMyZmMB4st8GF/IL6EMHLSVnUHSS5VjnfM= github.com/sagernet/sing-tun v0.0.0-20220808133432-d378b6ca536d h1:AQpkoUiF8FxYVI1lf2W9Rbkk914eHjVH9M8y+F/0+Nw= github.com/sagernet/sing-tun v0.0.0-20220808133432-d378b6ca536d/go.mod h1:gWwYd53AqXl+Y+q6WlXUc6PkqU28sfu5VTQhyeEIFbw= -github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2 h1:C8sc2MYiNx0O7uQ0nieJWq5qYeIHj20XHFWPlcgoQeY= -github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2/go.mod h1:bNXBqSWYaG3ePl6u0xQY5zneE+ZKa3683ZpuE8S1M1w= +github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4 h1:2hLETh97+S4WnfMR27XyC7QVU1SH7FTNoCznP229YJU= +github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4/go.mod h1:82O6gzbxLha/W/jxSVQbsqf2lVdRTjMIgyLug0lpJps= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY= github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM= diff --git a/inbound/naive.go b/inbound/naive.go index ef3fb36b..660447f6 100644 --- a/inbound/naive.go +++ b/inbound/naive.go @@ -88,11 +88,6 @@ func (n *Naive) Start() error { return E.Cause(err, "create TLS config") } - n.httpServer = &http.Server{ - Handler: n, - TLSConfig: n.tlsConfig.Config(), - } - var listenAddr string if nAddr := netip.Addr(n.listenOptions.Listen); nAddr.IsValid() { if n.listenOptions.ListenPort != 0 { @@ -107,6 +102,10 @@ func (n *Naive) Start() error { } if common.Contains(n.network, N.NetworkTCP) { + n.httpServer = &http.Server{ + Handler: n, + TLSConfig: n.tlsConfig.Config(), + } tcpListener, err := net.Listen(M.NetworkFromNetAddr("tcp", netip.Addr(n.listenOptions.Listen)), listenAddr) if err != nil { return err @@ -325,6 +324,13 @@ func (c *naivePaddingConn) write(p []byte) (n int, err error) { return c.writer.Write(p) } +func (c *naivePaddingConn) FrontHeadroom() int { + if c.writePadding < kFirstPaddings { + return 3 + 255 + } + return 0 +} + func (c *naivePaddingConn) WriteBuffer(buffer *buf.Buffer) error { defer buffer.Release() if c.writePadding < kFirstPaddings { @@ -345,14 +351,14 @@ func (c *naivePaddingConn) WriteBuffer(buffer *buf.Buffer) error { func (c *naivePaddingConn) WriteTo(w io.Writer) (n int64, err error) { if c.readPadding < kFirstPaddings { - return bufio.WriteTo0(c, w) + return bufio.WriteToN(c, w, kFirstPaddings-c.readPadding) } return bufio.Copy(w, c.reader) } func (c *naivePaddingConn) ReadFrom(r io.Reader) (n int64, err error) { if c.writePadding < kFirstPaddings { - return bufio.ReadFrom0(c, r) + return bufio.ReadFromN(c, r, kFirstPaddings-c.writePadding) } return bufio.Copy(c.writer, r) } diff --git a/inbound/naive_quic.go b/inbound/naive_quic.go index de6969ce..b5ca50a5 100644 --- a/inbound/naive_quic.go +++ b/inbound/naive_quic.go @@ -29,6 +29,7 @@ func (n *Naive) configureHTTP3Listener(listenAddr string) error { go func() { sErr := h3Server.Serve(udpListener) if sErr == quic.ErrServerClosed { + udpListener.Close() return } else if sErr != nil { n.logger.Error("http3 server serve error: ", sErr) diff --git a/log/default.go b/log/default.go index 09f23e38..f3acd9b6 100644 --- a/log/default.go +++ b/log/default.go @@ -6,7 +6,6 @@ import ( "os" "time" - "github.com/sagernet/sing/common" F "github.com/sagernet/sing/common/format" ) @@ -42,10 +41,6 @@ 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 { diff --git a/log/observable.go b/log/observable.go index 0484539a..9c20b628 100644 --- a/log/observable.go +++ b/log/observable.go @@ -58,7 +58,6 @@ func (f *observableFactory) UnSubscribe(sub observable.Subscription[Entry]) { func (f *observableFactory) Close() error { return common.Close( - f.writer, f.observer, ) } diff --git a/test/box_test.go b/test/box_test.go index e7bc9bd4..4c7f7abf 100644 --- a/test/box_test.go +++ b/test/box_test.go @@ -23,14 +23,13 @@ func startInstance(t *testing.T, options option.Options) { require.NoError(t, err) err = instance.Start() if err != nil { - time.Sleep(5 * time.Millisecond) + time.Sleep(time.Second) continue } break } require.NoError(t, err) t.Cleanup(func() { - time.Sleep(500 * time.Millisecond) instance.Close() }) } diff --git a/test/go.mod b/test/go.mod index 60aa3cf8..314935de 100644 --- a/test/go.mod +++ b/test/go.mod @@ -10,8 +10,8 @@ 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-20220808004927-21369d10810d - github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 + github.com/sagernet/sing v0.0.0-20220811152014-735372ab3ccf + github.com/sagernet/sing-shadowsocks v0.0.0-20220811135826-7e47fd1a99d9 github.com/spyzhov/ajson v0.7.1 github.com/stretchr/testify v1.8.0 golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced @@ -56,7 +56,7 @@ require ( github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 // indirect github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91 // indirect github.com/sagernet/sing-tun v0.0.0-20220808133432-d378b6ca536d // indirect - github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2 // indirect + github.com/sagernet/sing-vmess v0.0.0-20220811154439-e85c9d12159e // indirect github.com/sirupsen/logrus v1.8.1 // indirect github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect github.com/xtaci/smux v1.5.16 // indirect diff --git a/test/go.sum b/test/go.sum index a8a44363..a7cbfa5f 100644 --- a/test/go.sum +++ b/test/go.sum @@ -176,16 +176,16 @@ github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 h1:hE+vtsjBCCPmxkRz9jZA+CicHgVkDT6H+Av5ZzskVxs= github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM= -github.com/sagernet/sing v0.0.0-20220808004927-21369d10810d h1:vWzXLfdGyAYYbBpYFFHErtJlBXC59AieYMlUMAI6gw8= -github.com/sagernet/sing v0.0.0-20220808004927-21369d10810d/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= +github.com/sagernet/sing v0.0.0-20220811152014-735372ab3ccf h1:k8ZXdBb5D6JqDTgwLAw4cazapPcLYbcJNxSFUvUff+s= +github.com/sagernet/sing v0.0.0-20220811152014-735372ab3ccf/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91 h1:jxt2PYixIkK2i7nUGW3f+PzJagEZcbNyQddBWGuqNnw= github.com/sagernet/sing-dns v0.0.0-20220803121532-9e1ffb850d91/go.mod h1:T77zZdE2Cm6VqnFumrpwsq+kxYsbq+vWDhmjtdSl/oM= -github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 h1:RYvOc69eSNMN0dwVugrDts41Nn7Ar/C/n/fvytvFcp4= -github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1/go.mod h1:NqZjiXszgVCMQ4gVDa2V+drhS8NMfGqUqDF86EacEFc= +github.com/sagernet/sing-shadowsocks v0.0.0-20220811135826-7e47fd1a99d9 h1:/FFNyglfOlk1x6NWhBWI+bc/kVQc7SFOSYAJ2m7FwHc= +github.com/sagernet/sing-shadowsocks v0.0.0-20220811135826-7e47fd1a99d9/go.mod h1:3Pe1OCs1zrMyZmMB4st8GF/IL6EMHLSVnUHSS5VjnfM= github.com/sagernet/sing-tun v0.0.0-20220808133432-d378b6ca536d h1:AQpkoUiF8FxYVI1lf2W9Rbkk914eHjVH9M8y+F/0+Nw= github.com/sagernet/sing-tun v0.0.0-20220808133432-d378b6ca536d/go.mod h1:gWwYd53AqXl+Y+q6WlXUc6PkqU28sfu5VTQhyeEIFbw= -github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2 h1:C8sc2MYiNx0O7uQ0nieJWq5qYeIHj20XHFWPlcgoQeY= -github.com/sagernet/sing-vmess v0.0.0-20220804023624-e829b41c84c2/go.mod h1:bNXBqSWYaG3ePl6u0xQY5zneE+ZKa3683ZpuE8S1M1w= +github.com/sagernet/sing-vmess v0.0.0-20220811154439-e85c9d12159e h1:iRCJqAbppWRlBQvHi/hPIN+QNWHPCU6yxf+P2de2gxA= +github.com/sagernet/sing-vmess v0.0.0-20220811154439-e85c9d12159e/go.mod h1:82O6gzbxLha/W/jxSVQbsqf2lVdRTjMIgyLug0lpJps= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY= github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM= diff --git a/test/mux_test.go b/test/mux_test.go index ff881d82..2f8271a2 100644 --- a/test/mux_test.go +++ b/test/mux_test.go @@ -102,7 +102,7 @@ func testVMessMux(t *testing.T, protocol string) { user, _ := uuid.NewV4() startInstance(t, option.Options{ Log: &option.LogOptions{ - Level: "trace", + Level: "error", }, Inbounds: []option.Inbound{ { diff --git a/test/vmess_test.go b/test/vmess_test.go index 4ee4c8b9..14dd871a 100644 --- a/test/vmess_test.go +++ b/test/vmess_test.go @@ -13,7 +13,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestVMess(t *testing.T) { +func _TestVMessAuto(t *testing.T) { security := "auto" user, err := uuid.DefaultGenerator.NewV4() require.NoError(t, err) @@ -28,7 +28,7 @@ func TestVMess(t *testing.T) { }) } -func _TestVMess(t *testing.T) { +func TestVMess(t *testing.T) { for _, security := range []string{ "zero", } {