2021-12-05 14:21:05 +00:00
|
|
|
package upstream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2022-11-12 19:37:20 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-12-05 14:21:05 +00:00
|
|
|
|
|
|
|
"codeberg.org/codeberg/pages/server/cache"
|
2022-06-11 21:02:06 +00:00
|
|
|
"codeberg.org/codeberg/pages/server/gitea"
|
2021-12-05 14:21:05 +00:00
|
|
|
)
|
|
|
|
|
2022-11-12 19:37:20 +00:00
|
|
|
// canonicalDomainCacheTimeout specifies the timeout for the canonical domain cache.
|
|
|
|
var canonicalDomainCacheTimeout = 15 * time.Minute
|
|
|
|
|
|
|
|
const canonicalDomainConfig = ".domains"
|
|
|
|
|
2021-12-09 19:16:43 +00:00
|
|
|
// CheckCanonicalDomain returns the canonical domain specified in the repo (using the `.domains` file).
|
2022-11-12 19:43:44 +00:00
|
|
|
func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain, mainDomainSuffix string, canonicalDomainCache cache.SetGetKey) (string, bool) {
|
2022-04-20 21:42:01 +00:00
|
|
|
var (
|
|
|
|
domains []string
|
|
|
|
valid bool
|
|
|
|
)
|
2022-11-12 19:43:44 +00:00
|
|
|
if cachedValue, ok := canonicalDomainCache.Get(o.TargetOwner + "/" + o.TargetRepo + "/" + o.TargetBranch); ok {
|
2021-12-05 14:21:05 +00:00
|
|
|
domains = cachedValue.([]string)
|
|
|
|
for _, domain := range domains {
|
|
|
|
if domain == actualDomain {
|
|
|
|
valid = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-12 19:43:44 +00:00
|
|
|
body, err := giteaClient.GiteaRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, canonicalDomainConfig)
|
2022-04-20 21:42:01 +00:00
|
|
|
if err == nil {
|
|
|
|
for _, domain := range strings.Split(string(body), "\n") {
|
2021-12-05 14:21:05 +00:00
|
|
|
domain = strings.ToLower(domain)
|
|
|
|
domain = strings.TrimSpace(domain)
|
|
|
|
domain = strings.TrimPrefix(domain, "http://")
|
|
|
|
domain = strings.TrimPrefix(domain, "https://")
|
|
|
|
if len(domain) > 0 && !strings.HasPrefix(domain, "#") && !strings.ContainsAny(domain, "\t /") && strings.ContainsRune(domain, '.') {
|
|
|
|
domains = append(domains, domain)
|
|
|
|
}
|
|
|
|
if domain == actualDomain {
|
|
|
|
valid = true
|
|
|
|
}
|
|
|
|
}
|
2022-11-12 19:37:20 +00:00
|
|
|
} else {
|
2023-01-22 18:52:21 +00:00
|
|
|
if err != gitea.ErrorNotFound {
|
|
|
|
log.Error().Err(err).Msgf("could not read %s of %s/%s", canonicalDomainConfig, o.TargetOwner, o.TargetRepo)
|
|
|
|
} else {
|
|
|
|
log.Info().Err(err).Msgf("could not read %s of %s/%s", canonicalDomainConfig, o.TargetOwner, o.TargetRepo)
|
|
|
|
}
|
2021-12-05 14:21:05 +00:00
|
|
|
}
|
2022-11-12 19:43:44 +00:00
|
|
|
domains = append(domains, o.TargetOwner+mainDomainSuffix)
|
2021-12-05 14:21:05 +00:00
|
|
|
if domains[len(domains)-1] == actualDomain {
|
|
|
|
valid = true
|
|
|
|
}
|
2022-11-12 19:43:44 +00:00
|
|
|
if o.TargetRepo != "" && o.TargetRepo != "pages" {
|
|
|
|
domains[len(domains)-1] += "/" + o.TargetRepo
|
2021-12-05 14:21:05 +00:00
|
|
|
}
|
2022-11-12 19:43:44 +00:00
|
|
|
_ = canonicalDomainCache.Set(o.TargetOwner+"/"+o.TargetRepo+"/"+o.TargetBranch, domains, canonicalDomainCacheTimeout)
|
2021-12-05 14:21:05 +00:00
|
|
|
}
|
2021-12-09 19:16:43 +00:00
|
|
|
return domains[0], valid
|
2021-12-05 14:21:05 +00:00
|
|
|
}
|