diff --git a/cmd/dashboard/controller/controller.go b/cmd/dashboard/controller/controller.go index 99f3fc2..9c87b01 100644 --- a/cmd/dashboard/controller/controller.go +++ b/cmd/dashboard/controller/controller.go @@ -59,12 +59,12 @@ func routers(r *gin.Engine) { optionalAuth := api.Group("", optionalAuthMiddleware(authMiddleware)) optionalAuth.GET("/ws/server", commonHandler[any](serverStream)) optionalAuth.GET("/server-group", commonHandler[[]model.ServerGroup](listServerGroup)) - optionalAuth.GET("/ddns", listDDNS) // TODO auth := api.Group("", authMiddleware.MiddlewareFunc()) auth.GET("/refresh_token", authMiddleware.RefreshHandler) auth.PATCH("/server/:id", commonHandler[any](editServer)) + auth.GET("/ddns", commonHandler[[]model.DDNSProfile](listDDNS)) auth.POST("/ddns", commonHandler[any](newDDNS)) auth.PATCH("/ddns/:id", commonHandler[any](editDDNS)) diff --git a/cmd/dashboard/controller/ddns.go b/cmd/dashboard/controller/ddns.go index 22c217b..ddc0e93 100644 --- a/cmd/dashboard/controller/ddns.go +++ b/cmd/dashboard/controller/ddns.go @@ -8,16 +8,17 @@ import ( "strings" "github.com/gin-gonic/gin" + "golang.org/x/net/idna" + "github.com/naiba/nezha/model" "github.com/naiba/nezha/service/singleton" - "golang.org/x/net/idna" ) -// Add DDNS configuration -// @Summary Add DDNS configuration +// Add DDNS profile +// @Summary Add DDNS profile // @Security BearerAuth // @Schemes -// @Description Add DDNS configuration +// @Description Add DDNS profile // @Tags auth required // @Accept json // @param request body model.DDNSForm true "DDNS Request" @@ -67,17 +68,17 @@ func newDDNS(c *gin.Context) error { } singleton.OnDDNSUpdate() - c.JSON(http.StatusOK, model.Response{ - Code: http.StatusOK, + c.JSON(http.StatusOK, model.CommonResponse[any]{ + Success: true, }) return nil } -// Edit DDNS configuration -// @Summary Edit DDNS configuration +// Edit DDNS profile +// @Summary Edit DDNS profile // @Security BearerAuth // @Schemes -// @Description Edit DDNS configuration +// @Description Edit DDNS profile // @Tags auth required // @Accept json // @param request body model.DDNSForm true "DDNS Request" @@ -134,8 +135,8 @@ func editDDNS(c *gin.Context) error { } singleton.OnDDNSUpdate() - c.JSON(http.StatusOK, model.Response{ - Code: http.StatusOK, + c.JSON(http.StatusOK, model.CommonResponse[any]{ + Success: true, }) return nil } @@ -163,11 +164,61 @@ func batchDeleteDDNS(c *gin.Context) error { } singleton.OnDDNSUpdate() - c.JSON(http.StatusOK, model.CommonResponse[interface{}]{ + c.JSON(http.StatusOK, model.CommonResponse[any]{ Success: true, }) return nil } -// TODO -func listDDNS(c *gin.Context) {} +// List DDNS Profiles +// @Summary List DDNS profiles +// @Schemes +// @Description List DDNS profiles +// @Security BearerAuth +// @Tags auth required +// @param id query string false "Profile ID" +// @Produce json +// @Success 200 {object} model.CommonResponse[[]model.DDNSProfile] +// @Router /ddns [get] +func listDDNS(c *gin.Context) error { + var idList []uint64 + idQuery := c.Query("id") + + if idQuery != "" { + idListStr := strings.Split(idQuery, ",") + idList = make([]uint64, 0, len(idListStr)) + for _, v := range idListStr { + id, err := strconv.ParseUint(v, 10, 64) + if err != nil { + return err + } + idList = append(idList, id) + } + } + + var ddnsProfiles []model.DDNSProfile + + singleton.DDNSCacheLock.RLock() + if len(idList) > 0 { + ddnsProfiles = make([]model.DDNSProfile, 0, len(idList)) + for _, id := range idList { + if profile, ok := singleton.DDNSCache[id]; ok { + ddnsProfiles = append(ddnsProfiles, *profile) + } else { + return fmt.Errorf("profile id %d not found", id) + } + } + } else { + ddnsProfiles = make([]model.DDNSProfile, 0, len(singleton.DDNSCache)) + for _, profile := range singleton.DDNSCache { + ddnsProfiles = append(ddnsProfiles, *profile) + } + } + + singleton.DDNSCacheLock.RUnlock() + c.JSON(http.StatusOK, model.CommonResponse[[]model.DDNSProfile]{ + Success: true, + Data: ddnsProfiles, + }) + return nil +} diff --git a/cmd/dashboard/controller/server.go b/cmd/dashboard/controller/server.go index c59b409..ba46c19 100644 --- a/cmd/dashboard/controller/server.go +++ b/cmd/dashboard/controller/server.go @@ -5,6 +5,7 @@ import ( "strconv" "github.com/gin-gonic/gin" + "github.com/naiba/nezha/model" "github.com/naiba/nezha/pkg/utils" "github.com/naiba/nezha/service/singleton" diff --git a/cmd/dashboard/controller/server_group.go b/cmd/dashboard/controller/server_group.go index cf0b07d..1d5ac12 100644 --- a/cmd/dashboard/controller/server_group.go +++ b/cmd/dashboard/controller/server_group.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/naiba/nezha/model" "github.com/naiba/nezha/service/singleton" ) diff --git a/cmd/dashboard/controller/ws.go b/cmd/dashboard/controller/ws.go index 16c17d3..a3f3472 100644 --- a/cmd/dashboard/controller/ws.go +++ b/cmd/dashboard/controller/ws.go @@ -6,10 +6,11 @@ import ( "github.com/gin-gonic/gin" "github.com/gorilla/websocket" + "golang.org/x/sync/singleflight" + "github.com/naiba/nezha/model" "github.com/naiba/nezha/pkg/utils" "github.com/naiba/nezha/service/singleton" - "golang.org/x/sync/singleflight" ) var upgrader = websocket.Upgrader{ diff --git a/service/singleton/ddns.go b/service/singleton/ddns.go index 312516e..c0a8d9e 100644 --- a/service/singleton/ddns.go +++ b/service/singleton/ddns.go @@ -14,8 +14,8 @@ import ( ) var ( - ddnsCache map[uint64]*model.DDNSProfile - ddnsCacheLock sync.RWMutex + DDNSCache map[uint64]*model.DDNSProfile + DDNSCacheLock sync.RWMutex ) func initDDNS() { @@ -26,11 +26,11 @@ func initDDNS() { func OnDDNSUpdate() { var ddns []*model.DDNSProfile DB.Find(&ddns) - ddnsCacheLock.Lock() - defer ddnsCacheLock.Unlock() - ddnsCache = make(map[uint64]*model.DDNSProfile) + DDNSCacheLock.Lock() + defer DDNSCacheLock.Unlock() + DDNSCache = make(map[uint64]*model.DDNSProfile) for i := 0; i < len(ddns); i++ { - ddnsCache[ddns[i].ID] = ddns[i] + DDNSCache[ddns[i].ID] = ddns[i] } } @@ -40,15 +40,15 @@ func OnNameserverUpdate() { func GetDDNSProvidersFromProfiles(profileId []uint64, ip *ddns2.IP) ([]*ddns2.Provider, error) { profiles := make([]*model.DDNSProfile, 0, len(profileId)) - ddnsCacheLock.RLock() + DDNSCacheLock.RLock() for _, id := range profileId { - if profile, ok := ddnsCache[id]; ok { + if profile, ok := DDNSCache[id]; ok { profiles = append(profiles, profile) } else { return nil, fmt.Errorf("无法找到DDNS配置 ID %d", id) } } - ddnsCacheLock.RUnlock() + DDNSCacheLock.RUnlock() providers := make([]*ddns2.Provider, 0, len(profiles)) for _, profile := range profiles {