1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-09 01:10:43 +00:00

Getting your local IP should not depend on an Internet connection (#937)

* Fixes #917 - Getting your local IP should not depend on an Internet connection
This commit is contained in:
Thomas Christlieb 2020-11-21 11:34:46 +01:00 committed by GitHub
parent 9ffbf1320c
commit e89450daf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -633,20 +633,17 @@ func (v *MainMenu) onBtnTCPIPOkClicked() {
// getLocalIP returns local machine IP address
func (v *MainMenu) getLocalIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
// https://stackoverflow.com/a/28862477
host, _ := os.Hostname()
addrs, _ := net.LookupIP(host)
if err != nil {
v.logger.Error(err.Error())
return "cannot reach network"
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
return ipv4.String()
}
}
err = conn.Close()
if err != nil {
v.logger.Error(err.Error())
return "unexpected error occurred while closing test connection"
}
v.logger.Warning("no IPv4 Address could be found")
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
return "no IPv4 Address could be found"
}