2022-11-12 19:43:44 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"codeberg.org/codeberg/pages/html"
|
|
|
|
"codeberg.org/codeberg/pages/server/cache"
|
|
|
|
"codeberg.org/codeberg/pages/server/context"
|
|
|
|
"codeberg.org/codeberg/pages/server/dns"
|
|
|
|
"codeberg.org/codeberg/pages/server/gitea"
|
|
|
|
"codeberg.org/codeberg/pages/server/upstream"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleCustomDomain(log zerolog.Logger, ctx *context.Context, giteaClient *gitea.Client,
|
|
|
|
mainDomainSuffix string,
|
|
|
|
trimmedHost string,
|
|
|
|
pathElements []string,
|
2023-02-14 03:03:00 +00:00
|
|
|
firstDefaultBranch string,
|
2024-05-26 20:05:46 +00:00
|
|
|
canonicalDomainCache, redirectsCache cache.ICache,
|
2022-11-12 19:43:44 +00:00
|
|
|
) {
|
|
|
|
// Serve pages from custom domains
|
2024-05-26 20:05:46 +00:00
|
|
|
targetOwner, targetRepo, targetBranch := dns.GetTargetFromDNS(trimmedHost, mainDomainSuffix, firstDefaultBranch)
|
2022-11-12 19:43:44 +00:00
|
|
|
if targetOwner == "" {
|
|
|
|
html.ReturnErrorPage(ctx,
|
|
|
|
"could not obtain repo owner from custom domain",
|
|
|
|
http.StatusFailedDependency)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pathParts := pathElements
|
|
|
|
canonicalLink := false
|
|
|
|
if strings.HasPrefix(pathElements[0], "@") {
|
|
|
|
targetBranch = pathElements[0][1:]
|
|
|
|
pathParts = pathElements[1:]
|
|
|
|
canonicalLink = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to use the given repo on the given branch or the default branch
|
|
|
|
log.Debug().Msg("custom domain preparations, now trying with details from DNS")
|
|
|
|
if targetOpt, works := tryBranch(log, ctx, giteaClient, &upstream.Options{
|
|
|
|
TryIndexPages: true,
|
|
|
|
TargetOwner: targetOwner,
|
|
|
|
TargetRepo: targetRepo,
|
|
|
|
TargetBranch: targetBranch,
|
|
|
|
TargetPath: path.Join(pathParts...),
|
|
|
|
}, canonicalLink); works {
|
2022-11-12 20:04:34 +00:00
|
|
|
canonicalDomain, valid := targetOpt.CheckCanonicalDomain(giteaClient, trimmedHost, mainDomainSuffix, canonicalDomainCache)
|
2022-11-12 19:43:44 +00:00
|
|
|
if !valid {
|
2023-11-16 17:11:35 +00:00
|
|
|
html.ReturnErrorPage(ctx, "domain not specified in <code>.domains</code> file", http.StatusMisdirectedRequest)
|
2022-11-12 19:43:44 +00:00
|
|
|
return
|
|
|
|
} else if canonicalDomain != trimmedHost {
|
|
|
|
// only redirect if the target is also a codeberg page!
|
2024-05-26 20:05:46 +00:00
|
|
|
targetOwner, _, _ = dns.GetTargetFromDNS(strings.SplitN(canonicalDomain, "/", 2)[0], mainDomainSuffix, firstDefaultBranch)
|
2022-11-12 19:43:44 +00:00
|
|
|
if targetOwner != "" {
|
2023-01-11 00:00:37 +00:00
|
|
|
ctx.Redirect("https://"+canonicalDomain+"/"+targetOpt.TargetPath, http.StatusTemporaryRedirect)
|
2022-11-12 19:43:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
html.ReturnErrorPage(ctx, "target is no codeberg page", http.StatusFailedDependency)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msg("tryBranch, now trying upstream 7")
|
2023-03-30 21:36:31 +00:00
|
|
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
2022-11-12 19:43:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
html.ReturnErrorPage(ctx, "could not find target for custom domain", http.StatusFailedDependency)
|
|
|
|
}
|