mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-29 01:23:35 +02:00
Support canonical-domain-file configuration
This commit is contained in:
parent
557a295732
commit
2410137438
12 changed files with 95 additions and 68 deletions
|
@ -81,6 +81,12 @@ var (
|
||||||
Usage: "specifies the domain from which raw repository content shall be served, not set disable raw content hosting",
|
Usage: "specifies the domain from which raw repository content shall be served, not set disable raw content hosting",
|
||||||
EnvVars: []string{"RAW_DOMAIN"},
|
EnvVars: []string{"RAW_DOMAIN"},
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "canonical-domain-file",
|
||||||
|
Usage: "specifies the file from which the canonical domain shall be specified in",
|
||||||
|
EnvVars: []string{"CANONICAL_DOMAIN_FILE"},
|
||||||
|
Value: ".domains",
|
||||||
|
},
|
||||||
|
|
||||||
// #########################
|
// #########################
|
||||||
// ### Page Server Setup ###
|
// ### Page Server Setup ###
|
||||||
|
|
|
@ -9,15 +9,16 @@ type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
Host string `default:"[::]"`
|
Host string `default:"[::]"`
|
||||||
Port uint16 `default:"443"`
|
Port uint16 `default:"443"`
|
||||||
HttpPort uint16 `default:"80"`
|
HttpPort uint16 `default:"80"`
|
||||||
HttpServerEnabled bool `default:"true"`
|
HttpServerEnabled bool `default:"true"`
|
||||||
MainDomain string
|
MainDomain string
|
||||||
RawDomain string
|
RawDomain string
|
||||||
PagesBranches []string
|
CanonicalDomainFile string `default:".domains"`
|
||||||
AllowedCorsDomains []string
|
PagesBranches []string
|
||||||
BlacklistedPaths []string
|
AllowedCorsDomains []string
|
||||||
|
BlacklistedPaths []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ForgeConfig struct {
|
type ForgeConfig struct {
|
||||||
|
|
|
@ -75,6 +75,9 @@ func mergeServerConfig(ctx *cli.Context, config *ServerConfig) {
|
||||||
if ctx.IsSet("raw-domain") {
|
if ctx.IsSet("raw-domain") {
|
||||||
config.RawDomain = ctx.String("raw-domain")
|
config.RawDomain = ctx.String("raw-domain")
|
||||||
}
|
}
|
||||||
|
if ctx.IsSet("canonical-domain-file") {
|
||||||
|
config.CanonicalDomainFile = ctx.String("canonical-domain-file")
|
||||||
|
}
|
||||||
if ctx.IsSet("pages-branch") {
|
if ctx.IsSet("pages-branch") {
|
||||||
config.PagesBranches = ctx.StringSlice("pages-branch")
|
config.PagesBranches = ctx.StringSlice("pages-branch")
|
||||||
}
|
}
|
||||||
|
|
|
@ -136,15 +136,16 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T
|
||||||
cfg := &Config{
|
cfg := &Config{
|
||||||
LogLevel: "original",
|
LogLevel: "original",
|
||||||
Server: ServerConfig{
|
Server: ServerConfig{
|
||||||
Host: "original",
|
Host: "original",
|
||||||
Port: 8080,
|
Port: 8080,
|
||||||
HttpPort: 80,
|
HttpPort: 80,
|
||||||
HttpServerEnabled: false,
|
HttpServerEnabled: false,
|
||||||
MainDomain: "original",
|
MainDomain: "original",
|
||||||
RawDomain: "original",
|
RawDomain: "original",
|
||||||
PagesBranches: []string{"original"},
|
CanonicalDomainFile: "original",
|
||||||
AllowedCorsDomains: []string{"original"},
|
PagesBranches: []string{"original"},
|
||||||
BlacklistedPaths: []string{"original"},
|
AllowedCorsDomains: []string{"original"},
|
||||||
|
BlacklistedPaths: []string{"original"},
|
||||||
},
|
},
|
||||||
Forge: ForgeConfig{
|
Forge: ForgeConfig{
|
||||||
Root: "original",
|
Root: "original",
|
||||||
|
@ -176,15 +177,16 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T
|
||||||
expectedConfig := &Config{
|
expectedConfig := &Config{
|
||||||
LogLevel: "changed",
|
LogLevel: "changed",
|
||||||
Server: ServerConfig{
|
Server: ServerConfig{
|
||||||
Host: "changed",
|
Host: "changed",
|
||||||
Port: 8443,
|
Port: 8443,
|
||||||
HttpPort: 443,
|
HttpPort: 443,
|
||||||
HttpServerEnabled: true,
|
HttpServerEnabled: true,
|
||||||
MainDomain: "changed",
|
MainDomain: "changed",
|
||||||
RawDomain: "changed",
|
RawDomain: "changed",
|
||||||
PagesBranches: []string{"changed"},
|
CanonicalDomainFile: "changed",
|
||||||
AllowedCorsDomains: []string{"changed"},
|
PagesBranches: []string{"changed"},
|
||||||
BlacklistedPaths: append([]string{"changed"}, ALWAYS_BLACKLISTED_PATHS...),
|
AllowedCorsDomains: []string{"changed"},
|
||||||
|
BlacklistedPaths: append([]string{"changed"}, ALWAYS_BLACKLISTED_PATHS...),
|
||||||
},
|
},
|
||||||
Forge: ForgeConfig{
|
Forge: ForgeConfig{
|
||||||
Root: "changed",
|
Root: "changed",
|
||||||
|
@ -220,6 +222,7 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T
|
||||||
// Server
|
// Server
|
||||||
"--pages-domain", "changed",
|
"--pages-domain", "changed",
|
||||||
"--raw-domain", "changed",
|
"--raw-domain", "changed",
|
||||||
|
"--canonical-domain-file", "changed",
|
||||||
"--allowed-cors-domains", "changed",
|
"--allowed-cors-domains", "changed",
|
||||||
"--blacklisted-paths", "changed",
|
"--blacklisted-paths", "changed",
|
||||||
"--pages-branch", "changed",
|
"--pages-branch", "changed",
|
||||||
|
@ -273,27 +276,29 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
|
||||||
t,
|
t,
|
||||||
func(ctx *cli.Context) error {
|
func(ctx *cli.Context) error {
|
||||||
cfg := &ServerConfig{
|
cfg := &ServerConfig{
|
||||||
Host: "original",
|
Host: "original",
|
||||||
Port: 8080,
|
Port: 8080,
|
||||||
HttpPort: 80,
|
HttpPort: 80,
|
||||||
HttpServerEnabled: false,
|
HttpServerEnabled: false,
|
||||||
MainDomain: "original",
|
MainDomain: "original",
|
||||||
RawDomain: "original",
|
RawDomain: "original",
|
||||||
AllowedCorsDomains: []string{"original"},
|
CanonicalDomainFile: "original",
|
||||||
BlacklistedPaths: []string{"original"},
|
AllowedCorsDomains: []string{"original"},
|
||||||
|
BlacklistedPaths: []string{"original"},
|
||||||
}
|
}
|
||||||
|
|
||||||
mergeServerConfig(ctx, cfg)
|
mergeServerConfig(ctx, cfg)
|
||||||
|
|
||||||
expectedConfig := &ServerConfig{
|
expectedConfig := &ServerConfig{
|
||||||
Host: "changed",
|
Host: "changed",
|
||||||
Port: 8443,
|
Port: 8443,
|
||||||
HttpPort: 443,
|
HttpPort: 443,
|
||||||
HttpServerEnabled: true,
|
HttpServerEnabled: true,
|
||||||
MainDomain: "changed",
|
MainDomain: "changed",
|
||||||
RawDomain: "changed",
|
RawDomain: "changed",
|
||||||
AllowedCorsDomains: fixArrayFromCtx(ctx, "allowed-cors-domains", []string{"changed"}),
|
CanonicalDomainFile: "changed",
|
||||||
BlacklistedPaths: fixArrayFromCtx(ctx, "blacklisted-paths", append([]string{"changed"}, ALWAYS_BLACKLISTED_PATHS...)),
|
AllowedCorsDomains: fixArrayFromCtx(ctx, "allowed-cors-domains", []string{"changed"}),
|
||||||
|
BlacklistedPaths: fixArrayFromCtx(ctx, "blacklisted-paths", append([]string{"changed"}, ALWAYS_BLACKLISTED_PATHS...)),
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, expectedConfig, cfg)
|
assert.Equal(t, expectedConfig, cfg)
|
||||||
|
@ -303,6 +308,7 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
|
||||||
[]string{
|
[]string{
|
||||||
"--pages-domain", "changed",
|
"--pages-domain", "changed",
|
||||||
"--raw-domain", "changed",
|
"--raw-domain", "changed",
|
||||||
|
"--canonical-domain-file", "changed",
|
||||||
"--allowed-cors-domains", "changed",
|
"--allowed-cors-domains", "changed",
|
||||||
"--blacklisted-paths", "changed",
|
"--blacklisted-paths", "changed",
|
||||||
"--host", "changed",
|
"--host", "changed",
|
||||||
|
@ -326,6 +332,7 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE
|
||||||
{args: []string{"--enable-http-server"}, callback: func(sc *ServerConfig) { sc.HttpServerEnabled = true }},
|
{args: []string{"--enable-http-server"}, callback: func(sc *ServerConfig) { sc.HttpServerEnabled = true }},
|
||||||
{args: []string{"--pages-domain", "changed"}, callback: func(sc *ServerConfig) { sc.MainDomain = "changed" }},
|
{args: []string{"--pages-domain", "changed"}, callback: func(sc *ServerConfig) { sc.MainDomain = "changed" }},
|
||||||
{args: []string{"--raw-domain", "changed"}, callback: func(sc *ServerConfig) { sc.RawDomain = "changed" }},
|
{args: []string{"--raw-domain", "changed"}, callback: func(sc *ServerConfig) { sc.RawDomain = "changed" }},
|
||||||
|
{args: []string{"--canonical-domain-file", "changed"}, callback: func(sc *ServerConfig) { sc.CanonicalDomainFile = "changed" }},
|
||||||
{args: []string{"--pages-branch", "changed"}, callback: func(sc *ServerConfig) { sc.PagesBranches = []string{"changed"} }},
|
{args: []string{"--pages-branch", "changed"}, callback: func(sc *ServerConfig) { sc.PagesBranches = []string{"changed"} }},
|
||||||
{args: []string{"--allowed-cors-domains", "changed"}, callback: func(sc *ServerConfig) { sc.AllowedCorsDomains = []string{"changed"} }},
|
{args: []string{"--allowed-cors-domains", "changed"}, callback: func(sc *ServerConfig) { sc.AllowedCorsDomains = []string{"changed"} }},
|
||||||
{args: []string{"--blacklisted-paths", "changed"}, callback: func(sc *ServerConfig) { sc.BlacklistedPaths = []string{"changed"} }},
|
{args: []string{"--blacklisted-paths", "changed"}, callback: func(sc *ServerConfig) { sc.BlacklistedPaths = []string{"changed"} }},
|
||||||
|
@ -336,15 +343,16 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE
|
||||||
t,
|
t,
|
||||||
func(ctx *cli.Context) error {
|
func(ctx *cli.Context) error {
|
||||||
cfg := ServerConfig{
|
cfg := ServerConfig{
|
||||||
Host: "original",
|
Host: "original",
|
||||||
Port: 8080,
|
Port: 8080,
|
||||||
HttpPort: 80,
|
HttpPort: 80,
|
||||||
HttpServerEnabled: false,
|
HttpServerEnabled: false,
|
||||||
MainDomain: "original",
|
MainDomain: "original",
|
||||||
RawDomain: "original",
|
RawDomain: "original",
|
||||||
PagesBranches: []string{"original"},
|
CanonicalDomainFile: "original",
|
||||||
AllowedCorsDomains: []string{"original"},
|
PagesBranches: []string{"original"},
|
||||||
BlacklistedPaths: []string{"original"},
|
AllowedCorsDomains: []string{"original"},
|
||||||
|
BlacklistedPaths: []string{"original"},
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedConfig := cfg
|
expectedConfig := cfg
|
||||||
|
|
|
@ -32,6 +32,7 @@ func TLSConfig(mainDomainSuffix string,
|
||||||
giteaClient *gitea.Client,
|
giteaClient *gitea.Client,
|
||||||
acmeClient *AcmeClient,
|
acmeClient *AcmeClient,
|
||||||
firstDefaultBranch string,
|
firstDefaultBranch string,
|
||||||
|
canonicalDomainConfig string,
|
||||||
challengeCache, canonicalDomainCache cache.ICache,
|
challengeCache, canonicalDomainCache cache.ICache,
|
||||||
certDB database.CertDB,
|
certDB database.CertDB,
|
||||||
noDNS01 bool,
|
noDNS01 bool,
|
||||||
|
@ -100,7 +101,7 @@ func TLSConfig(mainDomainSuffix string,
|
||||||
TargetRepo: targetRepo,
|
TargetRepo: targetRepo,
|
||||||
TargetBranch: targetBranch,
|
TargetBranch: targetBranch,
|
||||||
}
|
}
|
||||||
_, valid := targetOpt.CheckCanonicalDomain(giteaClient, domain, mainDomainSuffix, canonicalDomainCache)
|
_, valid := targetOpt.CheckCanonicalDomain(giteaClient, domain, mainDomainSuffix, canonicalDomainConfig, canonicalDomainCache)
|
||||||
if !valid {
|
if !valid {
|
||||||
// We shouldn't obtain a certificate when we cannot check if the
|
// We shouldn't obtain a certificate when we cannot check if the
|
||||||
// repository has specified this domain in the `.domains` file.
|
// repository has specified this domain in the `.domains` file.
|
||||||
|
|
|
@ -92,6 +92,7 @@ func Handler(
|
||||||
cfg.MainDomain,
|
cfg.MainDomain,
|
||||||
trimmedHost,
|
trimmedHost,
|
||||||
pathElements,
|
pathElements,
|
||||||
|
cfg.CanonicalDomainFile,
|
||||||
canonicalDomainCache, redirectsCache)
|
canonicalDomainCache, redirectsCache)
|
||||||
} else if strings.HasSuffix(trimmedHost, cfg.MainDomain) {
|
} else if strings.HasSuffix(trimmedHost, cfg.MainDomain) {
|
||||||
log.Debug().Msg("subdomain request detected")
|
log.Debug().Msg("subdomain request detected")
|
||||||
|
@ -100,6 +101,7 @@ func Handler(
|
||||||
cfg.PagesBranches,
|
cfg.PagesBranches,
|
||||||
trimmedHost,
|
trimmedHost,
|
||||||
pathElements,
|
pathElements,
|
||||||
|
cfg.CanonicalDomainFile,
|
||||||
canonicalDomainCache, redirectsCache)
|
canonicalDomainCache, redirectsCache)
|
||||||
} else {
|
} else {
|
||||||
log.Debug().Msg("custom domain request detected")
|
log.Debug().Msg("custom domain request detected")
|
||||||
|
@ -108,6 +110,7 @@ func Handler(
|
||||||
trimmedHost,
|
trimmedHost,
|
||||||
pathElements,
|
pathElements,
|
||||||
cfg.PagesBranches[0],
|
cfg.PagesBranches[0],
|
||||||
|
cfg.CanonicalDomainFile,
|
||||||
canonicalDomainCache, redirectsCache)
|
canonicalDomainCache, redirectsCache)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ func handleCustomDomain(log zerolog.Logger, ctx *context.Context, giteaClient *g
|
||||||
trimmedHost string,
|
trimmedHost string,
|
||||||
pathElements []string,
|
pathElements []string,
|
||||||
firstDefaultBranch string,
|
firstDefaultBranch string,
|
||||||
|
canonicalDomainConfig string,
|
||||||
canonicalDomainCache, redirectsCache cache.ICache,
|
canonicalDomainCache, redirectsCache cache.ICache,
|
||||||
) {
|
) {
|
||||||
// Serve pages from custom domains
|
// Serve pages from custom domains
|
||||||
|
@ -47,7 +48,7 @@ func handleCustomDomain(log zerolog.Logger, ctx *context.Context, giteaClient *g
|
||||||
TargetBranch: targetBranch,
|
TargetBranch: targetBranch,
|
||||||
TargetPath: path.Join(pathParts...),
|
TargetPath: path.Join(pathParts...),
|
||||||
}, canonicalLink); works {
|
}, canonicalLink); works {
|
||||||
canonicalDomain, valid := targetOpt.CheckCanonicalDomain(giteaClient, trimmedHost, mainDomainSuffix, canonicalDomainCache)
|
canonicalDomain, valid := targetOpt.CheckCanonicalDomain(giteaClient, trimmedHost, mainDomainSuffix, canonicalDomainConfig, canonicalDomainCache)
|
||||||
if !valid {
|
if !valid {
|
||||||
html.ReturnErrorPage(ctx, "domain not specified in <code>.domains</code> file", http.StatusMisdirectedRequest)
|
html.ReturnErrorPage(ctx, "domain not specified in <code>.domains</code> file", http.StatusMisdirectedRequest)
|
||||||
return
|
return
|
||||||
|
@ -64,7 +65,7 @@ func handleCustomDomain(log zerolog.Logger, ctx *context.Context, giteaClient *g
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("tryBranch, now trying upstream 7")
|
log.Debug().Msg("tryBranch, now trying upstream 7")
|
||||||
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainConfig, canonicalDomainCache, redirectsCache)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ func handleRaw(log zerolog.Logger, ctx *context.Context, giteaClient *gitea.Clie
|
||||||
mainDomainSuffix string,
|
mainDomainSuffix string,
|
||||||
trimmedHost string,
|
trimmedHost string,
|
||||||
pathElements []string,
|
pathElements []string,
|
||||||
|
canonicalDomainConfig string,
|
||||||
canonicalDomainCache, redirectsCache cache.ICache,
|
canonicalDomainCache, redirectsCache cache.ICache,
|
||||||
) {
|
) {
|
||||||
// Serve raw content from RawDomain
|
// Serve raw content from RawDomain
|
||||||
|
@ -45,7 +46,7 @@ func handleRaw(log zerolog.Logger, ctx *context.Context, giteaClient *gitea.Clie
|
||||||
TargetPath: path.Join(pathElements[3:]...),
|
TargetPath: path.Join(pathElements[3:]...),
|
||||||
}, true); works {
|
}, true); works {
|
||||||
log.Trace().Msg("tryUpstream: serve raw domain with specified branch")
|
log.Trace().Msg("tryUpstream: serve raw domain with specified branch")
|
||||||
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainConfig, canonicalDomainCache, redirectsCache)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug().Msg("missing branch info")
|
log.Debug().Msg("missing branch info")
|
||||||
|
@ -62,7 +63,7 @@ func handleRaw(log zerolog.Logger, ctx *context.Context, giteaClient *gitea.Clie
|
||||||
TargetPath: path.Join(pathElements[2:]...),
|
TargetPath: path.Join(pathElements[2:]...),
|
||||||
}, true); works {
|
}, true); works {
|
||||||
log.Trace().Msg("tryUpstream: serve raw domain with default branch")
|
log.Trace().Msg("tryUpstream: serve raw domain with default branch")
|
||||||
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainConfig, canonicalDomainCache, redirectsCache)
|
||||||
} else {
|
} else {
|
||||||
html.ReturnErrorPage(ctx,
|
html.ReturnErrorPage(ctx,
|
||||||
fmt.Sprintf("raw domain could not find repo <code>%s/%s</code> or repo is empty", targetOpt.TargetOwner, targetOpt.TargetRepo),
|
fmt.Sprintf("raw domain could not find repo <code>%s/%s</code> or repo is empty", targetOpt.TargetOwner, targetOpt.TargetRepo),
|
||||||
|
|
|
@ -21,6 +21,7 @@ func handleSubDomain(log zerolog.Logger, ctx *context.Context, giteaClient *gite
|
||||||
defaultPagesBranches []string,
|
defaultPagesBranches []string,
|
||||||
trimmedHost string,
|
trimmedHost string,
|
||||||
pathElements []string,
|
pathElements []string,
|
||||||
|
canonicalDomainConfig string,
|
||||||
canonicalDomainCache, redirectsCache cache.ICache,
|
canonicalDomainCache, redirectsCache cache.ICache,
|
||||||
) {
|
) {
|
||||||
// Serve pages from subdomains of MainDomainSuffix
|
// Serve pages from subdomains of MainDomainSuffix
|
||||||
|
@ -53,7 +54,7 @@ func handleSubDomain(log zerolog.Logger, ctx *context.Context, giteaClient *gite
|
||||||
TargetPath: path.Join(pathElements[2:]...),
|
TargetPath: path.Join(pathElements[2:]...),
|
||||||
}, true); works {
|
}, true); works {
|
||||||
log.Trace().Msg("tryUpstream: serve with specified repo and branch")
|
log.Trace().Msg("tryUpstream: serve with specified repo and branch")
|
||||||
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainConfig, canonicalDomainCache, redirectsCache)
|
||||||
} else {
|
} else {
|
||||||
html.ReturnErrorPage(
|
html.ReturnErrorPage(
|
||||||
ctx,
|
ctx,
|
||||||
|
@ -85,7 +86,7 @@ func handleSubDomain(log zerolog.Logger, ctx *context.Context, giteaClient *gite
|
||||||
TargetPath: path.Join(pathElements[1:]...),
|
TargetPath: path.Join(pathElements[1:]...),
|
||||||
}, true); works {
|
}, true); works {
|
||||||
log.Trace().Msg("tryUpstream: serve default pages repo with specified branch")
|
log.Trace().Msg("tryUpstream: serve default pages repo with specified branch")
|
||||||
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainConfig, canonicalDomainCache, redirectsCache)
|
||||||
} else {
|
} else {
|
||||||
html.ReturnErrorPage(
|
html.ReturnErrorPage(
|
||||||
ctx,
|
ctx,
|
||||||
|
@ -110,7 +111,7 @@ func handleSubDomain(log zerolog.Logger, ctx *context.Context, giteaClient *gite
|
||||||
TargetPath: path.Join(pathElements[1:]...),
|
TargetPath: path.Join(pathElements[1:]...),
|
||||||
}, false); works {
|
}, false); works {
|
||||||
log.Debug().Msg("tryBranch, now trying upstream 5")
|
log.Debug().Msg("tryBranch, now trying upstream 5")
|
||||||
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainConfig, canonicalDomainCache, redirectsCache)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,7 +127,7 @@ func handleSubDomain(log zerolog.Logger, ctx *context.Context, giteaClient *gite
|
||||||
TargetPath: path.Join(pathElements...),
|
TargetPath: path.Join(pathElements...),
|
||||||
}, false); works {
|
}, false); works {
|
||||||
log.Debug().Msg("tryBranch, now trying upstream 6")
|
log.Debug().Msg("tryBranch, now trying upstream 6")
|
||||||
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainConfig, canonicalDomainCache, redirectsCache)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,7 +142,7 @@ func handleSubDomain(log zerolog.Logger, ctx *context.Context, giteaClient *gite
|
||||||
TargetPath: path.Join(pathElements...),
|
TargetPath: path.Join(pathElements...),
|
||||||
}, false); works {
|
}, false); works {
|
||||||
log.Debug().Msg("tryBranch, now trying upstream 6")
|
log.Debug().Msg("tryBranch, now trying upstream 6")
|
||||||
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainConfig, canonicalDomainCache, redirectsCache)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,12 +18,13 @@ import (
|
||||||
func tryUpstream(ctx *context.Context, giteaClient *gitea.Client,
|
func tryUpstream(ctx *context.Context, giteaClient *gitea.Client,
|
||||||
mainDomainSuffix, trimmedHost string,
|
mainDomainSuffix, trimmedHost string,
|
||||||
options *upstream.Options,
|
options *upstream.Options,
|
||||||
|
canonicalDomainConfig string,
|
||||||
canonicalDomainCache cache.ICache,
|
canonicalDomainCache cache.ICache,
|
||||||
redirectsCache cache.ICache,
|
redirectsCache cache.ICache,
|
||||||
) {
|
) {
|
||||||
// check if a canonical domain exists on a request on MainDomain
|
// check if a canonical domain exists on a request on MainDomain
|
||||||
if strings.HasSuffix(trimmedHost, mainDomainSuffix) && !options.ServeRaw {
|
if strings.HasSuffix(trimmedHost, mainDomainSuffix) && !options.ServeRaw {
|
||||||
canonicalDomain, _ := options.CheckCanonicalDomain(giteaClient, "", mainDomainSuffix, canonicalDomainCache)
|
canonicalDomain, _ := options.CheckCanonicalDomain(giteaClient, "", mainDomainSuffix, canonicalDomainConfig, canonicalDomainCache)
|
||||||
if !strings.HasSuffix(strings.SplitN(canonicalDomain, "/", 2)[0], mainDomainSuffix) {
|
if !strings.HasSuffix(strings.SplitN(canonicalDomain, "/", 2)[0], mainDomainSuffix) {
|
||||||
canonicalPath := ctx.Req.RequestURI
|
canonicalPath := ctx.Req.RequestURI
|
||||||
if options.TargetRepo != defaultPagesRepo {
|
if options.TargetRepo != defaultPagesRepo {
|
||||||
|
|
|
@ -101,6 +101,7 @@ func Serve(ctx *cli.Context) error {
|
||||||
giteaClient,
|
giteaClient,
|
||||||
acmeClient,
|
acmeClient,
|
||||||
cfg.Server.PagesBranches[0],
|
cfg.Server.PagesBranches[0],
|
||||||
|
cfg.Server.CanonicalDomainFile,
|
||||||
challengeCache, canonicalDomainCache,
|
challengeCache, canonicalDomainCache,
|
||||||
certDB,
|
certDB,
|
||||||
cfg.ACME.NoDNS01,
|
cfg.ACME.NoDNS01,
|
||||||
|
|
|
@ -14,12 +14,12 @@ import (
|
||||||
// canonicalDomainCacheTimeout specifies the timeout for the canonical domain cache.
|
// canonicalDomainCacheTimeout specifies the timeout for the canonical domain cache.
|
||||||
var canonicalDomainCacheTimeout = 15 * time.Minute
|
var canonicalDomainCacheTimeout = 15 * time.Minute
|
||||||
|
|
||||||
const canonicalDomainConfig = ".domains"
|
|
||||||
|
|
||||||
// CheckCanonicalDomain returns the canonical domain specified in the repo (using the `.domains` file).
|
// CheckCanonicalDomain returns the canonical domain specified in the repo (using the `.domains` file).
|
||||||
func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain, mainDomainSuffix string, canonicalDomainCache cache.ICache) (domain string, valid bool) {
|
func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain, mainDomainSuffix, canonicalDomainConfig string, canonicalDomainCache cache.ICache) (domain string, valid bool) {
|
||||||
|
canonicalDomainCacheKey := o.TargetOwner + "/" + o.TargetRepo + "/" + o.TargetBranch + "/" + canonicalDomainConfig
|
||||||
|
|
||||||
// Check if this request is cached.
|
// Check if this request is cached.
|
||||||
if cachedValue, ok := canonicalDomainCache.Get(o.TargetOwner + "/" + o.TargetRepo + "/" + o.TargetBranch); ok {
|
if cachedValue, ok := canonicalDomainCache.Get(canonicalDomainCacheKey); ok {
|
||||||
domains := cachedValue.([]string)
|
domains := cachedValue.([]string)
|
||||||
for _, domain := range domains {
|
for _, domain := range domains {
|
||||||
if domain == actualDomain {
|
if domain == actualDomain {
|
||||||
|
@ -62,7 +62,7 @@ func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add result to cache.
|
// Add result to cache.
|
||||||
_ = canonicalDomainCache.Set(o.TargetOwner+"/"+o.TargetRepo+"/"+o.TargetBranch, domains, canonicalDomainCacheTimeout)
|
_ = canonicalDomainCache.Set(canonicalDomainCacheKey, domains, canonicalDomainCacheTimeout)
|
||||||
|
|
||||||
// Return the first domain from the list and return if any of the domains
|
// Return the first domain from the list and return if any of the domains
|
||||||
// matched the requested domain.
|
// matched the requested domain.
|
||||||
|
|
Loading…
Add table
Reference in a new issue