Apply JnCrMx's patch for optional HTTP-only mode

This commit is contained in:
Ivan Davydov 2024-08-14 16:29:04 +03:00
parent 9524b1eb12
commit 5352937065
6 changed files with 62 additions and 40 deletions

View file

@ -152,6 +152,12 @@ var (
EnvVars: []string{"PROFILING_ADDRESS"}, EnvVars: []string{"PROFILING_ADDRESS"},
Value: "localhost:9999", Value: "localhost:9999",
}, },
&cli.BoolFlag{
Name: "http-only-mode",
Usage: "serve content directly via HTTP using the Host header to identify the repository",
EnvVars: []string{"HTTP_ONLY_MODE"},
Value: false,
},
// ############################ // ############################
// ### ACME Client Settings ### // ### ACME Client Settings ###

View file

@ -9,6 +9,7 @@ mainDomain = 'codeberg.page'
rawDomain = 'raw.codeberg.page' rawDomain = 'raw.codeberg.page'
allowedCorsDomains = ['fonts.codeberg.org', 'design.codeberg.org'] allowedCorsDomains = ['fonts.codeberg.org', 'design.codeberg.org']
blacklistedPaths = ['do/not/use'] blacklistedPaths = ['do/not/use']
httpOnlyMode = false
[forge] [forge]
root = 'https://codeberg.org' root = 'https://codeberg.org'

View file

@ -18,6 +18,7 @@ type ServerConfig struct {
PagesBranches []string PagesBranches []string
AllowedCorsDomains []string AllowedCorsDomains []string
BlacklistedPaths []string BlacklistedPaths []string
HttpOnlyMode bool `default:"false"`
} }
type ForgeConfig struct { type ForgeConfig struct {

View file

@ -84,6 +84,9 @@ func mergeServerConfig(ctx *cli.Context, config *ServerConfig) {
if ctx.IsSet("blacklisted-paths") { if ctx.IsSet("blacklisted-paths") {
config.BlacklistedPaths = ctx.StringSlice("blacklisted-paths") config.BlacklistedPaths = ctx.StringSlice("blacklisted-paths")
} }
if ctx.IsSet("http-only-mode") {
config.HttpOnlyMode = ctx.Bool("http-only-mode")
}
// add the paths that should always be blacklisted // add the paths that should always be blacklisted
config.BlacklistedPaths = append(config.BlacklistedPaths, ALWAYS_BLACKLISTED_PATHS...) config.BlacklistedPaths = append(config.BlacklistedPaths, ALWAYS_BLACKLISTED_PATHS...)

View file

@ -10,6 +10,7 @@ rawDomain = 'raw.codeberg.page'
pagesBranches = ["pages"] pagesBranches = ["pages"]
allowedCorsDomains = [] allowedCorsDomains = []
blacklistedPaths = [] blacklistedPaths = []
httpOnlyMode = false
[forge] [forge]
root = 'https://codeberg.org' root = 'https://codeberg.org'

View file

@ -79,6 +79,15 @@ func Serve(ctx *cli.Context) error {
return fmt.Errorf("could not create new gitea client: %v", err) return fmt.Errorf("could not create new gitea client: %v", err)
} }
var listener net.Listener
if cfg.Server.HttpOnlyMode {
log.Info().Msgf("Create TCP listener on %s", listeningHTTPAddress)
listener_, err := net.Listen("tcp", listeningHTTPAddress)
if err != nil {
return fmt.Errorf("couldn't create listener: %v", err)
}
listener = listener_
} else {
acmeClient, err := acme.CreateAcmeClient(cfg.ACME, cfg.Server.HttpServerEnabled, challengeCache) acmeClient, err := acme.CreateAcmeClient(cfg.ACME, cfg.Server.HttpServerEnabled, challengeCache)
if err != nil { if err != nil {
return err return err
@ -125,6 +134,7 @@ func Serve(ctx *cli.Context) error {
} }
}() }()
} }
}
if ctx.IsSet("enable-profiling") { if ctx.IsSet("enable-profiling") {
StartProfilingServer(ctx.String("profiling-address")) StartProfilingServer(ctx.String("profiling-address"))
@ -134,7 +144,7 @@ func Serve(ctx *cli.Context) error {
sslHandler := handler.Handler(cfg.Server, giteaClient, canonicalDomainCache, redirectsCache) sslHandler := handler.Handler(cfg.Server, giteaClient, canonicalDomainCache, redirectsCache)
// Start the ssl listener // Start the ssl listener
log.Info().Msgf("Start SSL server using TCP listener on %s", listener.Addr()) log.Info().Msgf("Start main server using TCP listener on %s", listener.Addr())
return http.Serve(listener, sslHandler) return http.Serve(listener, sslHandler)
} }