try each default branch instead of 'pages' only

This commit is contained in:
Simon Vieille 2022-08-16 15:58:22 +02:00
parent dc634287aa
commit 9c412966fc
Signed by: deblan
GPG key ID: 579388D585F70417
2 changed files with 33 additions and 23 deletions

View file

@ -48,6 +48,7 @@ func Serve(ctx *cli.Context) error {
giteaAPIToken := ctx.String("gitea-api-token") giteaAPIToken := ctx.String("gitea-api-token")
rawDomain := ctx.String("raw-domain") rawDomain := ctx.String("raw-domain")
mainDomainSuffix := []byte(ctx.String("pages-domain")) mainDomainSuffix := []byte(ctx.String("pages-domain"))
defaultBranches := strings.Split(strings.ReplaceAll(ctx.String("pages-branches"), " ", ""), ",")
rawInfoPage := ctx.String("raw-info-page") rawInfoPage := ctx.String("raw-info-page")
listeningAddress := fmt.Sprintf("%s:%s", ctx.String("host"), ctx.String("port")) listeningAddress := fmt.Sprintf("%s:%s", ctx.String("host"), ctx.String("port"))
enableHTTPServer := ctx.Bool("enable-http-server") enableHTTPServer := ctx.Bool("enable-http-server")
@ -73,6 +74,10 @@ func Serve(ctx *cli.Context) error {
mainDomainSuffix = append([]byte{'.'}, mainDomainSuffix...) mainDomainSuffix = append([]byte{'.'}, mainDomainSuffix...)
} }
if len(defaultBranches) == 0 {
defaultBranches = []string{"pages"}
}
keyCache := cache.NewKeyValueCache() keyCache := cache.NewKeyValueCache()
challengeCache := cache.NewKeyValueCache() challengeCache := cache.NewKeyValueCache()
// canonicalDomainCache stores canonical domains // canonicalDomainCache stores canonical domains
@ -95,7 +100,9 @@ func Serve(ctx *cli.Context) error {
giteaClient, giteaClient,
giteaRoot, rawInfoPage, giteaRoot, rawInfoPage,
BlacklistedPaths, allowedCorsDomains, BlacklistedPaths, allowedCorsDomains,
dnsLookupCache, canonicalDomainCache, branchTimestampCache, fileResponseCache) dnsLookupCache, canonicalDomainCache, branchTimestampCache, fileResponseCache,
defaultBranches,
)
fastServer := server.SetupServer(handler) fastServer := server.SetupServer(handler)
httpServer := server.SetupHTTPACMEChallengeServer(challengeCache) httpServer := server.SetupHTTPACMEChallengeServer(challengeCache)

View file

@ -23,6 +23,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
giteaRoot, rawInfoPage string, giteaRoot, rawInfoPage string,
blacklistedPaths, allowedCorsDomains [][]byte, blacklistedPaths, allowedCorsDomains [][]byte,
dnsLookupCache, canonicalDomainCache, branchTimestampCache, fileResponseCache cache.SetGetKey, dnsLookupCache, canonicalDomainCache, branchTimestampCache, fileResponseCache cache.SetGetKey,
defaultBranches []string,
) func(ctx *fasthttp.RequestCtx) { ) func(ctx *fasthttp.RequestCtx) {
return func(ctx *fasthttp.RequestCtx) { return func(ctx *fasthttp.RequestCtx) {
log := log.With().Strs("Handler", []string{string(ctx.Request.Host()), string(ctx.Request.Header.RequestURI())}).Logger() log := log.With().Strs("Handler", []string{string(ctx.Request.Host()), string(ctx.Request.Header.RequestURI())}).Logger()
@ -227,29 +228,31 @@ func Handler(mainDomainSuffix, rawDomain []byte,
return return
} }
// Check if the first directory is a repo with a "pages" branch for _, branch := range defaultBranches {
// example.codeberg.page/myrepo/index.html // Check if the first directory is a repo with a default branch
// example.codeberg.page/pages/... is not allowed here. // example.codeberg.page/myrepo/index.html
log.Debug().Msg("main domain preparations, now trying with specified repo") // example.codeberg.page/{PAGES_BRANCHE}/... is not allowed here.
if pathElements[0] != "pages" && tryBranch(log, log.Debug().Msg("main domain preparations, now trying with specified repo")
pathElements[0], "pages", pathElements[1:], "") { if pathElements[0] != branch && tryBranch(log,
log.Info().Msg("tryBranch, now trying upstream 5") pathElements[0], branch, pathElements[1:], "") {
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, log.Info().Msg("tryBranch, now trying upstream 5")
targetOptions, targetOwner, targetRepo, targetBranch, targetPath, tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
canonicalDomainCache, branchTimestampCache, fileResponseCache) targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
return canonicalDomainCache, branchTimestampCache, fileResponseCache)
} return
}
// Try to use the "pages" repo on its default branch // Try to use the "pages" repo on its default branch
// example.codeberg.page/index.html // example.codeberg.page/index.html
log.Debug().Msg("main domain preparations, now trying with default repo/branch") log.Debug().Msg("main domain preparations, now trying with default repo/branch")
if tryBranch(log, if tryBranch(log,
"pages", "", pathElements, "") { branch, "", pathElements, "") {
log.Info().Msg("tryBranch, now trying upstream 6") log.Info().Msg("tryBranch, now trying upstream 6")
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
targetOptions, targetOwner, targetRepo, targetBranch, targetPath, targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
canonicalDomainCache, branchTimestampCache, fileResponseCache) canonicalDomainCache, branchTimestampCache, fileResponseCache)
return return
}
} }
// Couldn't find a valid repo/branch // Couldn't find a valid repo/branch