Release 1.9.0: add VLESS, VMess and Trojan via Xray-core.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
package xray
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const githubAPILatest = "https://api.github.com/repos/XTLS/Xray-core/releases/latest"
|
||||
|
||||
// ResolveBinary finds xray executable.
|
||||
func ResolveBinary(binDir string) (string, error) {
|
||||
for _, name := range candidateNames() {
|
||||
candidates := []string{
|
||||
filepath.Join(binDir, name),
|
||||
filepath.Join(binDir, "xray", name),
|
||||
}
|
||||
if exe, err := os.Executable(); err == nil {
|
||||
dir := filepath.Dir(exe)
|
||||
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("xray binary not found in %s; run install-core", binDir)
|
||||
}
|
||||
|
||||
func candidateNames() []string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return []string{"xray.exe", "Xray.exe"}
|
||||
}
|
||||
return []string{"xray", "Xray"}
|
||||
}
|
||||
|
||||
// EnsureBinary downloads official Xray-core 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 := releaseZipName()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "downloading official Xray-core (%s)...\n", want)
|
||||
name, url, err := findReleaseAsset(want)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
zipPath := filepath.Join(binDir, name+".download")
|
||||
_ = os.Remove(zipPath)
|
||||
if err := downloadFile(zipPath, url); err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer os.Remove(zipPath)
|
||||
|
||||
destName := "xray"
|
||||
if runtime.GOOS == "windows" {
|
||||
destName = "xray.exe"
|
||||
}
|
||||
dest := filepath.Join(binDir, destName)
|
||||
if err := extractNamedFromZip(zipPath, destName, dest); err != nil {
|
||||
// Some zips nest the binary; try case-insensitive match.
|
||||
if err2 := extractXrayFromZip(zipPath, dest); err2 != nil {
|
||||
return "", fmt.Errorf("%v; %w", err, err2)
|
||||
}
|
||||
}
|
||||
_ = os.Chmod(dest, 0o755)
|
||||
return ResolveBinary(binDir)
|
||||
}
|
||||
|
||||
func releaseZipName() (string, error) {
|
||||
switch {
|
||||
case runtime.GOOS == "windows" && runtime.GOARCH == "amd64":
|
||||
return "Xray-windows-64.zip", nil
|
||||
case runtime.GOOS == "windows" && runtime.GOARCH == "arm64":
|
||||
return "Xray-windows-arm64-v8a.zip", nil
|
||||
case runtime.GOOS == "darwin" && runtime.GOARCH == "amd64":
|
||||
return "Xray-macos-64.zip", nil
|
||||
case runtime.GOOS == "darwin" && runtime.GOARCH == "arm64":
|
||||
return "Xray-macos-arm64-v8a.zip", nil
|
||||
case runtime.GOOS == "linux" && runtime.GOARCH == "amd64":
|
||||
return "Xray-linux-64.zip", nil
|
||||
case runtime.GOOS == "linux" && runtime.GOARCH == "arm64":
|
||||
return "Xray-linux-arm64-v8a.zip", nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported platform %s/%s for xray auto-download", runtime.GOOS, runtime.GOARCH)
|
||||
}
|
||||
}
|
||||
|
||||
type ghRelease struct {
|
||||
TagName string `json:"tag_name"`
|
||||
Assets []struct {
|
||||
Name string `json:"name"`
|
||||
BrowserDownloadURL string `json:"browser_download_url"`
|
||||
} `json:"assets"`
|
||||
}
|
||||
|
||||
func findReleaseAsset(exactName string) (name, downloadURL string, err error) {
|
||||
client := &http.Client{Timeout: 60 * time.Second}
|
||||
req, err := http.NewRequest(http.MethodGet, githubAPILatest, nil)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
req.Header.Set("Accept", "application/vnd.github+json")
|
||||
req.Header.Set("User-Agent", "navis-vpnclient")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("github api: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
|
||||
return "", "", fmt.Errorf("github api: %s: %s", resp.Status, string(body))
|
||||
}
|
||||
var rel ghRelease
|
||||
if err := json.NewDecoder(resp.Body).Decode(&rel); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
for _, a := range rel.Assets {
|
||||
if a.Name == exactName {
|
||||
return a.Name, a.BrowserDownloadURL, nil
|
||||
}
|
||||
}
|
||||
for _, a := range rel.Assets {
|
||||
if strings.EqualFold(a.Name, exactName) {
|
||||
return a.Name, a.BrowserDownloadURL, nil
|
||||
}
|
||||
}
|
||||
return "", "", fmt.Errorf("no asset %s in release %s", exactName, rel.TagName)
|
||||
}
|
||||
|
||||
func downloadFile(path, url string) error {
|
||||
client := &http.Client{Timeout: 15 * time.Minute}
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("download %s: %s", url, resp.Status)
|
||||
}
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
_, err = io.Copy(f, io.LimitReader(resp.Body, 120<<20))
|
||||
return err
|
||||
}
|
||||
|
||||
func extractNamedFromZip(zipPath, wantName, dest string) error {
|
||||
r, err := zip.OpenReader(zipPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Close()
|
||||
wantBase := strings.ToLower(filepath.Base(wantName))
|
||||
for _, f := range r.File {
|
||||
base := strings.ToLower(filepath.Base(f.Name))
|
||||
if base != wantBase {
|
||||
continue
|
||||
}
|
||||
return writeZipFile(f, dest)
|
||||
}
|
||||
return fmt.Errorf("%s not found in zip", wantName)
|
||||
}
|
||||
|
||||
func extractXrayFromZip(zipPath, dest string) error {
|
||||
r, err := zip.OpenReader(zipPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Close()
|
||||
for _, f := range r.File {
|
||||
base := strings.ToLower(filepath.Base(f.Name))
|
||||
if base == "xray" || base == "xray.exe" {
|
||||
return writeZipFile(f, dest)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("xray binary not found in archive")
|
||||
}
|
||||
|
||||
func writeZipFile(f *zip.File, dest string) error {
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rc.Close()
|
||||
_ = os.Remove(dest)
|
||||
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, io.LimitReader(rc, 80<<20))
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user