143 lines
3.8 KiB
Go
143 lines
3.8 KiB
Go
package hysteria2
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"vpnclient/internal/coredl"
|
|
)
|
|
|
|
const githubAPILatest = "https://api.github.com/repos/apernet/hysteria/releases/latest"
|
|
|
|
// ResolveBinary finds hysteria client binary.
|
|
func ResolveBinary(binDir string) (string, error) {
|
|
names := candidateNames()
|
|
candidates := []string{}
|
|
for _, name := range names {
|
|
candidates = append(candidates,
|
|
filepath.Join(binDir, name),
|
|
filepath.Join(binDir, "hysteria2", name),
|
|
filepath.Join(binDir, "hysteria", name),
|
|
)
|
|
}
|
|
if exe, err := os.Executable(); err == nil {
|
|
dir := filepath.Dir(exe)
|
|
for _, name := range names {
|
|
candidates = append(candidates, filepath.Join(dir, name), filepath.Join(dir, "bin", name))
|
|
}
|
|
}
|
|
for _, c := range candidates {
|
|
if st, err := os.Stat(c); err == nil && !st.IsDir() {
|
|
return c, nil
|
|
}
|
|
}
|
|
return "", fmt.Errorf("hysteria2 binary not found in %s; run install-core", binDir)
|
|
}
|
|
|
|
func candidateNames() []string {
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
return []string{
|
|
"hysteria.exe",
|
|
"hysteria-windows-amd64.exe",
|
|
"hysteria-windows-amd64-avx.exe",
|
|
}
|
|
case "darwin":
|
|
return []string{
|
|
"hysteria",
|
|
"hysteria-darwin-arm64",
|
|
"hysteria-darwin-amd64",
|
|
}
|
|
default:
|
|
return []string{"hysteria", "hysteria-linux-amd64", "hysteria-linux-arm64"}
|
|
}
|
|
}
|
|
|
|
// EnsureBinary downloads official Hysteria 2 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
|
|
}
|
|
want, err := releaseAssetName()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
fmt.Fprintf(os.Stderr, "downloading official hysteria2 release (%s)...\n", want)
|
|
asset, all, err := findReleaseAsset(want)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
sum, err := coredl.ResolveSHA(asset, all)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
destName := "hysteria"
|
|
if runtime.GOOS == "windows" {
|
|
destName = "hysteria.exe"
|
|
}
|
|
dest := filepath.Join(binDir, destName)
|
|
tmp := filepath.Join(binDir, asset.Name+".download")
|
|
if err := coredl.DownloadAndVerify(tmp, asset.BrowserDownloadURL, sum); err != nil {
|
|
return "", err
|
|
}
|
|
_ = os.Remove(dest)
|
|
if err := os.Rename(tmp, dest); err != nil {
|
|
_ = os.Remove(tmp)
|
|
return "", err
|
|
}
|
|
_ = os.Chmod(dest, 0o755)
|
|
if runtime.GOOS == "darwin" {
|
|
_ = exec.Command("xattr", "-dr", "com.apple.quarantine", dest).Run()
|
|
}
|
|
return ResolveBinary(binDir)
|
|
}
|
|
|
|
func releaseAssetName() (string, error) {
|
|
switch {
|
|
case runtime.GOOS == "windows" && runtime.GOARCH == "amd64":
|
|
return "hysteria-windows-amd64.exe", nil
|
|
case runtime.GOOS == "windows" && runtime.GOARCH == "arm64":
|
|
return "hysteria-windows-arm64.exe", nil
|
|
case runtime.GOOS == "linux" && runtime.GOARCH == "amd64":
|
|
return "hysteria-linux-amd64", nil
|
|
case runtime.GOOS == "darwin" && runtime.GOARCH == "amd64":
|
|
return "hysteria-darwin-amd64", nil
|
|
case runtime.GOOS == "darwin" && runtime.GOARCH == "arm64":
|
|
return "hysteria-darwin-arm64", nil
|
|
default:
|
|
return "", fmt.Errorf("unsupported platform %s/%s for hysteria2 auto-download", runtime.GOOS, runtime.GOARCH)
|
|
}
|
|
}
|
|
|
|
func findReleaseAsset(exactName string) (coredl.ReleaseAsset, []coredl.ReleaseAsset, error) {
|
|
_, assets, err := coredl.FetchLatestAssets(githubAPILatest)
|
|
if err != nil {
|
|
return coredl.ReleaseAsset{}, nil, err
|
|
}
|
|
var fallback coredl.ReleaseAsset
|
|
for _, a := range assets {
|
|
if a.Name == exactName {
|
|
return a, assets, nil
|
|
}
|
|
if exactName == "hysteria-windows-amd64.exe" && a.Name == "hysteria-windows-amd64-avx.exe" {
|
|
fallback = a
|
|
}
|
|
}
|
|
if fallback.Name != "" {
|
|
return fallback, assets, nil
|
|
}
|
|
for _, a := range assets {
|
|
if strings.HasPrefix(a.Name, strings.TrimSuffix(exactName, filepath.Ext(exactName))) {
|
|
return a, assets, nil
|
|
}
|
|
}
|
|
return coredl.ReleaseAsset{}, assets, fmt.Errorf("no asset %s in hysteria release", exactName)
|
|
}
|