2019-12-08 03:59:58 -05:00
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
2020-12-09 06:05:40 -05:00
|
|
|
|
"os"
|
2024-11-25 07:18:30 -05:00
|
|
|
|
"path/filepath"
|
2021-04-07 09:11:59 -04:00
|
|
|
|
"strings"
|
2019-12-20 10:58:09 -05:00
|
|
|
|
|
2025-02-28 09:02:54 -05:00
|
|
|
|
"github.com/go-viper/mapstructure/v2"
|
2025-03-03 06:02:25 -05:00
|
|
|
|
kmaps "github.com/knadh/koanf/maps"
|
2024-11-23 12:07:14 -05:00
|
|
|
|
"github.com/knadh/koanf/providers/env"
|
|
|
|
|
"github.com/knadh/koanf/providers/file"
|
|
|
|
|
"github.com/knadh/koanf/v2"
|
2025-03-03 06:02:25 -05:00
|
|
|
|
"sigs.k8s.io/yaml"
|
2024-11-23 12:07:14 -05:00
|
|
|
|
|
2024-11-28 06:38:54 -05:00
|
|
|
|
"github.com/nezhahq/nezha/pkg/utils"
|
2019-12-08 03:59:58 -05:00
|
|
|
|
)
|
|
|
|
|
|
2021-06-22 02:05:36 -04:00
|
|
|
|
const (
|
2024-11-21 11:19:36 -05:00
|
|
|
|
ConfigUsePeerIP = "NZ::Use-Peer-IP"
|
|
|
|
|
ConfigCoverAll = iota
|
2021-06-22 02:05:36 -04:00
|
|
|
|
ConfigCoverIgnoreAll
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-28 10:50:59 -05:00
|
|
|
|
type ConfigForGuests struct {
|
2025-03-03 06:02:25 -05:00
|
|
|
|
Language string `koanf:"language" json:"language"` // 系统语言,默认 zh_CN
|
|
|
|
|
SiteName string `koanf:"site_name" json:"site_name"`
|
|
|
|
|
CustomCode string `koanf:"custom_code" json:"custom_code,omitempty"`
|
|
|
|
|
CustomCodeDashboard string `koanf:"custom_code_dashboard" json:"custom_code_dashboard,omitempty"`
|
2025-02-28 09:02:54 -05:00
|
|
|
|
|
|
|
|
|
InstallHost string `koanf:"install_host" json:"install_host,omitempty"`
|
|
|
|
|
AgentTLS bool `koanf:"tls" json:"tls,omitempty"` // 用于前端判断生成的安装命令是否启用 TLS
|
2024-12-28 10:50:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-28 09:02:54 -05:00
|
|
|
|
type ConfigDashboard struct {
|
2025-03-03 06:02:25 -05:00
|
|
|
|
RealIPHeader string `koanf:"real_ip_header" json:"real_ip_header,omitempty"` // 真实IP
|
|
|
|
|
UserTemplate string `koanf:"user_template" json:"user_template,omitempty"`
|
|
|
|
|
AdminTemplate string `koanf:"admin_template" json:"admin_template,omitempty"`
|
2025-02-28 09:02:54 -05:00
|
|
|
|
|
|
|
|
|
EnablePlainIPInNotification bool `koanf:"enable_plain_ip_in_notification" json:"enable_plain_ip_in_notification,omitempty"` // 通知信息IP不打码
|
2022-04-15 11:23:51 -04:00
|
|
|
|
|
2022-04-14 15:13:53 -04:00
|
|
|
|
// IP变更提醒
|
2025-02-28 09:02:54 -05:00
|
|
|
|
EnableIPChangeNotification bool `koanf:"enable_ip_change_notification" json:"enable_ip_change_notification,omitempty"`
|
|
|
|
|
IPChangeNotificationGroupID uint64 `koanf:"ip_change_notification_group_id" json:"ip_change_notification_group_id"`
|
|
|
|
|
Cover uint8 `koanf:"cover" json:"cover"` // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;)
|
|
|
|
|
IgnoredIPNotification string `koanf:"ignored_ip_notification" json:"ignored_ip_notification,omitempty"` // 特定服务器IP(多个服务器用逗号分隔)
|
|
|
|
|
|
2025-03-03 06:02:25 -05:00
|
|
|
|
DNSServers string `koanf:"dns_servers" json:"dns_servers,omitempty"`
|
2025-02-28 09:02:54 -05:00
|
|
|
|
}
|
2024-10-22 11:44:50 -04:00
|
|
|
|
|
2025-02-28 09:02:54 -05:00
|
|
|
|
type Config struct {
|
|
|
|
|
ConfigForGuests
|
|
|
|
|
ConfigDashboard
|
2024-10-18 10:06:01 -04:00
|
|
|
|
|
2025-03-03 06:02:25 -05:00
|
|
|
|
AvgPingCount int `koanf:"avg_ping_count" json:"avg_ping_count,omitempty"`
|
|
|
|
|
|
|
|
|
|
Debug bool `koanf:"debug" json:"debug,omitempty"` // debug模式开关
|
|
|
|
|
Location string `koanf:"location" json:"location,omitempty"` // 时区,默认为 Asia/Shanghai
|
|
|
|
|
ForceAuth bool `koanf:"force_auth" json:"force_auth,omitempty"` // 强制要求认证
|
|
|
|
|
AgentSecretKey string `koanf:"agent_secret_key" json:"agent_secret_key,omitempty"`
|
|
|
|
|
JWTTimeout int `koanf:"jwt_timeout" json:"jwt_timeout,omitempty"` // JWT token过期时间(小时)
|
|
|
|
|
|
2025-02-28 09:02:54 -05:00
|
|
|
|
JWTSecretKey string `koanf:"jwt_secret_key" json:"jwt_secret_key,omitempty"`
|
|
|
|
|
ListenPort uint16 `koanf:"listen_port" json:"listen_port,omitempty"`
|
|
|
|
|
ListenHost string `koanf:"listen_host" json:"listen_host,omitempty"`
|
2024-10-27 01:10:07 -04:00
|
|
|
|
|
2024-12-28 10:50:59 -05:00
|
|
|
|
// oauth2 配置
|
2025-02-28 09:02:54 -05:00
|
|
|
|
Oauth2 map[string]*Oauth2Config `koanf:"oauth2" json:"oauth2,omitempty"`
|
|
|
|
|
|
|
|
|
|
// HTTPS 配置
|
2025-03-03 06:02:25 -05:00
|
|
|
|
HTTPS HTTPSConf `koanf:"https" json:"https"`
|
2024-12-28 10:50:59 -05:00
|
|
|
|
|
2024-11-23 12:07:14 -05:00
|
|
|
|
k *koanf.Koanf `json:"-"`
|
|
|
|
|
filePath string `json:"-"`
|
2024-04-27 01:36:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
2025-03-03 06:02:25 -05:00
|
|
|
|
type HTTPSConf struct {
|
|
|
|
|
InsecureTLS bool `koanf:"insecure_tls" json:"insecure_tls,omitempty"`
|
|
|
|
|
ListenPort uint16 `koanf:"listen_port" json:"listen_port,omitempty"`
|
|
|
|
|
TLSCertPath string `koanf:"tls_cert_path" json:"tls_cert_path,omitempty"`
|
|
|
|
|
TLSKeyPath string `koanf:"tls_key_path" json:"tls_key_path,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
|
// Read 读取配置文件并应用
|
2024-12-10 08:57:20 -05:00
|
|
|
|
func (c *Config) Read(path string, frontendTemplates []FrontendTemplate) error {
|
2024-11-23 12:07:14 -05:00
|
|
|
|
c.k = koanf.New(".")
|
|
|
|
|
c.filePath = path
|
|
|
|
|
|
|
|
|
|
err := c.k.Load(env.Provider("NZ_", ".", func(s string) string {
|
|
|
|
|
return strings.Replace(strings.ToLower(strings.TrimPrefix(s, "NZ_")), "_", ".", -1)
|
|
|
|
|
}), nil)
|
2019-12-08 03:59:58 -05:00
|
|
|
|
if err != nil {
|
2019-12-20 10:58:09 -05:00
|
|
|
|
return err
|
2019-12-08 03:59:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-23 12:07:14 -05:00
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
2025-03-03 06:02:25 -05:00
|
|
|
|
err = c.k.Load(file.Provider(path), new(utils.KubeYAML), koanf.WithMergeFunc(mergeDedup))
|
2024-11-23 12:07:14 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-28 09:02:54 -05:00
|
|
|
|
err = c.k.UnmarshalWithConf("", c, koanfConf(c))
|
2019-12-08 03:59:58 -05:00
|
|
|
|
if err != nil {
|
2019-12-20 10:58:09 -05:00
|
|
|
|
return err
|
2019-12-08 03:59:58 -05:00
|
|
|
|
}
|
2025-02-28 09:02:54 -05:00
|
|
|
|
|
2024-10-20 02:05:43 -04:00
|
|
|
|
if c.ListenPort == 0 {
|
|
|
|
|
c.ListenPort = 8008
|
|
|
|
|
}
|
2024-11-01 02:36:14 -04:00
|
|
|
|
if c.Language == "" {
|
2024-12-15 04:07:53 -05:00
|
|
|
|
c.Language = "en_US"
|
2024-11-01 02:36:14 -04:00
|
|
|
|
}
|
2022-10-12 11:06:25 -04:00
|
|
|
|
if c.Location == "" {
|
|
|
|
|
c.Location = "Asia/Shanghai"
|
|
|
|
|
}
|
2024-12-10 09:27:06 -05:00
|
|
|
|
var userTemplateValid, adminTemplateValid bool
|
2024-12-10 08:57:20 -05:00
|
|
|
|
for _, v := range frontendTemplates {
|
2024-12-10 09:27:06 -05:00
|
|
|
|
if !userTemplateValid && v.Path == c.UserTemplate && !v.IsAdmin {
|
2024-12-06 10:19:28 -05:00
|
|
|
|
userTemplateValid = true
|
2024-12-10 09:27:06 -05:00
|
|
|
|
}
|
|
|
|
|
if !adminTemplateValid && v.Path == c.AdminTemplate && v.IsAdmin {
|
|
|
|
|
adminTemplateValid = true
|
|
|
|
|
}
|
|
|
|
|
if userTemplateValid && adminTemplateValid {
|
2024-12-06 10:19:28 -05:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if c.UserTemplate == "" || !userTemplateValid {
|
|
|
|
|
c.UserTemplate = "user-dist"
|
|
|
|
|
}
|
2024-12-10 09:27:06 -05:00
|
|
|
|
if c.AdminTemplate == "" || !adminTemplateValid {
|
|
|
|
|
c.AdminTemplate = "admin-dist"
|
|
|
|
|
}
|
2024-02-12 01:16:04 -05:00
|
|
|
|
if c.AvgPingCount == 0 {
|
|
|
|
|
c.AvgPingCount = 2
|
|
|
|
|
}
|
2024-12-02 08:10:40 -05:00
|
|
|
|
if c.Cover == 0 {
|
|
|
|
|
c.Cover = 1
|
|
|
|
|
}
|
2024-10-20 11:23:04 -04:00
|
|
|
|
if c.JWTSecretKey == "" {
|
|
|
|
|
c.JWTSecretKey, err = utils.GenerateRandomString(1024)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err = c.Save(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-02 02:37:46 -05:00
|
|
|
|
// Add JWTTimeout default check
|
|
|
|
|
if c.JWTTimeout == 0 {
|
|
|
|
|
c.JWTTimeout = 1
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-20 11:23:04 -04:00
|
|
|
|
if c.AgentSecretKey == "" {
|
|
|
|
|
c.AgentSecretKey, err = utils.GenerateRandomString(32)
|
2024-10-19 23:47:45 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err = c.Save(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-07-13 00:51:59 -04:00
|
|
|
|
}
|
2020-12-09 06:05:40 -05:00
|
|
|
|
|
2019-12-20 10:58:09 -05:00
|
|
|
|
return nil
|
2019-12-08 03:59:58 -05:00
|
|
|
|
}
|
2020-12-09 06:05:40 -05:00
|
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
|
// Save 保存配置文件
|
2020-12-09 06:05:40 -05:00
|
|
|
|
func (c *Config) Save() error {
|
2025-03-03 06:02:25 -05:00
|
|
|
|
return c.save()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Config) save() error {
|
|
|
|
|
data, err := yaml.Marshal(c)
|
2020-12-09 06:05:40 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-11-25 07:18:30 -05:00
|
|
|
|
|
2025-03-03 06:02:25 -05:00
|
|
|
|
return c.write(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Config) write(data []byte) error {
|
2024-11-25 07:18:30 -05:00
|
|
|
|
dir := filepath.Dir(c.filePath)
|
2024-11-28 07:15:12 -05:00
|
|
|
|
if err := os.MkdirAll(dir, 0750); err != nil {
|
2024-11-25 07:18:30 -05:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-23 12:07:14 -05:00
|
|
|
|
return os.WriteFile(c.filePath, data, 0600)
|
2020-12-09 06:05:40 -05:00
|
|
|
|
}
|
2025-02-28 09:02:54 -05:00
|
|
|
|
|
|
|
|
|
func koanfConf(c any) koanf.UnmarshalConf {
|
|
|
|
|
return koanf.UnmarshalConf{
|
|
|
|
|
DecoderConfig: &mapstructure.DecoderConfig{
|
|
|
|
|
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
|
|
|
|
mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
|
utils.TextUnmarshalerHookFunc()),
|
|
|
|
|
Metadata: nil,
|
|
|
|
|
Result: c,
|
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
|
MatchName: func(mapKey, fieldName string) bool {
|
|
|
|
|
return strings.EqualFold(mapKey, fieldName) ||
|
|
|
|
|
strings.EqualFold(mapKey, strings.ReplaceAll(fieldName, "_", ""))
|
|
|
|
|
},
|
|
|
|
|
Squash: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-03 06:02:25 -05:00
|
|
|
|
|
|
|
|
|
func mergeDedup(src, dst map[string]any) error {
|
|
|
|
|
for key := range src {
|
|
|
|
|
if strings.IndexByte(key, '_') == -1 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
oldKey := strings.ReplaceAll(key, "_", "")
|
|
|
|
|
if _, ok := dst[oldKey]; ok {
|
|
|
|
|
src[oldKey] = src[key]
|
|
|
|
|
delete(src, key)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kmaps.Merge(src, dst)
|
|
|
|
|
return nil
|
|
|
|
|
}
|