Improve dns log

This commit is contained in:
世界
2022-08-03 18:55:39 +08:00
parent f6f3390490
commit 8e4de29409
12 changed files with 154 additions and 66 deletions

View File

@@ -20,7 +20,7 @@ func New(ctx context.Context, router adapter.Router, logger log.ContextLogger, o
case C.TypeBlock:
return NewBlock(logger, options.Tag), nil
case C.TypeDNS:
return NewDNS(router, logger, options.Tag), nil
return NewDNS(router, options.Tag), nil
case C.TypeSocks:
return NewSocks(router, logger, options.Tag, options.SocksOptions)
case C.TypeHTTP:

View File

@@ -10,7 +10,6 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/canceler"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
@@ -26,13 +25,12 @@ type DNS struct {
myOutboundAdapter
}
func NewDNS(router adapter.Router, logger log.ContextLogger, tag string) *DNS {
func NewDNS(router adapter.Router, tag string) *DNS {
return &DNS{
myOutboundAdapter{
protocol: C.TypeDNS,
network: []string{N.NetworkTCP, N.NetworkUDP},
router: router,
logger: logger,
tag: tag,
},
}
@@ -75,7 +73,6 @@ func (d *DNS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter
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)
@@ -124,7 +121,6 @@ func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metada
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)
}
timeout.Update()
go func() error {
@@ -150,17 +146,3 @@ func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metada
})
return group.Run(ctx)
}
func formatDNSQuestion(question dnsmessage.Question) string {
var qType string
qType = question.Type.String()
if len(qType) > 4 {
qType = qType[4:]
}
var qClass string
qClass = question.Class.String()
if len(qClass) > 5 {
qClass = qClass[5:]
}
return string(question.Name.Data[:question.Name.Length-1]) + " " + qType + " " + qClass
}