利用cloudflare tunnel进行内网穿透

  • 准备一个cloudflare账号
  • 一个域名,并解析再cloudflare上
  1. 安装cloudfalred
brew install cloudflared
  1. 配置cloudflared
# 登录授权
cloudflared tunnel login
# 执行上述命令后,终端会给出一个 URL 链接,在浏览器中打开。
# 登录你的 Cloudflare 账号。
# 选择一个你已经在 Cloudflare 后台创建好的域名。
# 点击 Authorize。
# 授权完成后,cloudflared 会自动在你的本地生成凭证文件。默认路径如下:
~/.cloudflared/[你的隧道ID].json
  
# 创建隧道
cloudflared tunnel create <隧道名称>
  
# 创建dns解析
cloudflared tunnel route dns <隧道名称> agent.example.com

# 新建配置文件
vim ~/.cloudflared/config.yml

tunnel: <你的隧道UUID>
protocol: http2
credentials-file: /Users/你的用户名/.cloudflared/<你的隧道UUID>.json
ingress:
  - hostname: agent.example.com
service: http://localhost:3010
  - service: http_status:404
  
# 运行tunnel
cloudflared tunnel --config ~/.cloudflared/config.yml run <隧道名称或UUID>
  1. 通过服务启动tunnel
mkdir -p $(brew  --prefix)/etc/cloudflared/
cp ~/.cloudflared/config.yml $(brew  --prefix)/etc/cloudflared
cp ~/.cloudflared/<你的UUID>.json $(brew  --prefix)/etc/cloudflared/ 

# 修改config.yml中的credentials-file路径
credentials-file: /opt/homebrew/etc/cloudflared/<你的UUID>.json
# 启动service
brew services start cloudflared
  1. 如果启动了surge的增强模式,需要增加下面的配置
[General]
...
# 排除所有 Cloudflare Tunnel 相关域名
dns-hijack-excluded-domains = *.argotunnel.com, *.cfargotunnel.com
  
[Rule]
# 排除所有 Cloudflare Tunnel 相关域名, 放在rule的第一行,避免被其他规则匹配
DOMAIN-SUFFIX,argotunnel.com,DIRECT,no-resolve
DOMAIN-SUFFIX,cfargotunnel.com,DIRECT,no-resolve
PROCESS-NAME,cloudflared,DIRECT
...