Prioritising addresses

So I have this bunch of laptops that all connect to a FreeNAS server running Syncthing. They’re set up to connect either through the server’s LAN address or to a forwarded port on my router (i.e. the server’s WAN address.)

Both types of connections work, but the latter is somewhat slower as the router has limited NAT throughput. So I much prefer LAN connections when the laptops are actually on the LAN, with WAN connections as a fallback when necessary. But it doesn’t seem like Syncthing has a preference. It seems to connect to whichever IP address it wants, or maybe it prioritises the last address it successfully connected to, or something like that (?). If there’s a pattern to it I can’t see it.

Whatever the reason, once a WAN connection is made Syncthing stays connected indefinitely and never seems to check if the preferred connection becomes available instead. Is this expected behaviour? Is there a way to change it?

I solved this by either enabling Local Discovery on all devices (the obvious option) or, if I don’t want to use the discovery functionality, by using a DNS domain name that points to the external IP address, but is resolved to the servers internal IP address in the LAN.

You can set device address to “[fixed ip], dynamic”, which will try the fixed ip before performing discovery.

From what I understood, he configured the device address as tcp://<interal ip address>:<port>,tcp://<external ip address>:<port> and expects that syncthing uses the first address and only tries the second if no connection is possible using the first. But what he observes is that syncthing is randomly choosing one of these addresses.

Yep, I specify two addresses hoping that the first one will be preferred whenever it’s available, ideally even if a connection has already been established to a lower-priority address. Maybe that’s hoping too much. :slight_smile:

I figure what might be happening is something like this:

  • guy turns on his laptop before he leaves for work
  • laptop connects to guy’s home wifi
  • Syncthing tries the server’s LAN IP because it’s preferred, but gets no response as the laptop is not on the server’s LAN
  • Syncthing tries the server’s WAN IP, is able to connect
  • guy puts his laptop in standby mode
  • guy arrives at work and turns laptop on again
  • Syncthing is now on the server’s LAN, but it only sees the transition as a dropped connection, so it immediately tries to reestablish the connection to the last address that worked, which is the WAN IP
  • connection is reestablished and Syncthing never thinks to try the preferred address again

Would that be about right? And if so, what would be a good way to prevent it from happening? I guess setting up a domain name that resolves differently on the server’s LAN might work, but I’d expect Syncthing to cache the IP address, so it might require a restart anyway (?).

Maybe the server could be set up to block incoming connections from its own WAN address? I’m not sure to do that without potentially breaking other stuff. Hm.

Syncthing tries to connect to the addresses given, in order, once a minute. The first one to succeed stays connected, forever or until it breaks and the process restarts.

There is an “upgrade” path from relayed to direct connections - that is, a direct connection is continuously attempted in the background even when connected via relay.

When using discovery, local addresses are tried before global ones.

But all statically configured addressed are created equal, Syncthing doesn’t distinguish between them.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.