-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve searching in CIDR ranges using cidranger (#181)
* improve searching in CIDR ranges using cidranger Signed-off-by: Amir Malka <[email protected]> * improve searching in CIDR ranges using cidranger Signed-off-by: Amir Malka <[email protected]> * fix Signed-off-by: Amir Malka <[email protected]> * remove known servers Signed-off-by: Amir Malka <[email protected]> --------- Signed-off-by: Amir Malka <[email protected]>
- Loading branch information
Showing
14 changed files
with
253 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package softwarecomposition | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/yl2chen/cidranger" | ||
) | ||
|
||
var _ IKnownServersFinder = (*KnownServersFinderImpl)(nil) | ||
var _ IKnownServerEntry = (*KnownServersFinderEntry)(nil) | ||
|
||
type KnownServersFinderImpl struct { | ||
ranger cidranger.Ranger | ||
} | ||
|
||
type KnownServersFinderEntry struct { | ||
knownServer KnownServerEntry | ||
network net.IPNet | ||
} | ||
|
||
func NewKnownServersFinderImpl(knownServers []KnownServer) IKnownServersFinder { | ||
// build the ranger for searching | ||
ranger := cidranger.NewPCTrieRanger() | ||
for _, knownServer := range knownServers { | ||
for _, knownServerEntry := range knownServer.Spec { | ||
if v := NewKnownServersFinderEntry(knownServerEntry); v != nil { | ||
_ = ranger.Insert(v) | ||
} | ||
} | ||
} | ||
return &KnownServersFinderImpl{ | ||
ranger: ranger, | ||
} | ||
} | ||
|
||
func (k *KnownServersFinderImpl) Contains(ip net.IP) ([]IKnownServerEntry, bool) { | ||
if k.ranger == nil { | ||
return nil, false | ||
} | ||
contains, _ := k.ranger.Contains(ip) | ||
if contains { | ||
if entries, err := k.ranger.ContainingNetworks(ip); err == nil && len(entries) > 0 { | ||
knownServersEntries := make([]IKnownServerEntry, 0, len(entries)) | ||
for _, entry := range entries { | ||
if v, ok := entry.(*KnownServersFinderEntry); ok { | ||
knownServersEntries = append(knownServersEntries, v) | ||
} | ||
} | ||
return knownServersEntries, true | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
func NewKnownServersFinderEntry(kse KnownServerEntry) *KnownServersFinderEntry { | ||
_, res, err := net.ParseCIDR(kse.IPBlock) | ||
if err != nil || res == nil { | ||
return nil | ||
} | ||
return &KnownServersFinderEntry{knownServer: kse, network: *res} | ||
} | ||
|
||
func (k *KnownServersFinderEntry) GetServer() string { | ||
return k.knownServer.Server | ||
} | ||
|
||
func (k *KnownServersFinderEntry) GetName() string { | ||
return k.knownServer.Name | ||
} | ||
|
||
func (k *KnownServersFinderEntry) GetIPBlock() string { | ||
return k.knownServer.IPBlock | ||
} | ||
|
||
func (k *KnownServersFinderEntry) Network() net.IPNet { | ||
return k.network | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package softwarecomposition | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestContains(t *testing.T) { | ||
knownServers := []KnownServer{ | ||
{ | ||
Spec: []KnownServerEntry{ | ||
{Server: "server1", Name: "name1", IPBlock: "192.168.1.0/24"}, | ||
{Server: "server2", Name: "name2", IPBlock: "10.0.0.0/8"}, | ||
{Server: "server3", Name: "name3", IPBlock: ""}, | ||
{Server: "server4", Name: "name4", IPBlock: "invalid"}, | ||
{Server: "server5", Name: "name5", IPBlock: "192.168.1.128/25"}, | ||
}, | ||
}, | ||
} | ||
|
||
finder := NewKnownServersFinderImpl(knownServers) | ||
|
||
tests := []struct { | ||
ip string | ||
expected bool | ||
expectedIPBlocks []string | ||
expectedServers []string | ||
}{ | ||
{"192.168.1.1", true, []string{"192.168.1.0/24"}, []string{"server1"}}, | ||
{"10.0.0.1", true, []string{"10.0.0.0/8"}, []string{"server2"}}, | ||
{"172.16.0.1", false, nil, nil}, | ||
{"192.168.1.200", true, []string{"192.168.1.0/24", "192.168.1.128/25"}, []string{"server1", "server5"}}, | ||
} | ||
|
||
for _, test := range tests { | ||
ip := net.ParseIP(test.ip) | ||
entries, contains := finder.Contains(ip) | ||
assert.Equal(t, test.expected, contains) | ||
if contains { | ||
assert.NotEmpty(t, entries) | ||
servers := []string{} | ||
for _, entry := range entries { | ||
servers = append(servers, entry.GetServer()) | ||
} | ||
ipBlocks := []string{} | ||
for _, entry := range entries { | ||
ipBlocks = append(ipBlocks, entry.GetIPBlock()) | ||
} | ||
assert.ElementsMatch(t, test.expectedIPBlocks, ipBlocks) | ||
assert.ElementsMatch(t, test.expectedServers, servers) | ||
} else { | ||
assert.Empty(t, entries) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.