better ip detection

This commit is contained in:
Egor Aristov 2025-02-10 16:40:49 +03:00
parent 541e97cfe6
commit 0f89c8428b
3 changed files with 34 additions and 0 deletions

20
cmd/webserver/ipranges.go Normal file
View File

@ -0,0 +1,20 @@
package main
var IpRanges = []string{
// Cloudflare:
"173.245.48.0/20",
"103.21.244.0/22",
"103.22.200.0/22",
"103.31.4.0/22",
"141.101.64.0/18",
"108.162.192.0/18",
"190.93.240.0/20",
"188.114.96.0/20",
"197.234.240.0/22",
"198.41.128.0/17",
"162.158.0.0/15",
"104.16.0.0/13",
"104.24.0.0/14",
"172.64.0.0/13",
"131.0.72.0/22",
}

View File

@ -11,9 +11,11 @@ import (
"github.com/labstack/gommon/log" "github.com/labstack/gommon/log"
"github.com/nats-io/nats.go" "github.com/nats-io/nats.go"
"golang.org/x/time/rate" "golang.org/x/time/rate"
"net"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"slices"
"time" "time"
) )
@ -50,6 +52,16 @@ func main() {
e.Use(middleware.Logger()) e.Use(middleware.Logger())
e.Use(middleware.Recover()) e.Use(middleware.Recover())
var trustOptions []echo.TrustOption
for _, ipRange := range slices.Concat(IpRanges, cfg.TrustedIpRanges) {
_, network, err := net.ParseCIDR(ipRange)
if err != nil {
log.Panicf("Invalid ip range: %s", ipRange)
}
trustOptions = append(trustOptions, echo.TrustIPRange(network))
}
e.IPExtractor = echo.ExtractIPFromXFFHeader(trustOptions...)
e.StaticFS("/", echo.MustSubFS(wizard_vue.EmbedFS, wizard_vue.FSPrefix)) e.StaticFS("/", echo.MustSubFS(wizard_vue.EmbedFS, wizard_vue.FSPrefix))
apiHandler := httpApi.New( apiHandler := httpApi.New(

View File

@ -21,6 +21,8 @@ type Config struct {
// Rate limits don't apply to cache // Rate limits don't apply to cache
RateLimitEvery float64 `env:"RATE_LIMIT_EVERY" env-default:"60" validate:"number,gt=0"` RateLimitEvery float64 `env:"RATE_LIMIT_EVERY" env-default:"60" validate:"number,gt=0"`
RateLimitBurst int `env:"RATE_LIMIT_BURST" env-default:"10" validate:"number,gte=0"` RateLimitBurst int `env:"RATE_LIMIT_BURST" env-default:"10" validate:"number,gte=0"`
// IP ranges of reverse proxies for correct real ip detection (cidr format, sep. by comma)
TrustedIpRanges []string `env:"TRUSTED_IP_RANGES" env-default:"" validate:"omitempty,dive,cidr"`
} }
func Read() (Config, error) { func Read() (Config, error) {