From ac24cda0b972fcaff2a31723b32eaf929b2ccee2 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Thu, 12 Nov 2026 16:04:10 -0700 Subject: [PATCH] added configuration for IP resolver --- config/config.go | 22 ++++++++++++++++++++++ config/default.yaml | 1 + main.go | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index d5abefa..1ad9c1e 100644 --- a/config/config.go +++ b/config/config.go @@ -21,10 +21,12 @@ import ( "os" "path/filepath" "reflect" + "slices" "git.erbosoft.com/amy/amsterdam/util" argparse "github.com/alexflint/go-arg" "github.com/dustin/go-humanize" + "github.com/labstack/echo/v5" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" ) @@ -109,6 +111,7 @@ type AmConfig struct { WelcomeTitle string `yaml:"welcomeTitle"` WelcomeMessage string `yaml:"welcomeMessage"` TopPostsTitle string `yaml:"topPostsTitle"` + IPResolver string `yaml:"ipResolver"` RateLimit struct { Rate float64 `yaml:"rate"` Burst int `yaml:"burst"` @@ -368,6 +371,22 @@ func AmOpenExternalContentPath() (fs.FS, error) { return root.FS(), nil } +// validIPResolvers contains the list of valid values for GlobalConfig.Site.IPResolver. +var validIPResolvers = [3]string{"direct", "realIP", "forwardedFor"} + +// AmGetConfiguredIPExtractor returns the configured IP Extractor function based on GlobalConfig. +func AmGetConfiguredIPExtractor() echo.IPExtractor { + switch GlobalConfig.Site.IPResolver { + case "direct": + return echo.ExtractIPDirect() + case "realIP": + return echo.ExtractIPFromRealIPHeader() + case "forwardedFor": + return echo.ExtractIPFromXFFHeader() + } + panic("improperly configured IP Extractor") +} + // SetupConfig loads the command line arguments, loads the config file, and prepares GlobalConfig. func SetupConfig() { argparse.MustParse(&CommandLine) @@ -398,6 +417,9 @@ func SetupConfig() { } // Error-check values. + if !slices.Contains(validIPResolvers[:], GlobalConfig.Site.IPResolver) { + panic(fmt.Sprintf("invalid IP resolver name \"%s\"", GlobalConfig.Site.IPResolver)) + } if GlobalConfig.Database.Pooling.MaxOpen < 0 { panic("maximum database connections must not be less than zero") } else if GlobalConfig.Database.Pooling.MaxOpen == 0 { diff --git a/config/default.yaml b/config/default.yaml index cd26565..e8aaa38 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -36,6 +36,7 @@ site: Welcome to the Amsterdam Web Communities System. To get the most out of this site, you should log in or create an account, using one of the links above. topPostsTitle: "Latest from the Conferences" + ipResolver: direct rateLimit: rate: 20.0 burst: 0 diff --git a/main.go b/main.go index 70e95ed..8656647 100644 --- a/main.go +++ b/main.go @@ -64,7 +64,7 @@ func setupEcho() *echo.Echo { e.Logger = slog.New(NewSlogLogrusHandler()) e.Renderer = &ui.TemplateRenderer{} e.HTTPErrorHandler = AmErrorHandler - e.IPExtractor = echo.LegacyIPExtractor() + e.IPExtractor = config.AmGetConfiguredIPExtractor() if !config.CommandLine.DebugPanic { e.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{ StackSize: int(config.GlobalComputedConfig.PanicRecoveryStack),