之前我在【https://www.nodeloc.com/d/17328】中遇到了多级域名转发 ssh 无法连接的问题,而最近我在《https://www.nodeloc.com/d/19445》一文中给出了白嫖多级域名 SSL 证书的方法。因此,在这个帖子中,我将尝试解决之前的问题。
获取免费 SSL 证书
参见《https://www.nodeloc.com/d/19445》。
安装和配置 cloudflared
首先在目标机器(以我的 56idc NAT Debian 11 小鸡为例)上,安装 cloudflared:
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 -o /usr/bin/cloudflared
chmod +x /usr/bin/cloudflared
然后授权登录 cloudflared 并添加一个名为 56idc-ssh 的隧道:
cloudflared tunnel login
cloudflared tunnel create 56idc-ssh
输出:
Tunnel credentials written to /root/.cloudflared/04377af6-xxxx-xxxx-xxxx-49a332fa4f2c.json. cloudflared chose this file based on where your origin certificate was found. Keep this file secret. To revoke these credentials, delete the tunnel.
Created tunnel 56idc-ssh with id 04377af6-xxxx-xxxx-xxxx-49a332fa4f2c
其中 04377af6-xxxx-xxxx-xxxx-49a332fa4f2c
为隧道 ID ,后面要用。
接着将隧道绑定到 56idc.ssh.yucho.top
这个域名上:
cloudflared tunnel route dns 56idc-ssh 56idc.ssh.yucho.top
然后用任意文本编辑器(例如我用的是 vim)在主目录下的 .cloudflared
文件夹中创建 config.yml
,输入:
tunnel: 04377af6-xxxx-xxxx-xxxx-49a332fa4f2c
credentials-file: /root/.cloudflared/04377af6-xxxx-xxxx-xxxx-49a332fa4f2c.json
ingress:
- hostname: 56idc.ssh.yucho.top
service: ssh://localhost:22
- service: http_status:404
其中 04377af6-xxxx-xxxx-xxxx-49a332fa4f2c
就是隧道 ID 。
保存文件,运行:
screen cloudflared tunnel run
然后客户端(这里以 Termux 为例)要通过 ssh 命令连接的话需要安装 cloudflared 并修改 ssh 命令配置文件,在后面加上:
Host 56idc.ssh.yucho.top
Hostname 56idc.ssh.yucho.top
ProxyCommand /data/data/com.termux/files/usr/bin/cloudflared access tcp --hostname %h
保存后,就可以使用 ssh root@56idc.ssh.yucho.top
连接小鸡了。