Skip to content

Commit

Permalink
use array instead of map for trusted proxy IP addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
xEricL committed Apr 11, 2024
1 parent bb41540 commit 7639f1e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 5 additions & 4 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ type Config struct {
//
// Default: []string
TrustedProxies []string `json:"trusted_proxies"`
trustedProxiesMap map[string]struct{}
trustedProxyIPs []net.IP
trustedProxyRanges []*net.IPNet

// If set to true, c.IP() and c.IPs() will validate IP addresses before returning them.
Expand Down Expand Up @@ -550,8 +550,7 @@ func New(config ...Config) *App {
if len(app.config.RequestMethods) == 0 {
app.config.RequestMethods = DefaultMethods
}

app.config.trustedProxiesMap = make(map[string]struct{}, len(app.config.TrustedProxies))
app.config.trustedProxyIPs = make([]net.IP, 0, len(app.config.TrustedProxies))
for _, ipAddress := range app.config.TrustedProxies {
app.handleTrustedProxy(ipAddress)
}
Expand Down Expand Up @@ -580,7 +579,9 @@ func (app *App) handleTrustedProxy(ipAddress string) {
app.config.trustedProxyRanges = append(app.config.trustedProxyRanges, ipNet)
}
} else {
app.config.trustedProxiesMap[ipAddress] = struct{}{}
if ip := net.ParseIP(ipAddress); ip != nil {
app.config.trustedProxyIPs = append(app.config.trustedProxyIPs, ip)
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1664,8 +1664,10 @@ func (c *DefaultCtx) IsProxyTrusted() bool {

ip := c.fasthttp.RemoteIP()

if _, trusted := c.app.config.trustedProxiesMap[ip.String()]; trusted {
return true
for _, trusted := range c.app.config.trustedProxyIPs {
if trusted.Equal(ip) {
return true
}
}

for _, ipNet := range c.app.config.trustedProxyRanges {
Expand Down

0 comments on commit 7639f1e

Please sign in to comment.