mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
Fix concurrent DNS writes corrupting shared connections
This commit is contained in:
@@ -56,7 +56,8 @@ type queryMultiplexer struct {
|
||||
|
||||
type multiplexConn struct {
|
||||
net.Conn
|
||||
readEpoch atomic.Uint64
|
||||
writeAccess sync.Mutex
|
||||
readEpoch atomic.Uint64
|
||||
}
|
||||
|
||||
type queryMultiplexerReadError struct {
|
||||
@@ -329,7 +330,9 @@ func (m *queryMultiplexer) exchangeAsync(ctx context.Context, message *mDNS.Msg,
|
||||
callback(nil, err)
|
||||
return
|
||||
}
|
||||
conn.writeAccess.Lock()
|
||||
writeErr := m.options.write(conn, message, queryId)
|
||||
conn.writeAccess.Unlock()
|
||||
if writeErr == nil {
|
||||
return
|
||||
}
|
||||
|
||||
+43
-28
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
@@ -19,38 +21,43 @@ import (
|
||||
mDNS "github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func HandleStreamDNSRequest(ctx context.Context, router adapter.DNSRouter, conn net.Conn, metadata adapter.InboundContext) error {
|
||||
var queryLength uint16
|
||||
err := binary.Read(conn, binary.BigEndian, &queryLength)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if queryLength == 0 {
|
||||
return dns.RcodeFormatError
|
||||
}
|
||||
buffer := buf.NewSize(int(queryLength))
|
||||
defer buffer.Release()
|
||||
_, err = buffer.ReadFullFrom(conn, int(queryLength))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var message mDNS.Msg
|
||||
err = message.Unpack(buffer.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metadataInQuery := metadata
|
||||
router.ExchangeAsync(adapter.WithContext(ctx, &metadataInQuery), &message, adapter.DNSQueryOptions{}, func(response *mDNS.Msg, err error) {
|
||||
func NewDNSConnection(ctx context.Context, router adapter.DNSRouter, conn net.Conn, metadata adapter.InboundContext) error {
|
||||
metadata.Destination = M.Socksaddr{}
|
||||
var writeAccess sync.Mutex
|
||||
for {
|
||||
conn.SetReadDeadline(time.Now().Add(C.DNSTimeout))
|
||||
var queryLength uint16
|
||||
err := binary.Read(conn, binary.BigEndian, &queryLength)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return
|
||||
return err
|
||||
}
|
||||
go writeStreamResponse(conn, response)
|
||||
})
|
||||
return nil
|
||||
if queryLength == 0 {
|
||||
return dns.RcodeFormatError
|
||||
}
|
||||
buffer := buf.NewSize(int(queryLength))
|
||||
_, err = buffer.ReadFullFrom(conn, int(queryLength))
|
||||
if err != nil {
|
||||
buffer.Release()
|
||||
return err
|
||||
}
|
||||
var message mDNS.Msg
|
||||
err = message.Unpack(buffer.Bytes())
|
||||
buffer.Release()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metadataInQuery := metadata
|
||||
router.ExchangeAsync(adapter.WithContext(ctx, &metadataInQuery), &message, adapter.DNSQueryOptions{}, func(response *mDNS.Msg, err error) {
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
go writeStreamResponse(conn, &writeAccess, response)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func writeStreamResponse(conn net.Conn, response *mDNS.Msg) {
|
||||
func writeStreamResponse(conn net.Conn, writeAccess *sync.Mutex, response *mDNS.Msg) {
|
||||
responseLength := response.Len()
|
||||
responseBuffer := buf.NewSize(3 + responseLength)
|
||||
defer responseBuffer.Release()
|
||||
@@ -63,7 +70,9 @@ func writeStreamResponse(conn net.Conn, response *mDNS.Msg) {
|
||||
}
|
||||
common.Must1(responseBuffer.Write(rawMessage))
|
||||
binary.BigEndian.PutUint16(responseBuffer.ExtendHeader(2), uint16(len(rawMessage)))
|
||||
writeAccess.Lock()
|
||||
conn.Write(responseBuffer.Bytes())
|
||||
writeAccess.Unlock()
|
||||
}
|
||||
|
||||
func NewDNSPacketConnection(ctx context.Context, router adapter.DNSRouter, conn N.PacketConn, cachedPackets []*N.PacketBuffer, metadata adapter.InboundContext) error {
|
||||
@@ -89,6 +98,7 @@ func NewDNSPacketConnection(ctx context.Context, router adapter.DNSRouter, conn
|
||||
}
|
||||
fastClose, cancel := context.WithCancelCause(ctx)
|
||||
timeout := canceler.New(fastClose, cancel, C.DNSTimeout)
|
||||
var writeAccess sync.Mutex
|
||||
var group task.Group
|
||||
group.Append0(func(_ context.Context) error {
|
||||
for {
|
||||
@@ -139,7 +149,9 @@ func NewDNSPacketConnection(ctx context.Context, router adapter.DNSRouter, conn
|
||||
cancel(truncateErr)
|
||||
return
|
||||
}
|
||||
writeAccess.Lock()
|
||||
writeErr := conn.WritePacket(responseBuffer, destination)
|
||||
writeAccess.Unlock()
|
||||
if writeErr != nil {
|
||||
cancel(writeErr)
|
||||
}
|
||||
@@ -157,6 +169,7 @@ func newDNSPacketConnection(ctx context.Context, router adapter.DNSRouter, conn
|
||||
rearHeadroom := N.CalculateRearHeadroom(conn)
|
||||
fastClose, cancel := context.WithCancelCause(ctx)
|
||||
timeout := canceler.New(fastClose, cancel, C.DNSTimeout)
|
||||
var writeAccess sync.Mutex
|
||||
var group task.Group
|
||||
group.Append0(func(_ context.Context) error {
|
||||
for {
|
||||
@@ -209,7 +222,9 @@ func newDNSPacketConnection(ctx context.Context, router adapter.DNSRouter, conn
|
||||
cancel(truncateErr)
|
||||
return
|
||||
}
|
||||
writeAccess.Lock()
|
||||
writeErr := conn.WritePacket(responseBuffer, destination)
|
||||
writeAccess.Unlock()
|
||||
if writeErr != nil {
|
||||
cancel(writeErr)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/adapter/outbound"
|
||||
@@ -44,17 +43,10 @@ func (d *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (n
|
||||
}
|
||||
|
||||
func (d *Outbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
||||
metadata.Destination = M.Socksaddr{}
|
||||
for {
|
||||
conn.SetReadDeadline(time.Now().Add(C.DNSTimeout))
|
||||
err := HandleStreamDNSRequest(ctx, d.router, conn, metadata)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
if onClose != nil {
|
||||
onClose(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
err := NewDNSConnection(ctx, d.router, conn, metadata)
|
||||
conn.Close()
|
||||
if onClose != nil {
|
||||
onClose(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-13
@@ -3,10 +3,8 @@ package route
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/dns"
|
||||
dnsOutbound "github.com/sagernet/sing-box/protocol/dns"
|
||||
R "github.com/sagernet/sing-box/route/rule"
|
||||
@@ -19,22 +17,15 @@ import (
|
||||
|
||||
func (r *Router) hijackDNSStream(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
||||
r.searchProcessInfo(ctx, &metadata)
|
||||
metadata.Destination = M.Socksaddr{}
|
||||
err := N.ReportConnHandshakeSuccess(conn, conn)
|
||||
if err != nil {
|
||||
return E.Cause(err, "report handshake success")
|
||||
}
|
||||
for {
|
||||
conn.SetReadDeadline(time.Now().Add(C.DNSTimeout))
|
||||
err = dnsOutbound.HandleStreamDNSRequest(ctx, r.dns, conn, metadata)
|
||||
if err != nil {
|
||||
if !E.IsClosedOrCanceled(err) {
|
||||
return err
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
err = dnsOutbound.NewDNSConnection(ctx, r.dns, conn, metadata)
|
||||
if err != nil && !E.IsClosedOrCanceled(err) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Router) hijackDNSPacket(ctx context.Context, conn N.PacketConn, packetBuffers []*N.PacketBuffer, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) error {
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
boxService "github.com/sagernet/sing-box/adapter/service"
|
||||
@@ -128,15 +127,7 @@ func (i *Service) Start(stage adapter.StartStage, scope *adapter.Scope) error {
|
||||
func (i *Service) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
||||
metadata.Inbound = i.Tag()
|
||||
metadata.InboundType = i.Type()
|
||||
metadata.Destination = M.Socksaddr{}
|
||||
for {
|
||||
conn.SetReadDeadline(time.Now().Add(C.DNSTimeout))
|
||||
err := dnsOutbound.HandleStreamDNSRequest(ctx, i.dnsRouter, conn, metadata)
|
||||
if err != nil {
|
||||
N.CloseOnHandshakeFailure(conn, onClose, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
N.CloseOnHandshakeFailure(conn, onClose, dnsOutbound.NewDNSConnection(ctx, i.dnsRouter, conn, metadata))
|
||||
}
|
||||
|
||||
func (i *Service) NewPacket(buffer *buf.Buffer, oob []byte, source M.Socksaddr) {
|
||||
|
||||
Reference in New Issue
Block a user