pages-server/server/dns/dns.go

62 lines
1.7 KiB
Go
Raw Normal View History

2021-12-05 14:21:05 +00:00
package dns
import (
"github.com/OrlovEvgeny/go-mcache"
2021-12-05 14:21:05 +00:00
"net"
"strings"
"time"
2021-12-05 14:21:05 +00:00
)
// lookupCacheTimeout specifies the timeout for the DNS lookup cache.
var lookupCacheTimeout = 15 * time.Minute
var defaultPagesRepo = "pages"
2021-12-05 14:21:05 +00:00
// GetTargetFromDNS searches for CNAME or TXT entries on the request domain ending with MainDomainSuffix.
// If everything is fine, it returns the target data.
func GetTargetFromDNS(domain, mainDomainSuffix, firstDefaultBranch string, dnsLookupCache *mcache.CacheDriver) (targetOwner, targetRepo, targetBranch string) {
2021-12-05 14:21:05 +00:00
// Get CNAME or TXT
var cname string
var err error
if cachedName, ok := dnsLookupCache.Get(domain); ok {
cname = cachedName.(string)
} else {
cname, err = net.LookupCNAME(domain)
cname = strings.TrimSuffix(cname, ".")
if err != nil || !strings.HasSuffix(cname, mainDomainSuffix) {
cname = ""
// TODO: check if the A record matches!
names, err := net.LookupTXT(domain)
if err == nil {
for _, name := range names {
name = strings.TrimSuffix(strings.TrimSpace(name), ".")
2021-12-05 14:21:05 +00:00
if strings.HasSuffix(name, mainDomainSuffix) {
cname = name
break
}
}
}
}
2021-12-05 15:24:26 +00:00
_ = dnsLookupCache.Set(domain, cname, lookupCacheTimeout)
2021-12-05 14:21:05 +00:00
}
if cname == "" {
return
}
cnameParts := strings.Split(strings.TrimSuffix(cname, mainDomainSuffix), ".")
targetOwner = cnameParts[len(cnameParts)-1]
if len(cnameParts) > 1 {
targetRepo = cnameParts[len(cnameParts)-2]
}
if len(cnameParts) > 2 {
targetBranch = cnameParts[len(cnameParts)-3]
}
if targetRepo == "" {
targetRepo = defaultPagesRepo
2021-12-05 14:21:05 +00:00
}
if targetBranch == "" && targetRepo != defaultPagesRepo {
targetBranch = firstDefaultBranch
2021-12-05 14:21:05 +00:00
}
// if targetBranch is still empty, the caller must find the default branch
return
}