move addr extractor to misc
This commit is contained in:
@@ -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
|
||||
}
|
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user