高性能内网穿透 Rathole

2,685次阅读
没有评论

共计 6573 个字符,预计需要花费 17 分钟才能阅读完成。

更新记录

2024-01-22

前段时间在使用 Rathole 中,遇到了带宽能够打满,但是 http 响应极慢的情况。

当时急于解决故障,重启了 server 和 client 后,故障恢复。没有定位到根因,暂时也不想定位😅。

改为部署 Frp 的最新版,发现 Frp 0.53.2 也可以打满带宽。于是穿透方式切回 Frp。

‍

Frp 的部署,仍可参考站内文章: 家宽建站的几种方式。

但是在 0.52.3 版本后,配置文件由 ini​ 改为了 toml​。下面给到 toml​ 示例:

# frps 示例
# cat /usr/local/frp_0.53.2_linux_amd64/frps.toml 
bindAddr = "0.0.0.0"
bindPort = 17000
auth.method = "token"
auth.token = "178a634a52f3a394fd2b53"
tcpMux = true

# frpc 示例
# cat /usr/local/frp_0.53.2_linux_amd64/frpc_nginx.toml 
serverAddr = "*.*.*.*"
serverPort = 17100
auth.method = "token"
auth.token = "178a634a52f3a394fd2b53"
tcpMux = true

[[proxies]]
name = "nginx"
type = "tcp"
localIP = "127.0.0.1"
localPort = 443
remotePort = 17000

​tcpMux​ 按需开启,作用是多路复用TCP连接。

‍

背景

前文 家宽建站的几种方式 介绍过,目前内网穿透方式是 Frp。在我的家宽升级到 80Mbps 上行速率后,实测跑不满带宽。

因为测试时链路比较长,流量路径是 客户端 –> Frp server –> Frp client –> nginx –> 资源文件。难以排查问题点。

选择缩短了链路,在 Frp server 端 scp 从 client 暴露的 ssh 端口拉取文件,发现 Frp 仍跑不满吞吐。

之前有看文章提到过新的穿透方案 Rathole,于是着手开始测试。

‍

部署

操作系统:server Ubuntu20.04、client CentOS 7

CPU架构:X86_64

rathole 版本:v.0.4.8

‍

Default

参考链接:github.com/rapiz1/rathol…

下文的步骤,在 server 和 client 端都是相同的。server 和 client 端仅有配置文件的差别,都是使用相同的二进制包。

需要说明,从v.0.5.0版本开始,需要源码编译二进制才会包含 libssl 库相关的依赖。而 docker 方式镜像更新缓慢,所以我使用了稍旧的版本 v.0.4.8​。

# 创建工作目录
mkdir /usr/local/rathole && cd mkdir /usr/local/rathole

# 下载程序
wget https://github.com/rapiz1/rathole/releases/download/v0.4.8/rathole-x86_64-unknown-linux-musl.zip && unzip rathole-x86_64-unknown-linux-musl.zip

‍

Server

# 编写配置文件
# vim /usr/local/rathole/server.toml
# server.toml
[server]
bind_addr = "0.0.0.0:23312"

[server.services.centos-ops-ssh]
token = "thisispasswd"
bind_addr = "0.0.0.0:61212"

# 配置systemd
# vim /etc/systemd/system/rathole_server.service
[Unit]
Description=Rathole Server Service
After=network.target

[Service]
Type=simple
Restart=on-failure
RestartSec=5s
LimitNOFILE=1048576

# with root
ExecStart=/usr/local/rathole/rathole -s /usr/local/rathole/server.toml
# without root
# ExecStart=%h/.local/bin/rathole -s %h/.local/etc/rathole/%i.toml

[Install]
WantedBy=multi-user.target

# 启动
# systemctl enable rathole_server.service && systemctl start rathole_server.service && systemctl status rathole_server.service
Created symlink /etc/systemd/system/multi-user.target.wants/rathole_server.service → /etc/systemd/system/rathole_server.service.
● rathole_server.service - Rathole Server Service
     Loaded: loaded (/etc/systemd/system/rathole_server.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2023-12-21 16:50:01 CST; 1min 39s ago
   Main PID: 1212171 (rathole)
      Tasks: 4 (limit: 2124)
     Memory: 6.5M
     CGroup: /system.slice/rathole_server.service
             └─1212171 /usr/local/rathole/rathole -s /usr/local/rathole/server.toml

Dec 21 16:50:01 wuyuidc-nb-1 systemd[1]: Started Rathole Server Service.
Dec 21 16:50:01 wuyuidc-nb-1 rathole[1212171]: 2023-12-21T08:50:01.994249Z  INFO rathole::server: Listening at 0.0.0.0:23312
Dec 21 16:50:01 wuyuidc-nb-1 rathole[1212171]: 2023-12-21T08:50:01.994782Z  INFO config_watcher{path="/usr/local/rathole/server.toml"}: rathole::config_watcher: Start watching the config

‍

Client

# 编写配置文件
# vim /usr/local/rathole/client.toml
# client.toml
[client]
remote_addr = "$serverip:23312"

[client.services.centos-ops-ssh]
token = "thisispasswd"
local_addr = "127.0.0.1:22"


# 配置systemd
# vim /etc/systemd/system/rathole_client.service
[Unit]
Description=Rathole Client Service
After=network.target

[Service]
Type=simple
Restart=on-failure
RestartSec=5s
LimitNOFILE=1048576

# with root
ExecStart=/usr/local/rathole/rathole -c /usr/local/rathole/client.toml
# without root
# ExecStart=%h/.local/bin/rathole -c %h/.local/etc/rathole/%i.toml

[Install]
WantedBy=multi-user.target

# 启动
# systemctl status rathole_client.service 
● rathole_client.service - Rathole Client Service
   Loaded: loaded (/etc/systemd/system/rathole_client.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2023-12-21 18:03:22 CST; 31min ago
 Main PID: 5004 (rathole)
    Tasks: 4
   Memory: 6.7M
   CGroup: /system.slice/rathole_client.service
           └─5004 /usr/local/rathole/rathole -c /usr/local/rathole/client.toml

Dec 21 18:03:22 centos-ops systemd[1]: Started Rathole Client Service.
Dec 21 18:03:22 centos-ops rathole[5004]: 2023-12-21T10:03:22.582223Z  INFO handle{service=centos-ops-ssh}: rathole::client: Starting 393b7178952d2fb6bfc39377d5ded35263efa60df3ed1578e75214ec96f6f767
Dec 21 18:03:22 centos-ops rathole[5004]: 2023-12-21T10:03:22.582774Z  INFO config_watcher{path="/usr/local/rathole/client.toml"}: rathole::config_watcher: Start watching the config
Dec 21 18:03:22 centos-ops rathole[5004]: 2023-12-21T10:03:22.650610Z  INFO handle{service=centos-ops-ssh}:run: rathole::client: Control channel established

请注意,让你的 client 能够访问到 server 的端口。

‍

Tips

配置文件其实很简洁,和 frp 类似,都是通过块来进行控制的。

区别在于穿透映射在 server 端的端口,rathole 是在 server 端的配置文件进行控制,而 frp 是在 client 端的配置文件。

‍

吞吐验证

‍

短链路

先用最短路径测试,此方式是 server 直连内网 VM ,使用 scp 拉取文件

Rathole:

# netstat -anltup|grep 61212
tcp        0      0 0.0.0.0:61212           0.0.0.0:*               LISTEN      1216813/rathole

# scp -P 61212 root@127.0.0.1:/imagehost/test-1G /tmp
root@127.0.0.1's password: 
test-1G                     100% 1000MB  10.8MB/s   01:32

‍

Frp:

# netstat -anltup|grep 21222
tcp6       0      0 :::21222                :::*                    LISTEN      190495/frps

# scp -P 21222 root@127.0.0.1:/imagehost/test-1G /tmp
root@127.0.0.1's password: 
test-1G                     100% 1000MB   5.9MB/s   02:50

‍

长链路

再换一种长链路测试,即客户端 –> 公网nginx –> Frp server –> Frp client –> 内网nginx –> ** –> 资源文件。

通过切换公网 nginx upstream,分别指向 rathole 和 frp 的端口进行验证。

‍

Rathole:

# wget https://<span style="font-weight: bold;" data-type="strong">.opshub.cn/s/PK5xGiBaDMmmsDy/download/</span>*.7z
--2023-12-21 18:07:10--  https://<span style="font-weight: bold;" data-type="strong">.opshub.cn/s/PK5xGiBaDMmmsDy/download/</span>.7z
Resolving <span style="font-weight: bold;" data-type="strong">.opshub.cn (</span>.opshub.cn)... *.*.*.49
Connecting to <span style="font-weight: bold;" data-type="strong">.opshub.cn (</span>.opshub.cn)|*.*.*.49|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 598558489 (571M) [application/x-7z-compressed]
Saving to: ‘***.7z.1’

***.7z.1                                             100%[======================<span style="font-weight: bold;" class="mark">>] 570.83M  10.3MB/s    in 52s   

2023-12-21 18:08:02 (11.0 MB/s) - ‘***.7z.1’ saved [598558489/598558489]

‍

Frp:

# wget https://<span style="font-weight: bold;" data-type="strong">.opshub.cn/s/PK5xGiBaDMmmsDy/download/</span>*.7z
--2023-12-21 18:11:57--  https://<span style="font-weight: bold;" data-type="strong">.opshub.cn/s/PK5xGiBaDMmmsDy/download/</span>*.7z
Resolving <span style="font-weight: bold;" data-type="strong">.opshub.cn (</span>.opshub.cn)... *.*.*.49
Connecting to <span style="font-weight: bold;" data-type="strong">.opshub.cn (</span>.opshub.cn)|*.*.*.49|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 598558489 (571M) [application/x-7z-compressed]
Saving to: ‘***.7z.2’

***.7z.2                                             100%[</span>=====================>] 570.83M  6.02MB/s    in 99s

‍

结论

Frp 的配置和 Rathole 几乎没有差异,没有进行过多的参数调整。只是简单指定了传输类型为 TCP,token,以及配置内外IP端口。

观察到 Frp 有一个参数用于配置 tls:tls_enable = true​。但是无论开启还是注释掉,测试时对速率几乎无影响。

‍

最终实测,在家宽 80Mbps 上行速率下,Rathole 能够跑满,而 Frp 只能达到约 50Mbps。

不排除 Frp 有其他参数可以优化传输速率,等日后有时间再进一步跟踪。

‍

Rathole 还有一个优势:使用人数没有 Frp 多。特殊场景下,它的数据包特征审计规则可能还不能被识别。

例如大型企业内网一般都会主动发现和拦截 Frp。😄

引用链接

正文完
 
pengyinwei
版权声明:本站原创文章,由 pengyinwei 2023-12-21发表,共计6573字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处:https://www.opshub.cn
评论(没有评论)