github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/p2p/net_android.go (about) 1 //go:build android 2 3 /* 4 * Copyright (C) 2021 The "MysteriumNetwork/node" Authors. 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package p2p 21 22 import ( 23 "net" 24 "strings" 25 26 "github.com/rs/zerolog/log" 27 ) 28 29 func defaultInterfaceAddress() string { 30 ifaces, err := net.Interfaces() 31 if err != nil { 32 log.Warn().Err(err).Msgf("Failed to get list of interfaces") 33 return "" 34 } 35 36 for _, i := range ifaces { 37 if ip := upAndRunning(i, "wlan"); ip != "" { 38 return ip 39 } 40 } 41 42 for _, i := range ifaces { 43 if ip := upAndRunning(i, "rmnet"); ip != "" { 44 return ip 45 } 46 } 47 48 return "" 49 } 50 51 func upAndRunning(iface net.Interface, prefix string) string { 52 if !strings.HasPrefix(iface.Name, prefix) { 53 return "" 54 } 55 56 if !strings.Contains(iface.Flags.String(), "up") { 57 return "" 58 } 59 60 i, err := net.InterfaceByName(iface.Name) 61 if err != nil { 62 log.Warn().Err(err).Msgf("Failed to get interface by name: %s", iface.Name) 63 return "" 64 } 65 66 addrs, err := i.Addrs() 67 if err != nil { 68 log.Warn().Err(err).Msgf("Failed to get interface addresses: %s", iface.Name) 69 return "" 70 } 71 72 for _, addr := range addrs { 73 ip, _, err := net.ParseCIDR(addr.String()) 74 if err != nil { 75 log.Warn().Err(err).Msgf("Failed to get interface addresses: %s", iface.Name) 76 return "" 77 } 78 79 if ip.To4() == nil { 80 return "" 81 } 82 83 return ip.String() 84 } 85 return "" 86 }