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"
|
||||
|
||||
"github.com/google/uuid"
|
||||
log "github.com/micro/go-micro/v2/logger"
|
||||
"github.com/micro/mdns"
|
||||
)
|
||||
|
||||
@ -148,7 +149,6 @@ func (m *mdnsRegistry) Register(service *Service, opts ...RegisterOption) error
|
||||
continue
|
||||
}
|
||||
|
||||
//
|
||||
host, pt, err := net.SplitHostPort(node.Address)
|
||||
if err != nil {
|
||||
gerr = err
|
||||
@ -270,10 +270,20 @@ func (m *mdnsRegistry) GetService(service string) ([]*Service, error) {
|
||||
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{
|
||||
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,
|
||||
})
|
||||
|
||||
|
@ -79,8 +79,8 @@ func Extract(addr string) (string, error) {
|
||||
}
|
||||
addrs = append(addrs, loAddrs...)
|
||||
|
||||
var ipAddr []byte
|
||||
var publicIP []byte
|
||||
var ipAddr string
|
||||
var publicIP string
|
||||
|
||||
for _, rawAddr := range addrs {
|
||||
var ip net.IP
|
||||
@ -94,22 +94,30 @@ func Extract(addr string) (string, error) {
|
||||
}
|
||||
|
||||
if !isPrivateIP(ip.String()) {
|
||||
publicIP = ip
|
||||
publicIP = ip.String()
|
||||
continue
|
||||
}
|
||||
|
||||
ipAddr = ip
|
||||
ipAddr = ip.String()
|
||||
break
|
||||
}
|
||||
|
||||
// return private ip
|
||||
if ipAddr != nil {
|
||||
return net.IP(ipAddr).String(), nil
|
||||
if len(ipAddr) > 0 {
|
||||
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
|
||||
if publicIP != nil {
|
||||
return net.IP(publicIP).String(), nil
|
||||
if len(publicIP) > 0 {
|
||||
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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user