Merge pull request #1313 from micro/upstream
fix ipv6 address usage in mdns registry and util/addr
This commit is contained in:
commit
f01664a551
@ -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,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -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")
|
||||||
|
Loading…
Reference in New Issue
Block a user