mirror of
https://github.com/SagerNet/sing-shadowsocks.git
synced 2025-04-03 20:07:40 +03:00
Add headroom interface
This commit is contained in:
parent
91d2780bfc
commit
a91eacdd01
8 changed files with 65 additions and 3 deletions
2
go.mod
2
go.mod
|
@ -3,7 +3,7 @@ module github.com/sagernet/sing-shadowsocks
|
|||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/sagernet/sing v0.0.0-20220726034811-bc109486f14e
|
||||
github.com/sagernet/sing v0.0.0-20220801112236-1bb95f9661fc
|
||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
|
||||
lukechampine.com/blake3 v1.1.7
|
||||
)
|
||||
|
|
4
go.sum
4
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-20220726034811-bc109486f14e h1:5lfrAc+vSv0iW6eHGNLyHC+a/k6BDGJvYxYxwB/68Kk=
|
||||
github.com/sagernet/sing v0.0.0-20220726034811-bc109486f14e/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
|
||||
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=
|
||||
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=
|
||||
|
|
15
none.go
15
none.go
|
@ -89,6 +89,13 @@ func (c *noneConn) WriteBuffer(buffer *buf.Buffer) error {
|
|||
return common.Error(c.Conn.Write(buffer.Bytes()))
|
||||
}
|
||||
|
||||
func (c *noneConn) Headroom() int {
|
||||
if !c.handshake {
|
||||
return M.SocksaddrSerializer.AddrPortLen(c.destination)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *noneConn) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
if !c.handshake {
|
||||
return bufio.ReadFrom0(c, r)
|
||||
|
@ -170,6 +177,10 @@ func (c *nonePacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|||
return len(p), nil
|
||||
}
|
||||
|
||||
func (c *nonePacketConn) Headroom() int {
|
||||
return M.MaxSocksaddrLength
|
||||
}
|
||||
|
||||
type NoneService struct {
|
||||
handler Handler
|
||||
udpNat *udpnat.Service[netip.AddrPort]
|
||||
|
@ -228,6 +239,10 @@ func (w *nonePacketWriter) Upstream() any {
|
|||
return w.source
|
||||
}
|
||||
|
||||
func (c *nonePacketWriter) Headroom() int {
|
||||
return M.MaxSocksaddrLength
|
||||
}
|
||||
|
||||
func (s *NoneService) NewError(ctx context.Context, err error) {
|
||||
s.handler.NewError(ctx, err)
|
||||
}
|
||||
|
|
|
@ -310,6 +310,10 @@ func (c *clientPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|||
return len(p), nil
|
||||
}
|
||||
|
||||
func (c *clientPacketConn) Headroom() int {
|
||||
return c.keySaltLength + M.MaxSocksaddrLength
|
||||
}
|
||||
|
||||
func (c *clientPacketConn) Upstream() any {
|
||||
return c.Conn
|
||||
}
|
||||
|
|
|
@ -280,6 +280,10 @@ 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 {
|
||||
return w.keySaltLength + M.MaxSocksaddrLength
|
||||
}
|
||||
|
||||
func (w *serverPacketWriter) Upstream() any {
|
||||
return w.source
|
||||
}
|
||||
|
|
|
@ -802,6 +802,26 @@ func (c *clientPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|||
return len(p), nil
|
||||
}
|
||||
|
||||
func (c *clientPacketConn) Headroom() int {
|
||||
var overHead int
|
||||
if c.udpCipher != nil {
|
||||
overHead = PacketNonceSize + shadowaead.Overhead
|
||||
} else {
|
||||
overHead = shadowaead.Overhead
|
||||
}
|
||||
overHead += 16 // packet header
|
||||
pskLen := len(c.pskList)
|
||||
if c.udpCipher == nil && pskLen > 1 {
|
||||
overHead += (pskLen - 1) * aes.BlockSize
|
||||
}
|
||||
overHead += 1 // header type
|
||||
overHead += 8 // timestamp
|
||||
overHead += 2 // padding length
|
||||
overHead += MaxPaddingLength
|
||||
overHead += M.MaxSocksaddrLength
|
||||
return overHead
|
||||
}
|
||||
|
||||
type udpSession struct {
|
||||
sessionId uint64
|
||||
packetId uint64
|
||||
|
|
|
@ -550,6 +550,21 @@ 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 {
|
||||
var hdrLen int
|
||||
if w.udpCipher != nil {
|
||||
hdrLen = PacketNonceSize
|
||||
}
|
||||
hdrLen += 16 // packet header
|
||||
hdrLen += 1 // header type
|
||||
hdrLen += 8 // timestamp
|
||||
hdrLen += 8 // remote session id
|
||||
hdrLen += 2 // padding length
|
||||
hdrLen += MaxPaddingLength
|
||||
hdrLen += M.MaxSocksaddrLength
|
||||
return hdrLen
|
||||
}
|
||||
|
||||
func (w *serverPacketWriter) Upstream() any {
|
||||
return w.source
|
||||
}
|
||||
|
|
|
@ -325,6 +325,10 @@ func (c *clientPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|||
return len(p), nil
|
||||
}
|
||||
|
||||
func (c *clientPacketConn) Headroom() int {
|
||||
return c.saltLength + M.MaxSocksaddrLength
|
||||
}
|
||||
|
||||
func (c *clientPacketConn) Upstream() any {
|
||||
return c.Conn
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue