What’s a Syncthing Relay?

A Syncthing Relay server helps to relay data between Syncthing nodes that couldn’t otherwise talk directly. To quote the Syncthing docs:

Syncthing can bounce traffic via a relay when it’s not possible to establish a direct connection between two devices. There are a number of public relays available for this purpose. The advantage is that it makes a connection possible where it would otherwise not be; the downside is that the transfer rate is much lower than a direct connection would allow.

If you have a server on the Internet with spare bandwidth (100GB+/mo) and want to host a public relay, then read on!

If you need a server, then checkout Digital Ocean’s $5/mo server using my magical promo code and you’ll get a $10 credit to start with.

Traditional Methods

The traditional way to hose the Syncthing Relay is to build the binary with a Go compiler and run it locally. If you’re lucky, there is a pre-built binary available or a package for your Linux distribution. This works fine for most people, but I like to keep services contained as they are easier to update and are somewhat isolated from my primary operating system.

Docker helps to do just that. The Syncthing Relay server is very simple and has no persistent state that needs to be maintained across service starts or upgrades.

Testing the Docker Image

Run the Docker container manually to check for firewall or network problems by running the following:

docker run --rm -p 22067:22067 -p 22070:22070 kylemanna/syncthing-relay

You should see something like this in your console:

❯❯❯ docker run --rm -p 22067:22067 -p 22070:22070 kylemanna/syncthing-relay
Started Syncthing Relay Docker Container.
2016/10/14 01:25:24 main.go:123: strelaysrv v0.14.8+13-g05c37e5 (go1.7.1 linux-amd64) [email protected] 2016-10-12 20:55:38 UTC
2016/10/14 01:25:24 main.go:129: Connection limit 52428
2016/10/14 01:25:24 main.go:142: Failed to load keypair. Generating one, this might take a while...
2016/10/14 01:25:29 main.go:218: URI: relay://
2016/10/14 01:25:29 main.go:221: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2016/10/14 01:25:29 main.go:222: !!  Joining default relay pools, this relay will be available for public use. !!
2016/10/14 01:25:29 main.go:223: !!      Use the -pools="" command line option to make the relay private.      !!
2016/10/14 01:25:29 main.go:224: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2016/10/14 01:25:30 pool.go:55: Joined https://relays.syncthing.net/endpoint rejoining in 48m0s

You should see something similar on your machine and verify that the IP address of your server shows up on the Syncthing Relay status page.

Something went wrong if the last line doesn’t say “Joined …” and the Syncthing Relay status page doesn’t list your server as well. The most likely cause is you may have a problem with a firewall. Ports 22067/tcp and 22070/tcp need to be forwarded. Docker should forward these on the local machine, but if there is an upstream firewall or NAT router, you’ll need to pass the traffic through.

I received the following error before I properly configured my firewall:

2016/10/14 01:25:30 pool.go:55: Failed to join https://relays.syncthing.net/endpoint due to an internal server error: test failed

Hit CTRL-C to stop the Docker container.

Automate the Docker Container

The command above ran in the current terminal, but we can do better by handing over the reigns to systemd service manager. Systemd will pull updates for the kylemanna/syncthing-relay image and start the container at boot time. Each time waiting for the network and Docker daemon to start-up. Should the Syncthing Relay service exit, systemd will wait 10 seconds and restart it.

All that’s needed to is to install a systemd service file that instructs systemd how to operate.

First — Install the Systemd Service File

cd /etc/systemd/system
sudo curl -O https://raw.githubusercontent.com/kylemanna/docker-syncthing-relay/master/init/docker-syncthing-relay.service
sudo systemctl daemon-reload

Second — Start the Service and Verify Operation

sudo systemctl start docker-syncthing-relay.service
sudo systemctl status docker-syncthing-relay.service

You should see similar output to the testing section above.

Third — Enable the Service

sudo systemctl enable docker-syncthing-relay.service

Systemd will now automatically start the Syncthing Docker container at boot time.

Learn More

Upstream Links: