mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-06 05:30:05 +00:00
theme-mdui by @MikoyChinese
This commit is contained in:
@@ -17,7 +17,7 @@ import (
|
||||
"github.com/naiba/nezha/model"
|
||||
"github.com/naiba/nezha/pkg/mygin"
|
||||
"github.com/naiba/nezha/proto"
|
||||
"github.com/naiba/nezha/service/dao"
|
||||
"github.com/naiba/nezha/service/singleton"
|
||||
)
|
||||
|
||||
type terminalContext struct {
|
||||
@@ -54,7 +54,7 @@ func (p *commonPage) issueViewPassword(c *gin.Context) {
|
||||
var vpf viewPasswordForm
|
||||
err := c.ShouldBind(&vpf)
|
||||
var hash []byte
|
||||
if err == nil && vpf.Password != dao.Conf.Site.ViewPassword {
|
||||
if err == nil && vpf.Password != singleton.Conf.Site.ViewPassword {
|
||||
err = errors.New("查看密码错误")
|
||||
}
|
||||
if err == nil {
|
||||
@@ -69,12 +69,12 @@ func (p *commonPage) issueViewPassword(c *gin.Context) {
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.SetCookie(dao.Conf.Site.CookieName+"-vp", string(hash), 60*60*24, "", "", false, false)
|
||||
c.SetCookie(singleton.Conf.Site.CookieName+"-vp", string(hash), 60*60*24, "", "", false, false)
|
||||
c.Redirect(http.StatusFound, c.Request.Referer())
|
||||
}
|
||||
|
||||
func (p *commonPage) checkViewPassword(c *gin.Context) {
|
||||
if dao.Conf.Site.ViewPassword == "" {
|
||||
if singleton.Conf.Site.ViewPassword == "" {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
@@ -84,11 +84,11 @@ func (p *commonPage) checkViewPassword(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 验证查看密码
|
||||
viewPassword, _ := c.Cookie(dao.Conf.Site.CookieName + "-vp")
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(viewPassword), []byte(dao.Conf.Site.ViewPassword)); err != nil {
|
||||
c.HTML(http.StatusOK, "theme-"+dao.Conf.Site.Theme+"/viewpassword", mygin.CommonEnvironment(c, gin.H{
|
||||
viewPassword, _ := c.Cookie(singleton.Conf.Site.CookieName + "-vp")
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(viewPassword), []byte(singleton.Conf.Site.ViewPassword)); err != nil {
|
||||
c.HTML(http.StatusOK, "theme-"+singleton.Conf.Site.Theme+"/viewpassword", mygin.CommonEnvironment(c, gin.H{
|
||||
"Title": "验证查看密码",
|
||||
"CustomCode": dao.Conf.Site.CustomCode,
|
||||
"CustomCode": singleton.Conf.Site.CustomCode,
|
||||
}))
|
||||
c.Abort()
|
||||
return
|
||||
@@ -98,22 +98,22 @@ func (p *commonPage) checkViewPassword(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (p *commonPage) service(c *gin.Context) {
|
||||
dao.AlertsLock.RLock()
|
||||
defer dao.AlertsLock.RUnlock()
|
||||
c.HTML(http.StatusOK, "theme-"+dao.Conf.Site.Theme+"/service", mygin.CommonEnvironment(c, gin.H{
|
||||
singleton.AlertsLock.RLock()
|
||||
defer singleton.AlertsLock.RUnlock()
|
||||
c.HTML(http.StatusOK, "theme-"+singleton.Conf.Site.Theme+"/service", mygin.CommonEnvironment(c, gin.H{
|
||||
"Title": "服务状态",
|
||||
"Services": dao.ServiceSentinelShared.LoadStats(),
|
||||
"CycleTransferStats": dao.AlertsCycleTransferStatsStore,
|
||||
"CustomCode": dao.Conf.Site.CustomCode,
|
||||
"Services": singleton.ServiceSentinelShared.LoadStats(),
|
||||
"CycleTransferStats": singleton.AlertsCycleTransferStatsStore,
|
||||
"CustomCode": singleton.Conf.Site.CustomCode,
|
||||
}))
|
||||
}
|
||||
|
||||
func (cp *commonPage) home(c *gin.Context) {
|
||||
dao.SortedServerLock.RLock()
|
||||
defer dao.SortedServerLock.RUnlock()
|
||||
c.HTML(http.StatusOK, "theme-"+dao.Conf.Site.Theme+"/home", mygin.CommonEnvironment(c, gin.H{
|
||||
"Servers": dao.SortedServerList,
|
||||
"CustomCode": dao.Conf.Site.CustomCode,
|
||||
singleton.SortedServerLock.RLock()
|
||||
defer singleton.SortedServerLock.RUnlock()
|
||||
c.HTML(http.StatusOK, "theme-"+singleton.Conf.Site.Theme+"/home", mygin.CommonEnvironment(c, gin.H{
|
||||
"Servers": singleton.SortedServerList,
|
||||
"CustomCode": singleton.Conf.Site.CustomCode,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -142,12 +142,12 @@ func (cp *commonPage) ws(c *gin.Context) {
|
||||
defer conn.Close()
|
||||
count := 0
|
||||
for {
|
||||
dao.SortedServerLock.RLock()
|
||||
singleton.SortedServerLock.RLock()
|
||||
err = conn.WriteJSON(Data{
|
||||
Now: time.Now().Unix() * 1000,
|
||||
Servers: dao.SortedServerList,
|
||||
Servers: singleton.SortedServerList,
|
||||
})
|
||||
dao.SortedServerLock.RUnlock()
|
||||
singleton.SortedServerLock.RUnlock()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
@@ -190,9 +190,9 @@ func (cp *commonPage) terminal(c *gin.Context) {
|
||||
var isAgent bool
|
||||
|
||||
if _, authorized := c.Get(model.CtxKeyAuthorizedUser); !authorized {
|
||||
dao.ServerLock.RLock()
|
||||
_, hasID := dao.SecretToID[c.Request.Header.Get("Secret")]
|
||||
dao.ServerLock.RUnlock()
|
||||
singleton.ServerLock.RLock()
|
||||
_, hasID := singleton.SecretToID[c.Request.Header.Get("Secret")]
|
||||
singleton.ServerLock.RUnlock()
|
||||
if !hasID {
|
||||
mygin.ShowErrorPage(c, mygin.ErrInfo{
|
||||
Code: http.StatusForbidden,
|
||||
@@ -225,9 +225,9 @@ func (cp *commonPage) terminal(c *gin.Context) {
|
||||
}
|
||||
isAgent = true
|
||||
} else {
|
||||
dao.ServerLock.RLock()
|
||||
server := dao.ServerList[terminal.serverID]
|
||||
dao.ServerLock.RUnlock()
|
||||
singleton.ServerLock.RLock()
|
||||
server := singleton.ServerList[terminal.serverID]
|
||||
singleton.ServerLock.RUnlock()
|
||||
if server == nil || server.TaskStream == nil {
|
||||
mygin.ShowErrorPage(c, mygin.ErrInfo{
|
||||
Code: http.StatusForbidden,
|
||||
@@ -407,9 +407,9 @@ func (cp *commonPage) createTerminal(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
dao.ServerLock.RLock()
|
||||
server := dao.ServerList[createTerminalReq.ID]
|
||||
dao.ServerLock.RUnlock()
|
||||
singleton.ServerLock.RLock()
|
||||
server := singleton.ServerList[createTerminalReq.ID]
|
||||
singleton.ServerLock.RUnlock()
|
||||
if server == nil || server.TaskStream == nil {
|
||||
mygin.ShowErrorPage(c, mygin.ErrInfo{
|
||||
Code: http.StatusForbidden,
|
||||
|
||||
@@ -14,13 +14,13 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/naiba/nezha/pkg/mygin"
|
||||
"github.com/naiba/nezha/service/dao"
|
||||
"github.com/naiba/nezha/service/singleton"
|
||||
)
|
||||
|
||||
func ServeWeb(port uint) *http.Server {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.Default()
|
||||
if dao.Conf.Debug {
|
||||
if singleton.Conf.Debug {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
pprof.Register(r)
|
||||
}
|
||||
|
||||
@@ -4,9 +4,10 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/naiba/nezha/model"
|
||||
"github.com/naiba/nezha/pkg/mygin"
|
||||
"github.com/naiba/nezha/service/dao"
|
||||
"github.com/naiba/nezha/service/singleton"
|
||||
)
|
||||
|
||||
type guestPage struct {
|
||||
@@ -34,7 +35,7 @@ func (gp *guestPage) serve() {
|
||||
func (gp *guestPage) login(c *gin.Context) {
|
||||
LoginType := "GitHub"
|
||||
RegistrationLink := "https://github.com/join"
|
||||
if dao.Conf.Oauth2.Type == model.ConfigTypeGitee {
|
||||
if singleton.Conf.Oauth2.Type == model.ConfigTypeGitee {
|
||||
LoginType = "Gitee"
|
||||
RegistrationLink = "https://gitee.com/signup"
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
"github.com/naiba/nezha/pkg/mygin"
|
||||
"github.com/naiba/nezha/pkg/utils"
|
||||
"github.com/naiba/nezha/proto"
|
||||
"github.com/naiba/nezha/service/dao"
|
||||
"github.com/naiba/nezha/service/singleton"
|
||||
)
|
||||
|
||||
type memberAPI struct {
|
||||
@@ -59,53 +59,53 @@ func (ma *memberAPI) delete(c *gin.Context) {
|
||||
var err error
|
||||
switch c.Param("model") {
|
||||
case "server":
|
||||
err = dao.DB.Unscoped().Delete(&model.Server{}, "id = ?", id).Error
|
||||
err = singleton.DB.Unscoped().Delete(&model.Server{}, "id = ?", id).Error
|
||||
if err == nil {
|
||||
// 删除服务器
|
||||
dao.ServerLock.Lock()
|
||||
delete(dao.SecretToID, dao.ServerList[id].Secret)
|
||||
delete(dao.ServerList, id)
|
||||
dao.ServerLock.Unlock()
|
||||
dao.ReSortServer()
|
||||
singleton.ServerLock.Lock()
|
||||
delete(singleton.SecretToID, singleton.ServerList[id].Secret)
|
||||
delete(singleton.ServerList, id)
|
||||
singleton.ServerLock.Unlock()
|
||||
singleton.ReSortServer()
|
||||
// 删除循环流量状态中的此服务器相关的记录
|
||||
dao.AlertsLock.Lock()
|
||||
for i := 0; i < len(dao.Alerts); i++ {
|
||||
if dao.AlertsCycleTransferStatsStore[dao.Alerts[i].ID] != nil {
|
||||
delete(dao.AlertsCycleTransferStatsStore[dao.Alerts[i].ID].ServerName, id)
|
||||
delete(dao.AlertsCycleTransferStatsStore[dao.Alerts[i].ID].Transfer, id)
|
||||
delete(dao.AlertsCycleTransferStatsStore[dao.Alerts[i].ID].NextUpdate, id)
|
||||
singleton.AlertsLock.Lock()
|
||||
for i := 0; i < len(singleton.Alerts); i++ {
|
||||
if singleton.AlertsCycleTransferStatsStore[singleton.Alerts[i].ID] != nil {
|
||||
delete(singleton.AlertsCycleTransferStatsStore[singleton.Alerts[i].ID].ServerName, id)
|
||||
delete(singleton.AlertsCycleTransferStatsStore[singleton.Alerts[i].ID].Transfer, id)
|
||||
delete(singleton.AlertsCycleTransferStatsStore[singleton.Alerts[i].ID].NextUpdate, id)
|
||||
}
|
||||
}
|
||||
dao.AlertsLock.Unlock()
|
||||
singleton.AlertsLock.Unlock()
|
||||
// 删除服务器相关循环流量记录
|
||||
dao.DB.Unscoped().Delete(&model.Transfer{}, "server_id = ?", id)
|
||||
singleton.DB.Unscoped().Delete(&model.Transfer{}, "server_id = ?", id)
|
||||
}
|
||||
case "notification":
|
||||
err = dao.DB.Unscoped().Delete(&model.Notification{}, "id = ?", id).Error
|
||||
err = singleton.DB.Unscoped().Delete(&model.Notification{}, "id = ?", id).Error
|
||||
if err == nil {
|
||||
dao.OnDeleteNotification(id)
|
||||
singleton.OnDeleteNotification(id)
|
||||
}
|
||||
case "monitor":
|
||||
err = dao.DB.Unscoped().Delete(&model.Monitor{}, "id = ?", id).Error
|
||||
err = singleton.DB.Unscoped().Delete(&model.Monitor{}, "id = ?", id).Error
|
||||
if err == nil {
|
||||
dao.ServiceSentinelShared.OnMonitorDelete(id)
|
||||
err = dao.DB.Unscoped().Delete(&model.MonitorHistory{}, "monitor_id = ?", id).Error
|
||||
singleton.ServiceSentinelShared.OnMonitorDelete(id)
|
||||
err = singleton.DB.Unscoped().Delete(&model.MonitorHistory{}, "monitor_id = ?", id).Error
|
||||
}
|
||||
case "cron":
|
||||
err = dao.DB.Unscoped().Delete(&model.Cron{}, "id = ?", id).Error
|
||||
err = singleton.DB.Unscoped().Delete(&model.Cron{}, "id = ?", id).Error
|
||||
if err == nil {
|
||||
dao.CronLock.RLock()
|
||||
defer dao.CronLock.RUnlock()
|
||||
cr := dao.Crons[id]
|
||||
singleton.CronLock.RLock()
|
||||
defer singleton.CronLock.RUnlock()
|
||||
cr := singleton.Crons[id]
|
||||
if cr != nil && cr.CronJobID != 0 {
|
||||
dao.Cron.Remove(cr.CronJobID)
|
||||
singleton.Cron.Remove(cr.CronJobID)
|
||||
}
|
||||
delete(dao.Crons, id)
|
||||
delete(singleton.Crons, id)
|
||||
}
|
||||
case "alert-rule":
|
||||
err = dao.DB.Unscoped().Delete(&model.AlertRule{}, "id = ?", id).Error
|
||||
err = singleton.DB.Unscoped().Delete(&model.AlertRule{}, "id = ?", id).Error
|
||||
if err == nil {
|
||||
dao.OnDeleteAlert(id)
|
||||
singleton.OnDeleteAlert(id)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
@@ -129,7 +129,7 @@ type searchResult struct {
|
||||
func (ma *memberAPI) searchServer(c *gin.Context) {
|
||||
var servers []model.Server
|
||||
likeWord := "%" + c.Query("word") + "%"
|
||||
dao.DB.Select("id,name").Where("id = ? OR name LIKE ? OR tag LIKE ? OR note LIKE ?",
|
||||
singleton.DB.Select("id,name").Where("id = ? OR name LIKE ? OR tag LIKE ? OR note LIKE ?",
|
||||
c.Query("word"), likeWord, likeWord, likeWord).Find(&servers)
|
||||
|
||||
var resp []searchResult
|
||||
@@ -172,10 +172,10 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) {
|
||||
if s.ID == 0 {
|
||||
s.Secret = utils.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, admin.ID))
|
||||
s.Secret = s.Secret[:18]
|
||||
err = dao.DB.Create(&s).Error
|
||||
err = singleton.DB.Create(&s).Error
|
||||
} else {
|
||||
isEdit = true
|
||||
err = dao.DB.Save(&s).Error
|
||||
err = singleton.DB.Save(&s).Error
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
@@ -186,26 +186,26 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if isEdit {
|
||||
dao.ServerLock.Lock()
|
||||
s.CopyFromRunningServer(dao.ServerList[s.ID])
|
||||
singleton.ServerLock.Lock()
|
||||
s.CopyFromRunningServer(singleton.ServerList[s.ID])
|
||||
// 如果修改了 Secret
|
||||
if s.Secret != dao.ServerList[s.ID].Secret {
|
||||
if s.Secret != singleton.ServerList[s.ID].Secret {
|
||||
// 删除旧 Secret-ID 绑定关系
|
||||
dao.SecretToID[s.Secret] = s.ID
|
||||
singleton.SecretToID[s.Secret] = s.ID
|
||||
// 设置新的 Secret-ID 绑定关系
|
||||
delete(dao.SecretToID, dao.ServerList[s.ID].Secret)
|
||||
delete(singleton.SecretToID, singleton.ServerList[s.ID].Secret)
|
||||
}
|
||||
dao.ServerList[s.ID] = &s
|
||||
dao.ServerLock.Unlock()
|
||||
singleton.ServerList[s.ID] = &s
|
||||
singleton.ServerLock.Unlock()
|
||||
} else {
|
||||
s.Host = &model.Host{}
|
||||
s.State = &model.HostState{}
|
||||
dao.ServerLock.Lock()
|
||||
dao.SecretToID[s.Secret] = s.ID
|
||||
dao.ServerList[s.ID] = &s
|
||||
dao.ServerLock.Unlock()
|
||||
singleton.ServerLock.Lock()
|
||||
singleton.SecretToID[s.Secret] = s.ID
|
||||
singleton.ServerList[s.ID] = &s
|
||||
singleton.ServerLock.Unlock()
|
||||
}
|
||||
dao.ReSortServer()
|
||||
singleton.ReSortServer()
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
})
|
||||
@@ -238,13 +238,13 @@ func (ma *memberAPI) addOrEditMonitor(c *gin.Context) {
|
||||
}
|
||||
if err == nil {
|
||||
if m.ID == 0 {
|
||||
err = dao.DB.Create(&m).Error
|
||||
err = singleton.DB.Create(&m).Error
|
||||
} else {
|
||||
err = dao.DB.Save(&m).Error
|
||||
err = singleton.DB.Save(&m).Error
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
err = dao.ServiceSentinelShared.OnMonitorUpdate(m)
|
||||
err = singleton.ServiceSentinelShared.OnMonitorUpdate(m)
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
@@ -282,7 +282,7 @@ func (ma *memberAPI) addOrEditCron(c *gin.Context) {
|
||||
cr.Cover = cf.Cover
|
||||
err = json.Unmarshal([]byte(cf.ServersRaw), &cr.Servers)
|
||||
}
|
||||
tx := dao.DB.Begin()
|
||||
tx := singleton.DB.Begin()
|
||||
if err == nil {
|
||||
if cf.ID == 0 {
|
||||
err = tx.Create(&cr).Error
|
||||
@@ -291,7 +291,7 @@ func (ma *memberAPI) addOrEditCron(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
cr.CronJobID, err = dao.Cron.AddFunc(cr.Scheduler, dao.CronTrigger(cr))
|
||||
cr.CronJobID, err = singleton.Cron.AddFunc(cr.Scheduler, singleton.CronTrigger(cr))
|
||||
}
|
||||
if err == nil {
|
||||
err = tx.Commit().Error
|
||||
@@ -306,15 +306,15 @@ func (ma *memberAPI) addOrEditCron(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
dao.CronLock.Lock()
|
||||
defer dao.CronLock.Unlock()
|
||||
crOld := dao.Crons[cr.ID]
|
||||
singleton.CronLock.Lock()
|
||||
defer singleton.CronLock.Unlock()
|
||||
crOld := singleton.Crons[cr.ID]
|
||||
if crOld != nil && crOld.CronJobID != 0 {
|
||||
dao.Cron.Remove(crOld.CronJobID)
|
||||
singleton.Cron.Remove(crOld.CronJobID)
|
||||
}
|
||||
|
||||
delete(dao.Crons, cr.ID)
|
||||
dao.Crons[cr.ID] = &cr
|
||||
delete(singleton.Crons, cr.ID)
|
||||
singleton.Crons[cr.ID] = &cr
|
||||
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
@@ -323,7 +323,7 @@ func (ma *memberAPI) addOrEditCron(c *gin.Context) {
|
||||
|
||||
func (ma *memberAPI) manualTrigger(c *gin.Context) {
|
||||
var cr model.Cron
|
||||
if err := dao.DB.First(&cr, "id = ?", c.Param("id")).Error; err != nil {
|
||||
if err := singleton.DB.First(&cr, "id = ?", c.Param("id")).Error; err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: err.Error(),
|
||||
@@ -331,7 +331,7 @@ func (ma *memberAPI) manualTrigger(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
dao.ManualTrigger(cr)
|
||||
singleton.ManualTrigger(cr)
|
||||
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
@@ -351,9 +351,9 @@ func (ma *memberAPI) forceUpdate(c *gin.Context) {
|
||||
var executeResult bytes.Buffer
|
||||
|
||||
for i := 0; i < len(forceUpdateServers); i++ {
|
||||
dao.ServerLock.RLock()
|
||||
server := dao.ServerList[forceUpdateServers[i]]
|
||||
dao.ServerLock.RUnlock()
|
||||
singleton.ServerLock.RLock()
|
||||
server := singleton.ServerList[forceUpdateServers[i]]
|
||||
singleton.ServerLock.RUnlock()
|
||||
if server != nil && server.TaskStream != nil {
|
||||
if err := server.TaskStream.Send(&proto.Task{
|
||||
Type: model.TaskTypeUpgrade,
|
||||
@@ -402,9 +402,9 @@ func (ma *memberAPI) addOrEditNotification(c *gin.Context) {
|
||||
}
|
||||
if err == nil {
|
||||
if n.ID == 0 {
|
||||
err = dao.DB.Create(&n).Error
|
||||
err = singleton.DB.Create(&n).Error
|
||||
} else {
|
||||
err = dao.DB.Save(&n).Error
|
||||
err = singleton.DB.Save(&n).Error
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
@@ -414,7 +414,7 @@ func (ma *memberAPI) addOrEditNotification(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
dao.OnRefreshOrAddNotification(n)
|
||||
singleton.OnRefreshOrAddNotification(n)
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
})
|
||||
@@ -453,9 +453,9 @@ func (ma *memberAPI) addOrEditAlertRule(c *gin.Context) {
|
||||
r.Enable = &enable
|
||||
r.ID = arf.ID
|
||||
if r.ID == 0 {
|
||||
err = dao.DB.Create(&r).Error
|
||||
err = singleton.DB.Create(&r).Error
|
||||
} else {
|
||||
err = dao.DB.Save(&r).Error
|
||||
err = singleton.DB.Save(&r).Error
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
@@ -465,7 +465,7 @@ func (ma *memberAPI) addOrEditAlertRule(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
dao.OnRefreshOrAddAlert(r)
|
||||
singleton.OnRefreshOrAddAlert(r)
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
})
|
||||
@@ -492,7 +492,7 @@ func (ma *memberAPI) logout(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
dao.DB.Model(admin).UpdateColumns(model.User{
|
||||
singleton.DB.Model(admin).UpdateColumns(model.User{
|
||||
Token: "",
|
||||
TokenExpired: time.Now(),
|
||||
})
|
||||
@@ -522,16 +522,16 @@ func (ma *memberAPI) updateSetting(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
dao.Conf.EnableIPChangeNotification = sf.EnableIPChangeNotification == "on"
|
||||
dao.Conf.Cover = sf.Cover
|
||||
dao.Conf.GRPCHost = sf.GRPCHost
|
||||
dao.Conf.IgnoredIPNotification = sf.IgnoredIPNotification
|
||||
dao.Conf.Site.Brand = sf.Title
|
||||
dao.Conf.Site.Theme = sf.Theme
|
||||
dao.Conf.Site.CustomCode = sf.CustomCode
|
||||
dao.Conf.Site.ViewPassword = sf.ViewPassword
|
||||
dao.Conf.Oauth2.Admin = sf.Admin
|
||||
if err := dao.Conf.Save(); err != nil {
|
||||
singleton.Conf.EnableIPChangeNotification = sf.EnableIPChangeNotification == "on"
|
||||
singleton.Conf.Cover = sf.Cover
|
||||
singleton.Conf.GRPCHost = sf.GRPCHost
|
||||
singleton.Conf.IgnoredIPNotification = sf.IgnoredIPNotification
|
||||
singleton.Conf.Site.Brand = sf.Title
|
||||
singleton.Conf.Site.Theme = sf.Theme
|
||||
singleton.Conf.Site.CustomCode = sf.CustomCode
|
||||
singleton.Conf.Site.ViewPassword = sf.ViewPassword
|
||||
singleton.Conf.Oauth2.Admin = sf.Admin
|
||||
if err := singleton.Conf.Save(); err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: fmt.Sprintf("请求错误:%s", err),
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/naiba/nezha/model"
|
||||
"github.com/naiba/nezha/pkg/mygin"
|
||||
"github.com/naiba/nezha/service/dao"
|
||||
"github.com/naiba/nezha/service/singleton"
|
||||
)
|
||||
|
||||
type memberPage struct {
|
||||
@@ -30,24 +30,24 @@ func (mp *memberPage) serve() {
|
||||
}
|
||||
|
||||
func (mp *memberPage) server(c *gin.Context) {
|
||||
dao.SortedServerLock.RLock()
|
||||
defer dao.SortedServerLock.RUnlock()
|
||||
singleton.SortedServerLock.RLock()
|
||||
defer singleton.SortedServerLock.RUnlock()
|
||||
c.HTML(http.StatusOK, "dashboard/server", mygin.CommonEnvironment(c, gin.H{
|
||||
"Title": "服务器管理",
|
||||
"Servers": dao.SortedServerList,
|
||||
"Servers": singleton.SortedServerList,
|
||||
}))
|
||||
}
|
||||
|
||||
func (mp *memberPage) monitor(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "dashboard/monitor", mygin.CommonEnvironment(c, gin.H{
|
||||
"Title": "服务监控",
|
||||
"Monitors": dao.ServiceSentinelShared.Monitors(),
|
||||
"Monitors": singleton.ServiceSentinelShared.Monitors(),
|
||||
}))
|
||||
}
|
||||
|
||||
func (mp *memberPage) cron(c *gin.Context) {
|
||||
var crons []model.Cron
|
||||
dao.DB.Find(&crons)
|
||||
singleton.DB.Find(&crons)
|
||||
c.HTML(http.StatusOK, "dashboard/cron", mygin.CommonEnvironment(c, gin.H{
|
||||
"Title": "计划任务",
|
||||
"Crons": crons,
|
||||
@@ -56,9 +56,9 @@ func (mp *memberPage) cron(c *gin.Context) {
|
||||
|
||||
func (mp *memberPage) notification(c *gin.Context) {
|
||||
var nf []model.Notification
|
||||
dao.DB.Find(&nf)
|
||||
singleton.DB.Find(&nf)
|
||||
var ar []model.AlertRule
|
||||
dao.DB.Find(&ar)
|
||||
singleton.DB.Find(&ar)
|
||||
c.HTML(http.StatusOK, "dashboard/notification", mygin.CommonEnvironment(c, gin.H{
|
||||
"Title": "报警通知",
|
||||
"Notifications": nf,
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/naiba/nezha/model"
|
||||
"github.com/naiba/nezha/pkg/mygin"
|
||||
"github.com/naiba/nezha/pkg/utils"
|
||||
"github.com/naiba/nezha/service/dao"
|
||||
"github.com/naiba/nezha/service/singleton"
|
||||
)
|
||||
|
||||
type oauth2controller struct {
|
||||
@@ -28,10 +28,10 @@ func (oa *oauth2controller) serve() {
|
||||
}
|
||||
|
||||
func (oa *oauth2controller) getCommonOauth2Config(c *gin.Context) *oauth2.Config {
|
||||
if dao.Conf.Oauth2.Type == model.ConfigTypeGitee {
|
||||
if singleton.Conf.Oauth2.Type == model.ConfigTypeGitee {
|
||||
return &oauth2.Config{
|
||||
ClientID: dao.Conf.Oauth2.ClientID,
|
||||
ClientSecret: dao.Conf.Oauth2.ClientSecret,
|
||||
ClientID: singleton.Conf.Oauth2.ClientID,
|
||||
ClientSecret: singleton.Conf.Oauth2.ClientSecret,
|
||||
Scopes: []string{},
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: "https://gitee.com/oauth/authorize",
|
||||
@@ -41,8 +41,8 @@ func (oa *oauth2controller) getCommonOauth2Config(c *gin.Context) *oauth2.Config
|
||||
}
|
||||
} else {
|
||||
return &oauth2.Config{
|
||||
ClientID: dao.Conf.Oauth2.ClientID,
|
||||
ClientSecret: dao.Conf.Oauth2.ClientSecret,
|
||||
ClientID: singleton.Conf.Oauth2.ClientID,
|
||||
ClientSecret: singleton.Conf.Oauth2.ClientSecret,
|
||||
Scopes: []string{},
|
||||
Endpoint: GitHubOauth2.Endpoint,
|
||||
}
|
||||
@@ -59,7 +59,7 @@ func (oa *oauth2controller) getRedirectURL(c *gin.Context) string {
|
||||
|
||||
func (oa *oauth2controller) login(c *gin.Context) {
|
||||
state := utils.RandStringBytesMaskImprSrcUnsafe(6)
|
||||
dao.Cache.Set(fmt.Sprintf("%s%s", model.CacheKeyOauth2State, c.ClientIP()), state, 0)
|
||||
singleton.Cache.Set(fmt.Sprintf("%s%s", model.CacheKeyOauth2State, c.ClientIP()), state, 0)
|
||||
url := oa.getCommonOauth2Config(c).AuthCodeURL(state, oauth2.AccessTypeOnline)
|
||||
c.Redirect(http.StatusFound, url)
|
||||
}
|
||||
@@ -67,7 +67,7 @@ func (oa *oauth2controller) login(c *gin.Context) {
|
||||
func (oa *oauth2controller) callback(c *gin.Context) {
|
||||
var err error
|
||||
// 验证登录跳转时的 State
|
||||
state, ok := dao.Cache.Get(fmt.Sprintf("%s%s", model.CacheKeyOauth2State, c.ClientIP()))
|
||||
state, ok := singleton.Cache.Get(fmt.Sprintf("%s%s", model.CacheKeyOauth2State, c.ClientIP()))
|
||||
if !ok || state.(string) != c.Query("state") {
|
||||
err = errors.New("非法的登录方式")
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func (oa *oauth2controller) callback(c *gin.Context) {
|
||||
var client *GitHubAPI.Client
|
||||
if err == nil {
|
||||
oc := oauth2Config.Client(ctx, otk)
|
||||
if dao.Conf.Oauth2.Type == model.ConfigTypeGitee {
|
||||
if singleton.Conf.Oauth2.Type == model.ConfigTypeGitee {
|
||||
client, err = GitHubAPI.NewEnterpriseClient("https://gitee.com/api/v5/", "https://gitee.com/api/v5/", oc)
|
||||
} else {
|
||||
client = GitHubAPI.NewClient(oc)
|
||||
@@ -99,7 +99,7 @@ func (oa *oauth2controller) callback(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
var isAdmin bool
|
||||
for _, admin := range strings.Split(dao.Conf.Oauth2.Admin, ",") {
|
||||
for _, admin := range strings.Split(singleton.Conf.Oauth2.Admin, ",") {
|
||||
if admin != "" && gu.GetLogin() == admin {
|
||||
isAdmin = true
|
||||
break
|
||||
@@ -115,8 +115,8 @@ func (oa *oauth2controller) callback(c *gin.Context) {
|
||||
}
|
||||
user := model.NewUserFromGitHub(gu)
|
||||
user.IssueNewToken()
|
||||
dao.DB.Save(&user)
|
||||
c.SetCookie(dao.Conf.Site.CookieName, user.Token, 60*60*24, "", "", false, false)
|
||||
singleton.DB.Save(&user)
|
||||
c.SetCookie(singleton.Conf.Site.CookieName, user.Token, 60*60*24, "", "", false, false)
|
||||
c.Status(http.StatusOK)
|
||||
c.Writer.WriteString("<script>window.location.href='/'</script>")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user