支持任意域名通过CNAME访问服务(开启选项后)

This commit is contained in:
刘祥超
2021-10-16 12:03:05 +08:00
parent 874694f662
commit 8935f35b4e
8 changed files with 180 additions and 25 deletions

35
internal/utils/lookup.go Normal file
View File

@@ -0,0 +1,35 @@
package utils
import (
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/miekg/dns"
)
// LookupCNAME 获取CNAME
func LookupCNAME(host string) (string, error) {
config, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
return "", err
}
c := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion(host+".", dns.TypeCNAME)
m.RecursionDesired = true
var lastErr error
for _, serverAddr := range config.Servers {
r, _, err := c.Exchange(m, configutils.QuoteIP(serverAddr)+":"+config.Port)
if err != nil {
lastErr = err
continue
}
if len(r.Answer) == 0 {
continue
}
return r.Answer[0].(*dns.CNAME).Target, nil
}
return "", lastErr
}