使用 wstunnel 在 macOS 上安全连接 Linux 服务器

背景

wstunnel 是一个基于 WebSocket 的隧道工具,主要优势包括:

  • 将任意 TCP/UDP 流量封装在 WebSocket 中
  • 支持 TLS 加密(wss://)
  • 轻量级且跨平台(Rust 编写)
  • 能有效绕过深度包检测(DPI)、

服务端配置

相关资料参考官方文档:https://github.com/erebe/wstunnel

1. 下载 wstunnel

image-20250626午後43714110

# 创建专用目录
mkdir ~/wstunnel && cd ~/wstunnel

# 下载最新版
chmod +x wstunnel

2. 简单运行测试

/wstunnel server wss://[::]:1022 --restrict-to 127.0.0.1:22

监听 1022 端口,转发到22端口。

3. 创建 systemd 服务

vi /etc/systemd/system/wstunnel.service

创建一个简单的服务:

# /etc/systemd/system/wstunnel.service
[Unit]
Description=wstunnel
After=network.target

[Service]
User=root
ExecStart=/root/wstunnel/wstunnel server wss://[::]:1022 --restrict-to 127.0.0.1:22
ExecReload=/bin/kill -SIGUSR1 $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target

3. 启动并启用服务

# 重载 systemd
sudo systemctl daemon-reload

# 启动服务
sudo systemctl start wstunnel

# 设置开机自启
sudo systemctl enable wstunnel

# 检查状态
sudo systemctl status wstunnel

客户端配置(macOS)

1. 安装 wstunnel

image-20250626午後43714110

我是 M2 芯片,下载 wstunnel_10.4.3_darwin_arm64.tar.gz 解压到合适的位置,赋权:

chmod +x wstunnel

2. 本地客户端

监听本地2222端口,ssh的目的IP和端口是127.0.0.1:22,后面是转发的服务器IP和1022端口

./wstunnel client -L tcp://2222:127.0.0.1:22 wss://<服务器IP>:1022

3. 连接

ssh -p 2222 root@localhost

使用 Stunnel 安全代理 SSH 连接(macOS客户端 + Debian服务器) 自建 DNS over HTTPS(DoH)并在 macOS 配置使用