2019-12-08 03:59:58 -05:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2020-12-19 09:14:36 -05:00
|
|
|
"encoding/json"
|
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"
|
2019-12-08 10:18:29 -05:00
|
|
|
"github.com/naiba/com"
|
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"
|
|
|
|
"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",
|
|
|
|
}))
|
|
|
|
|
|
|
|
mr.POST("/logout", ma.logout)
|
2019-12-20 10:58:09 -05:00
|
|
|
mr.POST("/server", ma.addOrEditServer)
|
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)
|
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":
|
|
|
|
dao.ServerLock.Lock()
|
|
|
|
defer dao.ServerLock.Unlock()
|
|
|
|
err = dao.DB.Delete(&model.Server{}, "id = ?", id).Error
|
|
|
|
if err == nil {
|
|
|
|
delete(dao.ServerList, strconv.FormatUint(id, 10))
|
|
|
|
}
|
|
|
|
case "notification":
|
|
|
|
err = dao.DB.Delete(&model.Notification{}, "id = ?", id).Error
|
|
|
|
case "alert-rule":
|
|
|
|
err = dao.DB.Delete(&model.AlertRule{}, "id = ?", id).Error
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-08 10:18:29 -05:00
|
|
|
type serverForm struct {
|
2020-03-22 08:55:27 -04:00
|
|
|
ID uint64
|
|
|
|
Name string `binding:"required"`
|
|
|
|
Secret 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
|
2019-12-08 10:18:29 -05:00
|
|
|
err := c.ShouldBindJSON(&sf)
|
|
|
|
if err == nil {
|
2019-12-11 01:41:12 -05:00
|
|
|
dao.ServerLock.Lock()
|
|
|
|
defer dao.ServerLock.Unlock()
|
2019-12-08 10:18:29 -05:00
|
|
|
s.Name = sf.Name
|
2020-03-22 08:55:27 -04:00
|
|
|
s.Secret = sf.Secret
|
|
|
|
s.ID = sf.ID
|
2019-12-20 10:58:09 -05:00
|
|
|
}
|
|
|
|
if sf.ID == 0 {
|
|
|
|
s.Secret = com.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, admin.ID))
|
2019-12-08 10:18:29 -05:00
|
|
|
s.Secret = s.Secret[:10]
|
|
|
|
err = dao.DB.Create(&s).Error
|
2019-12-20 10:58:09 -05:00
|
|
|
} else {
|
|
|
|
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
|
|
|
|
}
|
2019-12-09 05:14:31 -05:00
|
|
|
dao.ServerList[fmt.Sprintf("%d", s.ID)] = &s
|
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
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
var data map[string]string
|
|
|
|
err = json.Unmarshal([]byte(nf.RequestBody), &data)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
if err == nil && len(r.Rules) == 0 {
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
Message: fmt.Sprintf("请求错误:%s", "至少定义一条规则"),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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 {
|
2020-12-18 21:57:10 -05:00
|
|
|
Title string
|
|
|
|
Admin string
|
|
|
|
Theme string
|
|
|
|
CustomCSS 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
|
|
|
|
}
|
|
|
|
dao.Conf.Site.Brand = sf.Title
|
|
|
|
dao.Conf.Site.Theme = sf.Theme
|
2020-12-18 21:57:10 -05:00
|
|
|
dao.Conf.Site.CustomCSS = sf.CustomCSS
|
2020-12-09 06:05:40 -05:00
|
|
|
dao.Conf.GitHub.Admin = sf.Admin
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|