mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-25 18:11:28 +00:00
Fix hijack_dns
This commit is contained in:
parent
6048b1e270
commit
a104d18277
|
@ -16,6 +16,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewDNSConnection(ctx context.Context, router adapter.Router, logger log.Logger, conn net.Conn, metadata adapter.InboundContext) error {
|
func NewDNSConnection(ctx context.Context, router adapter.Router, logger log.Logger, conn net.Conn, metadata adapter.InboundContext) error {
|
||||||
|
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)
|
||||||
|
@ -44,32 +45,38 @@ func NewDNSConnection(ctx context.Context, router adapter.Router, logger log.Log
|
||||||
metadata.Domain = string(question.Name.Data[:question.Name.Length-1])
|
metadata.Domain = string(question.Name.Data[:question.Name.Length-1])
|
||||||
logger.WithContext(ctx).Debug("inbound dns query ", formatDNSQuestion(question), " from ", metadata.Source)
|
logger.WithContext(ctx).Debug("inbound dns query ", formatDNSQuestion(question), " from ", metadata.Source)
|
||||||
}
|
}
|
||||||
response, err := router.Exchange(adapter.WithContext(ctx, &metadata), &message)
|
go func() error {
|
||||||
|
response, err := router.Exchange(ctx, &message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buffer.FullReset()
|
_responseBuffer := buf.StackNewSize(1024)
|
||||||
responseBuffer, err := response.AppendPack(buffer.Index(0))
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = binary.Write(conn, binary.BigEndian, uint16(len(responseBuffer)))
|
responseBuffer.Truncate(len(n))
|
||||||
if err != nil {
|
binary.BigEndian.PutUint16(responseBuffer.ExtendHeader(2), uint16(len(n)))
|
||||||
|
_, err = conn.Write(responseBuffer.Bytes())
|
||||||
return err
|
return err
|
||||||
}
|
}()
|
||||||
_, err = conn.Write(responseBuffer)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDNSPacketConnection(ctx context.Context, router adapter.Router, logger log.Logger, conn N.PacketConn, metadata adapter.InboundContext) error {
|
func NewDNSPacketConnection(ctx context.Context, router adapter.Router, logger log.Logger, conn N.PacketConn, metadata adapter.InboundContext) error {
|
||||||
|
ctx = adapter.WithContext(ctx, &metadata)
|
||||||
|
_buffer := buf.StackNewSize(1024)
|
||||||
|
defer common.KeepAlive(_buffer)
|
||||||
|
buffer := common.Dup(_buffer)
|
||||||
|
defer buffer.Release()
|
||||||
for {
|
for {
|
||||||
buffer := buf.StackNewSize(1024)
|
buffer.FullReset()
|
||||||
destination, err := conn.ReadPacket(buffer)
|
destination, err := conn.ReadPacket(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
buffer.Release()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var message dnsmessage.Message
|
var message dnsmessage.Message
|
||||||
|
@ -83,18 +90,20 @@ func NewDNSPacketConnection(ctx context.Context, router adapter.Router, logger l
|
||||||
logger.WithContext(ctx).Debug("inbound dns query ", formatDNSQuestion(question), " from ", metadata.Source)
|
logger.WithContext(ctx).Debug("inbound dns query ", formatDNSQuestion(question), " from ", metadata.Source)
|
||||||
}
|
}
|
||||||
go func() error {
|
go func() error {
|
||||||
defer buffer.Release()
|
response, err := router.Exchange(ctx, &message)
|
||||||
response, err := router.Exchange(adapter.WithContext(ctx, &metadata), &message)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buffer.FullReset()
|
_responseBuffer := buf.StackNewSize(1024)
|
||||||
responseBuffer, err := response.AppendPack(buffer.Index(0))
|
defer common.KeepAlive(_responseBuffer)
|
||||||
|
responseBuffer := common.Dup(_responseBuffer)
|
||||||
|
defer responseBuffer.Release()
|
||||||
|
n, err := response.AppendPack(responseBuffer.Index(0))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buffer.Truncate(len(responseBuffer))
|
responseBuffer.Truncate(len(n))
|
||||||
err = conn.WritePacket(buffer, destination)
|
err = conn.WritePacket(responseBuffer, destination)
|
||||||
return err
|
return err
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue