2024-03-04 18:21:50 +08:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2024-03-07 13:12:40 +08:00
|
|
|
N "github.com/metacubex/mihomo/common/net"
|
2024-03-04 19:00:19 +08:00
|
|
|
"github.com/metacubex/mihomo/common/pool"
|
2024-03-04 18:21:50 +08:00
|
|
|
"github.com/metacubex/mihomo/component/dialer"
|
|
|
|
"github.com/metacubex/mihomo/component/resolver"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Dns struct {
|
|
|
|
*Base
|
|
|
|
}
|
|
|
|
|
|
|
|
type DnsOption struct {
|
|
|
|
BasicOption
|
|
|
|
Name string `proxy:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext implements C.ProxyAdapter
|
|
|
|
func (d *Dns) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
2024-03-07 13:12:40 +08:00
|
|
|
left, right := N.Pipe()
|
|
|
|
go resolver.RelayDnsConn(context.Background(), right, 0)
|
|
|
|
return NewConn(left, d), nil
|
2024-03-04 18:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
|
|
|
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())
|
|
|
|
|
2024-03-04 19:00:19 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2024-03-04 18:21:50 +08:00
|
|
|
return newPacketConn(&dnsPacketConn{
|
2024-03-04 19:00:19 +08:00
|
|
|
response: make(chan dnsPacket, 1),
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
2024-03-04 18:21:50 +08:00
|
|
|
}, d), nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 19:00:19 +08:00
|
|
|
type dnsPacket struct {
|
|
|
|
data []byte
|
|
|
|
put func()
|
|
|
|
addr net.Addr
|
|
|
|
}
|
|
|
|
|
2024-03-04 18:21:50 +08:00
|
|
|
// dnsPacketConn implements net.PacketConn
|
|
|
|
type dnsPacketConn struct {
|
2024-03-04 19:00:19 +08:00
|
|
|
response chan dnsPacket
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2024-03-04 18:21:50 +08:00
|
|
|
}
|
|
|
|
|
2024-03-04 19:00:19 +08:00
|
|
|
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
|
2024-03-04 18:21:50 +08:00
|
|
|
}
|
2024-03-04 19:00:19 +08:00
|
|
|
}
|
2024-03-04 18:21:50 +08:00
|
|
|
|
2024-03-04 19:00:19 +08:00
|
|
|
func (d *dnsPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
|
|
|
select {
|
|
|
|
case packet := <-d.response:
|
|
|
|
n = copy(p, packet.data)
|
|
|
|
if packet.put != nil {
|
|
|
|
packet.put()
|
|
|
|
}
|
|
|
|
return n, packet.addr, nil
|
|
|
|
case <-d.ctx.Done():
|
|
|
|
return 0, nil, net.ErrClosed
|
|
|
|
}
|
2024-03-04 18:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dnsPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
2024-03-07 13:12:40 +08:00
|
|
|
select {
|
|
|
|
case <-d.ctx.Done():
|
|
|
|
return 0, net.ErrClosed
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p) > resolver.SafeDnsPacketSize {
|
|
|
|
// wtf???
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 22:12:08 +08:00
|
|
|
buf := pool.Get(resolver.SafeDnsPacketSize)
|
2024-03-04 19:00:19 +08:00
|
|
|
put := func() { _ = pool.Put(buf) }
|
2024-03-07 13:12:40 +08:00
|
|
|
copy(buf, p) // avoid p be changed after WriteTo returned
|
2024-03-04 18:21:50 +08:00
|
|
|
|
2024-03-07 13:12:40 +08:00
|
|
|
go func() { // don't block the WriteTo function
|
2024-04-09 14:26:14 +08:00
|
|
|
ctx, cancel := context.WithTimeout(d.ctx, resolver.DefaultDnsRelayTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-03-07 13:12:40 +08:00
|
|
|
buf, err = resolver.RelayDnsPacket(ctx, buf[:len(p)], buf)
|
|
|
|
if err != nil {
|
|
|
|
put()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
packet := dnsPacket{
|
|
|
|
data: buf,
|
|
|
|
put: put,
|
|
|
|
addr: addr,
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case d.response <- packet:
|
|
|
|
break
|
|
|
|
case <-d.ctx.Done():
|
|
|
|
put()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return len(p), nil
|
2024-03-04 18:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dnsPacketConn) Close() error {
|
2024-03-04 19:00:19 +08:00
|
|
|
d.cancel()
|
2024-03-04 18:21:50 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*dnsPacketConn) LocalAddr() net.Addr {
|
2024-03-04 22:12:08 +08:00
|
|
|
return &net.UDPAddr{
|
|
|
|
IP: net.IPv4(127, 0, 0, 1),
|
|
|
|
Port: 53,
|
|
|
|
Zone: "",
|
|
|
|
}
|
2024-03-04 18:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*dnsPacketConn) SetDeadline(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*dnsPacketConn) SetReadDeadline(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*dnsPacketConn) SetWriteDeadline(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDnsWithOption(option DnsOption) *Dns {
|
|
|
|
return &Dns{
|
|
|
|
Base: &Base{
|
|
|
|
name: option.Name,
|
2024-03-04 19:00:19 +08:00
|
|
|
tp: C.Dns,
|
2024-03-04 18:21:50 +08:00
|
|
|
udp: true,
|
|
|
|
tfo: option.TFO,
|
|
|
|
mpTcp: option.MPTCP,
|
|
|
|
iface: option.Interface,
|
|
|
|
rmark: option.RoutingMark,
|
|
|
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|