fixed CIDR whitelist shared its IP cache map when it should not

This commit is contained in:
yusing 2024-11-10 03:25:33 +08:00
parent f97e3f65fe
commit cceebff93a

View file

@ -11,15 +11,14 @@ import (
type cidrWhitelist struct { type cidrWhitelist struct {
cidrWhitelistOpts cidrWhitelistOpts
m *Middleware m *Middleware
cachedAddr F.Map[string, bool] // cache for trusted IPs
} }
type cidrWhitelistOpts struct { type cidrWhitelistOpts struct {
Allow []*types.CIDR `json:"allow"` Allow []*types.CIDR `json:"allow"`
StatusCode int `json:"statusCode"` StatusCode int `json:"statusCode"`
Message string `json:"message"` Message string `json:"message"`
cachedAddr F.Map[string, bool] // cache for trusted IPs
} }
var ( var (
@ -28,7 +27,6 @@ var (
Allow: []*types.CIDR{}, Allow: []*types.CIDR{},
StatusCode: http.StatusForbidden, StatusCode: http.StatusForbidden,
Message: "IP not allowed", Message: "IP not allowed",
cachedAddr: F.NewMapOf[string, bool](),
} }
) )
@ -39,6 +37,7 @@ func NewCIDRWhitelist(opts OptionsRaw) (*Middleware, E.Error) {
before: wl.checkIP, before: wl.checkIP,
} }
wl.cidrWhitelistOpts = cidrWhitelistDefaults wl.cidrWhitelistOpts = cidrWhitelistDefaults
wl.cachedAddr = F.NewMapOf[string, bool]()
err := Deserialize(opts, &wl.cidrWhitelistOpts) err := Deserialize(opts, &wl.cidrWhitelistOpts)
if err != nil { if err != nil {
return nil, err return nil, err