Merge pull request #1313 from micro/upstream

fix ipv6 address usage in mdns registry and util/addr
This commit is contained in:
Василий Толстов 2020-03-07 23:50:03 +03:00 committed by GitHub
commit f01664a551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 11 deletions

View File

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
log "github.com/micro/go-micro/v2/logger"
"github.com/micro/mdns" "github.com/micro/mdns"
) )
@ -148,7 +149,6 @@ func (m *mdnsRegistry) Register(service *Service, opts ...RegisterOption) error
continue continue
} }
//
host, pt, err := net.SplitHostPort(node.Address) host, pt, err := net.SplitHostPort(node.Address)
if err != nil { if err != nil {
gerr = err gerr = err
@ -270,10 +270,20 @@ func (m *mdnsRegistry) GetService(service string) ([]*Service, error) {
Endpoints: txt.Endpoints, Endpoints: txt.Endpoints,
} }
} }
addr := ""
// prefer ipv4 addrs
if e.AddrV4 != nil {
addr = e.AddrV4.String()
// else use ipv6
} else if e.AddrV6 != nil {
addr = "[" + e.AddrV6.String() + "]"
} else {
log.Infof("[mdns]: invalid endpoint received: %v", e)
continue
}
s.Nodes = append(s.Nodes, &Node{ s.Nodes = append(s.Nodes, &Node{
Id: strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+"."), Id: strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+"."),
Address: fmt.Sprintf("%s:%d", e.AddrV4.String(), e.Port), Address: fmt.Sprintf("%s:%d", addr, e.Port),
Metadata: txt.Metadata, Metadata: txt.Metadata,
}) })

View File

@ -79,8 +79,8 @@ func Extract(addr string) (string, error) {
} }
addrs = append(addrs, loAddrs...) addrs = append(addrs, loAddrs...)
var ipAddr []byte var ipAddr string
var publicIP []byte var publicIP string
for _, rawAddr := range addrs { for _, rawAddr := range addrs {
var ip net.IP var ip net.IP
@ -94,22 +94,30 @@ func Extract(addr string) (string, error) {
} }
if !isPrivateIP(ip.String()) { if !isPrivateIP(ip.String()) {
publicIP = ip publicIP = ip.String()
continue continue
} }
ipAddr = ip ipAddr = ip.String()
break break
} }
// return private ip // return private ip
if ipAddr != nil { if len(ipAddr) > 0 {
return net.IP(ipAddr).String(), nil a := net.ParseIP(ipAddr)
if a == nil {
return "", fmt.Errorf("ip addr %s is invalid", ipAddr)
}
return a.String(), nil
} }
// return public or virtual ip // return public or virtual ip
if publicIP != nil { if len(publicIP) > 0 {
return net.IP(publicIP).String(), nil a := net.ParseIP(publicIP)
if a == nil {
return "", fmt.Errorf("ip addr %s is invalid", publicIP)
}
return a.String(), nil
} }
return "", fmt.Errorf("No IP address found, and explicit IP not provided") return "", fmt.Errorf("No IP address found, and explicit IP not provided")