Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add IsPrivateIP #483

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func IsHost(str string) bool
func IsIP(str string) bool
func IsIPv4(str string) bool
func IsIPv6(str string) bool
func IsPrivateIP(ip string) bool
func IsISBN(str string, version int) bool
func IsISBN10(str string) bool
func IsISBN13(str string) bool
Expand Down Expand Up @@ -370,6 +371,7 @@ Here is a list of available validators for struct fields (validator - used funct
"base64": IsBase64,
"datauri": IsDataURI,
"ip": IsIP,
"ip": IsPrivateIP
"port": IsPort,
"ipv4": IsIPv4,
"ipv6": IsIPv6,
Expand Down
38 changes: 38 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,44 @@ func IsEmail(str string) bool {
return rxEmail.MatchString(str)
}

// private ip lists
var aPrivateIP []*net.IPNet

// IsPrivateIP check if the string is an private IP
func IsPrivateIP(ip string) bool {
if 0 == len(aPrivateIP) {
for _, cidr := range []string{
"127.0.0.0/8", // IPv4 loopback
"10.0.0.0/8", // RFC1918
"172.16.0.0/12", // RFC1918
"192.168.0.0/16", // RFC1918
"169.254.0.0/16", // RFC3927 link-local
"::1/128", // IPv6 loopback
"fe80::/10", // IPv6 link-local
"fc00::/7", // IPv6 unique local addr
} {
_, block, err := net.ParseCIDR(cidr)
if err != nil {
continue
}
aPrivateIP = append(aPrivateIP, block)
}
}
oIp := net.ParseIP(ip)
if oIp == nil {
return false
}
if oIp.IsLoopback() || oIp.IsLinkLocalUnicast() || oIp.IsLinkLocalMulticast() || oIp.IsPrivate() {
return true
}
for _, s := range aPrivateIP {
if s.Contains(oIp) {
return true
}
}
return false
}

// IsExistingEmail checks if the string is an email of existing domain
func IsExistingEmail(email string) bool {

Expand Down
27 changes: 27 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3865,3 +3865,30 @@ func TestIsE164(t *testing.T) {
}
}
}

func TestIsPrivateIP(t *testing.T) {
t.Parallel()
// Without version
var tests = []struct {
param string
expected bool
}{
{"", false},
{"127.0.0.1", true},
{"127.0.2.1", true},
{"172.16.0.19", true},
{"fe80::b8:6771:9e7c:f6f1", true},
{"ee80::b8:6771:9e7c:f6f1", false},
{"fe80::1", true},
{"192.168.10.1", true},
{"::1", true},
{"2001:db8:0000:1:1:1:1:1", false},
{"300.0.0.0", false},
}
for _, test := range tests {
actual := IsPrivateIP(test.param)
if actual != test.expected {
t.Errorf("Expected IsPrivateIP(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}