From 1a554a61f3ba49beb41cad89ffa8a38f5fa50a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 30 May 2022 11:26:06 +0800 Subject: [PATCH] Improve none client --- go.mod | 2 +- go.sum | 4 ++-- none.go | 40 +++++++++++++++++++--------------------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index 9acccb8..7f759f3 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/sagernet/sing-shadowsocks go 1.18 require ( - github.com/sagernet/sing v0.0.0-20220528143524-3c8df0a57c72 + github.com/sagernet/sing v0.0.0-20220530023925-d634f5f8d1f9 golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e lukechampine.com/blake3 v1.1.7 ) diff --git a/go.sum b/go.sum index 83637db..c38cf22 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,8 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE= github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= -github.com/sagernet/sing v0.0.0-20220528143524-3c8df0a57c72 h1:A/kpbNaEg7ISgADmVV036rrkmng2sQU2AP1gJqnRVhE= -github.com/sagernet/sing v0.0.0-20220528143524-3c8df0a57c72/go.mod h1:w2HnJzXKHpD6F5Z/9XlSD4qbcpHY2RSZuQnFzqgELMg= +github.com/sagernet/sing v0.0.0-20220530023925-d634f5f8d1f9 h1:KqmiPU4dmM5bXsWF3i321bhdaY0iw9aSk9lHnKFqcA8= +github.com/sagernet/sing v0.0.0-20220530023925-d634f5f8d1f9/go.mod h1:w2HnJzXKHpD6F5Z/9XlSD4qbcpHY2RSZuQnFzqgELMg= golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM= golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= diff --git a/none.go b/none.go index a1085e0..6177e7f 100644 --- a/none.go +++ b/none.go @@ -60,9 +60,6 @@ type noneConn struct { } func (c *noneConn) clientHandshake() error { - c.access.Lock() - defer c.access.Unlock() - err := M.SocksaddrSerializer.WriteAddrPort(c.Conn, c.destination) if err != nil { return err @@ -71,37 +68,38 @@ func (c *noneConn) clientHandshake() error { return nil } -// TODO: add write buffer support func (c *noneConn) Write(b []byte) (n int, err error) { if c.handshake { return c.Conn.Write(b) } - - c.access.Lock() - if c.handshake { - c.access.Unlock() - return c.Conn.Write(b) - } - err = M.SocksaddrSerializer.WriteAddrPort(c.Conn, c.destination) if err != nil { return } c.handshake = true - c.access.Unlock() return c.Conn.Write(b) } -func (c *noneConn) ReadFrom(r io.Reader) (n int64, err error) { - if !c.handshake { - c.access.Lock() - if !c.handshake { - c.access.Unlock() - return bufio.ReadFrom0(c, r) - } - c.access.Unlock() +func (c *noneConn) WriteBuffer(buffer *buf.Buffer) error { + defer buffer.Release() + if c.handshake { + return common.Error(c.Conn.Write(buffer.Bytes())) } - return c.Conn.(io.ReaderFrom).ReadFrom(r) + + header := buf.With(buffer.ExtendHeader(M.SocksaddrSerializer.AddrPortLen(c.destination))) + err := M.SocksaddrSerializer.WriteAddrPort(header, c.destination) + if err != nil { + return err + } + c.handshake = true + return common.Error(c.Conn.Write(buffer.Bytes())) +} + +func (c *noneConn) ReadFrom(r io.Reader) (n int64, err error) { + if c.handshake { + return bufio.ReadFrom0(c, r) + } + return bufio.Copy(c.Conn, r) } func (c *noneConn) WriteTo(w io.Writer) (n int64, err error) {