Reject invalid connection

This commit is contained in:
世界
2023-09-07 09:09:03 +08:00
parent 4ea2d460f4
commit 6b943caf37
9 changed files with 93 additions and 60 deletions

View File

@@ -271,9 +271,13 @@ func (c *clientConn) Read(b []byte) (n int, err error) {
func (c *clientConn) Write(b []byte) (n int, err error) {
if !c.requestWritten {
request := buf.NewSize(2 + addressSerializer.AddrPortLen(c.destination) + len(b))
defer request.Release()
request.WriteByte(Version)
request.WriteByte(CommandConnect)
addressSerializer.WriteAddrPort(request, c.destination)
err = addressSerializer.WriteAddrPort(request, c.destination)
if err != nil {
return
}
request.Write(b)
_, err = c.stream.Write(request.Bytes())
if err != nil {

View File

@@ -17,6 +17,7 @@ import (
"github.com/sagernet/sing/common/atomic"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/cache"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
)
@@ -205,6 +206,9 @@ func (c *udpPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr)
if buffer.Len() > 0xffff {
return quic.ErrMessageTooLarge(0xffff)
}
if !destination.IsValid() {
return E.New("invalid destination address")
}
packetId := c.packetId.Add(1)
if packetId > math.MaxUint16 {
c.packetId.Store(0)
@@ -246,6 +250,10 @@ func (c *udpPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
if len(p) > 0xffff {
return 0, quic.ErrMessageTooLarge(0xffff)
}
destination := M.SocksaddrFromNet(addr)
if !destination.IsValid() {
return 0, E.New("invalid destination address")
}
packetId := c.packetId.Add(1)
if packetId > math.MaxUint16 {
c.packetId.Store(0)
@@ -256,7 +264,7 @@ func (c *udpPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
sessionID: c.sessionID,
packetID: uint16(packetId),
fragmentTotal: 1,
destination: M.SocksaddrFromNet(addr),
destination: destination,
data: buf.As(p),
}
if !c.udpStream && c.needFragment() && len(p) > c.udpMTU {