From 7e47fd1a99d9bf99bcdebe5a99c4fb4ff69ea780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 11 Aug 2022 21:58:26 +0800 Subject: [PATCH] Update headroom usage --- go.mod | 4 ++-- go.sum | 8 ++++---- none.go | 4 ++-- shadowaead/protocol.go | 14 +++++++++++++- shadowaead/service.go | 14 +++++++++++++- shadowaead_2022/protocol.go | 6 +++++- shadowaead_2022/service.go | 6 +++++- shadowstream/protocol.go | 2 +- 8 files changed, 45 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index a337743..b90f4d2 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,12 @@ module github.com/sagernet/sing-shadowsocks go 1.18 require ( - github.com/sagernet/sing v0.0.0-20220801112236-1bb95f9661fc + github.com/sagernet/sing v0.0.0-20220811135544-169983a8d773 golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d lukechampine.com/blake3 v1.1.7 ) require ( github.com/klauspost/cpuid/v2 v2.0.12 // indirect - golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect + golang.org/x/sys v0.0.0-20220731174439-a90be440212d // indirect ) diff --git a/go.sum b/go.sum index ecbfc4d..949a1f3 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,11 @@ 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-20220801112236-1bb95f9661fc h1:x7H64IiqyrpxPWl/KrWkknzEK4GmpqgfZeVKFVw6E/M= -github.com/sagernet/sing v0.0.0-20220801112236-1bb95f9661fc/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM= +github.com/sagernet/sing v0.0.0-20220811135544-169983a8d773 h1:n88c8oBC6GWbEW2+F4HkVAji+puSW3GYGrbnXmuVYsw= +github.com/sagernet/sing v0.0.0-20220811135544-169983a8d773/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220731174439-a90be440212d h1:Sv5ogFZatcgIMMtBSTTAgMYsicp25MXBubjXNDKwm80= +golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= lukechampine.com/blake3 v1.1.7 h1:GgRMhmdsuK8+ii6UZFDL8Nb+VyMwadAgcJyfYHxG6n0= lukechampine.com/blake3 v1.1.7/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA= diff --git a/none.go b/none.go index 97c3b5f..f82c592 100644 --- a/none.go +++ b/none.go @@ -89,7 +89,7 @@ func (c *noneConn) WriteBuffer(buffer *buf.Buffer) error { return common.Error(c.Conn.Write(buffer.Bytes())) } -func (c *noneConn) Headroom() int { +func (c *noneConn) FrontHeadroom() int { if !c.handshake { return M.SocksaddrSerializer.AddrPortLen(c.destination) } @@ -239,7 +239,7 @@ func (w *nonePacketWriter) Upstream() any { return w.source } -func (c *nonePacketWriter) Headroom() int { +func (w *nonePacketWriter) FrontHeadroom() int { return M.MaxSocksaddrLength } diff --git a/shadowaead/protocol.go b/shadowaead/protocol.go index b3d45bd..8848205 100644 --- a/shadowaead/protocol.go +++ b/shadowaead/protocol.go @@ -310,10 +310,22 @@ func (c *clientPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { return len(p), nil } -func (c *clientPacketConn) Headroom() int { +func (c *clientPacketConn) FrontHeadroom() int { return c.keySaltLength + M.MaxSocksaddrLength } +func (c *clientPacketConn) RearHeadroom() int { + return Overhead +} + +func (c *clientPacketConn) ReaderMTU() int { + return MaxPacketSize +} + +func (c *clientPacketConn) WriterMTU() int { + return MaxPacketSize +} + func (c *clientPacketConn) Upstream() any { return c.Conn } diff --git a/shadowaead/service.go b/shadowaead/service.go index cf870b2..806b81e 100644 --- a/shadowaead/service.go +++ b/shadowaead/service.go @@ -208,6 +208,10 @@ func (c *serverConn) Upstream() any { return c.Conn } +func (s *Service) ReaderMTU() int { + return MaxPacketSize +} + func (s *Service) WriteIsThreadUnsafe() { } @@ -280,10 +284,18 @@ func (w *serverPacketWriter) WritePacket(buffer *buf.Buffer, destination M.Socks return w.source.WritePacket(buffer, M.SocksaddrFromNet(w.nat.LocalAddr())) } -func (w *serverPacketWriter) Headroom() int { +func (w *serverPacketWriter) FrontHeadroom() int { return w.keySaltLength + M.MaxSocksaddrLength } +func (w *serverPacketWriter) RearHeadroom() int { + return Overhead +} + +func (w *serverPacketWriter) WriterMTU() int { + return MaxPacketSize +} + func (w *serverPacketWriter) Upstream() any { return w.source } diff --git a/shadowaead_2022/protocol.go b/shadowaead_2022/protocol.go index 02af375..2de36dd 100644 --- a/shadowaead_2022/protocol.go +++ b/shadowaead_2022/protocol.go @@ -802,7 +802,7 @@ func (c *clientPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { return len(p), nil } -func (c *clientPacketConn) Headroom() int { +func (c *clientPacketConn) FrontHeadroom() int { var overHead int if c.udpCipher != nil { overHead = PacketNonceSize + shadowaead.Overhead @@ -822,6 +822,10 @@ func (c *clientPacketConn) Headroom() int { return overHead } +func (c *clientPacketConn) RearHeadroom() int { + return shadowaead.Overhead +} + type udpSession struct { sessionId uint64 packetId uint64 diff --git a/shadowaead_2022/service.go b/shadowaead_2022/service.go index 92c08bf..245f2b9 100644 --- a/shadowaead_2022/service.go +++ b/shadowaead_2022/service.go @@ -550,7 +550,7 @@ func (w *serverPacketWriter) WritePacket(buffer *buf.Buffer, destination M.Socks return w.source.WritePacket(buffer, M.SocksaddrFromNet(w.nat.LocalAddr())) } -func (w *serverPacketWriter) Headroom() int { +func (w *serverPacketWriter) FrontHeadroom() int { var hdrLen int if w.udpCipher != nil { hdrLen = PacketNonceSize @@ -565,6 +565,10 @@ func (w *serverPacketWriter) Headroom() int { return hdrLen } +func (w *serverPacketWriter) RearHeadroom() int { + return shadowaead.Overhead +} + func (w *serverPacketWriter) Upstream() any { return w.source } diff --git a/shadowstream/protocol.go b/shadowstream/protocol.go index af9eff4..c6f2862 100644 --- a/shadowstream/protocol.go +++ b/shadowstream/protocol.go @@ -325,7 +325,7 @@ func (c *clientPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { return len(p), nil } -func (c *clientPacketConn) Headroom() int { +func (c *clientPacketConn) FrontHeadroom() int { return c.saltLength + M.MaxSocksaddrLength }