update Xray Core on Windows
This commit is contained in:
parent
6dd9b4b07c
commit
52e7592d52
@ -12,6 +12,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -376,6 +377,8 @@ func (s *ServerService) downloadXRay(version string) (string, error) {
|
|||||||
switch osName {
|
switch osName {
|
||||||
case "darwin":
|
case "darwin":
|
||||||
osName = "macos"
|
osName = "macos"
|
||||||
|
case "windows":
|
||||||
|
osName = "windows"
|
||||||
}
|
}
|
||||||
|
|
||||||
switch arch {
|
switch arch {
|
||||||
@ -419,19 +422,23 @@ func (s *ServerService) downloadXRay(version string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServerService) UpdateXray(version string) error {
|
func (s *ServerService) UpdateXray(version string) error {
|
||||||
|
// 1. Stop xray before doing anything
|
||||||
|
if err := s.StopXrayService(); err != nil {
|
||||||
|
logger.Warning("failed to stop xray before update:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Download the zip
|
||||||
zipFileName, err := s.downloadXRay(version)
|
zipFileName, err := s.downloadXRay(version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer os.Remove(zipFileName)
|
||||||
|
|
||||||
zipFile, err := os.Open(zipFileName)
|
zipFile, err := os.Open(zipFileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer zipFile.Close()
|
||||||
zipFile.Close()
|
|
||||||
os.Remove(zipFileName)
|
|
||||||
}()
|
|
||||||
|
|
||||||
stat, err := zipFile.Stat()
|
stat, err := zipFile.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -442,19 +449,15 @@ func (s *ServerService) UpdateXray(version string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.xrayService.StopXray()
|
// 3. Helper to extract files
|
||||||
defer func() {
|
|
||||||
err := s.xrayService.RestartXray(true)
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("start xray failed:", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
copyZipFile := func(zipName string, fileName string) error {
|
copyZipFile := func(zipName string, fileName string) error {
|
||||||
zipFile, err := reader.Open(zipName)
|
zipFile, err := reader.Open(zipName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer zipFile.Close()
|
||||||
|
os.MkdirAll(filepath.Dir(fileName), 0755)
|
||||||
os.Remove(fileName)
|
os.Remove(fileName)
|
||||||
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, fs.ModePerm)
|
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, fs.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -465,11 +468,23 @@ func (s *ServerService) UpdateXray(version string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 4. Extract correct binary
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
targetBinary := filepath.Join("bin", "xray-windows-amd64.exe")
|
||||||
|
err = copyZipFile("xray.exe", targetBinary)
|
||||||
|
} else {
|
||||||
err = copyZipFile("xray", xray.GetBinaryPath())
|
err = copyZipFile("xray", xray.GetBinaryPath())
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 5. Restart xray
|
||||||
|
if err := s.xrayService.RestartXray(true); err != nil {
|
||||||
|
logger.Error("start xray failed:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user