sing-box/outbound/dns.go

257 lines
6.5 KiB
Go
Raw Normal View History

2022-07-23 09:15:47 +08:00
package outbound
2022-07-10 09:15:01 +08:00
import (
"context"
"encoding/binary"
"net"
2022-07-23 09:15:47 +08:00
"os"
2022-07-10 09:15:01 +08:00
"github.com/sagernet/sing-box/adapter"
2022-07-15 08:42:02 +08:00
C "github.com/sagernet/sing-box/constant"
2022-08-26 13:13:44 +08:00
"github.com/sagernet/sing-dns"
2022-07-10 09:15:01 +08:00
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
2023-04-24 19:01:10 +08:00
"github.com/sagernet/sing/common/bufio"
2023-03-23 15:14:21 +08:00
"github.com/sagernet/sing/common/canceler"
2022-07-23 09:15:47 +08:00
M "github.com/sagernet/sing/common/metadata"
2022-07-10 09:15:01 +08:00
N "github.com/sagernet/sing/common/network"
2022-07-24 21:25:41 +08:00
"github.com/sagernet/sing/common/task"
2022-07-10 09:15:01 +08:00
2022-09-13 16:18:39 +08:00
mDNS "github.com/miekg/dns"
2022-07-10 09:15:01 +08:00
)
2022-07-23 09:15:47 +08:00
var _ adapter.Outbound = (*DNS)(nil)
2022-07-15 08:42:02 +08:00
type DNS struct {
2022-07-23 09:15:47 +08:00
myOutboundAdapter
2022-07-15 08:42:02 +08:00
}
2022-08-03 18:55:39 +08:00
func NewDNS(router adapter.Router, tag string) *DNS {
2022-07-23 09:15:47 +08:00
return &DNS{
myOutboundAdapter{
protocol: C.TypeDNS,
2022-07-30 00:29:22 +08:00
network: []string{N.NetworkTCP, N.NetworkUDP},
2022-07-23 09:15:47 +08:00
router: router,
tag: tag,
2022-07-15 08:42:02 +08:00
},
}
}
2022-07-23 09:15:47 +08:00
func (d *DNS) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
return nil, os.ErrInvalid
2022-07-15 08:42:02 +08:00
}
2022-07-23 09:15:47 +08:00
func (d *DNS) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
return nil, os.ErrInvalid
2022-07-15 08:42:02 +08:00
}
2022-07-23 09:15:47 +08:00
func (d *DNS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-07-24 21:25:41 +08:00
defer conn.Close()
2022-07-11 12:56:57 +08:00
ctx = adapter.WithContext(ctx, &metadata)
2022-07-10 09:15:01 +08:00
for {
2022-08-26 13:13:44 +08:00
err := d.handleConnection(ctx, conn, metadata)
2022-07-10 09:15:01 +08:00
if err != nil {
return err
}
2022-08-26 13:13:44 +08:00
}
}
func (d *DNS) handleConnection(ctx context.Context, 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.StackNewSize(int(queryLength))
defer common.KeepAlive(_buffer)
buffer := common.Dup(_buffer)
defer buffer.Release()
_, err = buffer.ReadFullFrom(conn, int(queryLength))
if err != nil {
return err
}
2022-09-13 16:18:39 +08:00
var message mDNS.Msg
2022-08-26 13:13:44 +08:00
err = message.Unpack(buffer.Bytes())
if err != nil {
return err
}
2022-09-04 12:39:43 +08:00
metadataInQuery := metadata
2022-08-26 13:13:44 +08:00
go func() error {
2022-09-04 12:39:43 +08:00
response, err := d.router.Exchange(adapter.WithContext(ctx, &metadataInQuery), &message)
2022-07-10 09:15:01 +08:00
if err != nil {
return err
}
2022-08-26 13:13:44 +08:00
_responseBuffer := buf.StackNewPacket()
defer common.KeepAlive(_responseBuffer)
responseBuffer := common.Dup(_responseBuffer)
defer responseBuffer.Release()
responseBuffer.Resize(2, 0)
2022-09-13 16:18:39 +08:00
n, err := response.PackBuffer(responseBuffer.FreeBytes())
2022-07-10 09:15:01 +08:00
if err != nil {
return err
}
2022-08-26 13:13:44 +08: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 09:15:01 +08:00
}
2022-07-23 09:15:47 +08:00
func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
2023-04-24 19:01:10 +08:00
var reader N.PacketReader = conn
var counters []N.CountFunc
2023-04-26 04:53:25 +08:00
var cachedPackets []*N.PacketBuffer
2023-04-24 19:01:10 +08:00
for {
reader, counters = N.UnwrapCountPacketReader(reader, counters)
if cachedReader, isCached := reader.(N.CachedPacketReader); isCached {
packet := cachedReader.ReadCachedPacket()
if packet != nil {
2023-04-26 04:53:25 +08:00
cachedPackets = append(cachedPackets, packet)
2023-04-24 19:01:10 +08:00
continue
}
}
if readWaiter, created := bufio.CreatePacketReadWaiter(reader); created {
2023-04-26 04:53:25 +08:00
return d.newPacketConnection(ctx, conn, readWaiter, counters, cachedPackets, metadata)
2023-04-24 19:01:10 +08:00
}
break
}
2022-07-11 12:56:57 +08:00
ctx = adapter.WithContext(ctx, &metadata)
2023-04-10 13:00:57 +08:00
fastClose, cancel := common.ContextWithCancelCause(ctx)
2022-07-25 22:02:39 +08:00
timeout := canceler.New(fastClose, cancel, C.DNSTimeout)
2022-08-03 17:11:53 +08:00
var group task.Group
group.Append0(func(ctx context.Context) error {
2022-08-26 13:13:44 +08:00
_buffer := buf.StackNewSize(dns.FixedPacketSize)
2022-07-25 22:02:39 +08:00
defer common.KeepAlive(_buffer)
buffer := common.Dup(_buffer)
defer buffer.Release()
2022-07-24 21:25:41 +08:00
for {
buffer.FullReset()
destination, err := conn.ReadPacket(buffer)
2022-07-10 09:15:01 +08:00
if err != nil {
2023-04-10 13:00:57 +08:00
cancel(err)
2022-07-10 09:15:01 +08:00
return err
}
2022-09-13 16:18:39 +08:00
var message mDNS.Msg
2022-07-24 21:25:41 +08:00
err = message.Unpack(buffer.Bytes())
2022-07-10 09:15:01 +08:00
if err != nil {
2023-04-10 13:00:57 +08:00
cancel(err)
2022-07-10 09:15:01 +08:00
return err
}
2022-07-25 22:02:39 +08:00
timeout.Update()
2022-09-04 12:39:43 +08:00
metadataInQuery := metadata
2022-07-24 21:25:41 +08:00
go func() error {
2022-09-04 12:39:43 +08:00
response, err := d.router.Exchange(adapter.WithContext(ctx, &metadataInQuery), &message)
2022-07-24 21:25:41 +08:00
if err != nil {
2023-04-10 13:00:57 +08:00
cancel(err)
2022-07-24 21:25:41 +08:00
return err
}
2022-07-25 22:02:39 +08:00
timeout.Update()
2022-08-02 13:43:25 +08:00
responseBuffer := buf.NewPacket()
2022-09-13 16:18:39 +08:00
n, err := response.PackBuffer(responseBuffer.FreeBytes())
2022-07-24 21:25:41 +08:00
if err != nil {
2023-04-10 13:00:57 +08:00
cancel(err)
2022-08-02 13:43:25 +08:00
responseBuffer.Release()
2022-07-24 21:25:41 +08:00
return err
}
responseBuffer.Truncate(len(n))
err = conn.WritePacket(responseBuffer, destination)
2023-04-10 13:00:57 +08:00
if err != nil {
cancel(err)
}
2022-07-24 21:25:41 +08:00
return err
}()
}
})
2022-08-03 17:11:53 +08:00
group.Cleanup(func() {
conn.Close()
})
2022-08-12 12:13:57 +08:00
return group.Run(fastClose)
2022-07-10 09:15:01 +08:00
}
2023-04-24 19:01:10 +08:00
func (d *DNS) newPacketConnection(ctx context.Context, conn N.PacketConn, readWaiter N.PacketReadWaiter, readCounters []N.CountFunc, cached []*N.PacketBuffer, metadata adapter.InboundContext) error {
ctx = adapter.WithContext(ctx, &metadata)
fastClose, cancel := common.ContextWithCancelCause(ctx)
timeout := canceler.New(fastClose, cancel, C.DNSTimeout)
var group task.Group
group.Append0(func(ctx context.Context) error {
var buffer *buf.Buffer
newBuffer := func() *buf.Buffer {
if buffer != nil {
buffer.Release()
}
buffer = buf.NewSize(dns.FixedPacketSize)
buffer.FullReset()
return buffer
}
for {
var message mDNS.Msg
var destination M.Socksaddr
var err error
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()
if err != nil {
cancel(err)
return err
}
destination = packet.Destination
} else {
destination, err = readWaiter.WaitReadPacket(newBuffer)
if err != nil {
if buffer != nil {
buffer.Release()
}
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 {
response, err := d.router.Exchange(adapter.WithContext(ctx, &metadataInQuery), &message)
if err != nil {
cancel(err)
return err
}
timeout.Update()
responseBuffer := buf.NewPacket()
n, err := response.PackBuffer(responseBuffer.FreeBytes())
if err != nil {
cancel(err)
responseBuffer.Release()
return err
}
responseBuffer.Truncate(len(n))
err = conn.WritePacket(responseBuffer, destination)
if err != nil {
cancel(err)
}
return err
}()
}
})
group.Cleanup(func() {
conn.Close()
})
return group.Run(fastClose)
}