2024-11-01 16:39:02 +00:00
|
|
|
package dns
|
2022-07-10 01:15:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-07-15 00:42:02 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2022-08-26 05:13:44 +00:00
|
|
|
"github.com/sagernet/sing-dns"
|
2022-07-10 01:15:01 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
"github.com/sagernet/sing/common/buf"
|
2023-04-24 11:01:10 +00:00
|
|
|
"github.com/sagernet/sing/common/bufio"
|
2023-03-23 07:14:21 +00:00
|
|
|
"github.com/sagernet/sing/common/canceler"
|
2022-07-23 01:15:47 +00:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2022-07-10 01:15:01 +00:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-07-24 13:25:41 +00:00
|
|
|
"github.com/sagernet/sing/common/task"
|
2022-07-10 01:15:01 +00:00
|
|
|
|
2022-09-13 08:18:39 +00:00
|
|
|
mDNS "github.com/miekg/dns"
|
2022-07-10 01:15:01 +00:00
|
|
|
)
|
|
|
|
|
2024-10-23 05:44:08 +00:00
|
|
|
func HandleStreamDNSRequest(ctx context.Context, router adapter.Router, conn net.Conn, metadata adapter.InboundContext) error {
|
2022-08-26 05:13:44 +00:00
|
|
|
var queryLength uint16
|
|
|
|
err := binary.Read(conn, binary.BigEndian, &queryLength)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if queryLength == 0 {
|
|
|
|
return dns.RCodeFormatError
|
|
|
|
}
|
2023-07-03 13:45:32 +00:00
|
|
|
buffer := buf.NewSize(int(queryLength))
|
2022-08-26 05:13:44 +00:00
|
|
|
defer buffer.Release()
|
|
|
|
_, err = buffer.ReadFullFrom(conn, int(queryLength))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-13 08:18:39 +00:00
|
|
|
var message mDNS.Msg
|
2022-08-26 05:13:44 +00:00
|
|
|
err = message.Unpack(buffer.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-04 04:39:43 +00:00
|
|
|
metadataInQuery := metadata
|
2022-08-26 05:13:44 +00:00
|
|
|
go func() error {
|
2024-10-23 05:44:08 +00:00
|
|
|
response, err := router.Exchange(adapter.WithContext(ctx, &metadataInQuery), &message)
|
2022-07-10 01:15:01 +00:00
|
|
|
if err != nil {
|
2024-11-06 09:30:40 +00:00
|
|
|
conn.Close()
|
2022-07-10 01:15:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-07-03 13:45:32 +00:00
|
|
|
responseBuffer := buf.NewPacket()
|
2022-08-26 05:13:44 +00:00
|
|
|
defer responseBuffer.Release()
|
|
|
|
responseBuffer.Resize(2, 0)
|
2022-09-13 08:18:39 +00:00
|
|
|
n, err := response.PackBuffer(responseBuffer.FreeBytes())
|
2022-07-10 01:15:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-26 05:13:44 +00:00
|
|
|
responseBuffer.Truncate(len(n))
|
|
|
|
binary.BigEndian.PutUint16(responseBuffer.ExtendHeader(2), uint16(len(n)))
|
|
|
|
_, err = conn.Write(responseBuffer.Bytes())
|
|
|
|
return err
|
|
|
|
}()
|
|
|
|
return nil
|
2022-07-10 01:15:01 +00:00
|
|
|
}
|
|
|
|
|
2024-10-23 05:44:08 +00:00
|
|
|
func NewDNSPacketConnection(ctx context.Context, router adapter.Router, conn N.PacketConn, cachedPackets []*N.PacketBuffer, metadata adapter.InboundContext) error {
|
2024-02-14 12:42:58 +00:00
|
|
|
metadata.Destination = M.Socksaddr{}
|
2023-04-24 11:01:10 +00:00
|
|
|
var reader N.PacketReader = conn
|
|
|
|
var counters []N.CountFunc
|
2024-10-23 05:44:08 +00:00
|
|
|
cachedPackets = common.Reverse(cachedPackets)
|
2023-04-24 11:01:10 +00:00
|
|
|
for {
|
|
|
|
reader, counters = N.UnwrapCountPacketReader(reader, counters)
|
|
|
|
if cachedReader, isCached := reader.(N.CachedPacketReader); isCached {
|
|
|
|
packet := cachedReader.ReadCachedPacket()
|
|
|
|
if packet != nil {
|
2023-04-25 20:53:25 +00:00
|
|
|
cachedPackets = append(cachedPackets, packet)
|
2023-04-24 11:01:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if readWaiter, created := bufio.CreatePacketReadWaiter(reader); created {
|
2024-02-14 12:42:58 +00:00
|
|
|
readWaiter.InitializeReadWaiter(N.ReadWaitOptions{})
|
2024-10-23 05:44:08 +00:00
|
|
|
return newDNSPacketConnection(ctx, router, conn, readWaiter, counters, cachedPackets, metadata)
|
2023-04-24 11:01:10 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2023-04-10 05:00:57 +00:00
|
|
|
fastClose, cancel := common.ContextWithCancelCause(ctx)
|
2022-07-25 14:02:39 +00:00
|
|
|
timeout := canceler.New(fastClose, cancel, C.DNSTimeout)
|
2022-08-03 09:11:53 +00:00
|
|
|
var group task.Group
|
2024-10-05 10:05:58 +00:00
|
|
|
group.Append0(func(_ context.Context) error {
|
2022-07-24 13:25:41 +00:00
|
|
|
for {
|
2022-09-13 08:18:39 +00:00
|
|
|
var message mDNS.Msg
|
2023-05-09 13:59:44 +00:00
|
|
|
var destination M.Socksaddr
|
|
|
|
var err error
|
|
|
|
if len(cachedPackets) > 0 {
|
|
|
|
packet := cachedPackets[0]
|
|
|
|
cachedPackets = cachedPackets[1:]
|
|
|
|
for _, counter := range counters {
|
|
|
|
counter(int64(packet.Buffer.Len()))
|
|
|
|
}
|
|
|
|
err = message.Unpack(packet.Buffer.Bytes())
|
|
|
|
packet.Buffer.Release()
|
|
|
|
if err != nil {
|
|
|
|
cancel(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
destination = packet.Destination
|
|
|
|
} else {
|
|
|
|
buffer := buf.NewPacket()
|
|
|
|
destination, err = conn.ReadPacket(buffer)
|
|
|
|
if err != nil {
|
|
|
|
buffer.Release()
|
|
|
|
cancel(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, counter := range counters {
|
|
|
|
counter(int64(buffer.Len()))
|
|
|
|
}
|
|
|
|
err = message.Unpack(buffer.Bytes())
|
|
|
|
buffer.Release()
|
|
|
|
if err != nil {
|
|
|
|
cancel(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
timeout.Update()
|
2022-07-10 01:15:01 +00:00
|
|
|
}
|
2022-09-04 04:39:43 +00:00
|
|
|
metadataInQuery := metadata
|
2022-07-24 13:25:41 +00:00
|
|
|
go func() error {
|
2024-10-23 05:44:08 +00:00
|
|
|
response, err := router.Exchange(adapter.WithContext(ctx, &metadataInQuery), &message)
|
2022-07-24 13:25:41 +00:00
|
|
|
if err != nil {
|
2023-04-10 05:00:57 +00:00
|
|
|
cancel(err)
|
2022-07-24 13:25:41 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-07-25 14:02:39 +00:00
|
|
|
timeout.Update()
|
2024-02-14 12:42:58 +00:00
|
|
|
responseBuffer, err := dns.TruncateDNSMessage(&message, response, 1024)
|
2022-07-24 13:25:41 +00:00
|
|
|
if err != nil {
|
2023-04-10 05:00:57 +00:00
|
|
|
cancel(err)
|
2022-07-24 13:25:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = conn.WritePacket(responseBuffer, destination)
|
2023-04-10 05:00:57 +00:00
|
|
|
if err != nil {
|
|
|
|
cancel(err)
|
|
|
|
}
|
2022-07-24 13:25:41 +00:00
|
|
|
return err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
})
|
2022-08-03 09:11:53 +00:00
|
|
|
group.Cleanup(func() {
|
|
|
|
conn.Close()
|
|
|
|
})
|
2022-08-12 04:13:57 +00:00
|
|
|
return group.Run(fastClose)
|
2022-07-10 01:15:01 +00:00
|
|
|
}
|
2023-04-24 11:01:10 +00:00
|
|
|
|
2024-10-23 05:44:08 +00:00
|
|
|
func newDNSPacketConnection(ctx context.Context, router adapter.Router, conn N.PacketConn, readWaiter N.PacketReadWaiter, readCounters []N.CountFunc, cached []*N.PacketBuffer, metadata adapter.InboundContext) error {
|
2023-04-24 11:01:10 +00:00
|
|
|
fastClose, cancel := common.ContextWithCancelCause(ctx)
|
|
|
|
timeout := canceler.New(fastClose, cancel, C.DNSTimeout)
|
|
|
|
var group task.Group
|
2024-10-05 10:05:58 +00:00
|
|
|
group.Append0(func(_ context.Context) error {
|
2023-04-24 11:01:10 +00:00
|
|
|
for {
|
2023-12-20 12:00:00 +00:00
|
|
|
var (
|
|
|
|
message mDNS.Msg
|
|
|
|
destination M.Socksaddr
|
|
|
|
err error
|
|
|
|
buffer *buf.Buffer
|
|
|
|
)
|
2023-04-24 11:01:10 +00:00
|
|
|
if len(cached) > 0 {
|
|
|
|
packet := cached[0]
|
|
|
|
cached = cached[1:]
|
|
|
|
for _, counter := range readCounters {
|
|
|
|
counter(int64(packet.Buffer.Len()))
|
|
|
|
}
|
|
|
|
err = message.Unpack(packet.Buffer.Bytes())
|
|
|
|
packet.Buffer.Release()
|
2024-10-23 05:44:08 +00:00
|
|
|
destination = packet.Destination
|
|
|
|
N.PutPacketBuffer(packet)
|
2023-04-24 11:01:10 +00:00
|
|
|
if err != nil {
|
|
|
|
cancel(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-20 12:00:00 +00:00
|
|
|
buffer, destination, err = readWaiter.WaitReadPacket()
|
2023-04-24 11:01:10 +00:00
|
|
|
if err != nil {
|
|
|
|
cancel(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, counter := range readCounters {
|
|
|
|
counter(int64(buffer.Len()))
|
|
|
|
}
|
|
|
|
err = message.Unpack(buffer.Bytes())
|
|
|
|
buffer.Release()
|
|
|
|
if err != nil {
|
|
|
|
cancel(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
timeout.Update()
|
|
|
|
}
|
|
|
|
metadataInQuery := metadata
|
|
|
|
go func() error {
|
2024-10-23 05:44:08 +00:00
|
|
|
response, err := router.Exchange(adapter.WithContext(ctx, &metadataInQuery), &message)
|
2023-04-24 11:01:10 +00:00
|
|
|
if err != nil {
|
|
|
|
cancel(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
timeout.Update()
|
2024-02-14 12:42:58 +00:00
|
|
|
responseBuffer, err := dns.TruncateDNSMessage(&message, response, 1024)
|
2023-04-24 11:01:10 +00:00
|
|
|
if err != nil {
|
|
|
|
cancel(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = conn.WritePacket(responseBuffer, destination)
|
|
|
|
if err != nil {
|
|
|
|
cancel(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
group.Cleanup(func() {
|
|
|
|
conn.Close()
|
|
|
|
})
|
|
|
|
return group.Run(fastClose)
|
|
|
|
}
|