解决客户端/服务端在线更新失败问题

This commit is contained in:
yisier
2026-07-12 22:51:56 -06:00
parent 1ebbee14a5
commit 7231be3944
2 changed files with 203 additions and 99 deletions
+1
View File
@@ -6,6 +6,7 @@
- 优化:
- 不再依赖外部 `web` 目录的静态文件
- 修复:
- 客户端/服务端在线更新失败 BUG
- `nps -server` 安装成功后 `web_port` 未正常显示BUG [#317](https://github.com/yisier/nps/issues/317)
- Bridge 握手增加 10s 读超时,校验失败/读失败立即关闭连接,减轻端口扫描导致的连接与 goroutine 堆积 [#281](https://github.com/yisier/nps/issues/281)
- 解决私密代理和P2P连接的TLS启用问题 [#143](https://github.com/yisier/nps/issues/143)
+202 -99
View File
@@ -137,9 +137,16 @@ WantedBy=multi-user.target
`
func UpdateNps() {
destPath := downloadLatest("server")
destPath, err := downloadLatest("server")
if err != nil {
log.Println("下载更新失败:", err)
return
}
//复制文件到对应目录
copyStaticFile(destPath, "nps")
if _, err := copyStaticFile(destPath, "nps"); err != nil {
log.Println("替换服务端文件失败:", err)
return
}
fmt.Println("Update completed, please restart")
}
@@ -148,16 +155,23 @@ func UpdateNpsNew() {
if err != nil {
log.Println("获取最新版本失败:", err)
return
} else {
fmt.Println("最新版本为:", latest)
if compareVersion(version.VERSION, latest) >= 0 {
fmt.Println("当前已是最新版本,无需更新")
return
}
}
destPath := downloadLatest2("server", filepath.Join(common.GetAppPath(), "temp"))
fmt.Println("最新版本为:", latest)
if compareVersion(version.VERSION, latest) >= 0 {
fmt.Println("当前已是最新版本,无需更新")
return
}
tempDir := filepath.Join(common.GetAppPath(), "temp")
destPath, err := downloadLatest2("server", tempDir)
if err != nil {
log.Println("下载更新失败:", err)
return
}
//复制文件到对应目录
copyStaticFileReplaceNps(destPath, common.GetAppPath())
if err := copyStaticFileReplaceNps(destPath, common.GetAppPath()); err != nil {
log.Println("替换服务端文件失败:", err)
return
}
fmt.Println("更新成功,请重启服务")
}
@@ -167,6 +181,9 @@ func fetchLatestVersion() (string, error) {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP %d", resp.StatusCode)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
@@ -175,6 +192,9 @@ func fetchLatestVersion() (string, error) {
if err := json.Unmarshal(b, rl); err != nil {
return "", err
}
if rl.TagName == "" {
return "", errors.New("无法解析最新版本号")
}
return rl.TagName, nil
}
@@ -191,9 +211,16 @@ func compareVersion(a, b string) int {
}
func UpdateNpc() {
destPath := downloadLatest("client")
destPath, err := downloadLatest("client")
if err != nil {
log.Println("下载更新失败:", err)
return
}
//复制文件到对应目录
copyStaticFile(destPath, "npc")
if _, err := copyStaticFile(destPath, "npc"); err != nil {
log.Println("替换客户端文件失败:", err)
return
}
fmt.Println("Update completed, please restart")
}
@@ -202,15 +229,22 @@ func UpdateNpcNew() {
if err != nil {
log.Println("获取最新版本失败:", err)
return
} else {
fmt.Println("最新版本为:", latest)
if compareVersion(version.VERSION, latest) >= 0 {
fmt.Println("当前已是最新版本,无需更新")
return
}
}
destPath := downloadLatest2("client", filepath.Join(common.GetAppPath(), "temp"))
copyStaticFileReplaceNpc(destPath, common.GetAppPath())
fmt.Println("最新版本为:", latest)
if compareVersion(version.VERSION, latest) >= 0 {
fmt.Println("当前已是最新版本,无需更新")
return
}
tempDir := filepath.Join(common.GetAppPath(), "temp")
destPath, err := downloadLatest2("client", tempDir)
if err != nil {
log.Println("下载更新失败:", err)
return
}
if err := copyStaticFileReplaceNpc(destPath, common.GetAppPath()); err != nil {
log.Println("替换客户端文件失败:", err)
return
}
fmt.Println("更新成功,请重启客户端")
}
@@ -218,69 +252,52 @@ type release struct {
TagName string `json:"tag_name"`
}
func downloadLatest(bin string) string {
// get version
data, err := http.Get("https://api.github.com/repos/yisier/nps/releases/latest")
if err != nil {
log.Fatal(err.Error())
}
b, err := ioutil.ReadAll(data.Body)
if err != nil {
log.Fatal(err)
}
rl := new(release)
json.Unmarshal(b, &rl)
version := rl.TagName
fmt.Println("the latest version is", version)
filename := runtime.GOOS + "_" + runtime.GOARCH + "_" + bin + ".zip"
// download latest package
downloadUrl := fmt.Sprintf("https://github.com/yisier/nps/releases/download/%s/%s", version, filename)
fmt.Println("download package from ", downloadUrl)
resp, err := http.Get(downloadUrl)
if err != nil {
log.Fatal(err.Error())
}
destPath, err := unpackit.Unpack(resp.Body, "")
if err != nil {
log.Fatal(err)
}
if bin == "server" {
destPath = strings.Replace(destPath, "/web", "", -1)
destPath = strings.Replace(destPath, `\web`, "", -1)
destPath = strings.Replace(destPath, "/views", "", -1)
destPath = strings.Replace(destPath, `\views`, "", -1)
} else {
destPath = strings.Replace(destPath, `\conf`, "", -1)
destPath = strings.Replace(destPath, "/conf", "", -1)
}
return destPath
func downloadLatest(bin string) (string, error) {
return downloadAndUnpack(bin, "")
}
func downloadLatest2(bin string, path string) string {
// get version
func downloadLatest2(bin string, path string) (string, error) {
return downloadAndUnpack(bin, path)
}
// downloadAndUnpack fetches the latest release package for the current OS/arch.
// Releases ship as .tar.gz (see build.assets.sh / release.yml).
func downloadAndUnpack(bin, unpackPath string) (string, error) {
data, err := http.Get("https://api.github.com/repos/yisier/nps/releases/latest")
if err != nil {
log.Fatal(err.Error())
return "", err
}
defer data.Body.Close()
if data.StatusCode != http.StatusOK {
return "", fmt.Errorf("获取版本信息失败: HTTP %d", data.StatusCode)
}
b, err := ioutil.ReadAll(data.Body)
if err != nil {
log.Fatal(err)
return "", err
}
rl := new(release)
json.Unmarshal(b, &rl)
version := rl.TagName
fmt.Println("the latest version is", version)
filename := runtime.GOOS + "_" + runtime.GOARCH + "_" + bin + ".zip"
// download latest package
downloadUrl := fmt.Sprintf("https://github.com/yisier/nps/releases/download/%s/%s", version, filename)
if err := json.Unmarshal(b, rl); err != nil {
return "", err
}
if rl.TagName == "" {
return "", errors.New("无法解析最新版本号")
}
ver := rl.TagName
fmt.Println("the latest version is", ver)
filename := runtime.GOOS + "_" + runtime.GOARCH + "_" + bin + ".tar.gz"
downloadUrl := fmt.Sprintf("https://github.com/yisier/nps/releases/download/%s/%s", ver, filename)
fmt.Println("download package from ", downloadUrl)
resp, err := http.Get(downloadUrl)
if err != nil {
log.Fatal(err.Error())
return "", err
}
destPath, err := unpackit.Unpack(resp.Body, path)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("下载失败: HTTP %d %s", resp.StatusCode, downloadUrl)
}
destPath, err := unpackit.Unpack(resp.Body, unpackPath)
if err != nil {
log.Fatal(err)
return "", err
}
if bin == "server" {
destPath = strings.Replace(destPath, "/web", "", -1)
@@ -291,51 +308,132 @@ func downloadLatest2(bin string, path string) string {
destPath = strings.Replace(destPath, `\conf`, "", -1)
destPath = strings.Replace(destPath, "/conf", "", -1)
}
return destPath
return destPath, nil
}
func copyStaticFile(srcPath, bin string) string {
func copyStaticFile(srcPath, bin string) (string, error) {
// nps web UI is embedded in the binary; no web/ files to copy.
binPath, _ := filepath.Abs(os.Args[0])
srcBin := filepath.Join(srcPath, bin)
if common.IsWindows() {
srcBin += ".exe"
}
if _, err := os.Stat(srcBin); err != nil {
return "", fmt.Errorf("更新包中未找到可执行文件 %s: %w", srcBin, err)
}
if !common.IsWindows() {
if _, err := copyFile(filepath.Join(srcPath, bin), "/usr/bin/"+bin); err != nil {
if _, err := copyFile(filepath.Join(srcPath, bin), "/usr/local/bin/"+bin); err != nil {
log.Fatalln(err)
} else {
binPath = "/usr/local/bin/" + bin
if _, err := copyFile(srcBin, "/usr/bin/"+bin); err != nil {
if _, err := copyFile(srcBin, "/usr/local/bin/"+bin); err != nil {
return "", err
}
binPath = "/usr/local/bin/" + bin
} else {
binPath = "/usr/bin/" + bin
}
} else {
copyFile(filepath.Join(srcPath, bin+".exe"), filepath.Join(common.GetAppPath(), bin+".exe"))
destBin := filepath.Join(common.GetAppPath(), bin+".exe")
if err := replaceExecutable(srcBin, destBin); err != nil {
return "", err
}
binPath = destBin
}
chMod(binPath, 0755)
return binPath
return binPath, nil
}
func copyStaticFileReplaceNps(srcPath, descPath string) string {
func copyStaticFileReplaceNps(srcPath, descPath string) error {
// Web UI is embedded in the binary; only replace the executable.
binPath, _ := filepath.Abs(os.Args[0])
if !common.IsWindows() {
os.Rename(filepath.Join(srcPath, "nps"), filepath.Join(descPath, "nps"))
} else {
os.Rename(filepath.Join(srcPath, "nps.exe"), filepath.Join(descPath, "nps.exe"))
}
chMod(binPath, 0755)
os.RemoveAll(srcPath)
return binPath
return replaceBinFromPackage(srcPath, descPath, "nps")
}
func copyStaticFileReplaceNpc(srcPath, descPath string) string {
binPath, _ := filepath.Abs(os.Args[0])
if !common.IsWindows() {
os.Rename(filepath.Join(srcPath, "npc"), filepath.Join(descPath, "npc"))
} else {
os.Rename(filepath.Join(srcPath, "npc.exe"), filepath.Join(descPath, "npc.exe"))
func copyStaticFileReplaceNpc(srcPath, descPath string) error {
return replaceBinFromPackage(srcPath, descPath, "npc")
}
func replaceBinFromPackage(srcPath, descPath, bin string) error {
srcBin := filepath.Join(srcPath, bin)
destBin := filepath.Join(descPath, bin)
if common.IsWindows() {
srcBin += ".exe"
destBin += ".exe"
}
chMod(binPath, 0755)
os.RemoveAll(srcPath)
return binPath
// Prefer replacing the actually running binary when its basename matches.
if exe, err := os.Executable(); err == nil {
if filepath.Base(exe) == filepath.Base(destBin) {
destBin = exe
}
}
if _, err := os.Stat(srcBin); err != nil {
// unpackit may return a nested root dir; search one level if needed
if found, findErr := findBinInDir(srcPath, filepath.Base(srcBin)); findErr == nil {
srcBin = found
} else {
return fmt.Errorf("更新包中未找到可执行文件 %s: %w", srcBin, err)
}
}
if err := replaceExecutable(srcBin, destBin); err != nil {
return err
}
chMod(destBin, 0755)
// Clean temp package; keep parent temp dir if still in use
_ = os.RemoveAll(srcPath)
return nil
}
func findBinInDir(root, name string) (string, error) {
var found string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil || info == nil || info.IsDir() {
return err
}
if info.Name() == name {
found = path
return errors.New("found")
}
return nil
})
if found != "" {
return found, nil
}
if err != nil {
return "", err
}
return "", os.ErrNotExist
}
// replaceExecutable places srcBin at destBin. On Windows a running executable
// cannot be overwritten, but it can be renamed aside first.
func replaceExecutable(srcBin, destBin string) error {
if _, err := os.Stat(srcBin); err != nil {
return fmt.Errorf("源文件不存在: %s: %w", srcBin, err)
}
if err := os.MkdirAll(filepath.Dir(destBin), 0755); err != nil {
return err
}
// Move the current binary out of the way when present (required on Windows
// while the process is still running).
if _, err := os.Stat(destBin); err == nil {
bak := destBin + ".old"
_ = os.Remove(bak)
if err := os.Rename(destBin, bak); err != nil {
return fmt.Errorf("无法备份当前程序 %s: %w", destBin, err)
}
}
// Same filesystem: rename is atomic. Fall back to copy across volumes.
if err := os.Rename(srcBin, destBin); err != nil {
if _, copyErr := copyFile(srcBin, destBin); copyErr != nil {
// Best-effort restore of previous binary
bak := destBin + ".old"
if _, statErr := os.Stat(bak); statErr == nil {
_ = os.Rename(bak, destBin)
}
return fmt.Errorf("替换可执行文件失败: %w", copyErr)
}
_ = os.Remove(srcBin)
}
return nil
}
func InstallNpc() {
@@ -346,7 +444,9 @@ func InstallNpc() {
log.Fatal(err)
}
}
copyStaticFile(common.GetAppPath(), "npc")
if _, err := copyStaticFile(common.GetAppPath(), "npc"); err != nil {
log.Fatalln(err)
}
}
func InstallNps() string {
@@ -360,7 +460,10 @@ func InstallNps() string {
}
chMod(filepath.Join(path, "conf"), 0766)
}
binPath := copyStaticFile(common.GetAppPath(), "nps")
binPath, err := copyStaticFile(common.GetAppPath(), "nps")
if err != nil {
log.Fatalln(err)
}
log.Println("install ok!")
log.Println("Web UI is embedded in the nps binary; no web/ directory is required")
log.Println("The new configuration file is located in", path, "you can edit them")