Syncthing是开源的文件同步程序,利用Syncthing项目的服务器,可以在多台计算机之间同步文件。具体分布可以查看Syncthing项目的中继服务器的分布
但为了提高文件同步的效率,我们可以通过自建发现服务器(discovery-server)和中继服务器(relay-server)的方法。
发现服务器:用于发现互联网上的同伴
Syncthing relies on a discovery server to find peers on the internet.
中继服务器:当双方无法直接建立数据连接时,才会利用中继服务器进行数据连接,若用中继服务器进行连接,则流量会流过服务器
Relaying is enabled by default but will only be used if two devices are unable to communicate directly with each other. When connected via a relay, Syncthing will periodically retry a direct connection and, if one is established, stop communicating via the relay.
version: "3"
services:
# 自建syncthing的发现服务器 discovery-servier
syncthing_discovery_server:
image: syncthing/discosrv
container_name: syncthing-discovery-server
command: -debug -listen=":8443"
environment:
- PUID=1000
- PGID=1000
volumes:
- ./syncthing/discosrv:/var/stdiscosrv
ports:
- 8443:8443 # Listen address (default “:8443”)
restart: always
# 自建syncthing的中继服务器 syncthing-relay-server
syncthing_relay_server:
image: syncthing/relaysrv:latest
container_name: syncthing-relay-server
command: -debug -pools="" -listen=":22067"
environment:
- PUID=1000
- PGID=1000
volumes:
- ./syncthing/strelaysrv:/var/strelaysrv
ports:
- 22067:22067 # 中继服务器的数据连接端口(必须开启)
#- 22070:22070 # 用于公用的中继服务器池,显示数据传输、客户端数量等状态,可不开启
restart: always
用 sudo docker-compose up -d 运行该docker-compose文件
在Lighthouse中防火墙放行相对应的端口,此处放行所有的port端口,即8443,22067端口
建好后,发现服务器和中继服务器各自会生成**device ID
**,同时,会生成自签名的证书和数据库。
Every device is identified by a device ID. The device ID is used for address resolution, authentication and authorization. The term “device ID” could interchangeably have been “key ID” since the device ID is a direct property of the public key in use.At first start, stdiscosrv will generate certificate files and database in the current directory
At first start, stdiscosrv will generate certificate files and database in the current directory
-debug:表示启动debug输出(建议开启)
-listen:表示数据监听的端口
-pools:加入的中继服务器池,若为空,则表示该为个人私有,不允许其他人使用该中继服务(建议为空)
- GLOBAL_RATE=100000000 # 全局速率限制 单位为bytes/s
- PER_SESSION_RATE=10000000 # 每个会话速率限制 单位为bytes/s
- MESSAGE_TIMEOUT=1m30s #等待相关消息到达的最长时间
- NATWORK_TIMEOUT=3m0s # 客户端和中继服务器之间的操作超时时间
- PING_INTERVAL=1m30s # ping的发送频率
在每个Syncthing Client上进行设置:
绿框全部开启,红框填入对应自建服务的地址
https://公网IP:8443/?id=自建发现服务器device ID
推荐写成:
default,https://公网IP:8443/?id=自建发现服务器device ID
default等价于
https://discovery-v4.syncthing.net/v2/
,https://discovery-v6.syncthing.net/v2/
和https://discovery.syncthing.net/v2/
,也就是利用Syncthing项目的发现服务
relay://公网IP:22067?id=自建中继服务器device ID
推荐写成:
default,relay://公网IP:22067/?id=自建中继服务器device ID
default等价于
tcp://0.0.0.0:22000
,quic://0.0.0.0:22000
和dynamic+https://relays.syncthing.net/endpoint
.同样也是利用Syncthing项目的发现服务 的中继服务
通过侦听程序和设备发现的数量,可以判断客户端是否成功连接上了自建服务
为了安全起见,发现服务器是利用https来提供服务的。官网提到了三种证书的选择:
第一种就是用CA机构发布的证书
第二种就是自建发现服务器在建好后根据device ID自动生成的证书(推荐)
第三种就是利用反向代理
前两种证书,对应的发现服务器地址,需要传递device ID,而第三种无需传递device ID
Use a CA-signed certificate pair for the domain name you will use for the discovery server. This is like any other HTTPS website; clients will authenticate the server based on its certificate and domain name. Use any certificate pair and let clients authenticate the server based on its “device ID” (similar to Syncthing-to-Syncthing authentication). This option can be used with the certificate automatically generated by the discovery server. Pass the
-http
flag if the discovery server is behind an SSL-secured reverse proxy. See below for configuration.
两个客户端在进行连接前,均已连上自建服务。
同步的基本步骤:互相添加对方为远程设备 ——> 选择共享文件夹
具体步骤(下面假设有Syncthing客户端A和客户端B):
在客户端A中,点击添加远程设备
,输入客户端B的device ID
高级
中设置的地址列表
表示寻找其他设备的发现服务器,用默认的 dynamic
即可, dynamic
表示用局域网发现和全球发现服务器寻找设备
The word
dynamic
(without any prefix) means to use local and global discovery to find the device.
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。