@@ -4,13 +4,17 @@ import (
44 "fmt"
55 "os"
66 "os/exec"
7+ "path/filepath"
78 "strconv"
89 "strings"
910
1011 "github.com/qingchencloud/cftunnel/internal/config"
1112)
1213
13- var pidFile = config .Dir () + "/cloudflared.pid"
14+ // pidFilePath 返回 PID 文件路径(函数调用替代包级变量,确保便携模式正确生效)
15+ func pidFilePath () string {
16+ return filepath .Join (config .Dir (), "cloudflared.pid" )
17+ }
1418
1519// Start 启动 cloudflared(token 模式)
1620func Start (token string ) error {
@@ -30,7 +34,7 @@ func Start(token string) error {
3034 }
3135
3236 os .MkdirAll (config .Dir (), 0700 )
33- os .WriteFile (pidFile , []byte (strconv .Itoa (cmd .Process .Pid )), 0600 )
37+ os .WriteFile (pidFilePath () , []byte (strconv .Itoa (cmd .Process .Pid )), 0600 )
3438 fmt .Printf ("cloudflared 已启动 (PID: %d)\n " , cmd .Process .Pid )
3539 return nil
3640}
@@ -44,7 +48,7 @@ func Stop() error {
4448 if err := processKill (pid ); err != nil {
4549 return fmt .Errorf ("停止 cloudflared 失败: %w" , err )
4650 }
47- os .Remove (pidFile )
51+ os .Remove (pidFilePath () )
4852 fmt .Println ("cloudflared 已停止" )
4953 return nil
5054}
@@ -65,7 +69,7 @@ func PID() int {
6569}
6670
6771func readPID () (int , error ) {
68- data , err := os .ReadFile (pidFile )
72+ data , err := os .ReadFile (pidFilePath () )
6973 if err != nil {
7074 return 0 , err
7175 }
0 commit comments