mihomo/dns/middleware.go

196 lines
4 KiB
Go
Raw Normal View History

2019-07-14 19:29:58 +08:00
package dns
import (
"net"
2019-07-14 19:29:58 +08:00
"strings"
"time"
2019-07-14 19:29:58 +08:00
"github.com/Dreamacro/clash/common/cache"
2019-07-14 19:29:58 +08:00
"github.com/Dreamacro/clash/component/fakeip"
"github.com/Dreamacro/clash/component/trie"
2019-07-14 19:29:58 +08:00
"github.com/Dreamacro/clash/log"
D "github.com/miekg/dns"
)
type handler func(r *D.Msg) (*D.Msg, error)
2019-09-11 17:00:55 +08:00
type middleware func(next handler) handler
2019-07-14 19:29:58 +08:00
func withHosts(hosts *trie.DomainTrie) middleware {
return func(next handler) handler {
return func(r *D.Msg) (*D.Msg, error) {
q := r.Question[0]
if !isIPRequest(q) {
return next(r)
}
record := hosts.Search(strings.TrimRight(q.Name, "."))
if record == nil {
return next(r)
}
ip := record.Data.(net.IP)
msg := r.Copy()
if v4 := ip.To4(); v4 != nil && q.Qtype == D.TypeA {
rr := &D.A{}
rr.Hdr = D.RR_Header{Name: q.Name, Rrtype: D.TypeA, Class: D.ClassINET, Ttl: dnsDefaultTTL}
rr.A = v4
msg.Answer = []D.RR{rr}
} else if v6 := ip.To16(); v6 != nil && q.Qtype == D.TypeAAAA {
rr := &D.AAAA{}
rr.Hdr = D.RR_Header{Name: q.Name, Rrtype: D.TypeAAAA, Class: D.ClassINET, Ttl: dnsDefaultTTL}
rr.AAAA = v6
msg.Answer = []D.RR{rr}
} else {
return next(r)
}
msg.SetRcode(r, D.RcodeSuccess)
msg.Authoritative = true
msg.RecursionAvailable = true
return msg, nil
}
}
}
func withMapping(mapping *cache.LruCache) middleware {
return func(next handler) handler {
return func(r *D.Msg) (*D.Msg, error) {
q := r.Question[0]
if !isIPRequest(q) {
return next(r)
}
msg, err := next(r)
if err != nil {
return nil, err
}
host := strings.TrimRight(q.Name, ".")
for _, ans := range msg.Answer {
var ip net.IP
var ttl uint32
switch a := ans.(type) {
case *D.A:
ip = a.A
ttl = a.Hdr.Ttl
case *D.AAAA:
ip = a.AAAA
ttl = a.Hdr.Ttl
default:
continue
}
mapping.SetWithExpire(ip.String(), host, time.Now().Add(time.Second*time.Duration(ttl)))
}
return msg, nil
}
}
}
2019-09-11 17:00:55 +08:00
func withFakeIP(fakePool *fakeip.Pool) middleware {
return func(next handler) handler {
return func(r *D.Msg) (*D.Msg, error) {
2019-09-11 17:00:55 +08:00
q := r.Question[0]
2020-10-09 00:04:24 +08:00
host := strings.TrimRight(q.Name, ".")
if fakePool.LookupHost(host) {
return next(r)
}
if q.Qtype == D.TypeAAAA {
msg := &D.Msg{}
msg.Answer = []D.RR{}
msg.SetRcode(r, D.RcodeSuccess)
msg.Authoritative = true
msg.RecursionAvailable = true
return msg, nil
} else if q.Qtype != D.TypeA {
return next(r)
2019-09-11 17:00:55 +08:00
}
rr := &D.A{}
rr.Hdr = D.RR_Header{Name: q.Name, Rrtype: D.TypeA, Class: D.ClassINET, Ttl: dnsDefaultTTL}
ip := fakePool.Lookup(host)
rr.A = ip
msg := r.Copy()
msg.Answer = []D.RR{rr}
setMsgTTL(msg, 1)
msg.SetRcode(r, D.RcodeSuccess)
msg.Authoritative = true
msg.RecursionAvailable = true
return msg, nil
2019-09-11 17:00:55 +08:00
}
2019-07-14 19:29:58 +08:00
}
}
func withResolver(resolver *Resolver) handler {
return func(r *D.Msg) (*D.Msg, error) {
2020-06-18 18:11:02 +08:00
q := r.Question[0]
// return a empty AAAA msg when ipv6 disabled
if !resolver.ipv6 && q.Qtype == D.TypeAAAA {
msg := &D.Msg{}
msg.Answer = []D.RR{}
msg.SetRcode(r, D.RcodeSuccess)
msg.Authoritative = true
msg.RecursionAvailable = true
return msg, nil
2020-06-18 18:11:02 +08:00
}
2019-07-14 19:29:58 +08:00
msg, err := resolver.Exchange(r)
if err != nil {
2019-09-11 17:00:55 +08:00
log.Debugln("[DNS Server] Exchange %s failed: %v", q.String(), err)
return msg, err
2019-07-14 19:29:58 +08:00
}
msg.SetRcode(r, msg.Rcode)
msg.Authoritative = true
return msg, nil
2019-07-14 19:29:58 +08:00
}
}
2019-09-11 17:00:55 +08:00
func compose(middlewares []middleware, endpoint handler) handler {
length := len(middlewares)
h := endpoint
for i := length - 1; i >= 0; i-- {
middleware := middlewares[i]
h = middleware(h)
2019-07-14 19:29:58 +08:00
}
2019-09-11 17:00:55 +08:00
return h
2019-07-14 19:29:58 +08:00
}
func newHandler(resolver *Resolver, mapper *ResolverEnhancer) handler {
2019-09-11 17:00:55 +08:00
middlewares := []middleware{}
2019-07-14 19:29:58 +08:00
if resolver.hosts != nil {
middlewares = append(middlewares, withHosts(resolver.hosts))
}
if mapper.mode == FAKEIP {
middlewares = append(middlewares, withFakeIP(mapper.fakePool))
}
if mapper.mode != NORMAL {
middlewares = append(middlewares, withMapping(mapper.mapping))
2019-07-14 19:29:58 +08:00
}
2019-09-11 17:00:55 +08:00
return compose(middlewares, withResolver(resolver))
2019-07-14 19:29:58 +08:00
}