mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-16 20:25:32 +08:00
Improve udp dns close
This commit is contained in:
parent
fde33fbb30
commit
9e9e6f7ee6
|
@ -325,7 +325,7 @@ func (a *myInboundAdapter) NewError(ctx context.Context, err error) {
|
||||||
func NewError(logger log.ContextLogger, ctx context.Context, err error) {
|
func NewError(logger log.ContextLogger, ctx context.Context, err error) {
|
||||||
common.Close(err)
|
common.Close(err)
|
||||||
if E.IsClosedOrCanceled(err) {
|
if E.IsClosedOrCanceled(err) {
|
||||||
logger.DebugContext(ctx, "connection closed")
|
logger.TraceContext(ctx, "connection closed: ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.ErrorContext(ctx, err)
|
logger.ErrorContext(ctx, err)
|
||||||
|
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
C "github.com/sagernet/sing-box/constant"
|
C "github.com/sagernet/sing-box/constant"
|
||||||
|
@ -14,6 +16,7 @@ import (
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
"github.com/sagernet/sing/common/task"
|
||||||
|
|
||||||
"golang.org/x/net/dns/dnsmessage"
|
"golang.org/x/net/dns/dnsmessage"
|
||||||
)
|
)
|
||||||
|
@ -45,6 +48,7 @@ func (d *DNS) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.Pa
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DNS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
func (d *DNS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
||||||
|
defer conn.Close()
|
||||||
ctx = adapter.WithContext(ctx, &metadata)
|
ctx = adapter.WithContext(ctx, &metadata)
|
||||||
_buffer := buf.StackNewSize(1024)
|
_buffer := buf.StackNewSize(1024)
|
||||||
defer common.KeepAlive(_buffer)
|
defer common.KeepAlive(_buffer)
|
||||||
|
@ -97,45 +101,69 @@ func (d *DNS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
||||||
|
defer conn.Close()
|
||||||
ctx = adapter.WithContext(ctx, &metadata)
|
ctx = adapter.WithContext(ctx, &metadata)
|
||||||
_buffer := buf.StackNewSize(1024)
|
_buffer := buf.StackNewSize(1024)
|
||||||
defer common.KeepAlive(_buffer)
|
defer common.KeepAlive(_buffer)
|
||||||
buffer := common.Dup(_buffer)
|
buffer := common.Dup(_buffer)
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
for {
|
var wg sync.WaitGroup
|
||||||
buffer.FullReset()
|
fastClose, cancel := context.WithCancel(ctx)
|
||||||
destination, err := conn.ReadPacket(buffer)
|
err := task.Run(fastClose, func() error {
|
||||||
if err != nil {
|
var count int
|
||||||
return err
|
for {
|
||||||
}
|
buffer.FullReset()
|
||||||
var message dnsmessage.Message
|
destination, err := conn.ReadPacket(buffer)
|
||||||
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])
|
|
||||||
d.logger.DebugContext(ctx, "inbound dns query ", formatDNSQuestion(question), " from ", metadata.Source)
|
|
||||||
}
|
|
||||||
go func() error {
|
|
||||||
response, err := d.router.Exchange(ctx, &message)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_responseBuffer := buf.StackNewSize(1024)
|
var message dnsmessage.Message
|
||||||
defer common.KeepAlive(_responseBuffer)
|
err = message.Unpack(buffer.Bytes())
|
||||||
responseBuffer := common.Dup(_responseBuffer)
|
|
||||||
defer responseBuffer.Release()
|
|
||||||
n, err := response.AppendPack(responseBuffer.Index(0))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
responseBuffer.Truncate(len(n))
|
if len(message.Questions) > 0 {
|
||||||
err = conn.WritePacket(responseBuffer, destination)
|
question := message.Questions[0]
|
||||||
return err
|
metadata.Domain = string(question.Name.Data[:question.Name.Length-1])
|
||||||
}()
|
d.logger.DebugContext(ctx, "inbound dns query ", formatDNSQuestion(question), " from ", metadata.Source)
|
||||||
}
|
}
|
||||||
|
wg.Add(1)
|
||||||
|
go func() error {
|
||||||
|
defer wg.Done()
|
||||||
|
response, err := d.router.Exchange(ctx, &message)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_responseBuffer := buf.StackNewSize(1024)
|
||||||
|
defer common.KeepAlive(_responseBuffer)
|
||||||
|
responseBuffer := common.Dup(_responseBuffer)
|
||||||
|
defer responseBuffer.Release()
|
||||||
|
n, err := response.AppendPack(responseBuffer.Index(0))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
responseBuffer.Truncate(len(n))
|
||||||
|
err = conn.WritePacket(responseBuffer, destination)
|
||||||
|
return err
|
||||||
|
}()
|
||||||
|
count++
|
||||||
|
if count == 2 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cancel()
|
||||||
|
return nil
|
||||||
|
}, func() error {
|
||||||
|
timer := time.NewTimer(5 * time.Second)
|
||||||
|
select {
|
||||||
|
case <-timer.C:
|
||||||
|
cancel()
|
||||||
|
case <-fastClose.Done():
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
wg.Wait()
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatDNSQuestion(question dnsmessage.Question) string {
|
func formatDNSQuestion(question dnsmessage.Question) string {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user