feat(nps): 添加配置文件自动生成功能

- 启动时自动检查并创建 conf 目录和 nps.conf 配置文件
- 配置文件中的 web_password、auth_key、auth_crypt_key 均随机生成
- 打印默认登录凭据到控制台,提升部署便利性
- 修改 loadSyncMapFromFile 函数避免文件不存在时的 panic 错误
- 更新 README.md 添加新版本更新日志说明
This commit is contained in:
yisier
2026-05-23 14:34:34 -06:00
parent d199b26ce2
commit 2650174eed
3 changed files with 96 additions and 0 deletions
+5
View File
@@ -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)
+88
View File
@@ -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
`
+3
View File
@@ -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)