added configuration for IP resolver

This commit is contained in:
amy
2026-11-12 16:04:10 -07:00
parent b6632b2d09
commit ac24cda0b9
3 changed files with 24 additions and 1 deletions
+22
View File
@@ -21,10 +21,12 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"slices"
"git.erbosoft.com/amy/amsterdam/util" "git.erbosoft.com/amy/amsterdam/util"
argparse "github.com/alexflint/go-arg" argparse "github.com/alexflint/go-arg"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"github.com/labstack/echo/v5"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@@ -109,6 +111,7 @@ type AmConfig struct {
WelcomeTitle string `yaml:"welcomeTitle"` WelcomeTitle string `yaml:"welcomeTitle"`
WelcomeMessage string `yaml:"welcomeMessage"` WelcomeMessage string `yaml:"welcomeMessage"`
TopPostsTitle string `yaml:"topPostsTitle"` TopPostsTitle string `yaml:"topPostsTitle"`
IPResolver string `yaml:"ipResolver"`
RateLimit struct { RateLimit struct {
Rate float64 `yaml:"rate"` Rate float64 `yaml:"rate"`
Burst int `yaml:"burst"` Burst int `yaml:"burst"`
@@ -368,6 +371,22 @@ func AmOpenExternalContentPath() (fs.FS, error) {
return root.FS(), nil 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. // SetupConfig loads the command line arguments, loads the config file, and prepares GlobalConfig.
func SetupConfig() { func SetupConfig() {
argparse.MustParse(&CommandLine) argparse.MustParse(&CommandLine)
@@ -398,6 +417,9 @@ func SetupConfig() {
} }
// Error-check values. // 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 { if GlobalConfig.Database.Pooling.MaxOpen < 0 {
panic("maximum database connections must not be less than zero") panic("maximum database connections must not be less than zero")
} else if GlobalConfig.Database.Pooling.MaxOpen == 0 { } else if GlobalConfig.Database.Pooling.MaxOpen == 0 {
+1
View File
@@ -36,6 +36,7 @@ site:
Welcome to the <strong>Amsterdam Web Communities System</strong>. To get the most out of this site, you should log in or create an account, Welcome to the <strong>Amsterdam Web Communities System</strong>. To get the most out of this site, you should log in or create an account,
using one of the links above. using one of the links above.
topPostsTitle: "Latest from the Conferences" topPostsTitle: "Latest from the Conferences"
ipResolver: direct
rateLimit: rateLimit:
rate: 20.0 rate: 20.0
burst: 0 burst: 0
+1 -1
View File
@@ -64,7 +64,7 @@ func setupEcho() *echo.Echo {
e.Logger = slog.New(NewSlogLogrusHandler()) e.Logger = slog.New(NewSlogLogrusHandler())
e.Renderer = &ui.TemplateRenderer{} e.Renderer = &ui.TemplateRenderer{}
e.HTTPErrorHandler = AmErrorHandler e.HTTPErrorHandler = AmErrorHandler
e.IPExtractor = echo.LegacyIPExtractor() e.IPExtractor = config.AmGetConfiguredIPExtractor()
if !config.CommandLine.DebugPanic { if !config.CommandLine.DebugPanic {
e.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{ e.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{
StackSize: int(config.GlobalComputedConfig.PanicRecoveryStack), StackSize: int(config.GlobalComputedConfig.PanicRecoveryStack),