github.com/xxf098/lite-proxy@v0.15.1-0.20230422081941-12c69f323218/dns/util.go (about) 1 package dns 2 3 import ( 4 "crypto/tls" 5 "net" 6 "time" 7 8 D "github.com/miekg/dns" 9 "github.com/xxf098/lite-proxy/common/cache" 10 ) 11 12 func putMsgToCache(c *cache.LruCache, key string, msg *D.Msg) { 13 var ttl uint32 14 switch { 15 case len(msg.Answer) != 0: 16 ttl = msg.Answer[0].Header().Ttl 17 case len(msg.Ns) != 0: 18 ttl = msg.Ns[0].Header().Ttl 19 case len(msg.Extra) != 0: 20 ttl = msg.Extra[0].Header().Ttl 21 default: 22 // log.D("[DNS] response msg empty: %#v", msg) 23 return 24 } 25 26 c.SetWithExpire(key, msg.Copy(), time.Now().Add(time.Second*time.Duration(ttl))) 27 } 28 29 func setMsgTTL(msg *D.Msg, ttl uint32) { 30 for _, answer := range msg.Answer { 31 answer.Header().Ttl = ttl 32 } 33 34 for _, ns := range msg.Ns { 35 ns.Header().Ttl = ttl 36 } 37 38 for _, extra := range msg.Extra { 39 extra.Header().Ttl = ttl 40 } 41 } 42 43 func isIPRequest(q D.Question) bool { 44 return q.Qclass == D.ClassINET && (q.Qtype == D.TypeA || q.Qtype == D.TypeAAAA) 45 } 46 47 func transform(servers []NameServer, resolver *Resolver) []dnsClient { 48 ret := []dnsClient{} 49 for _, s := range servers { 50 // if s.Net == "https" { 51 // ret = append(ret, newDoHClient(s.Addr, resolver)) 52 // continue 53 // } 54 55 host, port, _ := net.SplitHostPort(s.Addr) 56 ret = append(ret, &client{ 57 Client: &D.Client{ 58 Net: s.Net, 59 TLSConfig: &tls.Config{ 60 ClientSessionCache: globalSessionCache, 61 // alpn identifier, see https://tools.ietf.org/html/draft-hoffman-dprive-dns-tls-alpn-00#page-6 62 NextProtos: []string{"dns"}, 63 ServerName: host, 64 }, 65 UDPSize: 4096, 66 Timeout: 5 * time.Second, 67 }, 68 port: port, 69 host: host, 70 r: resolver, 71 }) 72 } 73 return ret 74 }