2019-12-08 16:59:58 +08:00
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
2020-12-09 19:05:40 +08:00
|
|
|
|
"os"
|
2021-04-07 21:11:59 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2019-12-20 23:58:09 +08:00
|
|
|
|
|
2024-10-20 11:47:45 +08:00
|
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
2019-12-08 16:59:58 +08:00
|
|
|
|
"github.com/spf13/viper"
|
2022-10-12 19:34:15 +08:00
|
|
|
|
"sigs.k8s.io/yaml"
|
2019-12-08 16:59:58 +08:00
|
|
|
|
)
|
|
|
|
|
|
2022-04-30 23:02:40 +08:00
|
|
|
|
var Languages = map[string]string{
|
|
|
|
|
"zh-CN": "简体中文",
|
2023-02-03 13:12:14 +08:00
|
|
|
|
"zh-TW": "繁體中文",
|
2022-04-30 23:02:40 +08:00
|
|
|
|
"en-US": "English",
|
|
|
|
|
"es-ES": "Español",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var Themes = map[string]string{
|
2023-11-07 13:46:28 +08:00
|
|
|
|
"default": "Default",
|
|
|
|
|
"daynight": "JackieSung DayNight",
|
|
|
|
|
"mdui": "Neko Mdui",
|
|
|
|
|
"hotaru": "Hotaru",
|
|
|
|
|
"angel-kanade": "AngelKanade",
|
2024-06-16 18:04:27 +08:00
|
|
|
|
"server-status": "ServerStatus",
|
2024-10-19 11:59:31 +08:00
|
|
|
|
"custom": "Custom(local)",
|
2022-06-03 09:45:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var DashboardThemes = map[string]string{
|
|
|
|
|
"default": "Default",
|
|
|
|
|
"custom": "Custom(local)",
|
2022-04-30 23:02:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 23:08:40 +08:00
|
|
|
|
const (
|
2024-05-03 08:47:53 +08:00
|
|
|
|
ConfigTypeGitHub = "github"
|
|
|
|
|
ConfigTypeGitee = "gitee"
|
|
|
|
|
ConfigTypeGitlab = "gitlab"
|
|
|
|
|
ConfigTypeJihulab = "jihulab"
|
|
|
|
|
ConfigTypeGitea = "gitea"
|
|
|
|
|
ConfigTypeCloudflare = "cloudflare"
|
2024-07-13 12:51:59 +08:00
|
|
|
|
ConfigTypeOidc = "oidc"
|
2021-03-02 23:08:40 +08:00
|
|
|
|
)
|
|
|
|
|
|
2021-06-22 14:05:36 +08:00
|
|
|
|
const (
|
|
|
|
|
ConfigCoverAll = iota
|
|
|
|
|
ConfigCoverIgnoreAll
|
|
|
|
|
)
|
|
|
|
|
|
2022-03-19 17:22:15 +08:00
|
|
|
|
type AgentConfig struct {
|
|
|
|
|
HardDrivePartitionAllowlist []string
|
|
|
|
|
NICAllowlist map[string]bool
|
|
|
|
|
v *viper.Viper
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-12 20:11:19 +08:00
|
|
|
|
// Read 从给定的文件目录加载配置文件
|
2022-03-19 17:22:15 +08:00
|
|
|
|
func (c *AgentConfig) Read(path string) error {
|
|
|
|
|
c.v = viper.New()
|
|
|
|
|
c.v.SetConfigFile(path)
|
|
|
|
|
err := c.v.ReadInConfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = c.v.Unmarshal(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *AgentConfig) Save() error {
|
|
|
|
|
data, err := yaml.Marshal(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-06-01 19:11:48 +08:00
|
|
|
|
return os.WriteFile(c.v.ConfigFileUsed(), data, 0600)
|
2022-03-19 17:22:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 22:51:02 +08:00
|
|
|
|
// Config 站点配置
|
2019-12-08 16:59:58 +08:00
|
|
|
|
type Config struct {
|
2024-10-20 11:47:45 +08:00
|
|
|
|
Debug bool // debug模式开关
|
|
|
|
|
|
|
|
|
|
Language string // 系统语言,默认 zh-CN
|
|
|
|
|
SiteName string
|
|
|
|
|
SecretKey string
|
2024-10-19 23:14:53 +08:00
|
|
|
|
ListenPort uint
|
|
|
|
|
InstallHost string
|
|
|
|
|
TLS bool
|
2024-10-20 11:47:45 +08:00
|
|
|
|
Location string // 时区,默认为 Asia/Shanghai
|
2022-02-19 14:29:06 +08:00
|
|
|
|
|
2024-10-20 11:47:45 +08:00
|
|
|
|
EnablePlainIPInNotification bool // 通知信息IP不打码
|
2022-04-15 23:23:51 +08:00
|
|
|
|
|
2022-04-15 03:13:53 +08:00
|
|
|
|
// IP变更提醒
|
2022-04-15 23:23:51 +08:00
|
|
|
|
EnableIPChangeNotification bool
|
|
|
|
|
IPChangeNotificationTag string
|
|
|
|
|
Cover uint8 // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;)
|
|
|
|
|
IgnoredIPNotification string // 特定服务器IP(多个服务器用逗号分隔)
|
2019-12-20 23:58:09 +08:00
|
|
|
|
|
2022-04-11 22:51:02 +08:00
|
|
|
|
IgnoredIPNotificationServerIDs map[uint64]bool // [ServerID] -> bool(值为true代表当前ServerID在特定服务器列表内)
|
2024-02-12 14:16:04 +08:00
|
|
|
|
AvgPingCount int
|
2024-10-20 11:47:45 +08:00
|
|
|
|
DNSServers string
|
2024-10-18 22:06:01 +08:00
|
|
|
|
|
2024-10-20 11:47:45 +08:00
|
|
|
|
v *viper.Viper
|
2024-04-27 13:36:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 22:51:02 +08:00
|
|
|
|
// Read 读取配置文件并应用
|
2019-12-20 23:58:09 +08:00
|
|
|
|
func (c *Config) Read(path string) error {
|
|
|
|
|
c.v = viper.New()
|
|
|
|
|
c.v.SetConfigFile(path)
|
|
|
|
|
err := c.v.ReadInConfig()
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 23:58:09 +08:00
|
|
|
|
err = c.v.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
|
|
|
|
}
|
|
|
|
|
|
2022-04-27 23:51:45 +08:00
|
|
|
|
if c.Language == "" {
|
|
|
|
|
c.Language = "zh-CN"
|
|
|
|
|
}
|
2022-04-14 21:06:42 +08:00
|
|
|
|
if c.EnableIPChangeNotification && c.IPChangeNotificationTag == "" {
|
|
|
|
|
c.IPChangeNotificationTag = "default"
|
|
|
|
|
}
|
2022-10-12 23:06:25 +08:00
|
|
|
|
if c.Location == "" {
|
|
|
|
|
c.Location = "Asia/Shanghai"
|
|
|
|
|
}
|
2024-02-12 14:16:04 +08:00
|
|
|
|
if c.AvgPingCount == 0 {
|
|
|
|
|
c.AvgPingCount = 2
|
|
|
|
|
}
|
2024-10-20 11:47:45 +08:00
|
|
|
|
if c.SecretKey == "" {
|
|
|
|
|
c.SecretKey, err = utils.GenerateRandomString(1024)
|
|
|
|
|
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-06-01 19:11:48 +08:00
|
|
|
|
return os.WriteFile(c.v.ConfigFileUsed(), data, 0600)
|
2020-12-09 19:05:40 +08:00
|
|
|
|
}
|