2019-12-08 03:59:58 -05:00
|
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
2021-11-04 00:06:20 -04:00
|
|
|
|
"bytes"
|
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"
|
2021-04-23 07:09:04 -04:00
|
|
|
|
"strings"
|
2019-12-08 03:59:58 -05:00
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
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"
|
2021-11-04 00:06:20 -04:00
|
|
|
|
"github.com/naiba/nezha/proto"
|
2022-01-08 22:54:14 -05:00
|
|
|
|
"github.com/naiba/nezha/service/singleton"
|
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)
|
2021-11-04 00:06:20 -04:00
|
|
|
|
mr.POST("/force-update", ma.forceUpdate)
|
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":
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Unscoped().Delete(&model.Server{}, "id = ?", id).Error
|
2020-12-19 10:11:16 -05:00
|
|
|
|
if err == nil {
|
2021-11-06 04:00:08 -04:00
|
|
|
|
// 删除服务器
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.ServerLock.Lock()
|
|
|
|
|
delete(singleton.SecretToID, singleton.ServerList[id].Secret)
|
|
|
|
|
delete(singleton.ServerList, id)
|
|
|
|
|
singleton.ServerLock.Unlock()
|
|
|
|
|
singleton.ReSortServer()
|
2021-11-06 04:00:08 -04:00
|
|
|
|
// 删除循环流量状态中的此服务器相关的记录
|
2022-01-08 22:54:14 -05:00
|
|
|
|
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)
|
2021-11-06 04:00:08 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.AlertsLock.Unlock()
|
2021-11-06 04:00:08 -04:00
|
|
|
|
// 删除服务器相关循环流量记录
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.DB.Unscoped().Delete(&model.Transfer{}, "server_id = ?", id)
|
2020-12-19 10:11:16 -05:00
|
|
|
|
}
|
|
|
|
|
case "notification":
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Unscoped().Delete(&model.Notification{}, "id = ?", id).Error
|
2020-12-19 23:18:27 -05:00
|
|
|
|
if err == nil {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.OnDeleteNotification(id)
|
2020-12-19 23:18:27 -05:00
|
|
|
|
}
|
2021-01-15 11:45:49 -05:00
|
|
|
|
case "monitor":
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Unscoped().Delete(&model.Monitor{}, "id = ?", id).Error
|
2021-01-16 02:05:35 -05:00
|
|
|
|
if err == nil {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.ServiceSentinelShared.OnMonitorDelete(id)
|
|
|
|
|
err = singleton.DB.Unscoped().Delete(&model.MonitorHistory{}, "monitor_id = ?", id).Error
|
2021-01-16 02:05:35 -05:00
|
|
|
|
}
|
2021-01-18 20:59:04 -05:00
|
|
|
|
case "cron":
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Unscoped().Delete(&model.Cron{}, "id = ?", id).Error
|
2021-01-18 20:59:04 -05:00
|
|
|
|
if err == nil {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.CronLock.RLock()
|
|
|
|
|
defer singleton.CronLock.RUnlock()
|
|
|
|
|
cr := singleton.Crons[id]
|
2021-09-29 23:59:57 -04:00
|
|
|
|
if cr != nil && cr.CronJobID != 0 {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.Cron.Remove(cr.CronJobID)
|
2021-01-18 20:59:04 -05:00
|
|
|
|
}
|
2022-01-08 22:54:14 -05:00
|
|
|
|
delete(singleton.Crons, id)
|
2021-01-18 20:59:04 -05:00
|
|
|
|
}
|
2020-12-19 10:11:16 -05:00
|
|
|
|
case "alert-rule":
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Unscoped().Delete(&model.AlertRule{}, "id = ?", id).Error
|
2020-12-19 23:18:27 -05:00
|
|
|
|
if err == nil {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.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") + "%"
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.DB.Select("id,name").Where("id = ? OR name LIKE ? OR tag LIKE ? OR note LIKE ?",
|
2021-01-20 09:15:47 -05:00
|
|
|
|
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-07-25 11:50:08 -04:00
|
|
|
|
if s.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]
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Create(&s).Error
|
2021-01-08 08:04:50 -05:00
|
|
|
|
} else {
|
2021-01-08 08:21:06 -05:00
|
|
|
|
isEdit = true
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Save(&s).Error
|
2021-01-08 08:04:50 -05:00
|
|
|
|
}
|
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 {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.ServerLock.Lock()
|
|
|
|
|
s.CopyFromRunningServer(singleton.ServerList[s.ID])
|
2021-07-25 11:50:08 -04:00
|
|
|
|
// 如果修改了 Secret
|
2022-01-08 22:54:14 -05:00
|
|
|
|
if s.Secret != singleton.ServerList[s.ID].Secret {
|
2021-07-25 11:50:08 -04:00
|
|
|
|
// 删除旧 Secret-ID 绑定关系
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.SecretToID[s.Secret] = s.ID
|
2021-07-25 11:50:08 -04:00
|
|
|
|
// 设置新的 Secret-ID 绑定关系
|
2022-01-08 22:54:14 -05:00
|
|
|
|
delete(singleton.SecretToID, singleton.ServerList[s.ID].Secret)
|
2021-07-25 11:50:08 -04:00
|
|
|
|
}
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.ServerList[s.ID] = &s
|
|
|
|
|
singleton.ServerLock.Unlock()
|
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{}
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.ServerLock.Lock()
|
|
|
|
|
singleton.SecretToID[s.Secret] = s.ID
|
|
|
|
|
singleton.ServerList[s.ID] = &s
|
|
|
|
|
singleton.ServerLock.Unlock()
|
2021-01-08 08:21:06 -05:00
|
|
|
|
}
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.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 {
|
2022-04-14 15:13:53 -04:00
|
|
|
|
ID uint64
|
|
|
|
|
Name string
|
|
|
|
|
Target string
|
|
|
|
|
Type uint8
|
|
|
|
|
Cover uint8
|
|
|
|
|
Notify string
|
|
|
|
|
NotificationTag string
|
|
|
|
|
SkipServersRaw string
|
|
|
|
|
Duration uint64
|
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
|
2021-04-23 07:09:04 -04:00
|
|
|
|
m.Target = strings.TrimSpace(mf.Target)
|
2021-01-15 11:45:49 -05:00
|
|
|
|
m.Type = mf.Type
|
|
|
|
|
m.ID = mf.ID
|
2021-04-22 09:53:31 -04:00
|
|
|
|
m.SkipServersRaw = mf.SkipServersRaw
|
2021-06-21 09:30:42 -04:00
|
|
|
|
m.Cover = mf.Cover
|
2021-04-17 11:36:37 -04:00
|
|
|
|
m.Notify = mf.Notify == "on"
|
2022-04-14 15:13:53 -04:00
|
|
|
|
m.NotificationTag = mf.NotificationTag
|
2021-09-02 11:45:21 -04:00
|
|
|
|
m.Duration = mf.Duration
|
2022-01-14 04:34:49 -05:00
|
|
|
|
err = m.InitSkipServers()
|
2021-01-15 11:45:49 -05:00
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
2022-04-15 08:34:25 -04:00
|
|
|
|
// 保证NotificationTag不为空
|
2022-04-14 15:13:53 -04:00
|
|
|
|
if m.NotificationTag == "" {
|
|
|
|
|
m.NotificationTag = "default"
|
|
|
|
|
}
|
2021-01-15 11:45:49 -05:00
|
|
|
|
if m.ID == 0 {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Create(&m).Error
|
2021-01-15 11:45:49 -05:00
|
|
|
|
} else {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Save(&m).Error
|
2021-01-15 11:45:49 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-02 11:45:21 -04:00
|
|
|
|
if err == nil {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.ServiceSentinelShared.OnMonitorUpdate(m)
|
2021-09-02 11:45:21 -04:00
|
|
|
|
}
|
2021-01-15 11:45:49 -05:00
|
|
|
|
if 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,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 20:59:04 -05:00
|
|
|
|
type cronForm struct {
|
2022-04-14 09:06:42 -04:00
|
|
|
|
ID uint64
|
|
|
|
|
Name string
|
|
|
|
|
Scheduler string
|
|
|
|
|
Command string
|
|
|
|
|
ServersRaw string
|
|
|
|
|
Cover uint8
|
|
|
|
|
PushSuccessful string
|
|
|
|
|
NotificationTag string
|
2021-01-18 20:59:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"
|
2022-04-14 09:06:42 -04:00
|
|
|
|
cr.NotificationTag = cf.NotificationTag
|
2021-01-18 20:59:04 -05:00
|
|
|
|
cr.ID = cf.ID
|
2021-06-21 09:30:42 -04:00
|
|
|
|
cr.Cover = cf.Cover
|
2022-03-18 11:13:22 -04:00
|
|
|
|
err = utils.Json.Unmarshal([]byte(cf.ServersRaw), &cr.Servers)
|
2021-01-18 20:59:04 -05:00
|
|
|
|
}
|
2022-01-08 22:54:14 -05:00
|
|
|
|
tx := singleton.DB.Begin()
|
2021-01-18 20:59:04 -05:00
|
|
|
|
if err == nil {
|
2022-04-15 08:34:25 -04:00
|
|
|
|
// 保证NotificationTag不为空
|
|
|
|
|
if cr.NotificationTag == "" {
|
|
|
|
|
cr.NotificationTag = "default"
|
|
|
|
|
}
|
2021-01-18 20:59:04 -05:00
|
|
|
|
if cf.ID == 0 {
|
2021-09-29 07:58:02 -04:00
|
|
|
|
err = tx.Create(&cr).Error
|
2021-01-18 20:59:04 -05:00
|
|
|
|
} else {
|
2021-09-29 07:58:02 -04:00
|
|
|
|
err = tx.Save(&cr).Error
|
2021-01-18 20:59:04 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-18 22:44:44 -04:00
|
|
|
|
if err == nil {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
cr.CronJobID, err = singleton.Cron.AddFunc(cr.Scheduler, singleton.CronTrigger(cr))
|
2021-01-18 20:59:04 -05:00
|
|
|
|
}
|
2021-09-29 07:58:02 -04:00
|
|
|
|
if err == nil {
|
|
|
|
|
err = tx.Commit().Error
|
|
|
|
|
} else {
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
}
|
2021-07-18 22:37:12 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
2021-07-18 22:44:44 -04:00
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", err),
|
2021-07-18 22:37:12 -04:00
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.CronLock.Lock()
|
|
|
|
|
defer singleton.CronLock.Unlock()
|
|
|
|
|
crOld := singleton.Crons[cr.ID]
|
2021-09-29 23:59:57 -04:00
|
|
|
|
if crOld != nil && crOld.CronJobID != 0 {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.Cron.Remove(crOld.CronJobID)
|
2021-05-27 08:48:12 -04:00
|
|
|
|
}
|
2021-01-18 20:59:04 -05:00
|
|
|
|
|
2022-01-08 22:54:14 -05:00
|
|
|
|
delete(singleton.Crons, cr.ID)
|
|
|
|
|
singleton.Crons[cr.ID] = &cr
|
2021-01-23 02:32:04 -05:00
|
|
|
|
|
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
|
2022-01-08 22:54:14 -05:00
|
|
|
|
if err := singleton.DB.First(&cr, "id = ?", c.Param("id")).Error; err != nil {
|
2021-01-23 20:41:35 -05:00
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.ManualTrigger(cr)
|
2021-01-23 20:41:35 -05:00
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-04 00:06:20 -04:00
|
|
|
|
func (ma *memberAPI) forceUpdate(c *gin.Context) {
|
|
|
|
|
var forceUpdateServers []uint64
|
|
|
|
|
if err := c.ShouldBindJSON(&forceUpdateServers); err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var executeResult bytes.Buffer
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(forceUpdateServers); i++ {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.ServerLock.RLock()
|
|
|
|
|
server := singleton.ServerList[forceUpdateServers[i]]
|
|
|
|
|
singleton.ServerLock.RUnlock()
|
2021-11-04 00:06:20 -04:00
|
|
|
|
if server != nil && server.TaskStream != nil {
|
|
|
|
|
if err := server.TaskStream.Send(&proto.Task{
|
|
|
|
|
Type: model.TaskTypeUpgrade,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
executeResult.WriteString(fmt.Sprintf("%d 下发指令失败 %+v<br/>", forceUpdateServers[i], err))
|
|
|
|
|
} else {
|
|
|
|
|
executeResult.WriteString(fmt.Sprintf("%d 下发指令成功<br/>", forceUpdateServers[i]))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
executeResult.WriteString(fmt.Sprintf("%d 离线<br/>", forceUpdateServers[i]))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
Message: executeResult.String(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-19 09:14:36 -05:00
|
|
|
|
type notificationForm struct {
|
|
|
|
|
ID uint64
|
|
|
|
|
Name string
|
2022-04-14 09:06:42 -04:00
|
|
|
|
Tag string // 分组名
|
2020-12-19 09:14:36 -05:00
|
|
|
|
URL string
|
|
|
|
|
RequestMethod int
|
|
|
|
|
RequestType int
|
2021-11-05 00:04:39 -04:00
|
|
|
|
RequestHeader string
|
2020-12-19 09:14:36 -05:00
|
|
|
|
RequestBody string
|
|
|
|
|
VerifySSL string
|
2022-04-22 14:07:46 -04:00
|
|
|
|
SkipCheck string
|
2020-12-19 09:14:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2022-04-14 09:06:42 -04:00
|
|
|
|
n.Tag = nf.Tag
|
2020-12-19 09:14:36 -05:00
|
|
|
|
n.RequestMethod = nf.RequestMethod
|
|
|
|
|
n.RequestType = nf.RequestType
|
2021-11-05 00:04:39 -04:00
|
|
|
|
n.RequestHeader = nf.RequestHeader
|
2020-12-19 09:14:36 -05:00
|
|
|
|
n.RequestBody = nf.RequestBody
|
|
|
|
|
n.URL = nf.URL
|
|
|
|
|
verifySSL := nf.VerifySSL == "on"
|
|
|
|
|
n.VerifySSL = &verifySSL
|
|
|
|
|
n.ID = nf.ID
|
2022-04-17 05:38:45 -04:00
|
|
|
|
ns := model.NotificationServerBundle{
|
|
|
|
|
Notification: &n,
|
|
|
|
|
Server: nil,
|
|
|
|
|
}
|
2022-04-22 14:07:46 -04:00
|
|
|
|
// 勾选了跳过检查
|
2022-04-23 02:46:33 -04:00
|
|
|
|
if nf.SkipCheck != "on" {
|
2022-04-22 14:07:46 -04:00
|
|
|
|
err = ns.Send("这是测试消息")
|
|
|
|
|
}
|
2020-12-19 23:18:27 -05:00
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
2022-04-14 09:06:42 -04:00
|
|
|
|
// 保证Tag不为空
|
|
|
|
|
if n.Tag == "" {
|
|
|
|
|
n.Tag = "default"
|
|
|
|
|
}
|
2020-12-19 09:14:36 -05:00
|
|
|
|
if n.ID == 0 {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Create(&n).Error
|
2020-12-19 09:14:36 -05:00
|
|
|
|
} else {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Save(&n).Error
|
2020-12-19 09:14:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-04-14 09:06:42 -04:00
|
|
|
|
singleton.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 {
|
2022-04-14 15:13:53 -04:00
|
|
|
|
ID uint64
|
|
|
|
|
Name string
|
|
|
|
|
RulesRaw string
|
|
|
|
|
NotificationTag string
|
|
|
|
|
Enable string
|
2020-12-19 10:11:16 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ma *memberAPI) addOrEditAlertRule(c *gin.Context) {
|
|
|
|
|
var arf alertRuleForm
|
|
|
|
|
var r model.AlertRule
|
|
|
|
|
err := c.ShouldBindJSON(&arf)
|
|
|
|
|
if err == nil {
|
2022-03-18 11:13:22 -04:00
|
|
|
|
err = utils.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++ {
|
2022-02-28 21:19:23 -05:00
|
|
|
|
if !r.Rules[i].IsTransferDurationRule() {
|
|
|
|
|
if r.Rules[i].Duration < 3 {
|
|
|
|
|
err = errors.New("错误:Duration 至少为 3")
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if r.Rules[i].CycleInterval < 1 {
|
|
|
|
|
err = errors.New("错误: cycle_interval 至少为 1")
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if r.Rules[i].CycleStart == nil {
|
|
|
|
|
err = errors.New("错误: cycle_start 未设置")
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if r.Rules[i].CycleStart.After(time.Now()) {
|
|
|
|
|
err = errors.New("错误: cycle_start 是个未来值")
|
|
|
|
|
break
|
|
|
|
|
}
|
2020-12-19 23:18:27 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-19 10:11:16 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
|
|
|
|
r.Name = arf.Name
|
|
|
|
|
r.RulesRaw = arf.RulesRaw
|
2022-04-14 15:13:53 -04:00
|
|
|
|
r.NotificationTag = arf.NotificationTag
|
2020-12-19 10:11:16 -05:00
|
|
|
|
enable := arf.Enable == "on"
|
|
|
|
|
r.Enable = &enable
|
|
|
|
|
r.ID = arf.ID
|
2022-04-15 08:34:25 -04:00
|
|
|
|
//保证NotificationTag不为空
|
|
|
|
|
if r.NotificationTag == "" {
|
|
|
|
|
r.NotificationTag = "default"
|
|
|
|
|
}
|
2020-12-19 10:11:16 -05:00
|
|
|
|
if r.ID == 0 {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Create(&r).Error
|
2020-12-19 10:11:16 -05:00
|
|
|
|
} else {
|
2022-01-08 22:54:14 -05:00
|
|
|
|
err = singleton.DB.Save(&r).Error
|
2020-12-19 10:11:16 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.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
|
|
|
|
|
}
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.DB.Model(admin).UpdateColumns(model.User{
|
2019-12-20 10:58:09 -05:00
|
|
|
|
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 {
|
2022-04-14 15:13:53 -04:00
|
|
|
|
Title string
|
|
|
|
|
Admin string
|
|
|
|
|
Theme string
|
|
|
|
|
CustomCode string
|
|
|
|
|
ViewPassword string
|
|
|
|
|
IgnoredIPNotification string
|
|
|
|
|
IPChangeNotificationTag string // IP变更提醒的通知组
|
|
|
|
|
GRPCHost string
|
|
|
|
|
Cover uint8
|
2022-02-19 01:29:06 -05:00
|
|
|
|
|
|
|
|
|
EnableIPChangeNotification string
|
|
|
|
|
EnablePlainIPInNotification 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
|
|
|
|
|
}
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.Conf.EnableIPChangeNotification = sf.EnableIPChangeNotification == "on"
|
2022-02-19 01:29:06 -05:00
|
|
|
|
singleton.Conf.EnablePlainIPInNotification = sf.EnablePlainIPInNotification == "on"
|
2022-01-08 22:54:14 -05:00
|
|
|
|
singleton.Conf.Cover = sf.Cover
|
|
|
|
|
singleton.Conf.GRPCHost = sf.GRPCHost
|
|
|
|
|
singleton.Conf.IgnoredIPNotification = sf.IgnoredIPNotification
|
2022-04-14 15:13:53 -04:00
|
|
|
|
singleton.Conf.IPChangeNotificationTag = sf.IPChangeNotificationTag
|
2022-01-08 22:54:14 -05:00
|
|
|
|
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
|
2022-04-15 08:34:25 -04:00
|
|
|
|
// 保证NotificationTag不为空
|
|
|
|
|
if singleton.Conf.IPChangeNotificationTag == "" {
|
|
|
|
|
singleton.Conf.IPChangeNotificationTag = "default"
|
|
|
|
|
}
|
2022-01-08 22:54:14 -05:00
|
|
|
|
if err := singleton.Conf.Save(); err != nil {
|
2020-12-09 06:05:40 -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,
|
|
|
|
|
})
|
|
|
|
|
}
|