Skip to content

[Master][bug]utils.LocalIp sometimes is a looback ip #637

@ZhouJunjun

Description

@ZhouJunjun

Sometimes value of utils.LocalIp is a loopback ip, cause in line 48:!ipnet.IP.IsLoopback() is only hit ip like 127.* or ::1
If some of server's net interface is a loopback interface, value of utils.LocalIp might be a loopback ip(which is not like 127.*)

https://github.com/apache/rocketmq-client-go/blob/master/internal/utils/net.go#L29-L54

so i think ClientIP4() should filter lookback interface first like this:

func GetLocalServerIp() (string, error) {
    if ifaceSlice, err := net.Interfaces(); err == nil && ifaceSlice != nil {
        for _, iface := range ifaceSlice {
            if iface.Flags&net.FlagLoopback != 0 {
                continue // loopback interface
            }
            if iface.Flags&net.FlagUp == 0 {
                continue // interface down
            }
            
            if tmpAddrSlice, err := iface.Addrs(); err == nil && tmpAddrSlice != nil {
                for _, address := range tmpAddrSlice {
                    if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
                        if ipNet.IP.To4() != nil {
                            return ipNet.IP.String(), nil
                        }
                    }
                }
            }
        }
    }
    return "", fmt.Errorf("ip not found")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions