Skip to content

Commit

Permalink
fix: fix ip is IsUnspecified
Browse files Browse the repository at this point in the history
  • Loading branch information
day253 authored Jun 29, 2024
1 parent 11c3cac commit 9267754
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion consul/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func parseAddr(addr net.Addr) (host string, port int, err error) {
return "", 0, fmt.Errorf("calling net.SplitHostPort failed, addr: %s, err: %w", addr.String(), err)
}

if host == "" || host == "::" {
ip := net.ParseIP(host)
if ip == nil || ip.IsUnspecified() {
detectHost := utils.LocalIP()
if detectHost == utils.UNKNOWN_IP_ADDR {
return "", 0, fmt.Errorf("get local ip error")
Expand Down
3 changes: 2 additions & 1 deletion etcd/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ func (e *etcdRegistry) getAddressOfRegistration(info *registry.Info) (string, er
}

// if host is empty or "::", use local ipv4 address as host
if host == "" || host == "::" {
ip := net.ParseIP(host)
if ip == nil || ip.IsUnspecified() {
host, err = getLocalIPv4Host()
if err != nil {
return "", fmt.Errorf("parse registry info addr error: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion eureka/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ func (e *eurekaRegistry) eurekaInstance(info *registry.Info) (*fargo.Instance, e
if portStr == "" {
return nil, fmt.Errorf("registry info addr missing port")
}
if host == "" || host == "::" {
ip := net.ParseIP(host)
if ip == nil || ip.IsUnspecified() {
host = utils.LocalIP()
if host == utils.UNKNOWN_IP_ADDR {
return nil, fmt.Errorf("get local ip error")
Expand Down
6 changes: 4 additions & 2 deletions nacos/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func (n *nacosRegistry) Register(info *registry.Info) error {
if err != nil {
return fmt.Errorf("parse registry info port error: %w", err)
}
if host == "" || host == "::" {
ip := net.ParseIP(host)
if ip == nil || ip.IsUnspecified() {
host = utils.LocalIP()
}
success, err := n.client.RegisterInstance(vo.RegisterInstanceParam{
Expand Down Expand Up @@ -121,7 +122,8 @@ func (n *nacosRegistry) Deregister(info *registry.Info) error {
if err != nil {
return fmt.Errorf("parse registry info port error: %w", err)
}
if host == "" || host == "::" {
ip := net.ParseIP(host)
if ip == nil || ip.IsUnspecified() {
host = utils.LocalIP()
}
success, err := n.client.DeregisterInstance(vo.DeregisterInstanceParam{
Expand Down
6 changes: 4 additions & 2 deletions nacos/v2/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func (n *nacosRegistry) Register(info *registry.Info) error {
if err != nil {
return fmt.Errorf("parse registry info port error: %w", err)
}
if host == "" || host == "::" {
ip := net.ParseIP(host)
if ip == nil || ip.IsUnspecified() {
host = utils.LocalIP()
}
success, err := n.client.RegisterInstance(vo.RegisterInstanceParam{
Expand Down Expand Up @@ -106,7 +107,8 @@ func (n *nacosRegistry) Deregister(info *registry.Info) error {
if err != nil {
return fmt.Errorf("parse registry info port error: %w", err)
}
if host == "" || host == "::" {
ip := net.ParseIP(host)
if ip == nil || ip.IsUnspecified() {
host = utils.LocalIP()
}
success, err := n.client.DeregisterInstance(vo.DeregisterInstanceParam{
Expand Down
3 changes: 2 additions & 1 deletion polaris/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func GetInfoHostAndPort(Addr string) (string, int, error) {
if port == "" {
return infoHost, 0, fmt.Errorf("registry info addr missing port")
}
if infoHost == "" || infoHost == "::" {
ip := net.ParseIP(infoHost)
if ip == nil || ip.IsUnspecified() {
infoHost = utils.LocalIP()
if infoHost == utils.UNKNOWN_IP_ADDR {
return "", 0, fmt.Errorf("get local ip error")
Expand Down
3 changes: 2 additions & 1 deletion servicecomb/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ func (scr *serviceCombRegistry) parseAddr(s string) (string, error) {
if err != nil {
return "", fmt.Errorf("parse addr error: %w", err)
}
if host == "" || host == "::" {
ip := net.ParseIP(host)
if ip == nil || ip.IsUnspecified() {
host = utils.LocalIP()
if host == utils.UNKNOWN_IP_ADDR {
return "", errors.New("get local ip error")
Expand Down
3 changes: 2 additions & 1 deletion zookeeper/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ func buildPath(info *registry.Info) (string, error) {
if port == "" {
return "", fmt.Errorf("registry info addr missing port")
}
if host == "" || host == "::" {
ip := net.ParseIP(host)
if ip == nil || ip.IsUnspecified() {
host = utils.LocalIP()
}
path = path + Separator + net.JoinHostPort(host, port)
Expand Down

0 comments on commit 9267754

Please sign in to comment.