From 4267814e5bc5c66e3d05e0e5c7b79bd89cd4044b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Tisa=CC=88ter?= Date: Wed, 2 Sep 2015 12:24:14 +0200 Subject: [PATCH] Treat 127.0.0.0/8 and 169.254.0.0/16 as private network --- consul/util.go | 24 +++++++++++++++++++----- consul/util_test.go | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/consul/util.go b/consul/util.go index a9544c0f2..3640c7a4e 100644 --- a/consul/util.go +++ b/consul/util.go @@ -20,8 +20,10 @@ import ( * Contains an entry for each private block: * 10.0.0.0/8 * 100.64.0.0/10 + * 127.0.0.0/8 + * 169.254.0.0/16 * 172.16.0.0/12 - * 192.168/16 + * 192.168.0.0/16 */ var privateBlocks []*net.IPNet @@ -42,7 +44,7 @@ func (s *serverParts) String() string { func init() { // Add each private block - privateBlocks = make([]*net.IPNet, 4) + privateBlocks = make([]*net.IPNet, 6) _, block, err := net.ParseCIDR("10.0.0.0/8") if err != nil { @@ -50,23 +52,35 @@ func init() { } privateBlocks[0] = block - _, block, err = net.ParseCIDR("172.16.0.0/12") + _, block, err = net.ParseCIDR("100.64.0.0/10") if err != nil { panic(fmt.Sprintf("Bad cidr. Got %v", err)) } privateBlocks[1] = block - _, block, err = net.ParseCIDR("192.168.0.0/16") + _, block, err = net.ParseCIDR("127.0.0.0/8") if err != nil { panic(fmt.Sprintf("Bad cidr. Got %v", err)) } privateBlocks[2] = block - _, block, err = net.ParseCIDR("100.64.0.0/10") + _, block, err = net.ParseCIDR("169.254.0.0/16") if err != nil { panic(fmt.Sprintf("Bad cidr. Got %v", err)) } privateBlocks[3] = block + + _, block, err = net.ParseCIDR("172.16.0.0/12") + if err != nil { + panic(fmt.Sprintf("Bad cidr. Got %v", err)) + } + privateBlocks[4] = block + + _, block, err = net.ParseCIDR("192.168.0.0/16") + if err != nil { + panic(fmt.Sprintf("Bad cidr. Got %v", err)) + } + privateBlocks[5] = block } // strContains checks if a list contains a string diff --git a/consul/util_test.go b/consul/util_test.go index d2f08c397..64a187ab1 100644 --- a/consul/util_test.go +++ b/consul/util_test.go @@ -113,7 +113,7 @@ func TestIsPrivateIP(t *testing.T) { if isPrivateIP("8.8.8.8") { t.Fatalf("bad") } - if isPrivateIP("127.0.0.1") { + if !isPrivateIP("127.0.0.1") { t.Fatalf("bad") } }