From bc453cfbc191009022257c59e07c1e6f5931a478 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 19 Aug 2019 14:08:48 +0800 Subject: [PATCH] Update middleware.go --- component/fakedns/middleware.go | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/component/fakedns/middleware.go b/component/fakedns/middleware.go index 68b779e..7cd23bd 100644 --- a/component/fakedns/middleware.go +++ b/component/fakedns/middleware.go @@ -4,18 +4,13 @@ import ( "net" "strings" - trie "github.com/xjasonlyu/tun2socks/common/domain-trie" - "github.com/xjasonlyu/tun2socks/common/fakeip" - "github.com/xjasonlyu/tun2socks/log" - D "github.com/miekg/dns" + + T "github.com/xjasonlyu/tun2socks/common/domain-trie" + F "github.com/xjasonlyu/tun2socks/common/fakeip" ) -var ( - BackendDNS []string -) - -func dnsExchange(r *D.Msg) (msg *D.Msg) { +func dnsExchange(backendDNS []string, r *D.Msg) (msg *D.Msg) { defer func() { if msg == nil { // empty DNS response @@ -29,16 +24,17 @@ func dnsExchange(r *D.Msg) (msg *D.Msg) { c := new(D.Client) c.Net = "tcp" - for _, dns := range BackendDNS { + for _, dns := range backendDNS { msg, _, _ = c.Exchange(r, dns) if msg != nil { + // success, exit query. break } } return msg } -func resolve(hosts *trie.Trie, pool *fakeip.Pool, r *D.Msg) (msg *D.Msg) { +func resolve(hosts *T.Trie, pool *F.Pool, backendDNS []string, r *D.Msg) (msg *D.Msg) { defer func() { if msg != nil { msg.SetReply(r) @@ -51,14 +47,13 @@ func resolve(hosts *trie.Trie, pool *fakeip.Pool, r *D.Msg) (msg *D.Msg) { q := r.Question[0] if q.Qtype != D.TypeA || q.Qclass != D.ClassINET { - log.Debugf("DNS Query: %v %v %v", q.Name, q.Qclass, q.Qtype) - return dnsExchange(r) + return dnsExchange(backendDNS, r) } return fakeResolve(pool, r) } -func fakeResolve(pool *fakeip.Pool, r *D.Msg) *D.Msg { +func fakeResolve(pool *F.Pool, r *D.Msg) *D.Msg { q := r.Question[0] host := strings.TrimRight(q.Name, ".") @@ -73,7 +68,7 @@ func fakeResolve(pool *fakeip.Pool, r *D.Msg) *D.Msg { return msg } -func hostResolve(hosts *trie.Trie, r *D.Msg) *D.Msg { +func hostResolve(hosts *T.Trie, r *D.Msg) *D.Msg { if hosts == nil { return nil } @@ -114,11 +109,3 @@ func hostResolve(hosts *trie.Trie, r *D.Msg) *D.Msg { setMsgTTL(msg, dnsDefaultTTL) return msg } - -func newHandler(hosts *trie.Trie, pool *fakeip.Pool) D.HandlerFunc { - return func(w D.ResponseWriter, r *D.Msg) { - msg := resolve(hosts, pool, r) - w.WriteMsg(msg) - return - } -}