package naive import ( "archive/zip" "fmt" "io" "os" "os/exec" "path/filepath" "runtime" "strings" "vpnclient/internal/coredl" ) const githubAPILatest = "https://api.github.com/repos/klzgrad/naiveproxy/releases/latest" // ResolveBinary finds naive.exe/naive in binDir or PATH. func ResolveBinary(binDir string) (string, error) { name := "naive" if runtime.GOOS == "windows" { name = "naive.exe" } candidates := []string{ filepath.Join(binDir, name), filepath.Join(binDir, "naiveproxy", name), } if exe, err := os.Executable(); err == nil { candidates = append(candidates, filepath.Join(filepath.Dir(exe), name)) candidates = append(candidates, filepath.Join(filepath.Dir(exe), "bin", name)) } for _, c := range candidates { if st, err := os.Stat(c); err == nil && !st.IsDir() { return c, nil } } return "", fmt.Errorf("naive binary not found (looked in %s); run: vpnclient install-core", binDir) } // EnsureBinary downloads the official Windows/Linux/macOS release if missing. func EnsureBinary(binDir string) (string, error) { if path, err := ResolveBinary(binDir); err == nil { return path, nil } if err := os.MkdirAll(binDir, 0o755); err != nil { return "", err } pattern, err := assetNameForPlatform() if err != nil { return "", err } fmt.Fprintf(os.Stderr, "downloading official naiveproxy release (%s)...\n", pattern) asset, all, err := findReleaseAsset(pattern) if err != nil { return "", err } sum, err := coredl.ResolveSHA(asset, all) if err != nil { return "", err } archivePath := filepath.Join(binDir, asset.Name+".download") if err := coredl.DownloadAndVerify(archivePath, asset.BrowserDownloadURL, sum); err != nil { return "", err } defer os.Remove(archivePath) if err := extractNaiveArchive(archivePath, binDir); err != nil { return "", err } path, err := ResolveBinary(binDir) if err != nil { return "", err } clearQuarantine(path) return path, nil } func clearQuarantine(path string) { if runtime.GOOS != "darwin" { return } _ = exec.Command("xattr", "-dr", "com.apple.quarantine", path).Run() } func assetNameForPlatform() (string, error) { switch { case runtime.GOOS == "windows" && runtime.GOARCH == "amd64": return "naiveproxy-*-win-x64.zip", nil case runtime.GOOS == "windows" && runtime.GOARCH == "arm64": return "naiveproxy-*-win-arm64.zip", nil case runtime.GOOS == "linux" && runtime.GOARCH == "amd64": return "naiveproxy-*-linux-x64.zip", nil case runtime.GOOS == "darwin" && runtime.GOARCH == "amd64": return "naiveproxy-*-mac-x64", nil case runtime.GOOS == "darwin" && runtime.GOARCH == "arm64": return "naiveproxy-*-mac-arm64", nil default: return "", fmt.Errorf("unsupported platform %s/%s for auto-download; place naive binary in bin/", runtime.GOOS, runtime.GOARCH) } } func findReleaseAsset(pattern string) (coredl.ReleaseAsset, []coredl.ReleaseAsset, error) { _, assets, err := coredl.FetchLatestAssets(githubAPILatest) if err != nil { return coredl.ReleaseAsset{}, nil, err } prefix := strings.Split(pattern, "*")[0] needle := "" if parts := strings.SplitN(pattern, "*", 2); len(parts) == 2 { needle = parts[1] } var best coredl.ReleaseAsset for _, a := range assets { name := a.Name if strings.Contains(name, "plugin") { continue } if !strings.HasPrefix(name, prefix) { continue } if needle != "" && !strings.Contains(name, needle) { continue } if strings.HasSuffix(name, ".zip") || strings.HasSuffix(name, ".tar.xz") || strings.HasSuffix(name, ".txz") { return a, assets, nil } if best.Name == "" { best = a } } if best.Name != "" { return best, assets, nil } return coredl.ReleaseAsset{}, assets, fmt.Errorf("no asset matching %s in naive release", pattern) } func extractNaiveArchive(archivePath, destDir string) error { lower := strings.ToLower(archivePath) lower = strings.TrimSuffix(lower, ".download") switch { case strings.HasSuffix(lower, ".zip"): return unzipNaive(archivePath, destDir) case strings.HasSuffix(lower, ".tar.xz"), strings.HasSuffix(lower, ".txz"): return extractTarXz(archivePath, destDir) default: return fmt.Errorf("unsupported archive: %s", filepath.Base(archivePath)) } } func extractTarXz(archivePath, destDir string) error { tmp, err := os.MkdirTemp(destDir, "naive-extract-*") if err != nil { return err } defer os.RemoveAll(tmp) cmd := exec.Command("tar", "-xJf", archivePath, "-C", tmp) if out, err := cmd.CombinedOutput(); err != nil { return fmt.Errorf("tar extract: %w (%s)", err, strings.TrimSpace(string(out))) } want := "naive" var found string _ = filepath.Walk(tmp, func(path string, info os.FileInfo, err error) error { if err != nil || info == nil || info.IsDir() { return nil } if info.Name() == want { found = path } return nil }) if found == "" { return fmt.Errorf("archive does not contain %s", want) } dest := filepath.Join(destDir, want) in, err := os.Open(found) if err != nil { return err } defer in.Close() out, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o755) if err != nil { return err } defer out.Close() if _, err := io.Copy(out, in); err != nil { return err } return nil } func unzipNaive(zipPath, destDir string) error { r, err := zip.OpenReader(zipPath) if err != nil { return err } defer r.Close() want := "naive" if runtime.GOOS == "windows" { want = "naive.exe" } var found bool for _, f := range r.File { base := filepath.Base(f.Name) if base != want { continue } if err := extractFile(f, filepath.Join(destDir, want)); err != nil { return err } found = true break } if !found { return fmt.Errorf("zip does not contain %s", want) } return nil } func extractFile(f *zip.File, dest string) error { rc, err := f.Open() if err != nil { return err } defer rc.Close() out, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o755) if err != nil { return err } defer out.Close() _, err = io.Copy(out, rc) return err }