Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ZhouJunjun opened this issue Mar 31, 2021 · 1 comment · Fixed by #700
Closed

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

ZhouJunjun opened this issue Mar 31, 2021 · 1 comment · Fixed by #700
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@ZhouJunjun
Copy link

ZhouJunjun commented Mar 31, 2021

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")
}
@ShannonDing
Copy link
Member

a PR is welcomed and it will be review asap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants