部分逻辑优化
This commit is contained in:
+28
-22
@@ -19,7 +19,7 @@ var (
|
||||
)
|
||||
|
||||
// GetFrpcPath 获取 frpc 二进制路径
|
||||
// 优先级: 本地缓存 > 内嵌二进制 > 系统 PATH
|
||||
// 优先级: 本地缓存 > 程序同目录 > 当前工作目录 > embed 提取到当前目录 > 系统 PATH
|
||||
func GetFrpcPath() (string, error) {
|
||||
frpcPathMutex.Lock()
|
||||
defer frpcPathMutex.Unlock()
|
||||
@@ -50,38 +50,44 @@ func GetFrpcPath() (string, error) {
|
||||
return "", fmt.Errorf("不支持的平台: %s/%s", runtime.GOOS, runtime.GOARCH)
|
||||
}
|
||||
|
||||
// 尝试从本地 bin 目录加载
|
||||
localPath := filepath.Join(".", "bin", fileName)
|
||||
if _, err := os.Stat(localPath); err == nil {
|
||||
cachedFrpcPath = localPath
|
||||
return localPath, nil
|
||||
// 方法1: 尝试从程序同目录加载
|
||||
execPath, err := os.Executable()
|
||||
if err == nil {
|
||||
execDir := filepath.Dir(execPath)
|
||||
localPath := filepath.Join(execDir, "frpc")
|
||||
if runtime.GOOS == "windows" {
|
||||
localPath += ".exe"
|
||||
}
|
||||
if _, err := os.Stat(localPath); err == nil {
|
||||
cachedFrpcPath = localPath
|
||||
return localPath, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 从 embed 提取到 data 目录(Windows 和 Linux 统一)
|
||||
dataDir := "./data"
|
||||
if err := os.MkdirAll(dataDir, 0755); err != nil {
|
||||
return "", fmt.Errorf("创建 data 目录失败: %w", err)
|
||||
}
|
||||
|
||||
tmpPath := filepath.Join(dataDir, "frpc")
|
||||
// 方法2: 尝试从当前工作目录加载
|
||||
cwdPath := "./frpc"
|
||||
if runtime.GOOS == "windows" {
|
||||
tmpPath += ".exe"
|
||||
cwdPath += ".exe"
|
||||
}
|
||||
if _, err := os.Stat(cwdPath); err == nil {
|
||||
cachedFrpcPath = cwdPath
|
||||
return cwdPath, nil
|
||||
}
|
||||
|
||||
// 方法3: 从 embed 提取到当前工作目录
|
||||
data, err := embeddedFrpc.ReadFile("bin/" + fileName)
|
||||
if err == nil {
|
||||
if err := os.WriteFile(tmpPath, data, 0755); err == nil {
|
||||
cachedFrpcPath = tmpPath
|
||||
return tmpPath, nil
|
||||
extractPath := "./frpc"
|
||||
if runtime.GOOS == "windows" {
|
||||
extractPath += ".exe"
|
||||
}
|
||||
// 如果写入失败,检查文件是否已存在
|
||||
if _, statErr := os.Stat(tmpPath); statErr == nil {
|
||||
cachedFrpcPath = tmpPath
|
||||
return tmpPath, nil
|
||||
if err := os.WriteFile(extractPath, data, 0755); err == nil {
|
||||
cachedFrpcPath = extractPath
|
||||
return extractPath, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 最后尝试从系统 PATH 查找
|
||||
// 方法4: 从系统 PATH 查找
|
||||
path, err := exec.LookPath("frpc")
|
||||
if err == nil {
|
||||
cachedFrpcPath = path
|
||||
|
||||
Reference in New Issue
Block a user