From 2650174eeda2cfe01187bdd94428fb87c2edb36d Mon Sep 17 00:00:00 2001 From: yisier <1340976576@qq.com> Date: Sat, 23 May 2026 14:34:34 -0600 Subject: [PATCH] =?UTF-8?q?feat(nps):=20=E6=B7=BB=E5=8A=A0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 启动时自动检查并创建 conf 目录和 nps.conf 配置文件 - 配置文件中的 web_password、auth_key、auth_crypt_key 均随机生成 - 打印默认登录凭据到控制台,提升部署便利性 - 修改 loadSyncMapFromFile 函数避免文件不存在时的 panic 错误 - 更新 README.md 添加新版本更新日志说明 --- README.md | 5 +++ cmd/nps/nps.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/file/file.go | 3 ++ 3 files changed, 96 insertions(+) diff --git a/README.md b/README.md index c16b8e6..993ffc7 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,11 @@ # 更新日志 +- 2026-05-23 v0.26.33 + - 新增: + - 配置文件自动生成:启动时若 conf 目录或 nps.conf 不存在,自动创建并写入默认配置,方便 Docker 部署 + - 默认 web_username/admin、web_password、auth_key、auth_crypt_key 均改为随机生成,并打印到终端,提升安全性 + - 2026-03-27 v0.26.32 - 修复: - 客户端注册参数未正确处理[快捷启动命令]和[TLS快捷启动命令] [#303](https://github.com/yisier/nps/issues/303) diff --git a/cmd/nps/nps.go b/cmd/nps/nps.go index e6eda79..26cdadd 100644 --- a/cmd/nps/nps.go +++ b/cmd/nps/nps.go @@ -72,6 +72,9 @@ func main() { } } + // auto-generate default config files if not exist + initConfig(filepath.Join(common.GetRunPath(), "conf")) + if err := beego.LoadAppConfig("ini", filepath.Join(common.GetRunPath(), "conf", "nps.conf")); err != nil { log.Fatalln("load config file error", err.Error()) } @@ -444,3 +447,88 @@ func run() { } go server.StartNewServer(bridgePort, task, beego.AppConfig.String("bridge_type"), timeout) } + +func initConfig(confDir string) { + if !common.FileExists(confDir) { + os.MkdirAll(confDir, 0755) + } + confPath := filepath.Join(confDir, "nps.conf") + if !common.FileExists(confPath) { + webPassword := crypt.GetRandomString(8) + authKey := crypt.GetRandomString(8) + authCryptKey := crypt.GetRandomString(16) + content := strings.Replace(defaultNpsConf, "web_password=123", "web_password="+webPassword, 1) + content = strings.Replace(content, "auth_key=123", "auth_key="+authKey, 1) + content = strings.Replace(content, "auth_crypt_key =213", "auth_crypt_key ="+authCryptKey, 1) + f, err := os.Create(confPath) + if err != nil { + return + } + defer f.Close() + f.WriteString(content) + logs.Info("Auto-generated default config file:", confPath) + logs.Info("Web login username: admin, password:", webPassword) + logs.Info("auth_key:", authKey) + logs.Info("auth_crypt_key:", authCryptKey) + } +} + +const defaultNpsConf = `appname = nps +runmode = dev + +http_proxy_ip=0.0.0.0 +http_proxy_port=80 +https_proxy_port=443 +https_just_proxy=true +https_default_cert_file=conf/server.pem +https_default_key_file=conf/server.key +show_http_proxy_port=true + +bridge_type=tcp +bridge_port=8024 +bridge_ip=0.0.0.0 + +public_vkey=123 + +flow_store_interval=1 + +log_level=6 +log_path=nps.log + +web_host=a.o.com +web_username=admin +web_password=123 +web_port = 8081 +web_ip=0.0.0.0 +web_base_url= +web_open_ssl=false +web_cert_file=conf/server.pem +web_key_file=conf/server.key + +auth_key=123 +auth_crypt_key =213 + +allow_user_login=true +allow_user_register=false +allow_user_change_username=true + +allow_flow_limit=true +allow_rate_limit=true +allow_tunnel_num_limit=true +allow_local_proxy=false +allow_connection_num_limit=true +allow_multi_ip=true +system_info_display=true + +http_add_origin_header=true + +http_cache=false +http_cache_length=100 + +disconnect_timeout=60 + +open_captcha=false + +tls_enable=true +tls_bridge_port=8025 +` diff --git a/lib/file/file.go b/lib/file/file.go index 00711cf..39de89d 100644 --- a/lib/file/file.go +++ b/lib/file/file.go @@ -158,6 +158,9 @@ func (s *JsonDb) GetHostId() int32 { } func loadSyncMapFromFile(filePath string, f func(value string)) { + if !common.FileExists(filePath) { + return + } b, err := common.ReadAllFromFile(filePath) if err != nil { panic(err)