Improve none client

This commit is contained in:
世界 2022-05-30 11:26:06 +08:00
parent 9fbb103c01
commit 1a554a61f3
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 22 additions and 24 deletions

2
go.mod
View file

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

4
go.sum
View file

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

40
none.go
View file

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