chore: share RelayDnsPacket function code
Some checks are pending
Build / build (map[goarch:386 goos:freebsd output:386]) (push) Waiting to run
Build / build (map[goarch:386 goos:linux goversion:1.20 output:386-go120]) (push) Waiting to run
Build / build (map[goarch:386 goos:linux output:386]) (push) Waiting to run
Build / build (map[abi:1 goarch:loong64 goos:linux output:loong64-abi1]) (push) Waiting to run
Build / build (map[abi:2 goarch:loong64 goos:linux output:loong64-abi2]) (push) Waiting to run
Build / build (map[goamd64:v1 goarch:amd64 goos:darwin goversion:1.20 output:amd64-compatible-go120]) (push) Waiting to run
Build / build (map[goamd64:v1 goarch:amd64 goos:darwin output:amd64-compatible]) (push) Waiting to run
Build / build (map[goamd64:v1 goarch:amd64 goos:freebsd output:amd64-compatible]) (push) Waiting to run
Build / build (map[goamd64:v1 goarch:amd64 goos:linux goversion:1.20 output:amd64-compatible-go120 test:test]) (push) Waiting to run
Build / build (map[goamd64:v1 goarch:amd64 goos:linux output:amd64-compatible test:test]) (push) Waiting to run
Build / build (map[goamd64:v1 goarch:amd64 goos:windows goversion:1.20 output:amd64-compatible-go120]) (push) Waiting to run
Build / build (map[goamd64:v1 goarch:amd64 goos:windows output:amd64-compatible]) (push) Waiting to run
Build / build (map[goamd64:v3 goarch:amd64 goos:darwin goversion:1.20 output:amd64-go120]) (push) Waiting to run
Build / build (map[goamd64:v3 goarch:amd64 goos:darwin output:amd64]) (push) Waiting to run
Build / build (map[goamd64:v3 goarch:amd64 goos:freebsd output:amd64]) (push) Waiting to run
Build / build (map[goamd64:v3 goarch:amd64 goos:linux goversion:1.20 output:amd64-go120]) (push) Waiting to run
Build / build (map[goamd64:v3 goarch:amd64 goos:linux output:amd64]) (push) Waiting to run
Build / build (map[goamd64:v3 goarch:amd64 goos:windows goversion:1.20 output:amd64-go120]) (push) Waiting to run
Build / build (map[goamd64:v3 goarch:amd64 goos:windows output:amd64]) (push) Waiting to run
Build / build (map[goarch:386 goos:android ndk:i686-linux-android34 output:386]) (push) Waiting to run
Build / build (map[goarch:386 goos:windows goversion:1.20 output:386-go120]) (push) Waiting to run
Build / build (map[goarch:386 goos:windows output:386]) (push) Waiting to run
Build / build (map[goarch:amd64 goos:android ndk:x86_64-linux-android34 output:amd64]) (push) Waiting to run
Build / build (map[goarch:arm goarm:7 goos:linux output:armv7]) (push) Waiting to run
Build / build (map[goarch:arm goarm:7 goos:windows output:armv7]) (push) Waiting to run
Build / build (map[goarch:arm goos:android ndk:armv7a-linux-androideabi34 output:armv7]) (push) Waiting to run
Build / build (map[goarch:arm64 goos:android ndk:aarch64-linux-android34 output:arm64-v8]) (push) Waiting to run
Build / build (map[goarch:arm64 goos:darwin goversion:1.20 output:arm64-go120]) (push) Waiting to run
Build / build (map[goarch:arm64 goos:darwin output:arm64]) (push) Waiting to run
Build / build (map[goarch:arm64 goos:freebsd output:arm64]) (push) Waiting to run
Build / build (map[goarch:arm64 goos:linux output:arm64]) (push) Waiting to run
Build / build (map[goarch:arm64 goos:windows output:arm64]) (push) Waiting to run
Build / build (map[goarch:mips goos:linux mips:hardfloat output:mips-hardfloat]) (push) Waiting to run
Build / build (map[goarch:mips goos:linux mips:softfloat output:mips-softfloat]) (push) Waiting to run
Build / build (map[goarch:mips64 goos:linux output:mips64]) (push) Waiting to run
Build / build (map[goarch:mips64le goos:linux output:mips64le]) (push) Waiting to run
Build / build (map[goarch:mipsle goos:linux mips:hardfloat output:mipsle-hardfloat]) (push) Waiting to run
Build / build (map[goarch:mipsle goos:linux mips:softfloat output:mipsle-softfloat]) (push) Waiting to run
Build / build (map[goarch:riscv64 goos:linux output:riscv64]) (push) Waiting to run
Build / build (map[goarch:s390x goos:linux output:s390x]) (push) Waiting to run
Build / Upload-Prerelease (push) Blocked by required conditions
Build / Upload-Release (push) Blocked by required conditions
Build / Docker (push) Blocked by required conditions
Trigger CMFA Update / trigger-CMFA-update (push) Waiting to run

This commit is contained in:
wwqgtxx 2024-03-04 22:12:08 +08:00
parent fe4acebb8b
commit 8b9813079b
3 changed files with 102 additions and 108 deletions

View file

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net"
"net/netip"
"time"
"github.com/metacubex/mihomo/common/pool"
@ -12,8 +11,6 @@ import (
"github.com/metacubex/mihomo/component/resolver"
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log"
D "github.com/miekg/dns"
)
type Dns struct {
@ -79,12 +76,12 @@ func (d *dnsPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
}
func (d *dnsPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
ctx, cancel := context.WithTimeout(d.ctx, time.Second*5)
ctx, cancel := context.WithTimeout(d.ctx, resolver.DefaultDnsRelayTimeout)
defer cancel()
buf := pool.Get(2048)
buf := pool.Get(resolver.SafeDnsPacketSize)
put := func() { _ = pool.Put(buf) }
buf, err = RelayDnsPacket(ctx, p, buf)
buf, err = resolver.RelayDnsPacket(ctx, p, buf)
if err != nil {
put()
return 0, err
@ -110,7 +107,11 @@ func (d *dnsPacketConn) Close() error {
}
func (*dnsPacketConn) LocalAddr() net.Addr {
return net.UDPAddrFromAddrPort(netip.MustParseAddrPort("127.0.0.1:53"))
return &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 53,
Zone: "",
}
}
func (*dnsPacketConn) SetDeadline(t time.Time) error {
@ -139,22 +140,3 @@ func NewDnsWithOption(option DnsOption) *Dns {
},
}
}
// copied from listener/sing_mux/dns.go
func RelayDnsPacket(ctx context.Context, payload []byte, target []byte) ([]byte, error) {
msg := &D.Msg{}
if err := msg.Unpack(payload); err != nil {
return nil, err
}
r, err := resolver.ServeMsg(ctx, msg)
if err != nil {
m := new(D.Msg)
m.SetRcode(msg, D.RcodeServerFailure)
return m.PackBuffer(target)
}
r.SetRcode(msg, r.Rcode)
r.Compress = true
return r.PackBuffer(target)
}

View file

@ -0,0 +1,88 @@
package resolver
import (
"context"
"encoding/binary"
"io"
"net"
"time"
"github.com/metacubex/mihomo/common/pool"
D "github.com/miekg/dns"
)
const DefaultDnsReadTimeout = time.Second * 10
const DefaultDnsRelayTimeout = time.Second * 5
const SafeDnsPacketSize = 2 * 1024 // safe size which is 1232 from https://dnsflagday.net/2020/, so 2048 is enough
func RelayDnsConn(ctx context.Context, conn net.Conn) error {
buff := pool.Get(pool.UDPBufferSize)
defer func() {
_ = pool.Put(buff)
_ = conn.Close()
}()
for {
if conn.SetReadDeadline(time.Now().Add(DefaultDnsReadTimeout)) != nil {
break
}
length := uint16(0)
if err := binary.Read(conn, binary.BigEndian, &length); err != nil {
break
}
if int(length) > len(buff) {
break
}
n, err := io.ReadFull(conn, buff[:length])
if err != nil {
break
}
err = func() error {
ctx, cancel := context.WithTimeout(ctx, DefaultDnsRelayTimeout)
defer cancel()
inData := buff[:n]
msg, err := RelayDnsPacket(ctx, inData, buff)
if err != nil {
return err
}
err = binary.Write(conn, binary.BigEndian, uint16(len(msg)))
if err != nil {
return err
}
_, err = conn.Write(msg)
if err != nil {
return err
}
return nil
}()
if err != nil {
return err
}
}
return nil
}
func RelayDnsPacket(ctx context.Context, payload []byte, target []byte) ([]byte, error) {
msg := &D.Msg{}
if err := msg.Unpack(payload); err != nil {
return nil, err
}
r, err := ServeMsg(ctx, msg)
if err != nil {
m := new(D.Msg)
m.SetRcode(msg, D.RcodeServerFailure)
return m.PackBuffer(target)
}
r.SetRcode(msg, r.Rcode)
r.Compress = true
return r.PackBuffer(target)
}

View file

@ -2,29 +2,21 @@ package sing_tun
import (
"context"
"encoding/binary"
"io"
"net"
"net/netip"
"sync"
"time"
"github.com/metacubex/mihomo/common/pool"
"github.com/metacubex/mihomo/component/resolver"
"github.com/metacubex/mihomo/listener/sing"
"github.com/metacubex/mihomo/log"
D "github.com/miekg/dns"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/network"
)
const DefaultDnsReadTimeout = time.Second * 10
const DefaultDnsRelayTimeout = time.Second * 5
type ListenerHandler struct {
*sing.ListenerHandler
DnsAdds []netip.AddrPort
@ -45,61 +37,11 @@ func (h *ListenerHandler) ShouldHijackDns(targetAddr netip.AddrPort) bool {
func (h *ListenerHandler) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
if h.ShouldHijackDns(metadata.Destination.AddrPort()) {
log.Debugln("[DNS] hijack tcp:%s", metadata.Destination.String())
buff := pool.Get(pool.UDPBufferSize)
defer func() {
_ = pool.Put(buff)
_ = conn.Close()
}()
for {
if conn.SetReadDeadline(time.Now().Add(DefaultDnsReadTimeout)) != nil {
break
}
length := uint16(0)
if err := binary.Read(conn, binary.BigEndian, &length); err != nil {
break
}
if int(length) > len(buff) {
break
}
n, err := io.ReadFull(conn, buff[:length])
if err != nil {
break
}
err = func() error {
ctx, cancel := context.WithTimeout(ctx, DefaultDnsRelayTimeout)
defer cancel()
inData := buff[:n]
msg, err := RelayDnsPacket(ctx, inData, buff)
if err != nil {
return err
}
err = binary.Write(conn, binary.BigEndian, uint16(len(msg)))
if err != nil {
return err
}
_, err = conn.Write(msg)
if err != nil {
return err
}
return nil
}()
if err != nil {
return err
}
}
return nil
return resolver.RelayDnsConn(ctx, conn)
}
return h.ListenerHandler.NewConnection(ctx, conn, metadata)
}
const SafeDnsPacketSize = 2 * 1024 // safe size which is 1232 from https://dnsflagday.net/2020/, so 2048 is enough
func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.PacketConn, metadata M.Metadata) error {
if h.ShouldHijackDns(metadata.Destination.AddrPort()) {
log.Debugln("[DNS] hijack udp:%s from %s", metadata.Destination.String(), metadata.Source.String())
@ -114,7 +56,7 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
rwOptions := network.ReadWaitOptions{
FrontHeadroom: network.CalculateFrontHeadroom(conn),
RearHeadroom: network.CalculateRearHeadroom(conn),
MTU: SafeDnsPacketSize,
MTU: resolver.SafeDnsPacketSize,
}
readWaiter, isReadWaiter := bufio.CreatePacketReadWaiter(conn)
if isReadWaiter {
@ -126,7 +68,7 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
dest M.Socksaddr
err error
)
_ = conn.SetReadDeadline(time.Now().Add(DefaultDnsReadTimeout))
_ = conn.SetReadDeadline(time.Now().Add(resolver.DefaultDnsReadTimeout))
readBuff = nil // clear last loop status, avoid repeat release
if isReadWaiter {
readBuff, dest, err = readWaiter.WaitReadPacket()
@ -147,15 +89,15 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
return err
}
go func() {
ctx, cancel := context.WithTimeout(ctx, DefaultDnsRelayTimeout)
ctx, cancel := context.WithTimeout(ctx, resolver.DefaultDnsRelayTimeout)
defer cancel()
inData := readBuff.Bytes()
writeBuff := readBuff
writeBuff.Resize(writeBuff.Start(), 0)
if len(writeBuff.FreeBytes()) < SafeDnsPacketSize { // only create a new buffer when space don't enough
if len(writeBuff.FreeBytes()) < resolver.SafeDnsPacketSize { // only create a new buffer when space don't enough
writeBuff = rwOptions.NewPacketBuffer()
}
msg, err := RelayDnsPacket(ctx, inData, writeBuff.FreeBytes())
msg, err := resolver.RelayDnsPacket(ctx, inData, writeBuff.FreeBytes())
if writeBuff != readBuff {
readBuff.Release()
}
@ -182,21 +124,3 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
}
return h.ListenerHandler.NewPacketConnection(ctx, conn, metadata)
}
func RelayDnsPacket(ctx context.Context, payload []byte, target []byte) ([]byte, error) {
msg := &D.Msg{}
if err := msg.Unpack(payload); err != nil {
return nil, err
}
r, err := resolver.ServeMsg(ctx, msg)
if err != nil {
m := new(D.Msg)
m.SetRcode(msg, D.RcodeServerFailure)
return m.PackBuffer(target)
}
r.SetRcode(msg, r.Rcode)
r.Compress = true
return r.PackBuffer(target)
}