1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00

Custom private blocks (#1705)

Co-authored-by: Asim Aslam <asim@aslam.me>
This commit is contained in:
Di Wu 2020-06-17 00:05:42 +08:00 committed by Dominic Wong
parent 5b161b88f7
commit c28f625cd4
2 changed files with 30 additions and 0 deletions

View File

@ -17,6 +17,15 @@ func init() {
}
}
// AppendPrivateBlocks append private network blocks
func AppendPrivateBlocks(bs ...string) {
for _, b := range bs {
if _, block, err := net.ParseCIDR(b); err == nil {
privateBlocks = append(privateBlocks, block)
}
}
}
func isPrivateIP(ipAddr string) bool {
ip := net.ParseIP(ipAddr)
for _, priv := range privateBlocks {

View File

@ -56,3 +56,24 @@ func TestExtractor(t *testing.T) {
}
}
func TestAppendPrivateBlocks(t *testing.T) {
tests := []struct {
addr string
expect bool
}{
{addr: "9.134.71.34", expect: true},
{addr: "8.10.110.34", expect: false}, // not in private blocks
}
AppendPrivateBlocks("9.134.0.0/16")
for _, test := range tests {
t.Run(test.addr, func(t *testing.T) {
res := isPrivateIP(test.addr)
if res != test.expect {
t.Fatalf("expected %t got %t", test.expect, res)
}
})
}
}