2019-12-08 03:59:58 -05:00
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
2020-12-09 06:05:40 -05:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
2021-04-07 09:11:59 -04:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2019-12-20 10:58:09 -05:00
|
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
|
"github.com/spf13/viper"
|
2020-12-09 06:05:40 -05:00
|
|
|
|
"gopkg.in/yaml.v2"
|
2019-12-08 03:59:58 -05:00
|
|
|
|
)
|
|
|
|
|
|
2022-04-30 11:02:40 -04:00
|
|
|
|
var Languages = map[string]string{
|
|
|
|
|
"zh-CN": "简体中文",
|
|
|
|
|
"en-US": "English",
|
|
|
|
|
"es-ES": "Español",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var Themes = map[string]string{
|
|
|
|
|
"default": "Default",
|
|
|
|
|
"daynight": "JackieSung DayNight",
|
|
|
|
|
"mdui": "Neko Mdui",
|
|
|
|
|
"hotaru": "Hotaru",
|
2022-06-02 21:45:11 -04:00
|
|
|
|
"custom": "Custom(local)",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var DashboardThemes = map[string]string{
|
|
|
|
|
"default": "Default",
|
|
|
|
|
"custom": "Custom(local)",
|
2022-04-30 11:02:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 10:08:40 -05:00
|
|
|
|
const (
|
2022-05-26 08:00:20 -04:00
|
|
|
|
ConfigTypeGitHub = "github"
|
|
|
|
|
ConfigTypeGitee = "gitee"
|
|
|
|
|
ConfigTypeGitlab = "gitlab"
|
|
|
|
|
ConfigTypeJihulab = "jihulab"
|
2021-03-02 10:08:40 -05:00
|
|
|
|
)
|
|
|
|
|
|
2021-06-22 02:05:36 -04:00
|
|
|
|
const (
|
|
|
|
|
ConfigCoverAll = iota
|
|
|
|
|
ConfigCoverIgnoreAll
|
|
|
|
|
)
|
|
|
|
|
|
2022-03-19 05:22:15 -04:00
|
|
|
|
type AgentConfig struct {
|
|
|
|
|
HardDrivePartitionAllowlist []string
|
|
|
|
|
NICAllowlist map[string]bool
|
|
|
|
|
v *viper.Viper
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-12 08:11:19 -04:00
|
|
|
|
// Read 从给定的文件目录加载配置文件
|
2022-03-19 05:22:15 -04: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
|
|
|
|
|
}
|
|
|
|
|
return ioutil.WriteFile(c.v.ConfigFileUsed(), data, os.ModePerm)
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
|
// Config 站点配置
|
2019-12-08 03:59:58 -05:00
|
|
|
|
type Config struct {
|
2022-04-27 11:51:45 -04:00
|
|
|
|
Debug bool // debug模式开关
|
|
|
|
|
Language string // 系统语言,默认 zh-CN
|
|
|
|
|
Site struct {
|
2022-06-02 21:45:11 -04:00
|
|
|
|
Brand string // 站点名称
|
|
|
|
|
CookieName string // 浏览器 Cookie 名称
|
|
|
|
|
Theme string
|
|
|
|
|
DashboardTheme string
|
|
|
|
|
CustomCode string
|
|
|
|
|
ViewPassword string // 前台查看密码
|
2019-12-08 03:59:58 -05:00
|
|
|
|
}
|
2021-03-02 10:08:40 -05:00
|
|
|
|
Oauth2 struct {
|
|
|
|
|
Type string
|
|
|
|
|
Admin string // 管理员用户名列表
|
2019-12-08 03:59:58 -05:00
|
|
|
|
ClientID string
|
|
|
|
|
ClientSecret string
|
|
|
|
|
}
|
2022-02-19 01:29:06 -05:00
|
|
|
|
HTTPPort uint
|
|
|
|
|
GRPCPort uint
|
|
|
|
|
GRPCHost string
|
|
|
|
|
ProxyGRPCPort uint
|
|
|
|
|
TLS bool
|
|
|
|
|
|
2022-04-15 11:23:51 -04:00
|
|
|
|
EnablePlainIPInNotification bool // 通知信息IP不打码
|
|
|
|
|
|
2022-04-14 15:13:53 -04:00
|
|
|
|
// IP变更提醒
|
2022-04-15 11:23:51 -04:00
|
|
|
|
EnableIPChangeNotification bool
|
|
|
|
|
IPChangeNotificationTag string
|
|
|
|
|
Cover uint8 // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;)
|
|
|
|
|
IgnoredIPNotification string // 特定服务器IP(多个服务器用逗号分隔)
|
2019-12-20 10:58:09 -05:00
|
|
|
|
|
2021-04-07 09:11:59 -04:00
|
|
|
|
v *viper.Viper
|
2022-04-11 10:51:02 -04:00
|
|
|
|
IgnoredIPNotificationServerIDs map[uint64]bool // [ServerID] -> bool(值为true代表当前ServerID在特定服务器列表内)
|
2019-12-08 03:59:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
|
// Read 读取配置文件并应用
|
2019-12-20 10:58:09 -05:00
|
|
|
|
func (c *Config) Read(path string) error {
|
|
|
|
|
c.v = viper.New()
|
|
|
|
|
c.v.SetConfigFile(path)
|
|
|
|
|
err := c.v.ReadInConfig()
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 10:58:09 -05:00
|
|
|
|
err = c.v.Unmarshal(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
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 06:05:40 -05:00
|
|
|
|
if c.Site.Theme == "" {
|
|
|
|
|
c.Site.Theme = "default"
|
|
|
|
|
}
|
2022-06-02 21:45:11 -04:00
|
|
|
|
if c.Site.DashboardTheme == "" {
|
|
|
|
|
c.Site.DashboardTheme = "default"
|
|
|
|
|
}
|
2022-04-27 11:51:45 -04:00
|
|
|
|
if c.Language == "" {
|
|
|
|
|
c.Language = "zh-CN"
|
|
|
|
|
}
|
2022-04-12 01:16:33 -04:00
|
|
|
|
if c.GRPCPort == 0 {
|
|
|
|
|
c.GRPCPort = 5555
|
|
|
|
|
}
|
2022-04-14 09:06:42 -04:00
|
|
|
|
if c.EnableIPChangeNotification && c.IPChangeNotificationTag == "" {
|
|
|
|
|
c.IPChangeNotificationTag = "default"
|
|
|
|
|
}
|
2020-12-09 06:05:40 -05:00
|
|
|
|
|
2021-04-07 09:11:59 -04:00
|
|
|
|
c.updateIgnoredIPNotificationID()
|
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
|
|
|
|
// updateIgnoredIPNotificationID 更新用于判断服务器ID是否属于特定服务器的map
|
2021-04-07 09:11:59 -04:00
|
|
|
|
func (c *Config) updateIgnoredIPNotificationID() {
|
2021-06-22 02:05:36 -04:00
|
|
|
|
c.IgnoredIPNotificationServerIDs = make(map[uint64]bool)
|
2021-04-07 09:11:59 -04: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 02:05:36 -04:00
|
|
|
|
c.IgnoredIPNotificationServerIDs[id] = true
|
2021-04-07 09:11:59 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
|
// Save 保存配置文件
|
2020-12-09 06:05:40 -05:00
|
|
|
|
func (c *Config) Save() error {
|
2021-04-07 09:11:59 -04:00
|
|
|
|
c.updateIgnoredIPNotificationID()
|
2020-12-09 06:05:40 -05:00
|
|
|
|
data, err := yaml.Marshal(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return ioutil.WriteFile(c.v.ConfigFileUsed(), data, os.ModePerm)
|
|
|
|
|
}
|