if ip and block is equal, return true
parent
b861d27ea6
commit
05ce6164e8
|
@ -40,22 +40,28 @@ 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
|
||||
}
|
||||
_, b, err := net.ParseCIDR(block)
|
||||
if err != nil {
|
||||
return false, errors.Wrapf(err, "Error Parsing block %s", b)
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
if b.Contains(i) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -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"},
|
||||
|
|
Loading…
Reference in New Issue