From d70ee291d4a6af2bc46bea7b773fb81ac375b72c Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Sun, 8 Oct 2023 18:39:18 -0400 Subject: [PATCH] forgot to come back for trusted headers --- README.md | 1 + cmd/echoip/main.go | 4 +--- config/config.go | 13 ++++++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b71038e..a7fe331 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,7 @@ ECHOIP_LISTEN=":8080" ECHOIP_TEMPLATE_DIR="html/" ECHOIP_REDIS_URL="redis://localhost:6379" ECHOIP_DATABASE="ipstack" +ECHOIP_TRUSTED_HEADERS="X-Real-IP,X-Forwaded-For" ECHOIP_IPSTACK_API_KEY="askdfj39sjdkf29dsjfk39sdfkj3" ECHOIP_GEOIP_COUNTRY_FILE="/full/path/to/file.db" ECHOIP_GEOIP_CITY_FILE="/full/path/to/file.db" diff --git a/cmd/echoip/main.go b/cmd/echoip/main.go index 37ecdd2..166e7f9 100644 --- a/cmd/echoip/main.go +++ b/cmd/echoip/main.go @@ -46,9 +46,7 @@ func main() { if err != nil { log.Printf("Error opening config file (/etc/echoip/config.toml): %s", err) - } - - if err == nil { + } else { var b []byte b, err = io.ReadAll(file) if err != nil { diff --git a/config/config.go b/config/config.go index 3a4b4e2..93d5c6e 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,7 @@ package config import ( "os" "strconv" + "strings" ) type IPStack struct { @@ -36,11 +37,10 @@ type Config struct { func GetConfig() (Config, error) { defaultConfig := Config{ - Listen: getenv_string("ECHOIP_LISTEN", ":8080"), - TemplateDir: getenv_string("ECHOIP_TEMPLATE_DIR", "html/"), - RedisUrl: getenv_string("ECHOIP_REDIS_URL", ""), - TrustedHeaders: nil, - Database: getenv_string("ECHOIP_DATABASE", "geoip"), + Listen: getenv_string("ECHOIP_LISTEN", ":8080"), + TemplateDir: getenv_string("ECHOIP_TEMPLATE_DIR", "html/"), + RedisUrl: getenv_string("ECHOIP_REDIS_URL", ""), + Database: getenv_string("ECHOIP_DATABASE", "geoip"), IPStack: IPStack{ ApiKey: getenv_string("ECHOIP_IPSTACK_API_KEY", ""), }, @@ -93,6 +93,9 @@ func GetConfig() (Config, error) { } defaultConfig.IPStack.EnableSecurity = ipStackEnableSecurity + trustedHeaders := getenv_string("ECHOIP_TRUSTED_HEADERS", "") + defaultConfig.TrustedHeaders = strings.Split(trustedHeaders, ",") + return defaultConfig, nil }