mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 12:48:14 -05:00
dev: add ddns list api (#445)
This commit is contained in:
parent
aa0d570b2b
commit
5efd995992
@ -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))
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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"
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/naiba/nezha/model"
|
||||
"github.com/naiba/nezha/service/singleton"
|
||||
)
|
||||
|
@ -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{
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user