From a9889730d6620366dbfa6476b2642873625331ff Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Thu, 12 Jan 2017 13:20:34 +0000 Subject: [PATCH] move addr extractor to misc --- broker/extractor.go | 70 ---------------------------------------- broker/extractor_test.go | 38 ---------------------- broker/http_broker.go | 3 +- server/extractor.go | 65 ------------------------------------- server/extractor_test.go | 19 ----------- server/rpc_server.go | 6 ++-- 6 files changed, 6 insertions(+), 195 deletions(-) delete mode 100644 broker/extractor.go delete mode 100644 broker/extractor_test.go diff --git a/broker/extractor.go b/broker/extractor.go deleted file mode 100644 index 4ba9fdc3..00000000 --- a/broker/extractor.go +++ /dev/null @@ -1,70 +0,0 @@ -package broker - -import ( - "fmt" - "net" -) - -var ( - privateBlocks []*net.IPNet -) - -func init() { - for _, b := range []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"} { - if _, block, err := net.ParseCIDR(b); err == nil { - privateBlocks = append(privateBlocks, block) - } - } -} - -func extractAddress(addr string) (string, error) { - if len(addr) > 0 && (addr != "0.0.0.0" && addr != "[::]") { - return addr, nil - } - - addrs, err := net.InterfaceAddrs() - if err != nil { - return "", fmt.Errorf("Failed to get interface addresses! Err: %v", err) - } - - var ipAddr []byte - - for _, rawAddr := range addrs { - var ip net.IP - switch addr := rawAddr.(type) { - case *net.IPAddr: - ip = addr.IP - case *net.IPNet: - ip = addr.IP - default: - continue - } - - if ip.To4() == nil { - continue - } - - if !isPrivateIP(ip.String()) { - continue - } - - ipAddr = ip - break - } - - if ipAddr == nil { - return "", fmt.Errorf("No private IP address found, and explicit IP not provided") - } - - return net.IP(ipAddr).String(), nil -} - -func isPrivateIP(ipAddr string) bool { - ip := net.ParseIP(ipAddr) - for _, priv := range privateBlocks { - if priv.Contains(ip) { - return true - } - } - return false -} diff --git a/broker/extractor_test.go b/broker/extractor_test.go deleted file mode 100644 index 382b9a7a..00000000 --- a/broker/extractor_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package broker - -import ( - "net" - "testing" -) - -func TestExtractor(t *testing.T) { - testData := []struct { - addr string - expect string - parse bool - }{ - {"127.0.0.1", "127.0.0.1", false}, - {"10.0.0.1", "10.0.0.1", false}, - {"", "", true}, - {"0.0.0.0", "", true}, - {"[::]", "", true}, - } - - for _, d := range testData { - addr, err := extractAddress(d.addr) - if err != nil { - t.Errorf("Unexpected error %v", err) - } - - if d.parse { - ip := net.ParseIP(addr) - if ip == nil { - t.Error("Unexpected nil IP") - } - - } else if addr != d.expect { - t.Errorf("Expected %s got %s", d.expect, addr) - } - } - -} diff --git a/broker/http_broker.go b/broker/http_broker.go index 782aff9e..bfbc4496 100644 --- a/broker/http_broker.go +++ b/broker/http_broker.go @@ -20,6 +20,7 @@ import ( "github.com/micro/go-micro/broker/codec/json" "github.com/micro/go-micro/errors" "github.com/micro/go-micro/registry" + "github.com/micro/misc/lib/addr" mls "github.com/micro/misc/lib/tls" "github.com/pborman/uuid" @@ -411,7 +412,7 @@ func (h *httpBroker) Subscribe(topic string, handler Handler, opts ...SubscribeO host := strings.Join(parts[:len(parts)-1], ":") port, _ := strconv.Atoi(parts[len(parts)-1]) - addr, err := extractAddress(host) + addr, err := addr.Extract(host) if err != nil { return nil, err } diff --git a/server/extractor.go b/server/extractor.go index db932d2a..f33348ed 100644 --- a/server/extractor.go +++ b/server/extractor.go @@ -2,25 +2,12 @@ package server import ( "fmt" - "net" "reflect" "strings" "github.com/micro/go-micro/registry" ) -var ( - privateBlocks []*net.IPNet -) - -func init() { - for _, b := range []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"} { - if _, block, err := net.ParseCIDR(b); err == nil { - privateBlocks = append(privateBlocks, block) - } - } -} - func extractValue(v reflect.Type, d int) *registry.Value { if d == 3 { return nil @@ -128,55 +115,3 @@ func extractSubValue(typ reflect.Type) *registry.Value { } return extractValue(reqType, 0) } - -func extractAddress(addr string) (string, error) { - if len(addr) > 0 && (addr != "0.0.0.0" && addr != "[::]") { - return addr, nil - } - - addrs, err := net.InterfaceAddrs() - if err != nil { - return "", fmt.Errorf("Failed to get interface addresses! Err: %v", err) - } - - var ipAddr []byte - - for _, rawAddr := range addrs { - var ip net.IP - switch addr := rawAddr.(type) { - case *net.IPAddr: - ip = addr.IP - case *net.IPNet: - ip = addr.IP - default: - continue - } - - if ip.To4() == nil { - continue - } - - if !isPrivateIP(ip.String()) { - continue - } - - ipAddr = ip - break - } - - if ipAddr == nil { - return "", fmt.Errorf("No private IP address found, and explicit IP not provided") - } - - return net.IP(ipAddr).String(), nil -} - -func isPrivateIP(ipAddr string) bool { - ip := net.ParseIP(ipAddr) - for _, priv := range privateBlocks { - if priv.Contains(ip) { - return true - } - } - return false -} diff --git a/server/extractor_test.go b/server/extractor_test.go index b20fb3d5..ee7d32b2 100644 --- a/server/extractor_test.go +++ b/server/extractor_test.go @@ -18,25 +18,6 @@ func (t *testHandler) Test(ctx context.Context, req *testRequest, rsp *testRespo return nil } -func TestExtractAddress(t *testing.T) { - data := []struct { - Input string - Output string - }{ - {"10.0.0.1", "10.0.0.1"}, - } - - for _, d := range data { - addr, err := extractAddress(d.Input) - if err != nil { - t.Errorf("Expected %s: %v", d.Output, err) - } - if addr != d.Output { - t.Errorf("Expected %s, got %s", d.Output, addr) - } - } -} - func TestExtractEndpoint(t *testing.T) { handler := &testHandler{} typ := reflect.TypeOf(handler) diff --git a/server/rpc_server.go b/server/rpc_server.go index d6ca68aa..ad77fc4f 100644 --- a/server/rpc_server.go +++ b/server/rpc_server.go @@ -15,6 +15,8 @@ import ( "github.com/micro/go-micro/registry" "github.com/micro/go-micro/transport" + "github.com/micro/misc/lib/addr" + "golang.org/x/net/context" ) @@ -204,7 +206,7 @@ func (s *rpcServer) Register() error { host = parts[0] } - addr, err := extractAddress(host) + addr, err := addr.Extract(host) if err != nil { return err } @@ -308,7 +310,7 @@ func (s *rpcServer) Deregister() error { host = parts[0] } - addr, err := extractAddress(host) + addr, err := addr.Extract(host) if err != nil { return err }