laratusk/cloudflare-tunnel 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laratusk/cloudflare-tunnel

Composer 安装命令:

composer require laratusk/cloudflare-tunnel

包简介

Start and manage Cloudflare Tunnels directly from Artisan. Supports both quick (random URL) and named (static hostname) tunnels with callback hooks.

README 文档

README

CI Latest Version on Packagist License

Start and manage Cloudflare Tunnels directly from Artisan. Expose your local Laravel application to the internet with a single command — using either a quick tunnel (random URL, zero config) or a named tunnel (static hostname, permanent URL).

php artisan cloudflare:tunnel

Features

  • Quick tunnels — Random *.trycloudflare.com URL, no account required
  • Named tunnels — Static hostname under your own domain (e.g. tunnel.example.com)
  • EventsTunnelConnected and TunnelDisconnected events for webhook registration, notifications, etc.
  • Graceful shutdown — Clean process termination on Ctrl+C

Requirements

  • PHP 8.2+
  • Laravel 10, 11, 12, or 13
  • The pcntl PHP extension
  • The cloudflared CLI binary

Installation

1. Install cloudflared

macOS (Homebrew):

brew install cloudflared

Linux (Debian/Ubuntu):

curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/cloudflared.list
sudo apt update && sudo apt install cloudflared

Other platforms: See the official download page.

Verify the installation:

cloudflared --version

2. Install the package

composer require laratusk/cloudflare-tunnel

3. Publish the config (optional)

php artisan vendor:publish --tag=cloudflare-tunnel-config

Quick Start

Quick Tunnel (zero config)

Run the command with no additional setup — you get a random public URL instantly:

php artisan cloudflare:tunnel
 Tunnel URL: https://random-words.trycloudflare.com
 Tunnel is running. Press Ctrl+C to stop.

Named Tunnel (static hostname)

A named tunnel gives you a permanent, predictable URL under your own domain. Follow these steps once to set it up:

Step 1: Authenticate with Cloudflare

cloudflared tunnel login

This opens your browser. Select the domain you want to use and authorize cloudflared.

Step 2: Create a tunnel

cloudflared tunnel create my-app

Note the tunnel ID in the output — you'll see it in cloudflared tunnel list too.

Step 3: Route DNS to the tunnel

cloudflared tunnel route dns my-app tunnel.example.com

This creates a CNAME record pointing tunnel.example.com to your tunnel.

Step 4: Create the cloudflared config file

Create ~/.cloudflared/config.yml:

tunnel: <TUNNEL-ID>
credentials-file: /path/to/.cloudflared/<TUNNEL-ID>.json

ingress:
  - hostname: tunnel.example.com
    service: http://127.0.0.1
    originRequest:
      httpHostHeader: my-app.test
  - service: http_status:404

Replace <TUNNEL-ID> with your actual tunnel ID. The httpHostHeader should match your local development domain. The credentials file path is printed when you create the tunnel.

Step 5: Configure the package

Add to your .env:

CLOUDFLARE_TUNNEL_MODE=named
CLOUDFLARE_TUNNEL_NAME=my-app
CLOUDFLARE_TUNNEL_HOSTNAME=tunnel.example.com

Step 6: Run it

php artisan cloudflare:tunnel
 Tunnel URL: https://tunnel.example.com
 Tunnel is running. Press Ctrl+C to stop.

Configuration

Environment Variable Default Description
CLOUDFLARE_TUNNEL_MODE quick quick or named
CLOUDFLARE_TUNNEL_NAME Named tunnel name (required for named mode)
CLOUDFLARE_TUNNEL_HOSTNAME Static hostname (required for named mode)
CLOUDFLARE_TUNNEL_LOCAL_URL http://127.0.0.1 Local URL to forward traffic to
CLOUDFLARE_TUNNEL_HOST_HEADER Override the Host header for local requests
CLOUDFLARE_TUNNEL_TIMEOUT 30 Seconds to wait for tunnel connection

Events

The package dispatches two events you can hook into with standard Laravel listeners:

Event Payload When
TunnelConnected string $url, TunnelMode $mode After the tunnel is established
TunnelDisconnected string $url Before the process exits (Ctrl+C)

Example: Register a Telegram webhook

Create a listener:

php artisan make:listener RegisterTelegramWebhook --event='\Laratusk\CloudflareTunnel\Events\TunnelConnected'
namespace App\Listeners;

use Laratusk\CloudflareTunnel\Events\TunnelConnected;
use Telegram\Bot\Laravel\Facades\Telegram;

class RegisterTelegramWebhook
{
    public function handle(TunnelConnected $event): void
    {
        Telegram::setWebhook(['url' => $event->url . '/api/telegram/webhook']);
    }
}
namespace App\Listeners;

use Laratusk\CloudflareTunnel\Events\TunnelDisconnected;
use Telegram\Bot\Laravel\Facades\Telegram;

class RemoveTelegramWebhook
{
    public function handle(TunnelDisconnected $event): void
    {
        Telegram::deleteWebhook();
    }
}

Laravel auto-discovers listeners, so no manual registration is needed.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

laratusk/cloudflare-tunnel 适用场景与选型建议

laratusk/cloudflare-tunnel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「development」 「laravel」 「cloudflare」 「tunnel」 「webhook」 「cloudflared」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 laratusk/cloudflare-tunnel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 laratusk/cloudflare-tunnel 我们能提供哪些服务?
定制开发 / 二次开发

基于 laratusk/cloudflare-tunnel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 42
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-02