sing-box/outbound/dns.go

153 lines
4.1 KiB
Go
Raw Normal View History

2022-07-23 01:15:47 +00:00
package outbound
2022-07-10 01:15:01 +00:00
import (
"context"
"encoding/binary"
"io"
"net"
2022-07-23 01:15:47 +00:00
"os"
2022-07-10 01:15:01 +00:00
"github.com/sagernet/sing-box/adapter"
2022-07-25 14:02:39 +00:00
"github.com/sagernet/sing-box/common/canceler"
2022-07-15 00:42:02 +00:00
C "github.com/sagernet/sing-box/constant"
2022-07-10 01:15:01 +00:00
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
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
"golang.org/x/net/dns/dnsmessage"
)
2022-07-23 01:15:47 +00:00
var _ adapter.Outbound = (*DNS)(nil)
2022-07-15 00:42:02 +00:00
type DNS struct {
2022-07-23 01:15:47 +00:00
myOutboundAdapter
2022-07-15 00:42:02 +00:00
}
2022-07-23 01:15:47 +00:00
func NewDNS(router adapter.Router, logger log.ContextLogger, tag string) *DNS {
return &DNS{
myOutboundAdapter{
protocol: C.TypeDNS,
2022-07-29 16:29:22 +00:00
network: []string{N.NetworkTCP, N.NetworkUDP},
2022-07-23 01:15:47 +00:00
router: router,
logger: logger,
tag: tag,
2022-07-15 00:42:02 +00:00
},
}
}
2022-07-23 01:15:47 +00:00
func (d *DNS) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
return nil, os.ErrInvalid
2022-07-15 00:42:02 +00:00
}
2022-07-23 01:15:47 +00:00
func (d *DNS) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
return nil, os.ErrInvalid
2022-07-15 00:42:02 +00:00
}
2022-07-23 01:15:47 +00:00
func (d *DNS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-07-24 13:25:41 +00:00
defer conn.Close()
2022-07-11 04:56:57 +00:00
ctx = adapter.WithContext(ctx, &metadata)
2022-07-10 01:15:01 +00:00
_buffer := buf.StackNewSize(1024)
defer common.KeepAlive(_buffer)
buffer := common.Dup(_buffer)
defer buffer.Release()
for {
var queryLength uint16
err := binary.Read(conn, binary.BigEndian, &queryLength)
if err != nil {
return err
}
if queryLength > 1024 {
return io.ErrShortBuffer
}
buffer.FullReset()
_, err = buffer.ReadFullFrom(conn, int(queryLength))
if err != nil {
return err
}
var message dnsmessage.Message
err = message.Unpack(buffer.Bytes())
if err != nil {
return err
}
if len(message.Questions) > 0 {
question := message.Questions[0]
metadata.Domain = string(question.Name.Data[:question.Name.Length-1])
2022-07-23 01:15:47 +00:00
d.logger.DebugContext(ctx, "inbound dns query ", formatDNSQuestion(question), " from ", metadata.Source)
2022-07-10 01:15:01 +00:00
}
2022-07-11 04:56:57 +00:00
go func() error {
2022-07-23 01:15:47 +00:00
response, err := d.router.Exchange(ctx, &message)
2022-07-11 04:56:57 +00:00
if err != nil {
return err
}
2022-07-31 00:48:21 +00:00
_responseBuffer := buf.StackNewPacket()
2022-07-11 04:56:57 +00:00
defer common.KeepAlive(_responseBuffer)
responseBuffer := common.Dup(_responseBuffer)
defer responseBuffer.Release()
responseBuffer.Resize(2, 0)
n, err := response.AppendPack(responseBuffer.Index(0))
if err != nil {
return err
}
responseBuffer.Truncate(len(n))
binary.BigEndian.PutUint16(responseBuffer.ExtendHeader(2), uint16(len(n)))
_, err = conn.Write(responseBuffer.Bytes())
2022-07-10 01:15:01 +00:00
return err
2022-07-11 04:56:57 +00:00
}()
2022-07-10 01:15:01 +00:00
}
}
2022-07-23 01:15:47 +00:00
func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
2022-07-24 13:25:41 +00:00
defer conn.Close()
2022-07-11 04:56:57 +00:00
ctx = adapter.WithContext(ctx, &metadata)
2022-07-24 13:25:41 +00:00
fastClose, cancel := context.WithCancel(ctx)
2022-07-25 14:02:39 +00:00
timeout := canceler.New(fastClose, cancel, C.DNSTimeout)
return task.Run(fastClose, func() error {
defer cancel()
_buffer := buf.StackNewSize(1024)
defer common.KeepAlive(_buffer)
buffer := common.Dup(_buffer)
defer buffer.Release()
2022-07-24 13:25:41 +00:00
for {
buffer.FullReset()
destination, err := conn.ReadPacket(buffer)
2022-07-10 01:15:01 +00:00
if err != nil {
return err
}
2022-07-24 13:25:41 +00:00
var message dnsmessage.Message
err = message.Unpack(buffer.Bytes())
2022-07-10 01:15:01 +00:00
if err != nil {
return err
}
2022-07-24 13:25:41 +00:00
if len(message.Questions) > 0 {
question := message.Questions[0]
metadata.Domain = string(question.Name.Data[:question.Name.Length-1])
d.logger.DebugContext(ctx, "inbound dns query ", formatDNSQuestion(question), " from ", metadata.Source)
}
2022-07-25 14:02:39 +00:00
timeout.Update()
2022-07-24 13:25:41 +00:00
go func() error {
response, err := d.router.Exchange(ctx, &message)
if err != nil {
return err
}
2022-07-25 14:02:39 +00:00
timeout.Update()
2022-08-02 05:43:25 +00:00
responseBuffer := buf.NewPacket()
2022-07-24 13:25:41 +00:00
n, err := response.AppendPack(responseBuffer.Index(0))
if err != nil {
2022-08-02 05:43:25 +00:00
responseBuffer.Release()
2022-07-24 13:25:41 +00:00
return err
}
responseBuffer.Truncate(len(n))
err = conn.WritePacket(responseBuffer, destination)
return err
}()
}
})
2022-07-10 01:15:01 +00:00
}
func formatDNSQuestion(question dnsmessage.Question) string {
return string(question.Name.Data[:question.Name.Length-1]) + " " + question.Type.String()[4:] + " " + question.Class.String()[5:]
}