rio/dns.go

65 lines
1.1 KiB
Go

package main
import (
"errors"
"net"
"strings"
"time"
"github.com/patrickmn/go-cache"
)
const (
TxtRepoKey = "repo="
)
var (
cnameCache = cache.New(1 * time.Hour, 1 * time.Hour)
txtRepoCache = cache.New(1 * time.Hour, 1 * time.Hour)
)
func lookupRepoTXT(domain string) (string, error) {
repoLookup, found := txtRepoCache.Get(domain)
if found {
return repoLookup.(string), nil
}
txts, err := net.LookupTXT("_rio-pages." + domain)
if err != nil {
return "", err
}
repo := ""
for _, txt := range txts {
if !strings.HasPrefix(txt, TxtRepoKey) {
continue
}
repo = strings.TrimPrefix(txt, TxtRepoKey)
break
}
txtRepoCache.Set(domain, repo, cache.DefaultExpiration)
return repo, nil
}
func lookupCNAME(domain string) (string, error) {
cname, found := cnameCache.Get(domain)
if found {
if cname == "" {
return "", errors.New("Previous request failure")
}
return cname.(string), nil
}
cname, err := net.LookupCNAME(domain)
if err == nil {
cnameCache.Set(domain, cname, cache.DefaultExpiration)
return cname.(string), nil
}
cnameCache.Set(domain, "", cache.DefaultExpiration)
return "", err
}