2022-01-10 14:41:31 +00:00
|
|
|
package doh
|
2022-01-10 14:20:23 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-11 04:23:15 +00:00
|
|
|
"errors"
|
2022-01-10 14:20:23 +00:00
|
|
|
"sync"
|
|
|
|
|
2022-05-08 13:21:49 +00:00
|
|
|
"regexp"
|
2022-01-10 14:20:23 +00:00
|
|
|
"github.com/babolivier/go-doh-client"
|
|
|
|
)
|
|
|
|
|
|
|
|
var resolver *doh.Resolver
|
|
|
|
var once sync.Once
|
|
|
|
|
|
|
|
func Init(dns string) {
|
|
|
|
getInstance().Host = dns
|
|
|
|
}
|
|
|
|
|
2022-01-10 14:41:31 +00:00
|
|
|
func Lookup(domain string) (string, error) {
|
2022-05-08 13:21:49 +00:00
|
|
|
ipRegex := "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
|
|
|
|
|
|
|
|
if r, _ := regexp.MatchString(ipRegex, domain); r {
|
|
|
|
return domain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-10 14:20:23 +00:00
|
|
|
a, _, err := resolver.LookupA(domain)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-03-11 04:23:15 +00:00
|
|
|
if len(a) < 1 {
|
|
|
|
return "", errors.New(" couldn't resolve the domain")
|
|
|
|
}
|
|
|
|
|
2022-01-10 14:20:23 +00:00
|
|
|
ip := a[0].IP4
|
|
|
|
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInstance() *doh.Resolver {
|
|
|
|
once.Do(func() {
|
|
|
|
resolver = &doh.Resolver{
|
|
|
|
Host: "",
|
|
|
|
Class: doh.IN,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return resolver
|
|
|
|
}
|