2024-10-20 23:23:04 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2024-10-22 10:25:45 +08:00
|
|
|
"fmt"
|
2024-10-20 23:23:04 +08:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-10-24 10:21:59 +08:00
|
|
|
"github.com/jinzhu/copier"
|
2024-10-21 16:22:30 +08:00
|
|
|
|
2024-10-20 23:23:04 +08:00
|
|
|
"github.com/naiba/nezha/model"
|
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
|
|
|
"github.com/naiba/nezha/service/singleton"
|
|
|
|
)
|
|
|
|
|
2024-10-23 17:34:15 +08:00
|
|
|
// List server
|
|
|
|
// @Summary List server
|
|
|
|
// @Security BearerAuth
|
|
|
|
// @Schemes
|
|
|
|
// @Description List server
|
|
|
|
// @Tags auth required
|
|
|
|
// @Produce json
|
2024-10-24 14:11:06 +08:00
|
|
|
// @Success 200 {object} model.CommonResponse[[]model.Server]
|
2024-10-23 17:34:15 +08:00
|
|
|
// @Router /server [get]
|
2024-10-23 23:06:11 +08:00
|
|
|
func listServer(c *gin.Context) ([]*model.Server, error) {
|
|
|
|
singleton.SortedServerLock.RLock()
|
|
|
|
defer singleton.SortedServerLock.RUnlock()
|
2024-10-24 10:21:59 +08:00
|
|
|
|
|
|
|
var ssl []*model.Server
|
|
|
|
if err := copier.Copy(&ssl, &singleton.SortedServerList); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ssl, nil
|
2024-10-23 17:34:15 +08:00
|
|
|
}
|
|
|
|
|
2024-10-20 23:23:04 +08:00
|
|
|
// Edit server
|
|
|
|
// @Summary Edit server
|
|
|
|
// @Security BearerAuth
|
|
|
|
// @Schemes
|
|
|
|
// @Description Edit server
|
|
|
|
// @Tags auth required
|
2024-10-21 23:00:51 +08:00
|
|
|
// @Accept json
|
|
|
|
// @Param id path uint true "Server ID"
|
|
|
|
// @Param body body model.ServerForm true "ServerForm"
|
2024-10-20 23:23:04 +08:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} model.CommonResponse[any]
|
|
|
|
// @Router /server/{id} [patch]
|
2024-10-23 17:56:51 +08:00
|
|
|
func updateServer(c *gin.Context) (any, error) {
|
2024-10-20 23:23:04 +08:00
|
|
|
idStr := c.Param("id")
|
|
|
|
id, err := strconv.ParseUint(idStr, 10, 64)
|
|
|
|
if err != nil {
|
2024-10-23 17:56:51 +08:00
|
|
|
return nil, err
|
2024-10-20 23:23:04 +08:00
|
|
|
}
|
2024-10-21 23:00:51 +08:00
|
|
|
var sf model.ServerForm
|
2024-10-20 23:23:04 +08:00
|
|
|
if err := c.ShouldBindJSON(&sf); err != nil {
|
2024-10-23 17:56:51 +08:00
|
|
|
return nil, err
|
2024-10-20 23:23:04 +08:00
|
|
|
}
|
2024-10-21 23:00:51 +08:00
|
|
|
|
|
|
|
var s model.Server
|
|
|
|
if err := singleton.DB.First(&s, id).Error; err != nil {
|
2024-10-23 17:56:51 +08:00
|
|
|
return nil, fmt.Errorf("server id %d does not exist", id)
|
2024-10-21 23:00:51 +08:00
|
|
|
}
|
|
|
|
|
2024-10-20 23:23:04 +08:00
|
|
|
s.Name = sf.Name
|
|
|
|
s.DisplayIndex = sf.DisplayIndex
|
|
|
|
s.Note = sf.Note
|
|
|
|
s.PublicNote = sf.PublicNote
|
|
|
|
s.HideForGuest = sf.HideForGuest
|
|
|
|
s.EnableDDNS = sf.EnableDDNS
|
|
|
|
s.DDNSProfiles = sf.DDNSProfiles
|
|
|
|
ddnsProfilesRaw, err := utils.Json.Marshal(s.DDNSProfiles)
|
|
|
|
if err != nil {
|
2024-10-23 17:56:51 +08:00
|
|
|
return nil, err
|
2024-10-20 23:23:04 +08:00
|
|
|
}
|
|
|
|
s.DDNSProfilesRaw = string(ddnsProfilesRaw)
|
|
|
|
|
|
|
|
if err := singleton.DB.Save(&s).Error; err != nil {
|
2024-10-23 17:56:51 +08:00
|
|
|
return nil, newGormError("%v", err)
|
2024-10-20 23:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
singleton.ServerLock.Lock()
|
|
|
|
s.CopyFromRunningServer(singleton.ServerList[s.ID])
|
|
|
|
singleton.ServerList[s.ID] = &s
|
|
|
|
singleton.ServerLock.Unlock()
|
|
|
|
singleton.ReSortServer()
|
2024-10-23 17:56:51 +08:00
|
|
|
|
|
|
|
return nil, nil
|
2024-10-20 23:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Batch delete server
|
|
|
|
// @Summary Batch delete server
|
|
|
|
// @Security BearerAuth
|
|
|
|
// @Schemes
|
|
|
|
// @Description Batch delete server
|
|
|
|
// @Tags auth required
|
|
|
|
// @Accept json
|
|
|
|
// @param request body []uint64 true "id list"
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} model.CommonResponse[any]
|
|
|
|
// @Router /batch-delete/server [post]
|
2024-10-23 17:56:51 +08:00
|
|
|
func batchDeleteServer(c *gin.Context) (any, error) {
|
2024-10-20 23:23:04 +08:00
|
|
|
var servers []uint64
|
|
|
|
if err := c.ShouldBindJSON(&servers); err != nil {
|
2024-10-23 17:56:51 +08:00
|
|
|
return nil, err
|
2024-10-20 23:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := singleton.DB.Unscoped().Delete(&model.Server{}, "id in (?)", servers).Error; err != nil {
|
2024-10-23 17:56:51 +08:00
|
|
|
return nil, newGormError("%v", err)
|
2024-10-20 23:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
singleton.ServerLock.Lock()
|
|
|
|
for i := 0; i < len(servers); i++ {
|
|
|
|
id := servers[i]
|
|
|
|
delete(singleton.ServerList, 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
singleton.AlertsLock.Unlock()
|
|
|
|
|
|
|
|
singleton.DB.Unscoped().Delete(&model.Transfer{}, "server_id = ?", id)
|
|
|
|
}
|
|
|
|
singleton.ServerLock.Unlock()
|
|
|
|
|
|
|
|
singleton.ReSortServer()
|
|
|
|
|
2024-10-23 17:56:51 +08:00
|
|
|
return nil, nil
|
2024-10-20 23:23:04 +08:00
|
|
|
}
|