From 9d425b05880236928c561da0b7152e41d795a5ac Mon Sep 17 00:00:00 2001 From: Catfriend1 <16361913+Catfriend1@users.noreply.github.com> Date: Wed, 25 Jun 2025 20:18:12 +0200 Subject: [PATCH] fix(beacon, osutil, upnp): fix local discovery send and intf detection on Android (#10196) Before: - Local discovery on Android 10+ is broken. The phone receives local discovery packets from other devices running Syncthing on the same network, e.g. a computer. But it doesn't send its own local discovery packets. - Startup of the beacon/broadcast.go and beacon/multicast.go "services" subsequently fail, see the log entries of "service.go" with "2 of 2 failures, backing off". Root cause: - Android 10+ restricts determining the network interfaces for privacy reasons. The interfaces and IP addresses cannot be determined. - There's a bug in the go "net" library. I can actually get the interfaces, but the fix was not implemented by the go team. Workaround: - The "community" found a workaround by creating a light wrapper around "net" called "anet" library. - "anet" adjusts the behaviour on Android 10+ and gets the interfaces plus their IP addresses, as required by Syncthing. After: - By using the "anet" lib, Syncthing is able to get the interface ip addresses and put them into the "AllAddresses" string array. - The "AllAddresses" string array is then announced on the local discovery multicast and broadcast packets, if enabled in Syncthing's config. - By correctly getting the interfaces and IP addresses using "anet" in "beacon/broadcast.go" and "beacon/multicast.go", the services start up fine again. Verification: - I've built "libSyncthingNative.so" with this PR applied for Android and put it into Syncthing-Fork v1.29.7.5 for testing. My two phones, Android 10 and Android 15 (arm64-v8a) immediately discovered each other using local discovery. - I can see the "sent XX bytes" and "recv XX bytes" on both phones in the log filtering for "SyncthingNativeCode" :-). Personal note: - Please go light on me, and, if it's not demanded too much of your time, please help me on this. I am no go programmer. Most things you think are easy or common sense aren't part of my knowledge set. I'd just like to help and hope we somehow can drive this home together to fix the problem. ---- ref: https://github.com/Catfriend1/syncthing-android/pull/1501 ref: https://github.com/Catfriend1/syncthing-android/issues/1500 ref: https://github.com/wlynxg/anet/blob/main/interface.go & https://github.com/wlynxg/anet/blob/main/interface_android.go With that fix, I can see the broadcast/multicast lines again and my phone can be discovered by other phones running the Syncthing app which wasn't possible before on Android 10+. ``` [ET76H] .346892 broadcast.go:107: DEBUG: sent 185 bytes to 192.168.x.255:21027 [ET76H] .347114 multicast.go:86: DEBUG: sent 185 bytes to [ff12::8384]:21027 on wlan0 ``` --------- Co-authored-by: Marcus B Spencer --- go.mod | 1 + go.sum | 2 ++ lib/beacon/broadcast.go | 10 +++++++--- lib/beacon/multicast.go | 6 ++++-- lib/osutil/net.go | 10 +++++++--- lib/upnp/igd_service.go | 4 +++- lib/upnp/upnp.go | 8 +++++--- 7 files changed, 29 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 8b483301c..11539fb3a 100644 --- a/go.mod +++ b/go.mod @@ -97,6 +97,7 @@ require ( github.com/stretchr/testify v1.10.0 // indirect github.com/tklauser/go-sysconf v0.3.14 // indirect github.com/tklauser/numcpus v0.9.0 // indirect + github.com/wlynxg/anet v0.0.5 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect go.uber.org/mock v0.5.2 // indirect golang.org/x/mod v0.24.0 // indirect diff --git a/go.sum b/go.sum index 5f505877f..751279963 100644 --- a/go.sum +++ b/go.sum @@ -274,6 +274,8 @@ github.com/vitrun/qart v0.0.0-20160531060029-bf64b92db6b0 h1:okhMind4q9H1OxF44gN github.com/vitrun/qart v0.0.0-20160531060029-bf64b92db6b0/go.mod h1:TTbGUfE+cXXceWtbTHq6lqcTvYPBKLNejBEbnUsQJtU= github.com/willabides/kongplete v0.4.0 h1:eivXxkp5ud5+4+NVN9e4goxC5mSh3n1RHov+gsblM2g= github.com/willabides/kongplete v0.4.0/go.mod h1:0P0jtWD9aTsqPSUAl4de35DLghrr57XcayPyvqSi2X8= +github.com/wlynxg/anet v0.0.5 h1:J3VJGi1gvo0JwZ/P1/Yc/8p63SoW98B5dHkYDmpgvvU= +github.com/wlynxg/anet v0.0.5/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= diff --git a/lib/beacon/broadcast.go b/lib/beacon/broadcast.go index d5e8eb6b1..0c06b38fa 100644 --- a/lib/beacon/broadcast.go +++ b/lib/beacon/broadcast.go @@ -10,6 +10,8 @@ import ( "context" "net" "time" + + "github.com/wlynxg/anet" ) func NewBroadcast(port int) Interface { @@ -44,7 +46,7 @@ func writeBroadcasts(ctx context.Context, inbox <-chan []byte, port int) error { return doneCtx.Err() } - intfs, err := net.Interfaces() + intfs, err := anet.Interfaces() if err != nil { l.Debugln("Failed to list interfaces:", err) // net.Interfaces() is broken on Android. see https://github.com/golang/go/issues/40569 @@ -52,12 +54,14 @@ func writeBroadcasts(ctx context.Context, inbox <-chan []byte, port int) error { } var dsts []net.IP - for _, intf := range intfs { + for i := range intfs { + intf := intfs[i] + if intf.Flags&net.FlagRunning == 0 || intf.Flags&net.FlagBroadcast == 0 { continue } - addrs, err := intf.Addrs() + addrs, err := anet.InterfaceAddrsByInterface(&intf) if err != nil { l.Debugln("Failed to list interface addresses:", err) // Interface discovery might work while retrieving the addresses doesn't. So log the error and carry on. diff --git a/lib/beacon/multicast.go b/lib/beacon/multicast.go index 4a2804c0d..28367ab6c 100644 --- a/lib/beacon/multicast.go +++ b/lib/beacon/multicast.go @@ -12,6 +12,8 @@ import ( "net" "time" + "github.com/wlynxg/anet" + "golang.org/x/net/ipv6" ) @@ -59,7 +61,7 @@ func writeMulticasts(ctx context.Context, inbox <-chan []byte, addr string) erro return doneCtx.Err() } - intfs, err := net.Interfaces() + intfs, err := anet.Interfaces() if err != nil { l.Debugln(err) return err @@ -117,7 +119,7 @@ func readMulticasts(ctx context.Context, outbox chan<- recv, addr string) error conn.Close() }() - intfs, err := net.Interfaces() + intfs, err := anet.Interfaces() if err != nil { l.Debugln(err) return err diff --git a/lib/osutil/net.go b/lib/osutil/net.go index fd7e6b1e2..42336599b 100644 --- a/lib/osutil/net.go +++ b/lib/osutil/net.go @@ -9,18 +9,22 @@ package osutil import ( "net" "strings" + + "github.com/wlynxg/anet" ) // GetInterfaceAddrs returns the IP networks of all interfaces that are up. // Point-to-point interfaces are exluded unless includePtP is true. func GetInterfaceAddrs(includePtP bool) ([]*net.IPNet, error) { - intfs, err := net.Interfaces() + intfs, err := anet.Interfaces() if err != nil { return nil, err } var addrs []net.Addr - for _, intf := range intfs { + for i := range intfs { + intf := intfs[i] + if intf.Flags&net.FlagRunning == 0 { continue } @@ -29,7 +33,7 @@ func GetInterfaceAddrs(includePtP bool) ([]*net.IPNet, error) { // which, for our purposes, do not qualify as LANs. continue } - intfAddrs, err := intf.Addrs() + intfAddrs, err := anet.InterfaceAddrsByInterface(&intf) if err != nil { return nil, err } diff --git a/lib/upnp/igd_service.go b/lib/upnp/igd_service.go index 36578bc58..accefe174 100644 --- a/lib/upnp/igd_service.go +++ b/lib/upnp/igd_service.go @@ -40,6 +40,8 @@ import ( "net" "time" + "github.com/wlynxg/anet" + "github.com/syncthing/syncthing/lib/nat" ) @@ -65,7 +67,7 @@ func (s *IGDService) AddPinhole(ctx context.Context, protocol nat.Protocol, intA return nil, errors.New("no interface") } - addrs, err := s.Interface.Addrs() + addrs, err := anet.InterfaceAddrsByInterface(s.Interface) if err != nil { return nil, err } diff --git a/lib/upnp/upnp.go b/lib/upnp/upnp.go index 7eacff12f..d42555f11 100644 --- a/lib/upnp/upnp.go +++ b/lib/upnp/upnp.go @@ -48,6 +48,8 @@ import ( "sync" "time" + "github.com/wlynxg/anet" + "github.com/syncthing/syncthing/lib/build" "github.com/syncthing/syncthing/lib/dialer" "github.com/syncthing/syncthing/lib/nat" @@ -104,7 +106,7 @@ const ( func Discover(ctx context.Context, _, timeout time.Duration) []nat.Device { var results []nat.Device - interfaces, err := net.Interfaces() + interfaces, err := anet.Interfaces() if err != nil { l.Infoln("Listing network interfaces:", err) return results @@ -386,7 +388,7 @@ func parseResponse(ctx context.Context, deviceType string, addr *net.UDPAddr, re } func localIPv4(netInterface *net.Interface) (net.IP, error) { - addrs, err := netInterface.Addrs() + addrs, err := anet.InterfaceAddrsByInterface(netInterface) if err != nil { return nil, err } @@ -629,7 +631,7 @@ func soapRequestWithIP(ctx context.Context, url, service, function, message stri } func interfaceHasGUAIPv6(intf net.Interface) (bool, error) { - addrs, err := intf.Addrs() + addrs, err := anet.InterfaceAddrsByInterface(&intf) if err != nil { return false, err }