From 7f68c38c7b5001432cbe32ae4f5912af9784f813 Mon Sep 17 00:00:00 2001 From: xvzc Date: Mon, 10 Jan 2022 23:20:23 +0900 Subject: [PATCH] add dns module --- dns/dns.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 dns/dns.go diff --git a/dns/dns.go b/dns/dns.go new file mode 100644 index 0000000..9776c1d --- /dev/null +++ b/dns/dns.go @@ -0,0 +1,36 @@ +package dns + +import ( + "sync" + + "github.com/babolivier/go-doh-client" +) + +var resolver *doh.Resolver +var once sync.Once + +func Init(dns string) { + getInstance().Host = dns +} + +func LookupOverHttps(domain string) (string, error) { + a, _, err := resolver.LookupA(domain) + if err != nil { + return "", err + } + + ip := a[0].IP4 + + return ip, nil +} + +func getInstance() *doh.Resolver { + once.Do(func() { + resolver = &doh.Resolver{ + Host: "", + Class: doh.IN, + } + }) + + return resolver +}