返回

CloudFlare Tunnel 内网穿透和反向代理

   
摘要GPT
摘要小助理暂时失联跑路啦……😜

将您的本地服务扩展到全球网络:Cloudflare Tunnel的魔力

Cloudflare Tunnel是您连接全球网络的完美伙伴。无论您是个人开发者还是企业用户,通过Cloudflare Tunnel,您可以轻松将您的本地或电脑上的服务提供给全球用户。这款强大的工具可以安全地将您的服务引导到Cloudflare的分布式网络中,确保数据的安全性和隐私。通过加密通信和强大的安全功能,您的Web服务将免受恶意攻击和数据泄露的威胁。

一键脚本

适用于debian/ubuntu

curl -sS -O https://raw.githubusercontent.com/woniu336/open_shell/main/cloudflared.sh && chmod +x cloudflared.sh && ./cloudflared.sh

后面可以不看了

1.下载与安装

安装教程: 点击打开

wget -q https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb && sudo dpkg -i cloudflared-linux-amd64.deb

有两种运行隧道的方式: 控制台命令行

区别: 控制台添加第2个隧道的时候提示你已经安装过cloudflared服务了,你需要卸载才能继续, 命令行则可以运行多个配置文件,也就是多个服务,所以没有限制

本文使用的是命令行方式

打开控制台–Access–Tunnels

Pasted image 20230821060949

2.登录

cloudflared tunnel login

会自动打开浏览器,登录后选择域名,完成验证

此时 Cloudflare 会创建一个 cert.pem 文件放在你的 ~/.cloudflared 目录下。

Pasted image 20230821061103

Pasted image 20230821061328

3.创建隧道

cloudflared tunnel create 隧道名

此时会输出一些隧道 ID 之类的信息

4.配置隧道

给隧道创建一个域名

cloudflared tunnel route dns 隧道名 刚才选择的域名

这里有个坑: 回到域名的DNS解析,把CNAME 记录名称改回来

比如: tt.baidu.com改为mm.baidu.com

内容改为: 隧道UUID.cfargotunnel.com

5. 创建配置文件

比如我打算放在 ~/.cloudflared/config.yml,文件内容如下:

tunnel: UUID
credentials-file: /root/.cloudflared/UUID.json
 
ingress:
  - hostname: xx.baidu.com
    service: http://localhost:80
  - service: http_status:404

启动隧道:

cloudflared tunnel --config ~/.cloudflared/config.yml run

此时会有一些调试信息,比如它告诉你连接到了哪些 Cloudflare 节点之类的:

022-03-26T06:52:31Z INF Starting tunnel tunnelID=xxxxxxx-5b0e-xxxx-8034-xxxxxxx
2022-03-26T06:52:31Z INF Version 2022.3.4
...
2022-03-26T06:52:31Z INF Generated Connector ID: 624aa020-a90a-4bef-91da-330c74edb02f
2022-03-26T06:52:31Z INF Initial protocol http2
2022-03-26T06:52:31Z INF Starting metrics server on 127.0.0.1:44143/metrics
2022-03-26T06:52:33Z INF Connection 34504363-646c-46a2-973d-bd112943c58f registered connIndex=0 location=KIX
2022-03-26T06:52:34Z INF Connection 7a3ec8f7-482c-4fe5-93c4-69d1177ca457 registered connIndex=1 location=NRT
2022-03-26T06:52:35Z INF Connection 7d571bdb-96d2-49d3-b8bf-14754aa6cf8b registered connIndex=2 location=KIX
2022-03-26T06:52:36Z INF Connection 473e30ae-e98b-4da1-8768-12bf5304c7ab registered connIndex=3 location=NRT

6. 其他

体验隧道

由官方提供测试域名 重新启动体验隧道域名会变

cloudflared tunnel --url localhost:你http服务端口号

运行隧道 (有时会失败)

cloudflared tunnel run --url localhost:你http服务端口号 隧道名字

删除隧道

cloudflared tunnel delete 隧道名

列出隧道

cloudflared tunnel list

配置为系统服务

执行

cloudflared service install

cloudflared 会新建 systemd 文件

开启服务

ystemctl start cloudflared.service

7. 卸载隧道

sudo cloudflared service uninstall

记得删除cloudflared.service残留文件

sudo rm /etc/systemd/system/cloudflared.service

后记

理论上不限制流量,比之前的worker法好一点,适合没有独立公网v4的nat,内网机器,省去自己内网穿透,端口转发的麻烦

另外,毕竟是穿透到国外,CF什么速度懂得都懂,跨境速度就挺差,国内穿透还是可以考虑阿里的钉钉免费内网穿透,走的阿里BGP(海外主电信副联通)

参考文档

https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/create-tunnel

https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/trycloudflare

使用 Hugo 构建
主题 hugo-magic小洋葱 魔改 由 Jimmy 设计
Written by Human, Not by AI