if ip and block is equal, return true
parent
b861d27ea6
commit
05ce6164e8
|
@ -40,13 +40,17 @@ func isInBlock(ip string, block string) (bool, error) {
|
||||||
return false, fmt.Errorf("CIDR is nil")
|
return false, fmt.Errorf("CIDR is nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ip == block {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
i := net.ParseIP(ip)
|
i := net.ParseIP(ip)
|
||||||
if i == nil {
|
if i == nil {
|
||||||
return false, fmt.Errorf("parsed IP is 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)
|
_, b, err := net.ParseCIDR(block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errors.Wrapf(err, "Error Parsing block %s", b)
|
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) {
|
if b.Contains(i) {
|
||||||
return true, nil
|
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
|
// 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
|
wanntAErr bool
|
||||||
}{
|
}{
|
||||||
{"", "192.168.0.1/32", false, true},
|
{"", "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.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},
|
{"192.168.0.1", "192.168.0.1/18", true, false},
|
||||||
{"abcd", "192.168.0.1/18", false, true},
|
{"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 {
|
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) {
|
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", 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", 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, ",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, "10.10.0.13,192.168.0.13"},
|
||||||
{"192.168.0.13", "NO_PROXY", true, "192.168.0.13/22"},
|
{"192.168.0.13", "NO_PROXY", true, "192.168.0.13/22"},
|
||||||
|
|
Loading…
Reference in New Issue