Skip to content

Commit 47e7221

Browse files
committed
fix(quick): 隔离 cloudflared 配置防止 UUID 解析失败 (close #13)
quick 模式启动 cloudflared 时显式指定空配置文件, 防止读取用户已有的 ~/.cloudflared/config.yml 中残留的 tunnel 字段触发 uuid.Parse("") 报错 invalid UUID length: 0
1 parent 6466315 commit 47e7221

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

internal/daemon/quick.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,25 @@ import (
77
"os"
88
"os/exec"
99
"os/signal"
10+
"path/filepath"
1011
"strings"
1112
"time"
1213

1314
"github.com/qingchencloud/cftunnel/internal/authproxy"
15+
"github.com/qingchencloud/cftunnel/internal/config"
1416
)
1517

18+
// quickConfigPath 返回 quick 模式专用的空配置文件路径
19+
// 防止 cloudflared 读取用户已有的 ~/.cloudflared/config.yml 导致 UUID 解析失败
20+
func quickConfigPath() string {
21+
p := filepath.Join(config.Dir(), "quick-config.yml")
22+
if _, err := os.Stat(p); os.IsNotExist(err) {
23+
os.MkdirAll(config.Dir(), 0700)
24+
os.WriteFile(p, []byte("# cftunnel quick mode - empty config\n"), 0600)
25+
}
26+
return p
27+
}
28+
1629
// StartQuick 启动免域名模式(前台运行,Ctrl+C 退出)
1730
func StartQuick(port string) error {
1831
binPath, err := EnsureCloudflared()
@@ -23,7 +36,10 @@ func StartQuick(port string) error {
2336
return fmt.Errorf("cloudflared 已在运行,请先执行 cftunnel down")
2437
}
2538

26-
cmd := exec.Command(binPath, "tunnel", "--url", "http://localhost:"+port)
39+
// 显式指定空配置文件,防止 cloudflared 读取用户已有的 ~/.cloudflared/config.yml
40+
// 避免残留的 tunnel: 字段触发 UUID 解析失败 (issue #13)
41+
cfgPath := quickConfigPath()
42+
cmd := exec.Command(binPath, "tunnel", "--config", cfgPath, "--url", "http://localhost:"+port)
2743

2844
// 捕获 stderr 提取随机域名
2945
stderr, err := cmd.StderrPipe()
@@ -111,8 +127,9 @@ func StartQuickWithAuth(port, username, password string) error {
111127
proxyPort := fmt.Sprintf("%d", proxy.ListenPort())
112128
fmt.Printf("鉴权代理已启动 127.0.0.1:%s → 127.0.0.1:%s\n", proxyPort, port)
113129

114-
// cloudflared 指向代理端口
115-
cmd := exec.Command(binPath, "tunnel", "--url", "http://localhost:"+proxyPort)
130+
// cloudflared 指向代理端口(同样隔离配置文件)
131+
cfgPath := quickConfigPath()
132+
cmd := exec.Command(binPath, "tunnel", "--config", cfgPath, "--url", "http://localhost:"+proxyPort)
116133

117134
stderr, err := cmd.StderrPipe()
118135
if err != nil {

0 commit comments

Comments
 (0)