Syncthing (Pulse) 是一个使用 Go 语言编写的开源 P2P 文件同步软件。官方的介绍是:

Pulse (previously Syncthing) replaces proprietary sync and cloud services with something open, trustworthy and distributed. Your data is your data alone and you deserve to choose where it is stored, if it is shared with some third party, and how it’s transmitted over the Internet.

当然这个东西跟 OwnCloud 之类的个人云服务软件还是有些区别的。最明显的一点是 Syncthing 没有文件管理界面,你平时怎么管理文件还怎么管理文件,它只是保证你各个设备上的文件一致而已,这可以看作一个缺点。但是它的优点也很明显,没有各种依赖,官方发布的打包只有一个二进制文件,使用起来相当方便。

其实就这一个二进制文件,直接下载回来运行 ./syncthing 就可以使用了,但是在服务器上搭建的话还是有一些要注意的问题。

首先是防火墙。 Syncthing 默认使用端口 22000/TCP 来进行同步,使用端口 21025/UDP 来进行 P2P 的发现广播。因此你需要在 iptables 中放行这两个端口。使用命令 iptables -I INPUT -p TCP --destination-port port_number -j ACCEPT 来打开一个端口。

随后,你可能会想把 80 端口和 8080 端口做一个映射,因为 Syncthing 默认在 8080 端口监听 WebGUI 请求。配置你的 nginx 站点设置,在对应于你 Syncthing 站点或路径的地方加入 proxy_pass http://0.0.0.0:8080; ,然后编辑 ~/.config/syncthing/config.xml ,将监听地址改为 0.0.0.0:8080 就好了。

现在运行 ./syncthing 启动 Syncthing ,应该可以在浏览器中输入你的 Syncthing 站点地址来访问你搭建的 Syncthing WebGUI 了。点击页面右上角的齿轮图标,打开设置界面,可以设置一个用户名和密码。设置完成之后,按照提示重启 Syncthing ,就会提示你输入用户名和密码了。 Syncthing 目前使用的是 HTTP Auth 。

最后,作为一个 Web 服务,你可能想要 Syncthing 开机运行。参考这个帖子,在 /etc/init/syncthing.conf 中写入如下内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
description "Syncthing P2P sync service"

start on (local-filesystems and net-device-up IFACE!=lo)
stop on runlevel [!2345]

env STNORESTART=yes
env HOME=你的主目录
setuid "你的用户名"
setgid "你的用户名"

exec syncthing二进制文件的路径

respawn

然后运行 initctl start syncthing 就可以马上启动 Syncthing 。之后每次机器启动, Syncthing 会自动开始运行。