if ip and block is equal, return true

pull/7619/head
Kenta Iso 2020-04-12 20:22:25 +09:00
parent b861d27ea6
commit 05ce6164e8
2 changed files with 21 additions and 12 deletions

View File

@ -40,13 +40,17 @@ func isInBlock(ip string, block string) (bool, error) {
return false, fmt.Errorf("CIDR is nil")
}
if ip == block {
return true, nil
}
i := net.ParseIP(ip)
if i == nil {
return false, fmt.Errorf("parsed IP is nil")
}
if !strings.Contains(block, "/") {
return false, nil
}
// check the block if it's CIDR
if strings.Contains(block, "/") {
_, b, err := net.ParseCIDR(block)
if err != nil {
return false, errors.Wrapf(err, "Error Parsing block %s", b)
@ -55,7 +59,9 @@ func isInBlock(ip string, block string) (bool, error) {
if b.Contains(i) {
return true, nil
}
return false, errors.Wrapf(err, "Error ip not in block")
}
return false, errors.New("Error ip not in block")
}
// ExcludeIP takes ip or CIDR as string and excludes it from the http(s)_proxy

View File

@ -53,11 +53,13 @@ func TestIsInBlock(t *testing.T) {
wanntAErr bool
}{
{"", "192.168.0.1/32", false, true},
{"192.168.0.1", "", false, true},
{"192.168.0.1", "192.168.0.1", true, false},
{"192.168.0.1", "192.168.0.1/32", true, false},
{"192.168.0.2", "192.168.0.1/32", false, false},
{"192.168.0.2", "192.168.0.1/32", false, true},
{"192.168.0.1", "192.168.0.1/18", true, false},
{"abcd", "192.168.0.1/18", false, true},
{"192.168.0.1", "foo", false, false},
{"192.168.0.1", "foo", false, true},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s in %s Want: %t WantAErr: %t", tc.ip, tc.block, tc.want, tc.wanntAErr), func(t *testing.T) {
@ -122,6 +124,7 @@ func TestCheckEnv(t *testing.T) {
{"192.168.0.13", "NO_PROXY", false, ""},
{"192.168.0.13", "NO_PROXY", false, ","},
{"192.168.0.13", "NO_PROXY", true, "192.168.0.13"},
{"192.168.0.13", "NO_PROXY", false, "192.168.0.14"},
{"192.168.0.13", "NO_PROXY", true, ",192.168.0.13"},
{"192.168.0.13", "NO_PROXY", true, "10.10.0.13,192.168.0.13"},
{"192.168.0.13", "NO_PROXY", true, "192.168.0.13/22"},