2019-12-08 03:59:58 -05:00
|
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
2020-12-19 09:14:36 -05:00
|
|
|
|
"encoding/json"
|
2020-12-19 23:18:27 -05:00
|
|
|
|
"errors"
|
2019-12-08 03:59:58 -05:00
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
2020-03-22 08:55:27 -04:00
|
|
|
|
"strconv"
|
2019-12-08 03:59:58 -05:00
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-01-18 20:59:04 -05:00
|
|
|
|
"github.com/robfig/cron/v3"
|
2019-12-08 03:59:58 -05:00
|
|
|
|
|
2020-11-10 21:07:45 -05:00
|
|
|
|
"github.com/naiba/nezha/model"
|
|
|
|
|
"github.com/naiba/nezha/pkg/mygin"
|
2021-01-18 20:59:04 -05:00
|
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
|
|
|
|
pb "github.com/naiba/nezha/proto"
|
2020-11-10 21:07:45 -05:00
|
|
|
|
"github.com/naiba/nezha/service/dao"
|
2019-12-08 03:59:58 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type memberAPI struct {
|
|
|
|
|
r gin.IRouter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ma *memberAPI) serve() {
|
|
|
|
|
mr := ma.r.Group("")
|
|
|
|
|
mr.Use(mygin.Authorize(mygin.AuthorizeOption{
|
|
|
|
|
Member: true,
|
|
|
|
|
IsPage: false,
|
|
|
|
|
Msg: "访问此接口需要登录",
|
|
|
|
|
Btn: "点此登录",
|
|
|
|
|
Redirect: "/login",
|
|
|
|
|
}))
|
|
|
|
|
|
2021-01-20 09:15:47 -05:00
|
|
|
|
mr.GET("/search-server", ma.searchServer)
|
2019-12-20 10:58:09 -05:00
|
|
|
|
mr.POST("/server", ma.addOrEditServer)
|
2021-01-15 11:45:49 -05:00
|
|
|
|
mr.POST("/monitor", ma.addOrEditMonitor)
|
2021-01-18 20:59:04 -05:00
|
|
|
|
mr.POST("/cron", ma.addOrEditCron)
|
2021-01-23 20:41:35 -05:00
|
|
|
|
mr.GET("/cron/:id/manual", ma.manualTrigger)
|
2020-12-19 09:14:36 -05:00
|
|
|
|
mr.POST("/notification", ma.addOrEditNotification)
|
2020-12-19 10:11:16 -05:00
|
|
|
|
mr.POST("/alert-rule", ma.addOrEditAlertRule)
|
2020-12-09 06:05:40 -05:00
|
|
|
|
mr.POST("/setting", ma.updateSetting)
|
2020-12-19 10:11:16 -05:00
|
|
|
|
mr.DELETE("/:model/:id", ma.delete)
|
2021-01-20 09:15:47 -05:00
|
|
|
|
mr.POST("/logout", ma.logout)
|
2020-03-22 08:55:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ma *memberAPI) delete(c *gin.Context) {
|
|
|
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
|
|
|
if id < 1 {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: "错误的 Server ID",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-12-08 10:18:29 -05:00
|
|
|
|
|
2020-12-19 10:11:16 -05:00
|
|
|
|
var err error
|
|
|
|
|
switch c.Param("model") {
|
|
|
|
|
case "server":
|
|
|
|
|
err = dao.DB.Delete(&model.Server{}, "id = ?", id).Error
|
|
|
|
|
if err == nil {
|
2021-01-17 09:18:36 -05:00
|
|
|
|
dao.ServerLock.Lock()
|
2021-01-30 04:10:51 -05:00
|
|
|
|
delete(dao.SecretToID, dao.ServerList[id].Secret)
|
2021-01-08 08:04:50 -05:00
|
|
|
|
delete(dao.ServerList, id)
|
2021-01-17 09:18:36 -05:00
|
|
|
|
dao.ServerLock.Unlock()
|
2021-01-08 08:04:50 -05:00
|
|
|
|
dao.ReSortServer()
|
2020-12-19 10:11:16 -05:00
|
|
|
|
}
|
|
|
|
|
case "notification":
|
|
|
|
|
err = dao.DB.Delete(&model.Notification{}, "id = ?", id).Error
|
2020-12-19 23:18:27 -05:00
|
|
|
|
if err == nil {
|
2021-01-23 20:41:35 -05:00
|
|
|
|
dao.OnDeleteNotification(id)
|
2020-12-19 23:18:27 -05:00
|
|
|
|
}
|
2021-01-15 11:45:49 -05:00
|
|
|
|
case "monitor":
|
|
|
|
|
err = dao.DB.Delete(&model.Monitor{}, "id = ?", id).Error
|
2021-01-16 02:05:35 -05:00
|
|
|
|
if err == nil {
|
2021-04-17 11:36:37 -04:00
|
|
|
|
dao.ServiceSentinelShared.OnMonitorDelete(id)
|
2021-01-16 02:05:35 -05:00
|
|
|
|
err = dao.DB.Delete(&model.MonitorHistory{}, "monitor_id = ?", id).Error
|
|
|
|
|
}
|
2021-01-18 20:59:04 -05:00
|
|
|
|
case "cron":
|
|
|
|
|
err = dao.DB.Delete(&model.Cron{}, "id = ?", id).Error
|
|
|
|
|
if err == nil {
|
|
|
|
|
dao.CronLock.RLock()
|
|
|
|
|
defer dao.CronLock.RUnlock()
|
2021-01-23 02:32:04 -05:00
|
|
|
|
cr := dao.Crons[id]
|
|
|
|
|
if cr != nil && cr.CronID != 0 {
|
|
|
|
|
dao.Cron.Remove(cr.CronID)
|
2021-01-18 20:59:04 -05:00
|
|
|
|
}
|
|
|
|
|
delete(dao.Crons, id)
|
|
|
|
|
}
|
2020-12-19 10:11:16 -05:00
|
|
|
|
case "alert-rule":
|
|
|
|
|
err = dao.DB.Delete(&model.AlertRule{}, "id = ?", id).Error
|
2020-12-19 23:18:27 -05:00
|
|
|
|
if err == nil {
|
2021-01-23 20:41:35 -05:00
|
|
|
|
dao.OnDeleteAlert(id)
|
2020-12-19 23:18:27 -05:00
|
|
|
|
}
|
2020-12-19 09:14:36 -05:00
|
|
|
|
}
|
2020-12-19 10:11:16 -05:00
|
|
|
|
if err != nil {
|
2020-12-19 09:14:36 -05:00
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: fmt.Sprintf("数据库错误:%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
})
|
2021-01-20 09:15:47 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type searchResult struct {
|
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
|
Value uint64 `json:"value,omitempty"`
|
|
|
|
|
Text string `json:"text,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 ?",
|
|
|
|
|
c.Query("word"), likeWord, likeWord, likeWord).Find(&servers)
|
|
|
|
|
|
|
|
|
|
var resp []searchResult
|
|
|
|
|
for i := 0; i < len(servers); i++ {
|
|
|
|
|
resp = append(resp, searchResult{
|
|
|
|
|
Value: servers[i].ID,
|
|
|
|
|
Name: servers[i].Name,
|
|
|
|
|
Text: servers[i].Name,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, map[string]interface{}{
|
|
|
|
|
"success": true,
|
|
|
|
|
"results": resp,
|
|
|
|
|
})
|
2020-12-19 09:14:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 10:18:29 -05:00
|
|
|
|
type serverForm struct {
|
2021-01-08 08:04:50 -05:00
|
|
|
|
ID uint64
|
|
|
|
|
Name string `binding:"required"`
|
|
|
|
|
DisplayIndex int
|
|
|
|
|
Secret string
|
2021-01-12 01:09:25 -05:00
|
|
|
|
Tag string
|
2021-01-20 06:24:59 -05:00
|
|
|
|
Note string
|
2019-12-08 10:18:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 10:58:09 -05:00
|
|
|
|
func (ma *memberAPI) addOrEditServer(c *gin.Context) {
|
|
|
|
|
admin := c.MustGet(model.CtxKeyAuthorizedUser).(*model.User)
|
2019-12-08 10:18:29 -05:00
|
|
|
|
var sf serverForm
|
2019-12-09 03:02:49 -05:00
|
|
|
|
var s model.Server
|
2021-01-08 08:21:06 -05:00
|
|
|
|
var isEdit bool
|
2019-12-08 10:18:29 -05:00
|
|
|
|
err := c.ShouldBindJSON(&sf)
|
|
|
|
|
if err == nil {
|
|
|
|
|
s.Name = sf.Name
|
2020-03-22 08:55:27 -04:00
|
|
|
|
s.Secret = sf.Secret
|
2021-01-08 08:04:50 -05:00
|
|
|
|
s.DisplayIndex = sf.DisplayIndex
|
2020-03-22 08:55:27 -04:00
|
|
|
|
s.ID = sf.ID
|
2021-01-12 01:09:25 -05:00
|
|
|
|
s.Tag = sf.Tag
|
2021-01-20 06:24:59 -05:00
|
|
|
|
s.Note = sf.Note
|
2021-01-08 08:04:50 -05:00
|
|
|
|
if sf.ID == 0 {
|
2021-01-18 20:59:04 -05:00
|
|
|
|
s.Secret = utils.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, admin.ID))
|
2021-01-30 04:10:51 -05:00
|
|
|
|
s.Secret = s.Secret[:18]
|
2021-01-08 08:04:50 -05:00
|
|
|
|
err = dao.DB.Create(&s).Error
|
|
|
|
|
} else {
|
2021-01-08 08:21:06 -05:00
|
|
|
|
isEdit = true
|
2021-01-08 08:04:50 -05:00
|
|
|
|
err = dao.DB.Save(&s).Error
|
|
|
|
|
}
|
2019-12-08 10:18:29 -05:00
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-01-08 08:21:06 -05:00
|
|
|
|
if isEdit {
|
2021-01-17 09:18:36 -05:00
|
|
|
|
dao.ServerLock.RLock()
|
2021-01-08 08:21:06 -05:00
|
|
|
|
s.Host = dao.ServerList[s.ID].Host
|
|
|
|
|
s.State = dao.ServerList[s.ID].State
|
2021-01-17 09:18:36 -05:00
|
|
|
|
dao.ServerList[s.ID] = &s
|
|
|
|
|
dao.ServerLock.RUnlock()
|
2021-01-08 08:21:06 -05:00
|
|
|
|
} else {
|
|
|
|
|
s.Host = &model.Host{}
|
2021-01-15 11:45:49 -05:00
|
|
|
|
s.State = &model.HostState{}
|
2021-01-17 09:18:36 -05:00
|
|
|
|
dao.ServerLock.Lock()
|
2021-01-30 04:10:51 -05:00
|
|
|
|
dao.SecretToID[s.Secret] = s.ID
|
2021-01-17 09:18:36 -05:00
|
|
|
|
dao.ServerList[s.ID] = &s
|
|
|
|
|
dao.ServerLock.Unlock()
|
2021-01-08 08:21:06 -05:00
|
|
|
|
}
|
2021-01-08 08:04:50 -05:00
|
|
|
|
dao.ReSortServer()
|
2019-12-08 10:18:29 -05:00
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
})
|
2019-12-08 03:59:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-15 11:45:49 -05:00
|
|
|
|
type monitorForm struct {
|
2021-04-22 09:53:31 -04:00
|
|
|
|
ID uint64
|
|
|
|
|
Name string
|
|
|
|
|
Target string
|
|
|
|
|
Type uint8
|
|
|
|
|
Notify string
|
|
|
|
|
SkipServersRaw string
|
2021-01-15 11:45:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ma *memberAPI) addOrEditMonitor(c *gin.Context) {
|
|
|
|
|
var mf monitorForm
|
|
|
|
|
var m model.Monitor
|
|
|
|
|
err := c.ShouldBindJSON(&mf)
|
|
|
|
|
if err == nil {
|
|
|
|
|
m.Name = mf.Name
|
|
|
|
|
m.Target = mf.Target
|
|
|
|
|
m.Type = mf.Type
|
|
|
|
|
m.ID = mf.ID
|
2021-04-22 09:53:31 -04:00
|
|
|
|
m.SkipServersRaw = mf.SkipServersRaw
|
2021-04-17 11:36:37 -04:00
|
|
|
|
m.Notify = mf.Notify == "on"
|
2021-01-15 11:45:49 -05:00
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
|
|
|
|
if m.ID == 0 {
|
|
|
|
|
err = dao.DB.Create(&m).Error
|
|
|
|
|
} else {
|
|
|
|
|
err = dao.DB.Save(&m).Error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
2021-04-17 11:36:37 -04:00
|
|
|
|
} else {
|
|
|
|
|
dao.ServiceSentinelShared.OnMonitorUpdate()
|
2021-01-15 11:45:49 -05:00
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 20:59:04 -05:00
|
|
|
|
type cronForm struct {
|
|
|
|
|
ID uint64
|
|
|
|
|
Name string
|
|
|
|
|
Scheduler string
|
|
|
|
|
Command string
|
|
|
|
|
ServersRaw string
|
|
|
|
|
PushSuccessful string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ma *memberAPI) addOrEditCron(c *gin.Context) {
|
|
|
|
|
var cf cronForm
|
|
|
|
|
var cr model.Cron
|
|
|
|
|
err := c.ShouldBindJSON(&cf)
|
|
|
|
|
if err == nil {
|
|
|
|
|
cr.Name = cf.Name
|
|
|
|
|
cr.Scheduler = cf.Scheduler
|
|
|
|
|
cr.Command = cf.Command
|
|
|
|
|
cr.ServersRaw = cf.ServersRaw
|
|
|
|
|
cr.PushSuccessful = cf.PushSuccessful == "on"
|
|
|
|
|
cr.ID = cf.ID
|
|
|
|
|
err = json.Unmarshal([]byte(cf.ServersRaw), &cr.Servers)
|
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
|
|
|
|
_, err = cron.ParseStandard(cr.Scheduler)
|
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
|
|
|
|
if cf.ID == 0 {
|
|
|
|
|
err = dao.DB.Create(&cr).Error
|
|
|
|
|
} else {
|
|
|
|
|
err = dao.DB.Save(&cr).Error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 04:10:51 -05:00
|
|
|
|
dao.CronLock.Lock()
|
|
|
|
|
defer dao.CronLock.Unlock()
|
2021-01-23 02:32:04 -05:00
|
|
|
|
crOld := dao.Crons[cr.ID]
|
|
|
|
|
if crOld != nil && crOld.CronID != 0 {
|
|
|
|
|
dao.Cron.Remove(crOld.CronID)
|
2021-01-18 20:59:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cr.CronID, err = dao.Cron.AddFunc(cr.Scheduler, func() {
|
2021-01-30 04:10:51 -05:00
|
|
|
|
dao.ServerLock.RLock()
|
|
|
|
|
defer dao.ServerLock.RUnlock()
|
2021-01-18 20:59:04 -05:00
|
|
|
|
for j := 0; j < len(cr.Servers); j++ {
|
|
|
|
|
if dao.ServerList[cr.Servers[j]].TaskStream != nil {
|
|
|
|
|
dao.ServerList[cr.Servers[j]].TaskStream.Send(&pb.Task{
|
|
|
|
|
Id: cr.ID,
|
|
|
|
|
Data: cr.Command,
|
|
|
|
|
Type: model.TaskTypeCommand,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
2021-01-23 20:41:35 -05:00
|
|
|
|
dao.SendNotification(fmt.Sprintf("计划任务:%s,服务器:%d 离线,无法执行。", cr.Name, cr.Servers[j]), false)
|
2021-01-18 20:59:04 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-23 02:32:04 -05:00
|
|
|
|
delete(dao.Crons, cr.ID)
|
|
|
|
|
dao.Crons[cr.ID] = &cr
|
|
|
|
|
|
2021-01-18 20:59:04 -05:00
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-23 20:41:35 -05:00
|
|
|
|
func (ma *memberAPI) manualTrigger(c *gin.Context) {
|
|
|
|
|
var cr model.Cron
|
|
|
|
|
if err := dao.DB.First(&cr, "id = ?", c.Param("id")).Error; err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dao.CronTrigger(&cr)
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-19 09:14:36 -05:00
|
|
|
|
type notificationForm struct {
|
|
|
|
|
ID uint64
|
|
|
|
|
Name string
|
|
|
|
|
URL string
|
|
|
|
|
RequestMethod int
|
|
|
|
|
RequestType int
|
|
|
|
|
RequestBody string
|
|
|
|
|
VerifySSL string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ma *memberAPI) addOrEditNotification(c *gin.Context) {
|
|
|
|
|
var nf notificationForm
|
|
|
|
|
var n model.Notification
|
|
|
|
|
err := c.ShouldBindJSON(&nf)
|
|
|
|
|
if err == nil {
|
|
|
|
|
n.Name = nf.Name
|
|
|
|
|
n.RequestMethod = nf.RequestMethod
|
|
|
|
|
n.RequestType = nf.RequestType
|
|
|
|
|
n.RequestBody = nf.RequestBody
|
|
|
|
|
n.URL = nf.URL
|
|
|
|
|
verifySSL := nf.VerifySSL == "on"
|
|
|
|
|
n.VerifySSL = &verifySSL
|
|
|
|
|
n.ID = nf.ID
|
2020-12-19 23:18:27 -05:00
|
|
|
|
err = n.Send("这是测试消息")
|
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
2020-12-19 09:14:36 -05:00
|
|
|
|
if n.ID == 0 {
|
|
|
|
|
err = dao.DB.Create(&n).Error
|
|
|
|
|
} else {
|
|
|
|
|
err = dao.DB.Save(&n).Error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-01-23 20:41:35 -05:00
|
|
|
|
dao.OnRefreshOrAddNotification(n)
|
2020-12-19 09:14:36 -05:00
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-19 10:11:16 -05:00
|
|
|
|
type alertRuleForm struct {
|
|
|
|
|
ID uint64
|
|
|
|
|
Name string
|
|
|
|
|
RulesRaw string
|
|
|
|
|
Enable string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ma *memberAPI) addOrEditAlertRule(c *gin.Context) {
|
|
|
|
|
var arf alertRuleForm
|
|
|
|
|
var r model.AlertRule
|
|
|
|
|
err := c.ShouldBindJSON(&arf)
|
|
|
|
|
if err == nil {
|
|
|
|
|
err = json.Unmarshal([]byte(arf.RulesRaw), &r.Rules)
|
2020-12-19 23:18:27 -05:00
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
|
|
|
|
if len(r.Rules) == 0 {
|
|
|
|
|
err = errors.New("至少定义一条规则")
|
|
|
|
|
} else {
|
|
|
|
|
for i := 0; i < len(r.Rules); i++ {
|
|
|
|
|
if r.Rules[i].Duration < 3 {
|
|
|
|
|
err = errors.New("Duration 至少为 3")
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-19 10:11:16 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
|
|
|
|
r.Name = arf.Name
|
|
|
|
|
r.RulesRaw = arf.RulesRaw
|
|
|
|
|
enable := arf.Enable == "on"
|
|
|
|
|
r.Enable = &enable
|
|
|
|
|
r.ID = arf.ID
|
|
|
|
|
if r.ID == 0 {
|
|
|
|
|
err = dao.DB.Create(&r).Error
|
|
|
|
|
} else {
|
|
|
|
|
err = dao.DB.Save(&r).Error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-01-23 20:41:35 -05:00
|
|
|
|
dao.OnRefreshOrAddAlert(r)
|
2020-12-19 10:11:16 -05:00
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
|
type logoutForm struct {
|
|
|
|
|
ID uint64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ma *memberAPI) logout(c *gin.Context) {
|
2019-12-20 10:58:09 -05:00
|
|
|
|
admin := c.MustGet(model.CtxKeyAuthorizedUser).(*model.User)
|
2019-12-08 03:59:58 -05:00
|
|
|
|
var lf logoutForm
|
|
|
|
|
if err := c.ShouldBindJSON(&lf); err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-12-20 10:58:09 -05:00
|
|
|
|
if lf.ID != admin.ID {
|
2019-12-08 03:59:58 -05:00
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", "用户ID不匹配"),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-12-20 10:58:09 -05:00
|
|
|
|
dao.DB.Model(admin).UpdateColumns(model.User{
|
|
|
|
|
Token: "",
|
|
|
|
|
TokenExpired: time.Now(),
|
|
|
|
|
})
|
2019-12-08 03:59:58 -05:00
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
})
|
|
|
|
|
}
|
2020-12-09 06:05:40 -05:00
|
|
|
|
|
|
|
|
|
type settingForm struct {
|
2021-01-13 09:30:28 -05:00
|
|
|
|
Title string
|
|
|
|
|
Admin string
|
|
|
|
|
Theme string
|
|
|
|
|
CustomCode string
|
2021-01-31 00:37:43 -05:00
|
|
|
|
ViewPassword string
|
2021-01-13 09:30:28 -05:00
|
|
|
|
EnableIPChangeNotification string
|
2021-04-07 09:11:59 -04:00
|
|
|
|
IgnoredIPNotification string
|
2021-03-02 10:08:40 -05:00
|
|
|
|
Oauth2Type string
|
2020-12-09 06:05:40 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ma *memberAPI) updateSetting(c *gin.Context) {
|
|
|
|
|
var sf settingForm
|
|
|
|
|
if err := c.ShouldBind(&sf); err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-01-13 09:30:28 -05:00
|
|
|
|
dao.Conf.EnableIPChangeNotification = sf.EnableIPChangeNotification == "on"
|
2021-04-07 09:11:59 -04:00
|
|
|
|
dao.Conf.IgnoredIPNotification = sf.IgnoredIPNotification
|
2020-12-09 06:05:40 -05:00
|
|
|
|
dao.Conf.Site.Brand = sf.Title
|
|
|
|
|
dao.Conf.Site.Theme = sf.Theme
|
2020-12-23 20:54:17 -05:00
|
|
|
|
dao.Conf.Site.CustomCode = sf.CustomCode
|
2021-01-31 00:37:43 -05:00
|
|
|
|
dao.Conf.Site.ViewPassword = sf.ViewPassword
|
2021-03-02 10:08:40 -05:00
|
|
|
|
dao.Conf.Oauth2.Type = sf.Oauth2Type
|
|
|
|
|
dao.Conf.Oauth2.Admin = sf.Admin
|
2020-12-09 06:05:40 -05:00
|
|
|
|
if err := dao.Conf.Save(); err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
})
|
|
|
|
|
}
|