mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-25 06:16:58 +00:00
Implement domain handling logic
Still lots of performance optimization required!
This commit is contained in:
parent
13b386d442
commit
675e56ee98
2 changed files with 133 additions and 12 deletions
97
domains.go
97
domains.go
|
@ -1,15 +1,94 @@
|
|||
package main
|
||||
|
||||
import "github.com/valyala/fasthttp"
|
||||
import (
|
||||
"github.com/OrlovEvgeny/go-mcache"
|
||||
"github.com/valyala/fasthttp"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// getTargetFromDNS searches for CNAME entries on the request domain, optionally with a "www." prefix, and checks if
|
||||
// the domain is included in the repository's "domains.txt" file. If everything is fine, it returns the target data.
|
||||
// TODO: use TXT records with A/AAAA/ALIAS
|
||||
func getTargetFromDNS(ctx *fasthttp.RequestCtx) (targetOwner, targetRepo, targetBranch, targetPath string) {
|
||||
// TODO: read CNAME record for host and "www.{host}" to get those values
|
||||
// TODO: check domains.txt
|
||||
// DnsLookupCacheTimeout specifies the timeout for the DNS lookup cache.
|
||||
var DnsLookupCacheTimeout = 15*time.Minute
|
||||
// dnsLookupCache stores DNS lookups for custom domains
|
||||
var dnsLookupCache = mcache.New()
|
||||
|
||||
// getTargetFromDNS searches for CNAME or TXT entries on the request domain ending with MainDomainSuffix, and checks if
|
||||
// the domain equals the repository's ".canonical-domain" file. If everything is fine, it returns the target data.
|
||||
func getTargetFromDNS(domain string) (targetOwner, targetRepo, targetBranch string) {
|
||||
// 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, string(MainDomainSuffix)) {
|
||||
cname = ""
|
||||
names, err := net.LookupTXT(domain)
|
||||
if err == nil {
|
||||
for _, name := range names {
|
||||
name = strings.TrimSuffix(name, ".")
|
||||
if strings.HasSuffix(name, string(MainDomainSuffix)) {
|
||||
cname = name
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = dnsLookupCache.Set(domain, cname, DnsLookupCacheTimeout)
|
||||
}
|
||||
if cname == "" {
|
||||
return
|
||||
}
|
||||
cnameParts := strings.Split(strings.TrimSuffix(cname, string(MainDomainSuffix)), ".")
|
||||
targetOwner = cnameParts[len(cnameParts)-1]
|
||||
if len(cnameParts) > 1 {
|
||||
targetRepo = cnameParts[len(cnameParts)-1]
|
||||
}
|
||||
if len(cnameParts) > 2 {
|
||||
targetBranch = cnameParts[len(cnameParts)-2]
|
||||
}
|
||||
if targetRepo == "" {
|
||||
targetRepo = "pages"
|
||||
}
|
||||
if targetBranch == "" && targetRepo != "pages" {
|
||||
targetBranch = "pages"
|
||||
}
|
||||
// if targetBranch is still empty, the caller must find the default branch
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: cache domains.txt for 15 minutes
|
||||
// TODO: canonical domains - redirect to first domain if domains.txt exists, also make sure owner.codeberg.page/pages/... redirects to /...
|
||||
|
||||
// CanonicalDomainCacheTimeout specifies the timeout for the canonical domain cache.
|
||||
var CanonicalDomainCacheTimeout = 15*time.Minute
|
||||
// canonicalDomainCache stores canonical domains
|
||||
var canonicalDomainCache = mcache.New()
|
||||
|
||||
// checkCanonicalDomain returns the canonical domain specified in the repo (using the file `.canonical-domain`).
|
||||
func checkCanonicalDomain(targetOwner, targetRepo, targetBranch string) (canonicalDomain string) {
|
||||
// Check if the canonical domain matches
|
||||
req := fasthttp.AcquireRequest()
|
||||
req.SetRequestURI(string(GiteaRoot) + "/api/v1/repos/" + targetOwner + "/" + targetRepo + "/raw/" + targetBranch + "/.canonical-domain")
|
||||
res := fasthttp.AcquireResponse()
|
||||
if cachedValue, ok := canonicalDomainCache.Get(string(req.RequestURI())); ok {
|
||||
canonicalDomain = cachedValue.(string)
|
||||
} else {
|
||||
err := upstreamClient.Do(req, res)
|
||||
if err == nil && res.StatusCode() == fasthttp.StatusOK {
|
||||
canonicalDomain = strings.TrimSpace(string(res.Body()))
|
||||
if strings.Contains(canonicalDomain, "/") {
|
||||
canonicalDomain = ""
|
||||
}
|
||||
}
|
||||
if canonicalDomain == "" {
|
||||
canonicalDomain = targetOwner + string(MainDomainSuffix)
|
||||
if targetRepo != "" && targetRepo != "pages" {
|
||||
canonicalDomain += "/" + targetRepo
|
||||
}
|
||||
}
|
||||
_ = canonicalDomainCache.Set(string(req.RequestURI()), canonicalDomain, CanonicalDomainCacheTimeout)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue