mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-01-08 10:23:43 +08:00
chore: rebuild DNS outbound code
This commit is contained in:
parent
3ec23c1fc5
commit
e867497315
@ -7,10 +7,12 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/metacubex/mihomo/common/pool"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
|
|
||||||
D "github.com/miekg/dns"
|
D "github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -32,52 +34,78 @@ func (d *Dns) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dia
|
|||||||
func (d *Dns) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (d *Dns) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
log.Debugln("[DNS] hijack udp:%s from %s", metadata.RemoteAddress(), metadata.SourceAddrPort())
|
log.Debugln("[DNS] hijack udp:%s from %s", metadata.RemoteAddress(), metadata.SourceAddrPort())
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
return newPacketConn(&dnsPacketConn{
|
return newPacketConn(&dnsPacketConn{
|
||||||
response: make(chan []byte),
|
response: make(chan dnsPacket, 1),
|
||||||
doneReading: make(chan int),
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
}, d), nil
|
}, d), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type dnsPacket struct {
|
||||||
|
data []byte
|
||||||
|
put func()
|
||||||
|
addr net.Addr
|
||||||
|
}
|
||||||
|
|
||||||
// dnsPacketConn implements net.PacketConn
|
// dnsPacketConn implements net.PacketConn
|
||||||
type dnsPacketConn struct {
|
type dnsPacketConn struct {
|
||||||
response chan []byte
|
response chan dnsPacket
|
||||||
writeTo net.Addr
|
ctx context.Context
|
||||||
doneReading chan int
|
cancel context.CancelFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *dnsPacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) {
|
||||||
|
select {
|
||||||
|
case packet := <-d.response:
|
||||||
|
return packet.data, packet.put, packet.addr, nil
|
||||||
|
case <-d.ctx.Done():
|
||||||
|
return nil, nil, nil, net.ErrClosed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dnsPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
func (d *dnsPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
||||||
buf := <-d.response
|
select {
|
||||||
|
case packet := <-d.response:
|
||||||
log.Debugln("[DNS] hijack ReadFrom, len %d", len(buf))
|
n = copy(p, packet.data)
|
||||||
|
if packet.put != nil {
|
||||||
if buf != nil {
|
packet.put()
|
||||||
n := copy(p, buf)
|
}
|
||||||
return n, d.writeTo, nil
|
return n, packet.addr, nil
|
||||||
|
case <-d.ctx.Done():
|
||||||
|
return 0, nil, net.ErrClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, nil, fmt.Errorf("read from closed dns packet conn")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dnsPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
func (d *dnsPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
||||||
log.Debugln("[DNS] hijack WriteTo %s, len %d", addr.String(), len(p))
|
ctx, cancel := context.WithTimeout(d.ctx, time.Second*5)
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
buf, err := RelayDnsPacket(ctx, p, make([]byte, 4096))
|
buf := pool.Get(2048)
|
||||||
|
put := func() { _ = pool.Put(buf) }
|
||||||
|
buf, err = RelayDnsPacket(ctx, p, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnln("[DNS] dns hijack: relay dns packet: %s", err)
|
put()
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
d.writeTo = addr
|
packet := dnsPacket{
|
||||||
d.response <- buf
|
data: buf,
|
||||||
|
put: put,
|
||||||
|
addr: addr,
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case d.response <- packet:
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
|
case <-d.ctx.Done():
|
||||||
|
put()
|
||||||
|
return 0, net.ErrClosed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dnsPacketConn) Close() error {
|
func (d *dnsPacketConn) Close() error {
|
||||||
close(d.response)
|
d.cancel()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,7 +129,7 @@ func NewDnsWithOption(option DnsOption) *Dns {
|
|||||||
return &Dns{
|
return &Dns{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
tp: C.Direct,
|
tp: C.Dns,
|
||||||
udp: true,
|
udp: true,
|
||||||
tfo: option.TFO,
|
tfo: option.TFO,
|
||||||
mpTcp: option.MPTCP,
|
mpTcp: option.MPTCP,
|
||||||
@ -130,14 +158,3 @@ func RelayDnsPacket(ctx context.Context, payload []byte, target []byte) ([]byte,
|
|||||||
r.Compress = true
|
r.Compress = true
|
||||||
return r.PackBuffer(target)
|
return r.PackBuffer(target)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDns() *Dns {
|
|
||||||
return &Dns{
|
|
||||||
Base: &Base{
|
|
||||||
name: "DNS",
|
|
||||||
tp: C.Dns,
|
|
||||||
udp: true,
|
|
||||||
prefer: C.DualStack,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user