Skip to content

Commit e644ff8

Browse files
kumapower17aldas
authored andcommitted
Replace custom private IP range check with built-in net.IP.IsPrivate() method
1 parent 40e2e8f commit e644ff8

File tree

1 file changed

+1
-11
lines changed

1 file changed

+1
-11
lines changed

ip.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,24 +179,14 @@ func newIPChecker(configs []TrustOption) *ipChecker {
179179
return checker
180180
}
181181

182-
// Go1.16+ added `ip.IsPrivate()` but until that use this implementation
183-
func isPrivateIPRange(ip net.IP) bool {
184-
if ip4 := ip.To4(); ip4 != nil {
185-
return ip4[0] == 10 ||
186-
ip4[0] == 172 && ip4[1]&0xf0 == 16 ||
187-
ip4[0] == 192 && ip4[1] == 168
188-
}
189-
return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc
190-
}
191-
192182
func (c *ipChecker) trust(ip net.IP) bool {
193183
if c.trustLoopback && ip.IsLoopback() {
194184
return true
195185
}
196186
if c.trustLinkLocal && ip.IsLinkLocalUnicast() {
197187
return true
198188
}
199-
if c.trustPrivateNet && isPrivateIPRange(ip) {
189+
if c.trustPrivateNet && ip.IsPrivate() {
200190
return true
201191
}
202192
for _, trustedRange := range c.trustExtraRanges {

0 commit comments

Comments
 (0)