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:
parent
9a0c0361a0
commit
54af31a2af
5 changed files with 186 additions and 98 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue