1
0
mirror of https://github.com/labstack/echo.git synced 2025-11-25 22:32:23 +02:00

Replace custom private IP range check with built-in net.IP.IsPrivate() method

This commit is contained in:
kumapower17
2025-10-15 23:41:19 +08:00
committed by Martti T.
parent 40e2e8faf9
commit e644ff8f7b

12
ip.go
View File

@@ -179,16 +179,6 @@ func newIPChecker(configs []TrustOption) *ipChecker {
return checker return checker
} }
// Go1.16+ added `ip.IsPrivate()` but until that use this implementation
func isPrivateIPRange(ip net.IP) bool {
if ip4 := ip.To4(); ip4 != nil {
return ip4[0] == 10 ||
ip4[0] == 172 && ip4[1]&0xf0 == 16 ||
ip4[0] == 192 && ip4[1] == 168
}
return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc
}
func (c *ipChecker) trust(ip net.IP) bool { func (c *ipChecker) trust(ip net.IP) bool {
if c.trustLoopback && ip.IsLoopback() { if c.trustLoopback && ip.IsLoopback() {
return true return true
@@ -196,7 +186,7 @@ func (c *ipChecker) trust(ip net.IP) bool {
if c.trustLinkLocal && ip.IsLinkLocalUnicast() { if c.trustLinkLocal && ip.IsLinkLocalUnicast() {
return true return true
} }
if c.trustPrivateNet && isPrivateIPRange(ip) { if c.trustPrivateNet && ip.IsPrivate() {
return true return true
} }
for _, trustedRange := range c.trustExtraRanges { for _, trustedRange := range c.trustExtraRanges {