1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-08 05:47:07 +09:00

localdiscovery fixes:

replace zeroconf with fork; sort interfaces; disable ipv6, tollerate long zeroconf shutodown
This commit is contained in:
Roman Khafizianov 2023-04-25 18:17:59 +02:00 committed by Mikhail Iudin
parent 9a0c0361a0
commit 54af31a2af
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
5 changed files with 186 additions and 98 deletions

View file

@ -1,8 +1,13 @@
package addrs
import (
"golang.org/x/exp/slices"
"net"
"regexp"
"strconv"
"golang.org/x/exp/slices"
"github.com/anytypeio/go-anytype-middleware/util/slice"
)
type Interface struct {
@ -32,6 +37,49 @@ func (i InterfacesAddrs) Equal(other InterfacesAddrs) bool {
return slices.Equal(myStr, otherStr)
}
var re = regexp.MustCompile(`^(.*?)([0-9]*)$`)
func (i InterfacesAddrs) SortWithPriority(priority []string) {
f := func(ifName string) (prefix string, num int) {
res := re.FindStringSubmatch(ifName)
if len(res) > 1 {
prefix = res[1]
}
if len(res) > 2 {
num, _ = strconv.Atoi(res[2])
}
return
}
slices.SortFunc(i.Interfaces, func(a, b net.Interface) bool {
aPrefix, aNum := f(a.Name)
bPrefix, bNum := f(b.Name)
aPrioirity := slice.FindPos(priority, aPrefix)
bPrioirity := slice.FindPos(priority, bPrefix)
if aPrefix == bPrefix {
return aNum < bNum
} else if aPrioirity == -1 && bPrioirity == -1 {
// sort alphabetically
return aPrefix < bPrefix
} else if aPrioirity != -1 && bPrioirity != -1 {
// in case we have [eth, wlan]
if aPrioirity == bPrioirity {
// prioritize eth0 over wlan0
return aPrioirity < bPrioirity
}
// prioritise wlan1 over eth8
return aNum < bNum
} else if aPrioirity != -1 {
return true
} else {
return false
}
})
}
func getStrings(i InterfacesAddrs) (allStrings []string) {
for _, i := range i.Interfaces {
allStrings = append(allStrings, i.Name)

View file

@ -3,7 +3,11 @@
package addrs
import "net"
import (
"net"
"github.com/anytypeio/go-anytype-middleware/util/slice"
)
func SetInterfaceAddrsGetter(getter InterfaceAddrsGetter) {}
@ -28,5 +32,9 @@ func GetInterfacesAddrs() (iAddrs InterfacesAddrs, err error) {
return
}
iAddrs.Interfaces = ifaces
iAddrs.Interfaces = slice.Filter(iAddrs.Interfaces, func(iface net.Interface) bool {
return iface.Flags&net.FlagUp != 0 && iface.Flags&net.FlagMulticast != 0
})
return
}