1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-03 10:17:03 +00:00

- fixing incorrect IP comparison. unsafeCompare was comparing between converted IP address with IP string.

- `IPInRange` should be both inclusive to accommodate /32 private subnet additions
- added more tests
This commit is contained in:
Muhammad Iqbal Alaydrus
2022-11-22 17:31:22 +07:00
parent 6a49bc8315
commit 5db7ea1732
2 changed files with 31 additions and 7 deletions

View File

@@ -4,7 +4,6 @@ import (
"bytes"
"net"
"strings"
"unsafe"
)
/* Based on:
@@ -18,14 +17,9 @@ type IPRange struct {
End string `ini:"end" json:"end" yaml:"End" toml:"End"`
}
func unsafeCompare(a []byte, b string) int {
bb := *(*[]byte)(unsafe.Pointer(&b))
return bytes.Compare(a, bb)
}
// IPInRange reports whether a given IP Address is within a range given.
func IPInRange(r IPRange, ipAddress net.IP) bool {
return unsafeCompare(ipAddress, r.Start) >= 0 && unsafeCompare(ipAddress, r.End) < 0
return bytes.Compare(ipAddress, net.ParseIP(r.Start)) >= 0 && bytes.Compare(ipAddress, net.ParseIP(r.End)) <= 0
}
// IPIsPrivateSubnet reports whether this "ipAddress" is in a private subnet.