diff --git a/README.md b/README.md index c8ead20..9105b6f 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,20 @@ ## 更新日志 +- 2023-02-24 v0.26.15 + ***修复***:nps 在外部路径启动时找不到配置文件 + ***新增***:增加 nps 启动参数,`-conf_path=D:\test\nps`,可用于加载指定nps配置文件和web文件目录。 + window 使用示例: +- 启动:`nps.exe -conf_path=D:\test\nps` +- 安装:`nps.exe install -conf_path=D:\test\nps` + - 安装启动:`nps start` + linux 使用示例: +- 直接启动:`./nps -conf_path=/app/nps` +- 安装:`./nps install -conf_path=/app/nps` +- 安装启动:`nps start -conf_path=/app/nps` + + + - 2022-12-30 v0.26.14 ***修复***:API 鉴权漏洞修复 diff --git a/cmd/nps/nps.go b/cmd/nps/nps.go index 5b1c5b4..bb95acd 100644 --- a/cmd/nps/nps.go +++ b/cmd/nps/nps.go @@ -28,8 +28,9 @@ import ( ) var ( - level string - ver = flag.Bool("version", false, "show current version") + level string + ver = flag.Bool("version", false, "show current version") + confPath = flag.String("conf_path", "", "set current confPath") ) func main() { @@ -40,6 +41,18 @@ func main() { common.PrintVersion() return } + + // *confPath why get null value ? + for _, v := range os.Args[1:] { + switch v { + case "install", "start", "stop", "uninstall", "restart": + continue + } + if strings.Contains(v, "-conf_path=") { + common.ConfPath = strings.Replace(v, "-conf_path=", "", -1) + } + } + if err := beego.LoadAppConfig("ini", filepath.Join(common.GetRunPath(), "conf", "nps.conf")); err != nil { log.Fatalln("load config file error", err.Error()) } @@ -66,6 +79,15 @@ func main() { Description: "一款轻量级、功能强大的内网穿透代理服务器。支持tcp、udp流量转发,支持内网http代理、内网socks5代理,同时支持snappy压缩、站点保护、加密传输、多路复用、header修改等。支持web图形化管理,集成多用户模式。", Option: options, } + + for _, v := range os.Args[1:] { + switch v { + case "install", "start", "stop", "uninstall", "restart": + continue + } + svcConfig.Arguments = append(svcConfig.Arguments, v) + } + svcConfig.Arguments = append(svcConfig.Arguments, "service") if len(os.Args) > 1 && os.Args[1] == "service" { _ = logs.SetLogger(logs.AdapterFile, `{"level":`+level+`,"filename":"`+logPath+`","daily":false,"maxlines":100000,"color":true}`) @@ -149,9 +171,9 @@ func main() { case "update": install.UpdateNps() return - default: - logs.Error("command is not support") - return + //default: + // logs.Error("command is not support") + // return } } @@ -203,6 +225,8 @@ func run() { logs.Error("Getting bridge_port error", err) os.Exit(0) } + + logs.Info("the config path is:" + common.GetRunPath()) logs.Info("the version of server is %s ,allow client core version to be %s", version.VERSION, version.GetVersion()) connection.InitConnectionService() //crypt.InitTls(filepath.Join(common.GetRunPath(), "conf", "server.pem"), filepath.Join(common.GetRunPath(), "conf", "server.key")) diff --git a/lib/common/run.go b/lib/common/run.go index 6370071..7bd091b 100644 --- a/lib/common/run.go +++ b/lib/common/run.go @@ -6,13 +6,20 @@ import ( "runtime" ) +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 ./ func GetRunPath() string { var path string - if len(os.Args) <= 1 { - return "./" + if len(os.Args) == 1 { + if !IsWindows() { + dir, _ := filepath.Abs(filepath.Dir(os.Args[0])) //返回 + return dir + "/" + } else { + return "./" + } } else { if path = GetInstallPath(); !FileExists(path) { return GetAppPath() @@ -24,6 +31,11 @@ func GetRunPath() string { //Different systems get different installation paths func GetInstallPath() string { var path string + + if ConfPath != "" { + return ConfPath + } + if IsWindows() { path = `C:\Program Files\nps` } else {