侧边栏壁纸
博主头像
v林羽博主等级

行动起来,活在当下

  • 累计撰写 171 篇文章
  • 累计创建 34 个标签
  • 累计收到 11 条评论

目 录CONTENT

文章目录

【使用手册】之--自建Syncthing发现服务器和中继服务器

v林羽
2023-04-01 / 0 评论 / 1 点赞 / 2164 阅读 / 8075 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-11-01,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

【使用手册】之–自建 Syncthing 发现服务器和中继服务器

#系统 #Windows #教程 #工具 #手册 #Docker #Linux #Docker-compose

Syncthing 实现方法,首先通过发现服务器 (Discosrv)发现和索引用户,当用户设备接入发现服务器后,服务器会进行设备的连接通告,当设备之间同意连接后进行连接;两台设备建立连接后,数据需要通过 NAT 穿透的方式实现交换,如果 NAT 穿透不成功,就需要中继 (Relaysrv)服务器传输数据。Syncthing 官方的发现服务器 (Global Discovery)及社区贡献的中继服务器多集中在欧洲国内较少,使用 Syncthing 时经常会出现连接慢、时常断开、速度不稳定等问题。本篇利用一台国内的闲置服务器提供发现和中继服务加快同步效率,也可以解决隐私问题。

Syncthing 社区 Relay 服务器池位置: https://relays.syncthing.net/

同步程序 syncthing: https://github.com/syncthing/syncthing

发现服务器 stdiscosrv: https://github.com/syncthing/discosrv

中继服务器 strelaysrv: https://github.com/syncthing/relaysrv

文档: https://docs.syncthing.net/index.html

1. Windwos Server 安装发现和中继服务

1.1. 发现服务

https://github.com/syncthing/discosrv/releases 找到 windows 版本,下载解压后打开 cmd 运行:stdiscosrv.exe -debug 启动 stdiscosrv 服务,默认端口 8443 可以通过 -listen ":8443" 指定监听端口,其他参数参看 -help。程序启动后,记下生成的 Server device ID,后面添加发现服务时会用到。同时,在程序文件夹下自动生成同步发现服务器数据库 discovery.db 和服务器证书 cert. pem、key.pem 证书不变,重装后 Server device ID 不变。

C:\syncthing\stdiscosrv-windows-amd64-v1.18.6>stdiscosrv.exe -help
Usage of stdiscosrv.exe:
  -cert string
        Certificate file (default "./cert.pem")
  -db-dir string
        Database directory (default "./discovery.db")
  -debug
        Print debug output
  -http
        Listen on HTTP (behind an HTTPS proxy)
  -key string
        Key file (default "./key.pem")
  -listen string
        Listen address (default ":8443")
  -metrics-listen string
        Metrics listen address
  -replicate string
        Replication peers, id@address, comma separated
  -replication-listen string
        Replication listen address (default ":19200")
  -version
        Show version

C:\syncthing\stdiscosrv-windows-amd64-v1.18.6>

1.2. 中继服务

https://github.com/syncthing/relaysrv 找到 windows 版本,下载解压后打开 cmd 运行:strelaysrv.exe -debug -pools="" -protocol=tcp4 启动 strelaysrv 服务,默认端口 22067(连接端口)、22070(服务器状态端口),可以通过 -listen ":22067" 指定连接端口,-pools="" 不公开此服务器(默认会加入 Syncthing 官方 Relay 服务器池中共享中继服务器,会消耗大量流量,建议有流量上限的服务器不公开),-protocol=tcp4 只启用 TPv4 协议,其他参数参看 -help。程序启动后,记下生成的 URI,后面添加中继服务时会用到,同时,在程序文件夹下自动生成服务器证书。

C:\syncthing\strelaysrv-windows-amd64-v1.22.1>strelaysrv.exe -help
Usage of strelaysrv.exe:
  -debug
        Enable debug output
  -ext-address string
        An optional address to advertise as being available on.
                Allows listening on an unprivileged port with port forwarding from e.g. 443, and be connected to on port 443.
  -global-rate int
        Global rate limit, in bytes/s
  -keys string
        Directory where cert.pem and key.pem is stored (default ".")
  -listen string
        Protocol listen address (default ":22067")
  -message-timeout duration
        Maximum amount of time we wait for relevant messages to arrive (default 1m0s)
  -nat
        Use UPnP/NAT-PMP to acquire external port mapping
  -nat-lease int
        NAT lease length in minutes (default 60)
  -nat-renewal int
        NAT renewal frequency in minutes (default 30)
  -nat-timeout int
        NAT discovery timeout in seconds (default 10)
  -network-buffer int
        Network buffer size (two of these per proxied connection) (default 65536)
  -network-timeout duration
        Timeout for network operations between the client and the relay.
                If no data is received between the client and the relay in this period of time, the connection is terminated.
                Furthermore, if no data is sent between either clients being relayed within this period of time, the session is also terminated. (default 2m0s)
  -per-session-rate int
        Per session rate limit, in bytes/s
  -ping-interval duration
        How often pings are sent (default 1m0s)
  -pools string
        Comma separated list of relay pool addresses to join (default "https://relays.syncthing.net/endpoint")
  -pprof
        Enable the built in profiling on the status server
  -protocol string
        Protocol used for listening. 'tcp' for IPv4 and IPv6, 'tcp4' for IPv4, 'tcp6' for IPv6 (default "tcp")
  -provided-by string
        An optional description about who provides the relay
  -status-srv string
        Listen address for status service (blank to disable) (default ":22070")
  -token string
        Token to restrict access to the relay (optional). Disables joining any pools.
  -version
        Show version

C:\syncthing\strelaysrv-windows-amd64-v1.22.1>

注意:服务器防火墙放行端口8443,22067。

2. Linux 使用 Docker 方式部署发现和中继服务

推荐使用 docker-compose 方式部署,yaml 文件如下:

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

获取发现服务的 Server device ID 和中继服务的 URI

# 查看syncthing-discovery-server日志获取Server device ID
docker logs syncthing-discovery-server

# 查看syncthing-relay-server日志获取URI
docker logs syncthing-relay-server

注意:服务器防火墙放行端口8443,22067。

3. Syncthing 中配置发现和中继服务器

打开 Syncthing 操作 -> 设置 -> 连接

# 协议监听地址,中继服务URI
relay://公网IP:22067?id=中继服务器device ID

# 全局发现服务器
https://公网IP:8443/?id=发现服务器device ID

如果希望保留 Syncthing 官方发现服务器和社区中继服务器,可以在地址前加上 default, 如:

default,relay://公网IP:22067?id=中继服务器device ID
default,https://公网IP:8443/?id=发现服务器device ID
1

评论区