Cloudflare Tunnel内网穿透
要让本地知识库被外部访问,通常需要将局域网中的服务暴露到公网上。我们可以使用内网穿透工具创建一个安全的隧道,将本地服务映射到一个公网可访问的地址上。这里主要介绍Cloudflare Tunnel。
ngrok是我接触到的第一个内网穿透工具,但是不开代理的情况下无法工作,不得不找另外一个比较靠谱的工具。偶然了解到Cloudflare也提供这项服务,自然就想了解这一个工具。
Cloudflared establishes outbound connections (tunnels) between your resources and Cloudflare's global network. Tunnels are persistent objects that route traffic to DNS records. Within the same tunnel, you can run as many 'cloudflared' processes (connectors) as needed. These processes will establish connections to Cloudflare and send traffic to the nearest Cloudflare data center.
临时测试
如果你只是想临时测试下穿透的效果,只需要简单执行两个命令(Macbook为例):
# Download and install cloudflared via Homebrew
brew install cloudflared
# Run the following terminal command to start a free tunnel
cloudflared tunnel --url http://localhost:8080
第一个是Cloudflare的安装命令;第二个是将本地运行的服务穿透到随机生成的外网。第二个命令后会生成一个外部可以访问的临时域名,临时测试的域名不是固定的,而且连接的稳定性会差一些,但使用非常方便。
固定域名
注册Cloudflare的账号并登陆账号,主界面可进入Zero Trust界面,在Networks > Tunnels下增加一个Tunnel。增加Tunnel之后会提示如何运行服务,即Install and run a connector,也就是复制运行两行代码。
运行服务之后就可以绑定域名并访问了。固定域名需要在Cloudflare里有一个域名。如果觉得网速不够快,也可以付费升级方案。实测发现用docker安装的稳定性会高一些,前提是先安装docker。以下是具体的命令:
docker run --name cloudflared --network host --detach cloudflare/cloudflared:latest tunnel --no-autoupdate run --token YOUR_TUNNEL_TOKEN