2019-12-08 16:59:58 +08:00
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
2020-12-09 19:05:40 +08:00
|
|
|
|
"os"
|
2024-11-25 20:18:30 +08:00
|
|
|
|
"path/filepath"
|
2021-04-07 21:11:59 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2019-12-20 23:58:09 +08:00
|
|
|
|
|
2024-11-24 01:07:14 +08:00
|
|
|
|
kyaml "github.com/knadh/koanf/parsers/yaml"
|
|
|
|
|
"github.com/knadh/koanf/providers/env"
|
|
|
|
|
"github.com/knadh/koanf/providers/file"
|
|
|
|
|
"github.com/knadh/koanf/v2"
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
|
2024-11-28 19:38:54 +08:00
|
|
|
|
"github.com/nezhahq/nezha/pkg/utils"
|
2019-12-08 16:59:58 +08:00
|
|
|
|
)
|
|
|
|
|
|
2021-06-22 14:05:36 +08:00
|
|
|
|
const (
|
2024-11-22 00:19:36 +08:00
|
|
|
|
ConfigUsePeerIP = "NZ::Use-Peer-IP"
|
|
|
|
|
ConfigCoverAll = iota
|
2021-06-22 14:05:36 +08:00
|
|
|
|
ConfigCoverIgnoreAll
|
|
|
|
|
)
|
|
|
|
|
|
2019-12-08 16:59:58 +08:00
|
|
|
|
type Config struct {
|
2024-11-22 00:19:36 +08:00
|
|
|
|
Debug bool `mapstructure:"debug" json:"debug,omitempty"` // debug模式开关
|
|
|
|
|
RealIPHeader string `mapstructure:"real_ip_header" json:"real_ip_header,omitempty"` // 真实IP
|
2024-10-20 11:47:45 +08:00
|
|
|
|
|
2024-11-16 20:57:03 +08:00
|
|
|
|
Language string `mapstructure:"language" json:"language"` // 系统语言,默认 zh_CN
|
|
|
|
|
SiteName string `mapstructure:"site_name" json:"site_name"`
|
2024-12-06 23:19:28 +08:00
|
|
|
|
UserTemplate string `mapstructure:"user_template" json:"user_template,omitempty"`
|
2024-12-10 22:27:06 +08:00
|
|
|
|
AdminTemplate string `mapstructure:"admin_template" json:"admin_template,omitempty"`
|
2024-10-22 23:44:50 +08:00
|
|
|
|
JWTSecretKey string `mapstructure:"jwt_secret_key" json:"jwt_secret_key,omitempty"`
|
|
|
|
|
AgentSecretKey string `mapstructure:"agent_secret_key" json:"agent_secret_key,omitempty"`
|
|
|
|
|
ListenPort uint `mapstructure:"listen_port" json:"listen_port,omitempty"`
|
2024-12-05 17:01:21 +08:00
|
|
|
|
ListenHost string `mapstructure:"listen_host" json:"listen_host,omitempty"`
|
2024-10-22 23:44:50 +08:00
|
|
|
|
InstallHost string `mapstructure:"install_host" json:"install_host,omitempty"`
|
|
|
|
|
TLS bool `mapstructure:"tls" json:"tls,omitempty"`
|
|
|
|
|
Location string `mapstructure:"location" json:"location,omitempty"` // 时区,默认为 Asia/Shanghai
|
2022-02-19 14:29:06 +08:00
|
|
|
|
|
2024-10-22 23:44:50 +08:00
|
|
|
|
EnablePlainIPInNotification bool `mapstructure:"enable_plain_ip_in_notification" json:"enable_plain_ip_in_notification,omitempty"` // 通知信息IP不打码
|
2022-04-15 23:23:51 +08:00
|
|
|
|
|
2022-04-15 03:13:53 +08:00
|
|
|
|
// IP变更提醒
|
2024-10-27 13:10:07 +08:00
|
|
|
|
EnableIPChangeNotification bool `mapstructure:"enable_ip_change_notification" json:"enable_ip_change_notification,omitempty"`
|
2024-11-16 20:57:03 +08:00
|
|
|
|
IPChangeNotificationGroupID uint64 `mapstructure:"ip_change_notification_group_id" json:"ip_change_notification_group_id"`
|
|
|
|
|
Cover uint8 `mapstructure:"cover" json:"cover"` // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;)
|
2024-10-27 13:10:07 +08:00
|
|
|
|
IgnoredIPNotification string `mapstructure:"ignored_ip_notification" json:"ignored_ip_notification,omitempty"` // 特定服务器IP(多个服务器用逗号分隔)
|
2024-10-22 23:44:50 +08:00
|
|
|
|
|
2024-10-27 13:10:07 +08:00
|
|
|
|
IgnoredIPNotificationServerIDs map[uint64]bool `mapstructure:"ignored_ip_notification_server_ids" json:"ignored_ip_notification_server_ids,omitempty"` // [ServerID] -> bool(值为true代表当前ServerID在特定服务器列表内)
|
2024-10-22 23:44:50 +08:00
|
|
|
|
AvgPingCount int `mapstructure:"avg_ping_count" json:"avg_ping_count,omitempty"`
|
|
|
|
|
DNSServers string `mapstructure:"dns_servers" json:"dns_servers,omitempty"`
|
2024-10-18 22:06:01 +08:00
|
|
|
|
|
2024-10-27 13:10:07 +08:00
|
|
|
|
CustomCode string `mapstructure:"custom_code" json:"custom_code,omitempty"`
|
|
|
|
|
CustomCodeDashboard string `mapstructure:"custom_code_dashboard" json:"custom_code_dashboard,omitempty"`
|
|
|
|
|
|
2024-11-24 01:07:14 +08:00
|
|
|
|
k *koanf.Koanf `json:"-"`
|
|
|
|
|
filePath string `json:"-"`
|
2024-04-27 13:36:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 22:51:02 +08:00
|
|
|
|
// Read 读取配置文件并应用
|
2024-12-10 21:57:20 +08:00
|
|
|
|
func (c *Config) Read(path string, frontendTemplates []FrontendTemplate) error {
|
2024-11-24 01:07:14 +08: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 16:59:58 +08:00
|
|
|
|
if err != nil {
|
2019-12-20 23:58:09 +08:00
|
|
|
|
return err
|
2019-12-08 16:59:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-24 01:07:14 +08:00
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
|
err = c.k.Load(file.Provider(path), kyaml.Parser())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = c.k.Unmarshal("", c)
|
2019-12-08 16:59:58 +08:00
|
|
|
|
if err != nil {
|
2019-12-20 23:58:09 +08:00
|
|
|
|
return err
|
2019-12-08 16:59:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-20 14:05:43 +08:00
|
|
|
|
if c.ListenPort == 0 {
|
|
|
|
|
c.ListenPort = 8008
|
|
|
|
|
}
|
2024-11-01 14:36:14 +08:00
|
|
|
|
if c.Language == "" {
|
|
|
|
|
c.Language = "zh_CN"
|
|
|
|
|
}
|
2022-10-12 23:06:25 +08:00
|
|
|
|
if c.Location == "" {
|
|
|
|
|
c.Location = "Asia/Shanghai"
|
|
|
|
|
}
|
2024-12-10 22:27:06 +08:00
|
|
|
|
var userTemplateValid, adminTemplateValid bool
|
2024-12-10 21:57:20 +08:00
|
|
|
|
for _, v := range frontendTemplates {
|
2024-12-10 22:27:06 +08:00
|
|
|
|
if !userTemplateValid && v.Path == c.UserTemplate && !v.IsAdmin {
|
2024-12-06 23:19:28 +08:00
|
|
|
|
userTemplateValid = true
|
2024-12-10 22:27:06 +08:00
|
|
|
|
}
|
|
|
|
|
if !adminTemplateValid && v.Path == c.AdminTemplate && v.IsAdmin {
|
|
|
|
|
adminTemplateValid = true
|
|
|
|
|
}
|
|
|
|
|
if userTemplateValid && adminTemplateValid {
|
2024-12-06 23:19:28 +08:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if c.UserTemplate == "" || !userTemplateValid {
|
|
|
|
|
c.UserTemplate = "user-dist"
|
|
|
|
|
}
|
2024-12-10 22:27:06 +08:00
|
|
|
|
if c.AdminTemplate == "" || !adminTemplateValid {
|
|
|
|
|
c.AdminTemplate = "admin-dist"
|
|
|
|
|
}
|
2024-02-12 14:16:04 +08:00
|
|
|
|
if c.AvgPingCount == 0 {
|
|
|
|
|
c.AvgPingCount = 2
|
|
|
|
|
}
|
2024-12-02 21:10:40 +08:00
|
|
|
|
if c.Cover == 0 {
|
|
|
|
|
c.Cover = 1
|
|
|
|
|
}
|
2024-10-20 23:23:04 +08:00
|
|
|
|
if c.JWTSecretKey == "" {
|
|
|
|
|
c.JWTSecretKey, err = utils.GenerateRandomString(1024)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err = c.Save(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.AgentSecretKey == "" {
|
|
|
|
|
c.AgentSecretKey, err = utils.GenerateRandomString(32)
|
2024-10-20 11:47:45 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err = c.Save(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-07-13 12:51:59 +08:00
|
|
|
|
}
|
2020-12-09 19:05:40 +08:00
|
|
|
|
|
2021-04-07 21:11:59 +08:00
|
|
|
|
c.updateIgnoredIPNotificationID()
|
2019-12-20 23:58:09 +08:00
|
|
|
|
return nil
|
2019-12-08 16:59:58 +08:00
|
|
|
|
}
|
2020-12-09 19:05:40 +08:00
|
|
|
|
|
2022-04-11 22:51:02 +08:00
|
|
|
|
// updateIgnoredIPNotificationID 更新用于判断服务器ID是否属于特定服务器的map
|
2021-04-07 21:11:59 +08:00
|
|
|
|
func (c *Config) updateIgnoredIPNotificationID() {
|
2021-06-22 14:05:36 +08:00
|
|
|
|
c.IgnoredIPNotificationServerIDs = make(map[uint64]bool)
|
2021-04-07 21:11:59 +08:00
|
|
|
|
splitedIDs := strings.Split(c.IgnoredIPNotification, ",")
|
|
|
|
|
for i := 0; i < len(splitedIDs); i++ {
|
|
|
|
|
id, _ := strconv.ParseUint(splitedIDs[i], 10, 64)
|
|
|
|
|
if id > 0 {
|
2021-06-22 14:05:36 +08:00
|
|
|
|
c.IgnoredIPNotificationServerIDs[id] = true
|
2021-04-07 21:11:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 22:51:02 +08:00
|
|
|
|
// Save 保存配置文件
|
2020-12-09 19:05:40 +08:00
|
|
|
|
func (c *Config) Save() error {
|
2021-04-07 21:11:59 +08:00
|
|
|
|
c.updateIgnoredIPNotificationID()
|
2020-12-09 19:05:40 +08:00
|
|
|
|
data, err := yaml.Marshal(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-11-25 20:18:30 +08:00
|
|
|
|
|
|
|
|
|
dir := filepath.Dir(c.filePath)
|
2024-11-28 20:15:12 +08:00
|
|
|
|
if err := os.MkdirAll(dir, 0750); err != nil {
|
2024-11-25 20:18:30 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-24 01:07:14 +08:00
|
|
|
|
return os.WriteFile(c.filePath, data, 0600)
|
2020-12-09 19:05:40 +08:00
|
|
|
|
}
|