mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-07 21:37:04 +09:00
GO-4807 p2p: iface ip conversion fix
This commit is contained in:
parent
520223346d
commit
e2c1bf41c7
2 changed files with 22 additions and 8 deletions
|
@ -53,6 +53,17 @@ func WrapInterfaces(ifaces []net.Interface) []NetInterfaceWithAddrCache {
|
|||
return m
|
||||
}
|
||||
|
||||
func AddrToIP(addr net.Addr) (net.IP, bool) {
|
||||
switch ip := addr.(type) {
|
||||
case *net.IPNet:
|
||||
return ip.IP, true
|
||||
case *net.IPAddr:
|
||||
return ip.IP, true
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
// GetAddr returns ipv4 only addresses for interface or cached one if set
|
||||
func (i NetInterfaceWithAddrCache) GetAddr() []net.Addr {
|
||||
if i.cachedAddrs != nil {
|
||||
|
@ -67,8 +78,8 @@ func (i NetInterfaceWithAddrCache) GetAddr() []net.Addr {
|
|||
}
|
||||
// filter-out ipv6
|
||||
i.cachedAddrs = slice.Filter(i.cachedAddrs, func(addr net.Addr) bool {
|
||||
if ip, ok := addr.(*net.IPNet); ok {
|
||||
if ip.IP.To4() == nil {
|
||||
if ip, ok := AddrToIP(addr); ok {
|
||||
if ip.To4() == nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
@ -88,13 +88,13 @@ func (l *localDiscovery) getDiscoveryPossibility(newAddrs addrs.InterfacesAddrs)
|
|||
continue
|
||||
}
|
||||
}
|
||||
addrs := iface.GetAddr()
|
||||
if len(addrs) == 0 {
|
||||
ifaceAddrs := iface.GetAddr()
|
||||
if len(ifaceAddrs) == 0 {
|
||||
continue
|
||||
}
|
||||
for _, addr := range addrs {
|
||||
if ip, ok := addr.(*gonet.IPNet); ok {
|
||||
ipv4 := ip.IP.To4()
|
||||
for _, addr := range ifaceAddrs {
|
||||
if ip, ok := addrs.AddrToIP(addr); ok {
|
||||
ipv4 := ip.To4()
|
||||
if ipv4 == nil {
|
||||
continue
|
||||
}
|
||||
|
@ -147,7 +147,10 @@ func (l *localDiscovery) RegisterDiscoveryPossibilityHook(hook func(state Discov
|
|||
func (l *localDiscovery) getAddresses() (ipv4, ipv6 []gonet.IP) {
|
||||
for _, iface := range l.interfacesAddrs.Interfaces {
|
||||
for _, addr := range iface.GetAddr() {
|
||||
ip := addr.(*gonet.IPNet).IP
|
||||
ip, ok := addrs.AddrToIP(addr)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if ip.To4() != nil {
|
||||
ipv4 = append(ipv4, ip)
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue