diff --git a/cmd/npc/npc.go b/cmd/npc/npc.go index 45fa226..d4ec4bd 100644 --- a/cmd/npc/npc.go +++ b/cmd/npc/npc.go @@ -9,6 +9,7 @@ import ( "github.com/cnlh/nps/lib/common" "github.com/cnlh/nps/lib/config" "github.com/cnlh/nps/lib/file" + "github.com/cnlh/nps/lib/install" "github.com/cnlh/nps/lib/version" "github.com/kardianos/service" "os" @@ -90,6 +91,9 @@ func main() { case "register": flag.CommandLine.Parse(os.Args[2:]) client.RegisterLocalIp(*serverAddr, *verifyKey, *connType, *proxyUrl, *registerTime) + case "update": + install.UpdateNpc() + return case "nat": nat, host, err := stun.NewClient().Discover() if err != nil || host == nil { @@ -102,6 +106,9 @@ func main() { } if *srv != "" { + if *srv == "install" { + install.InstallNpc() + } err := service.Control(s, *srv) if err != nil { logs.Error("Valid actions: %q\n", service.ControlAction, err.Error()) @@ -116,7 +123,7 @@ type npc struct { } func (p *npc) Start(s service.Service) error { - p.run() + go p.run() return nil } func (p *npc) Stop(s service.Service) error { diff --git a/cmd/nps/nps.go b/cmd/nps/nps.go index 508cd40..95ddf9c 100644 --- a/cmd/nps/nps.go +++ b/cmd/nps/nps.go @@ -101,7 +101,7 @@ func main() { } return case "update": - install.Update() + install.UpdateNps() return default: logs.Error("command is not support") diff --git a/go.mod b/go.mod index 18cfc2e..141d73c 100644 --- a/go.mod +++ b/go.mod @@ -6,16 +6,17 @@ require ( fyne.io/fyne v1.2.0 github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect github.com/astaxie/beego v1.12.0 + github.com/bradfitz/iter v0.0.0-20190303215204-33e6a9893b0c // indirect github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c github.com/ccding/go-stun v0.0.0-20180726100737-be486d185f3d github.com/dsnet/compress v0.0.1 // indirect github.com/go-ole/go-ole v1.2.4 // indirect github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db - github.com/kardianos/service v1.0.0 // indirect + github.com/hooklift/assert v0.0.0-20170704181755-9d1defd6d214 // indirect + github.com/kardianos/service v1.0.0 github.com/klauspost/cpuid v1.2.1 // indirect github.com/klauspost/pgzip v1.2.1 // indirect github.com/klauspost/reedsolomon v1.9.2 // indirect - github.com/luisiturrios/gowin v0.0.0-20150312095849-89dcf1aa3c14 // indirect github.com/panjf2000/ants/v2 v2.2.2 github.com/pkg/errors v0.8.1 github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect diff --git a/go.sum b/go.sum index 10b69da..a1c8f67 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkK github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd/go.mod h1:1b+Y/CofkYwXMUU0OhQqGvsY2Bvgr4j6jfT699wyZKQ= github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542/go.mod h1:kSeGC/p1AbBiEp5kat81+DSQrZenVBZXklMLaELspWU= github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60= +github.com/bradfitz/iter v0.0.0-20190303215204-33e6a9893b0c h1:FUUopH4brHNO2kJoNN3pV+OBEYmgraLT/KHZrMM69r0= +github.com/bradfitz/iter v0.0.0-20190303215204-33e6a9893b0c/go.mod h1:PyRFw1Lt2wKX4ZVSQ2mk+PeDa1rxyObEDlApuIsUKuo= github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c h1:aprLqMn7gSPT+vdDSl+/E6NLEuArwD/J7IWd8bJt5lQ= github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c/go.mod h1:Ie6SubJv/NTO9Q0UBH0QCl3Ve50lu9hjbi5YJUw03TE= github.com/casbin/casbin v1.7.0/go.mod h1:c67qKN6Oum3UF5Q1+BByfFxkwKvhwW57ITjqwtzR1KE= @@ -31,8 +33,6 @@ github.com/exfly/beego v1.12.0-export-init h1:VQNYKdXhAwZGUaFmQv8Aj921O3rQJZRIF8 github.com/exfly/beego v1.12.0-export-init/go.mod h1:fysx+LZNZKnvh4GED/xND7jWtjCR6HzydR2Hh2Im57o= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/gen2brain/go-unarr v0.0.0-20190203132630-dd30f8db8e40 h1:rnd7FAy39Y3+YOboqzQmGj62n9lJMoMr1TlIc1FTd/o= -github.com/gen2brain/go-unarr v0.0.0-20190203132630-dd30f8db8e40/go.mod h1:29wlntTq0xoOBDzoKlxLhCDh/G2GqMBam0P17/tMslE= github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw= github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk= github.com/go-gl/glfw v0.0.0-20181213070059-819e8ce5125f h1:7MsFMbSn8Lcw0blK4+NEOf8DuHoOBDhJsHz04yh13pM= @@ -47,6 +47,8 @@ github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff/go.mod h1:wfqRWLHRBs github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= +github.com/hooklift/assert v0.0.0-20170704181755-9d1defd6d214 h1:WgfvpuKg42WVLkxNwzfFraXkTXPK36bMqXvMFN67clI= +github.com/hooklift/assert v0.0.0-20170704181755-9d1defd6d214/go.mod h1:kj6hFWqfwSjFjLnYW5PK1DoxZ4O0uapwHRmd9jhln4E= github.com/jackmordaunt/icns v0.0.0-20181231085925-4f16af745526/go.mod h1:UQkeMHVoNcyXYq9otUupF7/h/2tmHlhrS2zw7ZVvUqc= github.com/josephspurrier/goversioninfo v0.0.0-20190124120936-8611f5a5ff3f/go.mod h1:eJTEwMjXb7kZ633hO3Ln9mBUCOjX2+FlTljvpl9SYdE= github.com/kardianos/service v1.0.0 h1:HgQS3mFfOlyntWX8Oke98JcJLqt1DBcHR4kxShpYef0= @@ -61,8 +63,6 @@ github.com/klauspost/pgzip v1.2.1/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQ github.com/klauspost/reedsolomon v1.9.2 h1:E9CMS2Pqbv+C7tsrYad4YC9MfhnMVWhMRsTi7U0UB18= github.com/klauspost/reedsolomon v1.9.2/go.mod h1:CwCi+NUr9pqSVktrkN+Ondf06rkhYZ/pcNv7fu+8Un4= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/luisiturrios/gowin v0.0.0-20150312095849-89dcf1aa3c14 h1:v2RpRAI7mSnyET19MZUU+3mi8S21HZHk2vBDYp1CClA= -github.com/luisiturrios/gowin v0.0.0-20150312095849-89dcf1aa3c14/go.mod h1:I2wAi7YNrvMJWpvtZ7MOn1Q7B46zYRQKC8vXZF478z8= github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= diff --git a/lib/install/install.go b/lib/install/install.go index ef07d95..66bb28b 100644 --- a/lib/install/install.go +++ b/lib/install/install.go @@ -16,15 +16,25 @@ import ( "strings" ) -func Update() { - downloadLatest() +func UpdateNps() { + destPath := downloadLatest("server") + //复制文件到对应目录 + copyStaticFile(destPath, "nps") + fmt.Println("Update completed, please restart") +} + +func UpdateNpc() { + destPath := downloadLatest("client") + //复制文件到对应目录 + copyStaticFile(destPath, "npc") + fmt.Println("Update completed, please restart") } type release struct { TagName string `json:"tag_name"` } -func downloadLatest() { +func downloadLatest(bin string) string { // get version data, err := http.Get("https://api.github.com/repos/cnlh/nps/releases/latest") if err != nil { @@ -38,7 +48,7 @@ func downloadLatest() { json.Unmarshal(b, &rl) version := rl.TagName fmt.Println("the latest version is", version) - filename := runtime.GOOS + "_" + runtime.GOARCH + "_server" + ".tar.gz" + filename := runtime.GOOS + "_" + runtime.GOARCH + "_" + bin + ".tar.gz" // download latest package downloadUrl := fmt.Sprintf("https://github.com/cnlh/nps/releases/download/%s/%s", version, filename) fmt.Println("download package from ", downloadUrl) @@ -50,47 +60,63 @@ func downloadLatest() { if err != nil { log.Fatal(err) } - destPath = strings.Replace(destPath, "/web", "", -1) - destPath = strings.Replace(destPath, `\web`, "", -1) - destPath = strings.Replace(destPath, "/views", "", -1) - destPath = strings.Replace(destPath, `\views`, "", -1) - //复制文件到对应目录 - copyStaticFile(destPath) - fmt.Println("Update completed, please restart") + 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 copyStaticFile(srcPath string) string { +func copyStaticFile(srcPath, bin string) string { path := common.GetInstallPath() - //复制文件到对应目录 - if err := CopyDir(filepath.Join(srcPath, "web", "views"), filepath.Join(path, "web", "views")); err != nil { - log.Fatalln(err) + if bin == "nps" { + //复制文件到对应目录 + if err := CopyDir(filepath.Join(srcPath, "web", "views"), filepath.Join(path, "web", "views")); err != nil { + log.Fatalln(err) + } + chMod(filepath.Join(path, "web", "views"), 0766) + if err := CopyDir(filepath.Join(srcPath, "web", "static"), filepath.Join(path, "web", "static")); err != nil { + log.Fatalln(err) + } + chMod(filepath.Join(path, "web", "static"), 0766) } - chMod(filepath.Join(path, "web", "views"), 0766) - if err := CopyDir(filepath.Join(srcPath, "web", "static"), filepath.Join(path, "web", "static")); err != nil { - log.Fatalln(err) - } - chMod(filepath.Join(path, "web", "static"), 0766) binPath, _ := filepath.Abs(os.Args[0]) if !common.IsWindows() { - if _, err := copyFile(filepath.Join(srcPath, "nps"), "/usr/bin/nps"); err != nil { - if _, err := copyFile(filepath.Join(srcPath, "nps"), "/usr/local/bin/nps"); err != nil { + 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 { - copyFile(filepath.Join(srcPath, "nps"), "/usr/local/bin/nps-update") - binPath = "/usr/local/bin/nps" + copyFile(filepath.Join(srcPath, "nps"), "/usr/local/bin/"+bin+"-update") + binPath = "/usr/local/bin/" + bin } } else { - copyFile(filepath.Join(srcPath, "nps"), "/usr/bin/nps-update") - binPath = "/usr/bin/nps" + copyFile(filepath.Join(srcPath, "nps"), "/usr/bin/"+bin+"-update") + binPath = "/usr/bin/" + bin } } else { - copyFile(filepath.Join(srcPath, "nps.exe"), filepath.Join(common.GetAppPath(), "nps-update.exe")) - copyFile(filepath.Join(srcPath, "nps.exe"), filepath.Join(common.GetAppPath(), "nps.exe")) + copyFile(filepath.Join(srcPath, bin+".exe"), filepath.Join(common.GetAppPath(), bin+"-update.exe")) + copyFile(filepath.Join(srcPath, bin+".exe"), filepath.Join(common.GetAppPath(), bin+".exe")) } chMod(binPath, 0755) return binPath } +func InstallNpc() { + path := common.GetInstallPath() + if !common.FileExists(path) { + err := os.Mkdir(path, 0755) + if err != nil { + log.Fatal(err) + } + } + copyStaticFile(common.GetAppPath(), "npc") +} + func InstallNps() string { path := common.GetInstallPath() if common.FileExists(path) { @@ -103,7 +129,7 @@ func InstallNps() string { } chMod(filepath.Join(path, "conf"), 0766) } - binPath := copyStaticFile(common.GetAppPath()) + binPath := copyStaticFile(common.GetAppPath(), "nps") log.Println("install ok!") log.Println("Static files and configuration files in the current directory will be useless") log.Println("The new configuration file is located in", path, "you can edit them")