mirror of
https://github.com/nezhahq/nezha.git
synced 2025-02-02 01:28:13 -05:00
add setting api (#461)
This commit is contained in:
parent
68d7e16773
commit
b4edb4cc95
@ -55,6 +55,8 @@ func routers(r *gin.Engine) {
|
|||||||
optionalAuth.GET("/ws/server", commonHandler(serverStream))
|
optionalAuth.GET("/ws/server", commonHandler(serverStream))
|
||||||
optionalAuth.GET("/server-group", commonHandler(listServerGroup))
|
optionalAuth.GET("/server-group", commonHandler(listServerGroup))
|
||||||
|
|
||||||
|
optionalAuth.GET("/setting", commonHandler(listConfig))
|
||||||
|
|
||||||
auth := api.Group("", authMiddleware.MiddlewareFunc())
|
auth := api.Group("", authMiddleware.MiddlewareFunc())
|
||||||
|
|
||||||
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
|
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
|
||||||
@ -114,6 +116,8 @@ func routers(r *gin.Engine) {
|
|||||||
auth.PATCH("/nat/:id", commonHandler(updateNAT))
|
auth.PATCH("/nat/:id", commonHandler(updateNAT))
|
||||||
auth.POST("/batch-delete/nat", commonHandler(batchDeleteNAT))
|
auth.POST("/batch-delete/nat", commonHandler(batchDeleteNAT))
|
||||||
|
|
||||||
|
auth.PATCH("/setting", commonHandler(updateConfig))
|
||||||
|
|
||||||
r.NoRoute(fallbackToFrontend)
|
r.NoRoute(fallbackToFrontend)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -606,29 +606,31 @@ func (ma *memberAPI) updateSetting(c *gin.Context) {
|
|||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
|
|
||||||
singleton.Conf.Language = sf.Language
|
/*
|
||||||
singleton.Conf.EnableIPChangeNotification = sf.EnableIPChangeNotification == "on"
|
singleton.Conf.Language = sf.Language
|
||||||
singleton.Conf.EnablePlainIPInNotification = sf.EnablePlainIPInNotification == "on"
|
singleton.Conf.EnableIPChangeNotification = sf.EnableIPChangeNotification == "on"
|
||||||
singleton.Conf.Cover = sf.Cover
|
singleton.Conf.EnablePlainIPInNotification = sf.EnablePlainIPInNotification == "on"
|
||||||
singleton.Conf.InstallHost = sf.InstallHost
|
singleton.Conf.Cover = sf.Cover
|
||||||
singleton.Conf.IgnoredIPNotification = sf.IgnoredIPNotification
|
singleton.Conf.InstallHost = sf.InstallHost
|
||||||
singleton.Conf.IPChangeNotificationTag = sf.IPChangeNotificationTag
|
singleton.Conf.IgnoredIPNotification = sf.IgnoredIPNotification
|
||||||
singleton.Conf.SiteName = sf.SiteName
|
singleton.Conf.IPChangeNotificationTag = sf.IPChangeNotificationTag
|
||||||
singleton.Conf.DNSServers = sf.CustomNameservers
|
singleton.Conf.SiteName = sf.SiteName
|
||||||
// 保证NotificationTag不为空
|
singleton.Conf.DNSServers = sf.CustomNameservers
|
||||||
if singleton.Conf.IPChangeNotificationTag == "" {
|
// 保证NotificationTag不为空
|
||||||
singleton.Conf.IPChangeNotificationTag = "default"
|
if singleton.Conf.IPChangeNotificationTag == "" {
|
||||||
}
|
singleton.Conf.IPChangeNotificationTag = "default"
|
||||||
if err := singleton.Conf.Save(); err != nil {
|
}
|
||||||
|
if err := singleton.Conf.Save(); err != nil {
|
||||||
|
c.JSON(http.StatusOK, model.Response{
|
||||||
|
Code: http.StatusBadRequest,
|
||||||
|
Message: fmt.Sprintf("请求错误:%s", err),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 更新DNS服务器
|
||||||
|
singleton.OnNameserverUpdate()
|
||||||
c.JSON(http.StatusOK, model.Response{
|
c.JSON(http.StatusOK, model.Response{
|
||||||
Code: http.StatusBadRequest,
|
Code: http.StatusOK,
|
||||||
Message: fmt.Sprintf("请求错误:%s", err),
|
|
||||||
})
|
})
|
||||||
return
|
*/
|
||||||
}
|
|
||||||
// 更新DNS服务器
|
|
||||||
singleton.OnNameserverUpdate()
|
|
||||||
c.JSON(http.StatusOK, model.Response{
|
|
||||||
Code: http.StatusOK,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ func (mp *memberPage) nat(c *gin.Context) {
|
|||||||
func (mp *memberPage) setting(c *gin.Context) {
|
func (mp *memberPage) setting(c *gin.Context) {
|
||||||
c.HTML(http.StatusOK, "dashboard-", gin.H{
|
c.HTML(http.StatusOK, "dashboard-", gin.H{
|
||||||
// "Title": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "Settings"}),
|
// "Title": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "Settings"}),
|
||||||
"Languages": model.Languages,
|
//"Languages": model.Languages,
|
||||||
"DashboardThemes": model.DashboardThemes,
|
//"DashboardThemes": model.DashboardThemes,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
71
cmd/dashboard/controller/setting.go
Normal file
71
cmd/dashboard/controller/setting.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"github.com/naiba/nezha/model"
|
||||||
|
"github.com/naiba/nezha/service/singleton"
|
||||||
|
)
|
||||||
|
|
||||||
|
// List settings
|
||||||
|
// @Summary List settings
|
||||||
|
// @Schemes
|
||||||
|
// @Description List settings
|
||||||
|
// @Security BearerAuth
|
||||||
|
// @Tags common
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {object} model.CommonResponse[model.Config]
|
||||||
|
// @Router /setting [get]
|
||||||
|
func listConfig(c *gin.Context) (model.Config, error) {
|
||||||
|
_, isMember := c.Get(model.CtxKeyAuthorizedUser)
|
||||||
|
authorized := isMember // TODO || isViewPasswordVerfied
|
||||||
|
|
||||||
|
conf := *singleton.Conf
|
||||||
|
if !authorized {
|
||||||
|
conf = model.Config{
|
||||||
|
SiteName: conf.SiteName,
|
||||||
|
Language: conf.Language,
|
||||||
|
CustomCode: conf.CustomCode,
|
||||||
|
CustomCodeDashboard: conf.CustomCodeDashboard,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return conf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Edit config
|
||||||
|
// @Summary Edit config
|
||||||
|
// @Security BearerAuth
|
||||||
|
// @Schemes
|
||||||
|
// @Description Edit config
|
||||||
|
// @Tags auth required
|
||||||
|
// @Accept json
|
||||||
|
// @Param body body model.SettingForm true "SettingForm"
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {object} model.CommonResponse[any]
|
||||||
|
// @Router /setting [patch]
|
||||||
|
func updateConfig(c *gin.Context) (any, error) {
|
||||||
|
var sf model.SettingForm
|
||||||
|
if err := c.ShouldBindJSON(&sf); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
singleton.Conf.Language = sf.Language
|
||||||
|
singleton.Conf.EnableIPChangeNotification = sf.EnableIPChangeNotification
|
||||||
|
singleton.Conf.EnablePlainIPInNotification = sf.EnablePlainIPInNotification
|
||||||
|
singleton.Conf.Cover = sf.Cover
|
||||||
|
singleton.Conf.InstallHost = sf.InstallHost
|
||||||
|
singleton.Conf.IgnoredIPNotification = sf.IgnoredIPNotification
|
||||||
|
singleton.Conf.IPChangeNotificationGroupID = sf.IPChangeNotificationGroupID
|
||||||
|
singleton.Conf.SiteName = sf.SiteName
|
||||||
|
singleton.Conf.DNSServers = sf.CustomNameservers
|
||||||
|
singleton.Conf.CustomCode = sf.CustomCode
|
||||||
|
singleton.Conf.CustomCodeDashboard = sf.CustomCodeDashboard
|
||||||
|
|
||||||
|
if err := singleton.Conf.Save(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
singleton.OnNameserverUpdate()
|
||||||
|
return nil, nil
|
||||||
|
}
|
@ -10,38 +10,6 @@ import (
|
|||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Languages = map[string]string{
|
|
||||||
"zh-CN": "简体中文",
|
|
||||||
"zh-TW": "繁體中文",
|
|
||||||
"en-US": "English",
|
|
||||||
"es-ES": "Español",
|
|
||||||
}
|
|
||||||
|
|
||||||
var Themes = map[string]string{
|
|
||||||
"default": "Default",
|
|
||||||
"daynight": "JackieSung DayNight",
|
|
||||||
"mdui": "Neko Mdui",
|
|
||||||
"hotaru": "Hotaru",
|
|
||||||
"angel-kanade": "AngelKanade",
|
|
||||||
"server-status": "ServerStatus",
|
|
||||||
"custom": "Custom(local)",
|
|
||||||
}
|
|
||||||
|
|
||||||
var DashboardThemes = map[string]string{
|
|
||||||
"default": "Default",
|
|
||||||
"custom": "Custom(local)",
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
ConfigTypeGitHub = "github"
|
|
||||||
ConfigTypeGitee = "gitee"
|
|
||||||
ConfigTypeGitlab = "gitlab"
|
|
||||||
ConfigTypeJihulab = "jihulab"
|
|
||||||
ConfigTypeGitea = "gitea"
|
|
||||||
ConfigTypeCloudflare = "cloudflare"
|
|
||||||
ConfigTypeOidc = "oidc"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ConfigCoverAll = iota
|
ConfigCoverAll = iota
|
||||||
ConfigCoverIgnoreAll
|
ConfigCoverIgnoreAll
|
||||||
@ -62,16 +30,19 @@ type Config struct {
|
|||||||
EnablePlainIPInNotification bool `mapstructure:"enable_plain_ip_in_notification" json:"enable_plain_ip_in_notification,omitempty"` // 通知信息IP不打码
|
EnablePlainIPInNotification bool `mapstructure:"enable_plain_ip_in_notification" json:"enable_plain_ip_in_notification,omitempty"` // 通知信息IP不打码
|
||||||
|
|
||||||
// IP变更提醒
|
// IP变更提醒
|
||||||
EnableIPChangeNotification bool `mapstructure:"enable_ip_change_notification" json:"enable_ip_change_notification,omitempty"`
|
EnableIPChangeNotification bool `mapstructure:"enable_ip_change_notification" json:"enable_ip_change_notification,omitempty"`
|
||||||
IPChangeNotificationTag string `mapstructure:"ip_change_notification_tag" json:"ip_change_notification_tag,omitempty"`
|
IPChangeNotificationGroupID uint64 `mapstructure:"ip_change_notification_group_id" json:"ip_change_notification_group_id,omitempty"`
|
||||||
Cover uint8 `mapstructure:"cover" json:"cover,omitempty"` // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;)
|
Cover uint8 `mapstructure:"cover" json:"cover,omitempty"` // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;)
|
||||||
IgnoredIPNotification string `mapstructure:"ignored_ip_notification" json:"ignored_ip_notification,omitempty"` // 特定服务器IP(多个服务器用逗号分隔)
|
IgnoredIPNotification string `mapstructure:"ignored_ip_notification" json:"ignored_ip_notification,omitempty"` // 特定服务器IP(多个服务器用逗号分隔)
|
||||||
|
|
||||||
IgnoredIPNotificationServerIDs map[uint64]bool `mapstructure:"ignored_ip_notification_server_i_ds" json:"ignored_ip_notification_server_i_ds,omitempty"` // [ServerID] -> bool(值为true代表当前ServerID在特定服务器列表内)
|
IgnoredIPNotificationServerIDs map[uint64]bool `mapstructure:"ignored_ip_notification_server_ids" json:"ignored_ip_notification_server_ids,omitempty"` // [ServerID] -> bool(值为true代表当前ServerID在特定服务器列表内)
|
||||||
AvgPingCount int `mapstructure:"avg_ping_count" json:"avg_ping_count,omitempty"`
|
AvgPingCount int `mapstructure:"avg_ping_count" json:"avg_ping_count,omitempty"`
|
||||||
DNSServers string `mapstructure:"dns_servers" json:"dns_servers,omitempty"`
|
DNSServers string `mapstructure:"dns_servers" json:"dns_servers,omitempty"`
|
||||||
|
|
||||||
v *viper.Viper
|
CustomCode string `mapstructure:"custom_code" json:"custom_code,omitempty"`
|
||||||
|
CustomCodeDashboard string `mapstructure:"custom_code_dashboard" json:"custom_code_dashboard,omitempty"`
|
||||||
|
|
||||||
|
v *viper.Viper `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read 读取配置文件并应用
|
// Read 读取配置文件并应用
|
||||||
@ -91,12 +62,6 @@ func (c *Config) Read(path string) error {
|
|||||||
if c.ListenPort == 0 {
|
if c.ListenPort == 0 {
|
||||||
c.ListenPort = 8008
|
c.ListenPort = 8008
|
||||||
}
|
}
|
||||||
if c.Language == "" {
|
|
||||||
c.Language = "zh-CN"
|
|
||||||
}
|
|
||||||
if c.EnableIPChangeNotification && c.IPChangeNotificationTag == "" {
|
|
||||||
c.IPChangeNotificationTag = "default"
|
|
||||||
}
|
|
||||||
if c.Location == "" {
|
if c.Location == "" {
|
||||||
c.Location = "Asia/Shanghai"
|
c.Location = "Asia/Shanghai"
|
||||||
}
|
}
|
||||||
|
16
model/setting_api.go
Normal file
16
model/setting_api.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
type SettingForm struct {
|
||||||
|
CustomNameservers string `json:"custom_nameservers,omitempty"`
|
||||||
|
IgnoredIPNotification string `json:"ignored_ip_notification,omitempty"`
|
||||||
|
IPChangeNotificationGroupID uint64 `json:"ip_change_notification_group_id,omitempty"` // IP变更提醒的通知组
|
||||||
|
Cover uint8 `json:"cover,omitempty"`
|
||||||
|
SiteName string `json:"site_name,omitempty"`
|
||||||
|
Language string `json:"language,omitempty"`
|
||||||
|
InstallHost string `json:"install_host,omitempty"`
|
||||||
|
CustomCode string `json:"custom_code,omitempty"`
|
||||||
|
CustomCodeDashboard string `json:"custom_code_dashboard,omitempty"`
|
||||||
|
|
||||||
|
EnableIPChangeNotification bool `json:"enable_ip_change_notification,omitempty"`
|
||||||
|
EnablePlainIPInNotification bool `json:"enable_plain_ip_in_notification,omitempty"`
|
||||||
|
}
|
@ -54,18 +54,12 @@ func (s *NezhaHandler) ReportTask(c context.Context, r *pb.TaskResult) (*pb.Rece
|
|||||||
curServer := model.Server{}
|
curServer := model.Server{}
|
||||||
copier.Copy(&curServer, singleton.ServerList[clientID])
|
copier.Copy(&curServer, singleton.ServerList[clientID])
|
||||||
if cr.PushSuccessful && r.GetSuccessful() {
|
if cr.PushSuccessful && r.GetSuccessful() {
|
||||||
// singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("[%s] %s, %s\n%s", singleton.Localizer.MustLocalize(
|
singleton.SendNotification(cr.NotificationGroupID, fmt.Sprintf("[%s] %s, %s\n%s", "Scheduled Task Executed Successfully",
|
||||||
// &i18n.LocalizeConfig{
|
cr.Name, singleton.ServerList[clientID].Name, r.GetData()), nil, &curServer)
|
||||||
// MessageID: "ScheduledTaskExecutedSuccessfully",
|
|
||||||
// },
|
|
||||||
// ), cr.Name, singleton.ServerList[clientID].Name, r.GetData()), nil, &curServer)
|
|
||||||
}
|
}
|
||||||
if !r.GetSuccessful() {
|
if !r.GetSuccessful() {
|
||||||
// singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("[%s] %s, %s\n%s", singleton.Localizer.MustLocalize(
|
singleton.SendNotification(cr.NotificationGroupID, fmt.Sprintf("[%s] %s, %s\n%s", "Scheduled Task Executed Failed",
|
||||||
// &i18n.LocalizeConfig{
|
cr.Name, singleton.ServerList[clientID].Name, r.GetData()), nil, &curServer)
|
||||||
// MessageID: "ScheduledTaskExecutedFailed",
|
|
||||||
// },
|
|
||||||
// ), cr.Name, singleton.ServerList[clientID].Name, r.GetData()), nil, &curServer)
|
|
||||||
}
|
}
|
||||||
singleton.DB.Model(cr).Updates(model.Cron{
|
singleton.DB.Model(cr).Updates(model.Cron{
|
||||||
LastExecutedAt: time.Now().Add(time.Second * -1 * time.Duration(r.GetDelay())),
|
LastExecutedAt: time.Now().Add(time.Second * -1 * time.Duration(r.GetDelay())),
|
||||||
@ -156,16 +150,14 @@ func (s *NezhaHandler) ReportSystemInfo(c context.Context, r *pb.Host) (*pb.Rece
|
|||||||
host.IP != "" &&
|
host.IP != "" &&
|
||||||
singleton.ServerList[clientID].Host.IP != host.IP {
|
singleton.ServerList[clientID].Host.IP != host.IP {
|
||||||
|
|
||||||
// singleton.SendNotification(singleton.Conf.IPChangeNotificationTag,
|
singleton.SendNotification(singleton.Conf.IPChangeNotificationGroupID,
|
||||||
// fmt.Sprintf(
|
fmt.Sprintf(
|
||||||
// "[%s] %s, %s => %s",
|
"[%s] %s, %s => %s",
|
||||||
// singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{
|
"IPChanged",
|
||||||
// MessageID: "IPChanged",
|
singleton.ServerList[clientID].Name, singleton.IPDesensitize(singleton.ServerList[clientID].Host.IP),
|
||||||
// }),
|
singleton.IPDesensitize(host.IP),
|
||||||
// singleton.ServerList[clientID].Name, singleton.IPDesensitize(singleton.ServerList[clientID].Host.IP),
|
),
|
||||||
// singleton.IPDesensitize(host.IP),
|
nil)
|
||||||
// ),
|
|
||||||
// nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -602,14 +602,17 @@ func StatusCodeToString(statusCode int) string {
|
|||||||
switch statusCode {
|
switch statusCode {
|
||||||
case StatusNoData:
|
case StatusNoData:
|
||||||
// return Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "StatusNoData"})
|
// return Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "StatusNoData"})
|
||||||
|
return "No Data"
|
||||||
case StatusGood:
|
case StatusGood:
|
||||||
// return Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "StatusGood"})
|
// return Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "StatusGood"})
|
||||||
|
return "Good"
|
||||||
case StatusLowAvailability:
|
case StatusLowAvailability:
|
||||||
// return Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "StatusLowAvailability"})
|
// return Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "StatusLowAvailability"})
|
||||||
|
return "Low Availability"
|
||||||
case StatusDown:
|
case StatusDown:
|
||||||
// return Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "StatusDown"})
|
// return Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "StatusDown"})
|
||||||
|
return "Down"
|
||||||
default:
|
default:
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user