旧笔记本就闲置下来, 想把它改造成一个家用小服务器. 比起目前云厂商买的小服务器, 笔记本的配置还算可以. 云厂商买的低配轻量服务器, 除了部署博客以外别的什么也干不了, 加之前两天自己根据 huggingface 上机器学习的模型写了一个英汉互译的 RESTful 服务, 本来打算部署在公网上直接通过 alfred workflow 来使用结果发现连安装 pytorch 都会 OOM. 索性尝试把闲置的笔记本利用起来.

我的需求就是在笔记本部署一些简单的 TCP 服务可以直接暴露在公网使用, 或者能 SSH 上去编译 kernel 模块, 找了一圈内网穿透的工具, 配置使用都略带繁琐, 所以自己写了一个轻量级的内网穿透工具: spleen.

通过在一台具有公网 IP 的小型服务器(阿里云轻量)部署 spleen 进行转发, 可以随时随地访问(闲置笔记本)的 TCP 服务, 例如 SSH, HTTP/S 等.

spleen

轻量级内网穿透工具, 使用 spleen 可以支持通过外网访问不具备公网 IP 的家庭服务器/内网主机.

介绍

通过在一台具有公网 IP 的小型服务器(阿里云轻量)部署 spleen, 可以随时随地访问你的家庭服务器/内网主机(闲置笔记本)的 TCP 服务, 例如 SSHHTTP/S 等.

例如 SSH 服务, 在顺利部署 spleen 的客户端和服务端后, 通过公网服务器(假定 IP 为1.1.1.1), 可以直接通过端口映射来连接你的家庭服务器/内网主机:

执行 ssh -p 5000 leviathan@1.1.1.1 # 即可直接连接到家庭服务器/内网主机

如何使用

公网服务器部署 spleen-server

  • 通过 release 下载对应架构的 spleen 包:
# wget 下载 (请自行替换最新版本)
> wget https://github.com/Leviathan1995/spleen/releases/download/v0.0.2/spleen_0.0.2_Linux_64-bit.tar.gz

# 解压
> tar -zxvf spleen_0.0.2_Linux_64-bit.tar.gz
> cd spleen_0.0.2_Linux_64-bit

# 配置端口转发规则
> vim .server.json

{
  "ServerIP"   : "0.0.0.0",  # 公网服务器监听地址
  "ServerPort"   : 1234,  # 公网服务器监听端口, 该端口用来与家庭服务器/内网主机建立通信隧道
  "MappingPort" : [ # 端口映射规则
	"5000:22",  # 即访问公网服务器的 5000 端口就等于访问家庭服务器/内网主机的 22 端口
	"5001:3306"
	...
  ]
}

# 启动
> ./spleen-server -c .server.json
2022/01/12 19:39:39 The server listening for the intranet server at 0.0.0.0:1234 successful.
2022/01/12 19:39:39 The server listening at 0.0.0.0:5001 successful.
2022/01/12 19:39:39 The server listening at 0.0.0.0:5000 successful.

家庭服务器/内网主机部署 spleen-client

  • 通过 release 下载对应架构的 spleen 包:
# wget 下载 (请自行替换最新版本)
> wget https://github.com/Leviathan1995/spleen/releases/download/v0.0.2/spleen_0.0.2_Linux_64-bit.tar.gz

# 解压
> tar -zxvf spleen_0.0.2_Linux_64-bit.tar.gz
> cd spleen_0.0.2_Linux_64-bit

# 配置公网服务器地址
> vim .client.json

{
  "server_ip"  : "127.0.0.1", # 公网服务器 IP
  "server_port": 1234 # 公网服务器监听端口
}

# 启动
> ./spleen-client -c .client.json # 默认预留 10 个活跃连接
2022/01/12 18:55:19 Connect to the server 127.0.0.1:1234 successful.
2022/01/12 18:55:19 Connect to the server 127.0.0.1:1234 successful.
2022/01/12 18:55:19 Connect to the server 127.0.0.1:1234 successful.
2022/01/12 18:55:19 Connect to the server 127.0.0.1:1234 successful.
2022/01/12 18:55:19 Connect to the server 127.0.0.1:1234 successful.
2022/01/12 18:55:19 Connect to the server 127.0.0.1:1234 successful.
2022/01/12 18:55:19 Connect to the server 127.0.0.1:1234 successful.
2022/01/12 18:55:19 Connect to the server 127.0.0.1:1234 successful.
2022/01/12 18:55:19 Connect to the server 127.0.0.1:1234 successful.
2022/01/12 18:55:19 Connect to the server 127.0.0.1:1234 successful.

SSH 服务样例

当我们分别按照上述步骤在公网服务器部署了 spleen-server、家庭服务器/内网主机部署了 spleen-client后, 通过设定的转发规则 5000:22即访问公网服务器的 5000 端口就等于访问家庭服务器/内网主机的 22 端口. 我们可以直接使用 SSH 连接家庭服务器/内网主机, 假如公网 IP 为 1.1.1.1:

ssh -p 5000 leviathan@1.1.1.1 # 即可直接连接到家庭服务器/内网主机

TODO

  • 增加安全性配置, 鉴权
  • 支持 UDP 转发
  • 支持 QUIC 传输协议提升访问速度
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。