准备事项

  • Debian 11
  • Root 权限

安装运行

  1. 安装指令, 安装完成后使用cloudflared命令来调用. (官方文档)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 添加 Cloudflare GPG 密钥
    sudo mkdir -p --mode=0755 /usr/share/keyrings
    curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null

    # 添加存储库到列表中
    echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared bullseye main' | tee /etc/apt/sources.list.d/cloudflared.list

    # 安装
    apt-get update && apt-get install cloudflared
  2. 使用以下命令可调起浏览器或给出登录地址, 完成账户登录.

    1
    cloudflared tunnel login
  3. 创建隧道,名称随意。创建完成后会给出一个<TUNNEL_ID>,写好后面要用。

    1
    2
    3
    4
    cloudflared tunnel create <NAME>

    #例子
    cloudflared tunnel create Example
  4. 绑定<NAME>隧道到<DOMAIN>域名,如果想要绑定到子域名那么<DOMAIN>请填写完整的子域名。

    1
    2
    3
    4
    cloudflared tunnel route dns <NAME> <DOMAIN>

    #例子
    cloudflared tunnel route dns Example a.example.com
  5. 此时已经可以正常使用,不过我们使用配置文件启动,所以还需要一些步骤。

  6. 打开/root/.cloudflared/(可能有所不同,请自行查找.cloudflared文件夹),并新建一个config.yml填入以下内容。

    其中<TUNNEL_ID>为你的隧道ID,在创建时会给出。

    credentials-file部分请根据你的实际路径填写。

    ingress这里,每一条-就是一个规则,其中hostname请填写刚刚创建时填写的<DOMAIN>service就是填写你想要绑定的东西,http则代表http协议,文章末尾的表格给出了所有支持的服务。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    tunnel: <TUNNEL_ID>
    credentials-file: /主目录/.cloudflared/<TUNNEL_ID>.json

    ingress:
    - hostname: <DOMAIN>
    service: <SERVICE>://localhost:80
    - service: http_status:404

    #例子
    tunnel: 81f41a92-7556-41dg-831h-21x5575fuh6f
    credentials-file: /root/.cloudflared/81f41a92-7556-41dg-831h-21x5575fuh6f.json

    ingress:
    - hostname: a.example.com
    service: http://localhost:80
    - service: http_status:404
  7. 在完成编辑后,直接运行即可,此时的服务还是运行在前台的,可以使用nohup或screen来后台运行,但不推荐。

    1
    cloudflared tunnel run

将Cloudflared添加到service

  1. 这是我最推荐的用法,也是本教程的最后一步,完成以上步骤后才能添加到service。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #运行以下命令创建服务
    cloudflared service install
    #运行以下命令启动服务
    systemctl start cloudflared
    #运行以下命令设置开机自启
    systemctl enable cloudflared
    #运行以下命令查看服务状态
    systemctl status cloudflared
    #运行以下命令关闭服务
    systenctl stop cloudflared

删除隧道

  1. 运行以下指令删除隧道(执行后会立即将本地隧道文件删除,DNS记录将于24-48小时后删除,域名绑定请手动删除)

    1
    2
    3
    4
    5
    cloudflared tunnel delete <NAME>
    #添加 -f 强制删除运行中的tunnel
    cloudflared tunnel delete -f <NAME>
    #例子 删除我们刚刚创建的隧道
    cloudflared tunnel delete Example

注意事项

  1. 因为地区不同,部分tunnel存在ERR Connection terminated的问题,使用service会造成大量日志文件导致硬盘爆炸,如果发现有类似的问题,使用以下代码运行。(错误的原因大致为tunnel为了容错默认会连接到3个服务器,如果当前地区数量不够,那么tunnel就会重复的连接到同一台上而造成错误)

    1
    nohup cloudflared tunnel --ha-connections 1 run & #ha-connections的作用就是限制备份服务器的连接数量
  2. 在使用一段时间后可能在这里会发现大量的API KEY,具体原因未知,可进行删除,暂未发现影响。

所有支持的协议

Service Description Example service value
HTTP/S Incoming HTTP requests are proxied directly to your local service. https://localhost:8000
HTTP/S over Unix socket Just like HTTP/S, but using a Unix socket instead. unix:/home/production/echo.sock
TCP TCP connections are proxied to your local service. tcp://localhost:2222
SSH SSH connections are proxied to your local service. ssh://localhost:22
RDP RDP connections are proxied to your local service. rdp://localhost:3389
kubectl bastion mode cloudflared will act like a jumphost, allowing access to any local address. bastion
Hello World Test server for validating your Cloudflare Tunnel setup. hello_world
HTTP status Responds to all requests with the given HTTP status. http_status:404

参考

https://developers.cloudflare.com/cloudflare-one/connections

https://pkg.cloudflare.com/

https://github.com/cloudflare/cloudflared