diff --git a/README.md b/README.md index 3174e96..f84c482 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,12 @@ https://natnps.com/ # 更新日志 +- 2025-05-28 v0.26.25 + 新增: + - nps增加`nps(.exe) -server` 命令,用于管理NPS服务,安装和卸载服务在 Linux 下需要有 sudo 权限,Windows 下需要有管理员权限。![img.png](image/new/server.png) + - 增加【TLS快捷启动命令】,可用于在tls模式下,快速启动客户端。 [257](https://github.com/yisier/nps/issues/257) + + - 2025-04-16 v0.26.24 新增: - 隧道支持复制功能,在隧道页面增加复制按钮,除了端口号为随机生成外,其他参数全部拷贝。[251](https://github.com/yisier/nps/issues/251) diff --git a/build.sh b/build.sh index 4234f32..7e736b3 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,5 @@ #/bash/sh -export VERSION=0.26.24 +export VERSION=0.26.25 export GOPROXY=direct sudo apt-get update diff --git a/cmd/npc/npc.go b/cmd/npc/npc.go index cbea6d7..6167fde 100644 --- a/cmd/npc/npc.go +++ b/cmd/npc/npc.go @@ -344,14 +344,25 @@ func inputCmd() { func startNpcServer(startCmd string) { var serAddr string var vkey string + var tls string array := strings.Fields(startCmd) serAddr = array[0] vkey = array[1] - + if len(array) > 2 { + tls = array[2] + } go func() { for { - logs.Info("start cmd:-server=" + serAddr + " -vkey=" + vkey) - logs.Info("the version of client is %s, the core version of client is %s", version.VERSION, version.GetVersion()) + if tls == "-tls_enable=true" { + client.SetTlsEnable(true) + logs.Info("start cmd:-server=" + serAddr + " -vkey=" + vkey + " " + tls) + logs.Info("the version of client is %s, the core version of client is %s,tls enable is %t", version.VERSION, version.GetVersion(), client.GetTlsEnable()) + } else { + client.SetTlsEnable(false) + logs.Info("start cmd:-server=" + serAddr + " -vkey=" + vkey) + logs.Info("the version of client is %s, the core version of client is %s", version.VERSION, version.GetVersion()) + } + client.NewRPClient(serAddr, vkey, *connType, *proxyUrl, nil, *disconnectTime).Start() logs.Info("Client closed! It will be reconnected in five seconds") time.Sleep(time.Second * 5) diff --git a/cmd/nps/nps.go b/cmd/nps/nps.go index e54cccc..e3325b9 100644 --- a/cmd/nps/nps.go +++ b/cmd/nps/nps.go @@ -1,9 +1,13 @@ package main import ( + "bufio" "ehang.io/nps/bridge" "ehang.io/nps/lib/daemon" + "ehang.io/nps/server" "flag" + "fmt" + "github.com/fatih/color" "log" "os" "os/exec" @@ -16,7 +20,6 @@ import ( "ehang.io/nps/lib/file" "ehang.io/nps/lib/install" "ehang.io/nps/lib/version" - "ehang.io/nps/server" "ehang.io/nps/server/connection" "ehang.io/nps/server/tool" "ehang.io/nps/web/routers" @@ -30,9 +33,11 @@ import ( ) var ( - level string - ver = flag.Bool("version", false, "show current version") - confPath = flag.String("conf_path", "", "set current confPath") + level string + ver = flag.Bool("version", false, "show current version") + confPath = flag.String("conf_path", "", "set current confPath") + serverCmd = flag.Bool("server", false, "NPS管理脚本") + npsLogPath = flag.String("log_path", "", "nps log path") ) func main() { @@ -45,7 +50,13 @@ func main() { common.PrintVersion() return } + if *serverCmd { + printSlogan() + inputCmd() + return + } + var logPath string // *confPath why get null value ? for _, v := range os.Args[1:] { switch v { @@ -55,6 +66,10 @@ func main() { if strings.Contains(v, "-conf_path=") { common.ConfPath = strings.Replace(v, "-conf_path=", "", -1) } + + if strings.Contains(v, "-log_path=") { + logPath = strings.Replace(v, "-log_path=", "", -1) + } } if err := beego.LoadAppConfig("ini", filepath.Join(common.GetRunPath(), "conf", "nps.conf")); err != nil { @@ -68,13 +83,17 @@ func main() { logs.Reset() logs.EnableFuncCallDepth(true) logs.SetLogFuncCallDepth(3) - logPath := beego.AppConfig.String("log_path") + if logPath == "" { - logPath = common.GetLogPath() - } - if common.IsWindows() { - logPath = strings.Replace(logPath, "\\", "\\\\", -1) + logPath := beego.AppConfig.String("log_path") + if logPath == "" { + logPath = common.GetLogPath() + } + if common.IsWindows() { + logPath = strings.Replace(logPath, "\\", "\\\\", -1) + } } + // init service options := make(service.KeyValue) svcConfig := &service.Config{ @@ -186,6 +205,185 @@ func main() { _ = s.Run() } +func printSlogan() { + green := color.New(color.FgGreen).SprintFunc() + // 第一次输入,如果输入 1,2,3,4 则需要输入秘钥,否则 + + fmt.Printf("%s", green("")) + + fmt.Printf("\033[32;0m欢迎使用 NPS 管理脚本 \n") + fmt.Printf("\033[0m") // 重置颜色 + + fmt.Printf("\n") + + fmt.Printf("\u001B[32m输入[1]\u001B[0m - 安装 NPS\n") + fmt.Printf("\u001B[32m输入[2]\u001B[0m - 卸载 NPS\n") + fmt.Printf("\u001B[32m输入[3]\u001B[0m - 更新 NPS\n") + fmt.Printf("---------------------\n") + fmt.Printf("\u001B[32m输入[4]\u001B[0m - 查看状态\n") + fmt.Printf("---------------------\n") + fmt.Printf("\u001B[32m输入[5]\u001B[0m - 启动 NPS\n") + fmt.Printf("\u001B[32m输入[6]\u001B[0m - 停止 NPS\n") + fmt.Printf("\u001B[32m输入[7]\u001B[0m - 重启 NPS\n") + fmt.Printf("---------------------\n") + fmt.Printf("\u001B[32m输入[0]\u001B[0m - 退出脚本\n") + fmt.Printf("---------------------\n") + fmt.Printf("\n") + +} + +func inputCmd() { + var flag string + fmt.Printf("请输入[0-7]:") + + stdin := bufio.NewReader(os.Stdin) + _, err := fmt.Fscanln(stdin, &flag) + if err != nil { + fmt.Println("输入有误") + } else { + if flag == "0" { + os.Exit(0) + } + + // init service + + prg := &nps{ + exit: make(chan struct{}), + } + options := make(service.KeyValue) + svcConfig := &service.Config{ + Name: "Nps", + DisplayName: "nps内网穿透代理服务器", + Description: "一款轻量级、功能强大的内网穿透代理服务器。支持tcp、udp流量转发,支持内网http代理、内网socks5代理,同时支持snappy压缩、站点保护、加密传输、多路复用、header修改等。支持web图形化管理,集成多用户模式。", + Option: options, + } + s, _ := service.New(prg, svcConfig) + + switch flag { + case "1": + // uninstall before + _ = service.Control(s, "stop") + _ = service.Control(s, "uninstall") + binPath := install.InstallNpsToCurrentDir() + + beego.LoadAppConfig("ini", filepath.Join(common.GetAppPath(), "conf", "nps.conf")) + + logPath := filepath.Join(common.GetAppPath(), "nps.log") + if common.IsWindows() { + logPath = strings.Replace(logPath, "\\", "\\\\", -1) + } + svcConfig.Arguments = append(svcConfig.Arguments, "service") + svcConfig.Arguments = append(svcConfig.Arguments, "-conf_path="+common.GetAppPath()) + svcConfig.Arguments = append(svcConfig.Arguments, "-log_path="+logPath) + + fmt.Println("日志文件路径为:", logPath) + + svcConfig.Executable = binPath + s, err := service.New(prg, svcConfig) + + if service.Platform() == "unix-systemv" { + logs.Info("unix-systemv service") + confPath := "/etc/init.d/" + svcConfig.Name + os.Symlink(confPath, "/etc/rc.d/S90"+svcConfig.Name) + os.Symlink(confPath, "/etc/rc.d/K02"+svcConfig.Name) + } + + err = service.Control(s, "install") + if err != nil { + logs.Error("Valid actions: %q\n%s", service.ControlAction, err.Error()) + } else { + fmt.Println("NPS服务安装成功") + } + + err = service.Control(s, "start") + if err != nil { + fmt.Println("启动NPS服务失败", err) + } else { + fmt.Println("NPS服务已启动,管理面板访问地址:127.0.0.1:" + beego.AppConfig.String("web_port")) + } + + break + case "2": + // 卸载系统服务 + err := service.Control(s, "stop") + if err != nil { + fmt.Println("NPS服务停止失败", err) + } else { + fmt.Println("NPS服务已停止") + } + + err = service.Control(s, "uninstall") + if err != nil { + logs.Error("NPS服务卸载失败") + } + if service.Platform() == "unix-systemv" { + logs.Info("unix-systemv service") + os.Remove("/etc/rc.d/S90" + svcConfig.Name) + os.Remove("/etc/rc.d/K02" + svcConfig.Name) + } + + if err == nil { + fmt.Println("NPS服务已卸载成功") + } + break + case "3": + install.UpdateNpsNew() + return + case "4": + // 查看状态 + var statusMsg = "" + status, err := s.Status() + if err != nil { + statusMsg = "\u001B[31m未运行\u001B[0m" + } else { + if status == 1 { + statusMsg = "\u001B[32m运行中\u001B[0m" + } else { + statusMsg = "\u001B[31m未运行\u001B[0m" + } + } + fmt.Println("NPS服务状态:" + statusMsg) + break + case "5": + // 启动 NPS + err := service.Control(s, "start") + if err != nil { + fmt.Println("NPS服务启动失败", err) + } else { + fmt.Println("NPS服务启动成功") + } + + break + case "6": + // 停止 NPS + err := service.Control(s, "stop") + if err != nil { + fmt.Println("NPS服务停止失败", err) + } else { + fmt.Println("NPS服务停止成功") + } + + break + case "7": + // 重启 NPS + err := service.Control(s, "restart") + if err != nil { + fmt.Println("NPS服务重启失败", err) + } else { + fmt.Println("NPS服务重启成功") + } + + break + } + } + + inputCmd() +} + +func installNps() { + +} + type nps struct { exit chan struct{} } @@ -232,6 +430,7 @@ func run() { os.Exit(0) } + logs.Info("日志路径111:" + *npsLogPath) logs.Info("the config path is:" + common.GetRunPath()) logs.Info("the version of server is %s ,allow client core version to be %s,tls enable is %t", version.VERSION, version.GetVersion(), bridge.ServerTlsEnable) connection.InitConnectionService() diff --git a/image/new/server.png b/image/new/server.png new file mode 100644 index 0000000..cb10d18 Binary files /dev/null and b/image/new/server.png differ diff --git a/lib/common/run.go b/lib/common/run.go index 7bd091b..da99734 100644 --- a/lib/common/run.go +++ b/lib/common/run.go @@ -8,10 +8,14 @@ import ( var ConfPath string -//Get the currently selected configuration file directory -//For non-Windows systems, select the /etc/nps as config directory if exist, or select ./ -//windows system, select the C:\Program Files\nps as config directory if exist, or select ./ +// Get the currently selected configuration file directory +// For non-Windows systems, select the /etc/nps as config directory if exist, or select ./ +// windows system, select the C:\Program Files\nps as config directory if exist, or select ./ func GetRunPath() string { + if ConfPath != "" { + return ConfPath + } + var path string if len(os.Args) == 1 { if !IsWindows() { @@ -28,7 +32,7 @@ func GetRunPath() string { return path } -//Different systems get different installation paths +// Different systems get different installation paths func GetInstallPath() string { var path string @@ -45,7 +49,7 @@ func GetInstallPath() string { return path } -//Get the absolute path to the running directory +// Get the absolute path to the running directory func GetAppPath() string { if path, err := filepath.Abs(filepath.Dir(os.Args[0])); err == nil { return path @@ -53,7 +57,7 @@ func GetAppPath() string { return os.Args[0] } -//Determine whether the current system is a Windows system? +// Determine whether the current system is a Windows system? func IsWindows() bool { if runtime.GOOS == "windows" { return true @@ -61,7 +65,7 @@ func IsWindows() bool { return false } -//interface log file path +// interface log file path func GetLogPath() string { var path string if IsWindows() { @@ -72,7 +76,13 @@ func GetLogPath() string { return path } -//interface npc log file path +func GetLogPathCurrentPath() string { + var path string + path = filepath.Join(GetAppPath(), "nps.log") + return path +} + +// interface npc log file path func GetNpcLogPath() string { var path string if IsWindows() { @@ -83,7 +93,7 @@ func GetNpcLogPath() string { return path } -//interface pid file path +// interface pid file path func GetTmpPath() string { var path string if IsWindows() { @@ -94,7 +104,7 @@ func GetTmpPath() string { return path } -//config file path +// config file path func GetConfigPath() string { var path string if IsWindows() { diff --git a/lib/install/install.go b/lib/install/install.go index 4d970ac..b37ccfd 100644 --- a/lib/install/install.go +++ b/lib/install/install.go @@ -141,6 +141,13 @@ func UpdateNps() { fmt.Println("Update completed, please restart") } +func UpdateNpsNew() { + destPath := downloadLatest2("server", filepath.Join(common.GetAppPath(), "temp")) + //复制文件到对应目录 + copyStaticFileReplaceNps(destPath, common.GetAppPath()) + fmt.Println("更新成功,请重启服务") +} + func UpdateNpc() { destPath := downloadLatest("client") //复制文件到对应目录 @@ -190,6 +197,43 @@ func downloadLatest(bin string) string { return destPath } +func downloadLatest2(bin string, path 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 + ".tar.gz" + // 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, path) + 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 copyStaticFile(srcPath, bin string) string { path := common.GetInstallPath() if bin == "nps" { @@ -226,6 +270,22 @@ func copyStaticFile(srcPath, bin string) string { return binPath } +func copyStaticFileReplaceNps(srcPath, descPath string) string { + //复制文件到对应目录 + os.Rename(filepath.Join(srcPath, "web"), filepath.Join(descPath, "web")) + chMod(filepath.Join(descPath, "web"), 0766) + + 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 +} + func InstallNpc() { path := common.GetInstallPath() if !common.FileExists(path) { @@ -266,6 +326,21 @@ now!`) chMod(common.GetLogPath(), 0777) return binPath } + +func InstallNpsToCurrentDir() string { + path := common.GetAppPath() + log.Println("install path:" + path) + log.Println("install ok!") + chMod(filepath.Join(path, "nps.log"), 0777) + + if !common.IsWindows() { + path = filepath.Join(path, "nps") + } else { + path = filepath.Join(path, "nps.exe") + } + return path +} + func MkidrDirAll(path string, v ...string) { for _, item := range v { if err := os.MkdirAll(filepath.Join(path, item), 0755); err != nil { @@ -310,7 +385,7 @@ func CopyDir(srcPath string, destPath string) error { return err } -//生成目录并拷贝文件 +// 生成目录并拷贝文件 func copyFile(src, dest string) (w int64, err error) { srcFile, err := os.Open(src) if err != nil { @@ -345,7 +420,7 @@ func copyFile(src, dest string) (w int64, err error) { return io.Copy(dstFile, srcFile) } -//检测文件夹路径时候存在 +// 检测文件夹路径时候存在 func pathExists(path string) (bool, error) { _, err := os.Stat(path) if err == nil { diff --git a/lib/version/version.go b/lib/version/version.go index 423bdac..59a9911 100644 --- a/lib/version/version.go +++ b/lib/version/version.go @@ -1,6 +1,6 @@ package version -const VERSION = "0.26.24" +const VERSION = "0.26.25" // Compulsory minimum version, Minimum downward compatibility to this version func GetVersion() string { diff --git a/web/static/page/languages.xml b/web/static/page/languages.xml index 6b3958b..bd7b759 100644 --- a/web/static/page/languages.xml +++ b/web/static/page/languages.xml @@ -198,6 +198,10 @@ 快捷启动命令 Quickly command + + TLS快捷启动命令 + TLS Quickly command + 客户端命令 diff --git a/web/views/client/list.html b/web/views/client/list.html index 88d4a4a..ab58ddb 100644 --- a/web/views/client/list.html +++ b/web/views/client/list.html @@ -74,6 +74,7 @@ + ': ' + row.CreateTime + ' 

' + ': ' + row.LastOnlineTime + ' 

' + ': ' + encodeToBase64('{{.ip}}:{{.p}} ' + row.VerifyKey) + ' 
' + + ': ' + encodeToBase64('{{.ip}}:{{.tls_p}} ' + row.VerifyKey + ' -tls_enable=true') + ' 
' + ': ' + "./npc{{.win}} -server={{.ip}}:{{.p}} -vkey=" + row.VerifyKey + " -type=" +{{.bridgeType}} +"
" + ': ' + "./npc{{.win}} -server={{.ip}}:{{.tls_p}} -vkey=" + row.VerifyKey + " -tls_enable=true" },