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 := api.Group("", optionalAuthMiddleware(authMiddleware))
|
||||||
optionalAuth.GET("/ws/server", commonHandler[any](serverStream))
|
optionalAuth.GET("/ws/server", commonHandler[any](serverStream))
|
||||||
optionalAuth.GET("/server-group", commonHandler[[]model.ServerGroup](listServerGroup))
|
optionalAuth.GET("/server-group", commonHandler[[]model.ServerGroup](listServerGroup))
|
||||||
optionalAuth.GET("/ddns", listDDNS) // TODO
|
|
||||||
|
|
||||||
auth := api.Group("", authMiddleware.MiddlewareFunc())
|
auth := api.Group("", authMiddleware.MiddlewareFunc())
|
||||||
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
|
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
|
||||||
auth.PATCH("/server/:id", commonHandler[any](editServer))
|
auth.PATCH("/server/:id", commonHandler[any](editServer))
|
||||||
|
|
||||||
|
auth.GET("/ddns", commonHandler[[]model.DDNSProfile](listDDNS))
|
||||||
auth.POST("/ddns", commonHandler[any](newDDNS))
|
auth.POST("/ddns", commonHandler[any](newDDNS))
|
||||||
auth.PATCH("/ddns/:id", commonHandler[any](editDDNS))
|
auth.PATCH("/ddns/:id", commonHandler[any](editDDNS))
|
||||||
|
|
||||||
|
@ -8,16 +8,17 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"golang.org/x/net/idna"
|
||||||
|
|
||||||
"github.com/naiba/nezha/model"
|
"github.com/naiba/nezha/model"
|
||||||
"github.com/naiba/nezha/service/singleton"
|
"github.com/naiba/nezha/service/singleton"
|
||||||
"golang.org/x/net/idna"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Add DDNS configuration
|
// Add DDNS profile
|
||||||
// @Summary Add DDNS configuration
|
// @Summary Add DDNS profile
|
||||||
// @Security BearerAuth
|
// @Security BearerAuth
|
||||||
// @Schemes
|
// @Schemes
|
||||||
// @Description Add DDNS configuration
|
// @Description Add DDNS profile
|
||||||
// @Tags auth required
|
// @Tags auth required
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @param request body model.DDNSForm true "DDNS Request"
|
// @param request body model.DDNSForm true "DDNS Request"
|
||||||
@ -67,17 +68,17 @@ func newDDNS(c *gin.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
singleton.OnDDNSUpdate()
|
singleton.OnDDNSUpdate()
|
||||||
c.JSON(http.StatusOK, model.Response{
|
c.JSON(http.StatusOK, model.CommonResponse[any]{
|
||||||
Code: http.StatusOK,
|
Success: true,
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Edit DDNS configuration
|
// Edit DDNS profile
|
||||||
// @Summary Edit DDNS configuration
|
// @Summary Edit DDNS profile
|
||||||
// @Security BearerAuth
|
// @Security BearerAuth
|
||||||
// @Schemes
|
// @Schemes
|
||||||
// @Description Edit DDNS configuration
|
// @Description Edit DDNS profile
|
||||||
// @Tags auth required
|
// @Tags auth required
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @param request body model.DDNSForm true "DDNS Request"
|
// @param request body model.DDNSForm true "DDNS Request"
|
||||||
@ -134,8 +135,8 @@ func editDDNS(c *gin.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
singleton.OnDDNSUpdate()
|
singleton.OnDDNSUpdate()
|
||||||
c.JSON(http.StatusOK, model.Response{
|
c.JSON(http.StatusOK, model.CommonResponse[any]{
|
||||||
Code: http.StatusOK,
|
Success: true,
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -163,11 +164,61 @@ func batchDeleteDDNS(c *gin.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
singleton.OnDDNSUpdate()
|
singleton.OnDDNSUpdate()
|
||||||
c.JSON(http.StatusOK, model.CommonResponse[interface{}]{
|
c.JSON(http.StatusOK, model.CommonResponse[any]{
|
||||||
Success: true,
|
Success: true,
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// List DDNS Profiles
|
||||||
func listDDNS(c *gin.Context) {}
|
// @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"
|
"strconv"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"github.com/naiba/nezha/model"
|
"github.com/naiba/nezha/model"
|
||||||
"github.com/naiba/nezha/pkg/utils"
|
"github.com/naiba/nezha/pkg/utils"
|
||||||
"github.com/naiba/nezha/service/singleton"
|
"github.com/naiba/nezha/service/singleton"
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"github.com/naiba/nezha/model"
|
"github.com/naiba/nezha/model"
|
||||||
"github.com/naiba/nezha/service/singleton"
|
"github.com/naiba/nezha/service/singleton"
|
||||||
)
|
)
|
||||||
|
@ -6,10 +6,11 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"golang.org/x/sync/singleflight"
|
||||||
|
|
||||||
"github.com/naiba/nezha/model"
|
"github.com/naiba/nezha/model"
|
||||||
"github.com/naiba/nezha/pkg/utils"
|
"github.com/naiba/nezha/pkg/utils"
|
||||||
"github.com/naiba/nezha/service/singleton"
|
"github.com/naiba/nezha/service/singleton"
|
||||||
"golang.org/x/sync/singleflight"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{
|
var upgrader = websocket.Upgrader{
|
||||||
|
@ -14,8 +14,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ddnsCache map[uint64]*model.DDNSProfile
|
DDNSCache map[uint64]*model.DDNSProfile
|
||||||
ddnsCacheLock sync.RWMutex
|
DDNSCacheLock sync.RWMutex
|
||||||
)
|
)
|
||||||
|
|
||||||
func initDDNS() {
|
func initDDNS() {
|
||||||
@ -26,11 +26,11 @@ func initDDNS() {
|
|||||||
func OnDDNSUpdate() {
|
func OnDDNSUpdate() {
|
||||||
var ddns []*model.DDNSProfile
|
var ddns []*model.DDNSProfile
|
||||||
DB.Find(&ddns)
|
DB.Find(&ddns)
|
||||||
ddnsCacheLock.Lock()
|
DDNSCacheLock.Lock()
|
||||||
defer ddnsCacheLock.Unlock()
|
defer DDNSCacheLock.Unlock()
|
||||||
ddnsCache = make(map[uint64]*model.DDNSProfile)
|
DDNSCache = make(map[uint64]*model.DDNSProfile)
|
||||||
for i := 0; i < len(ddns); i++ {
|
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) {
|
func GetDDNSProvidersFromProfiles(profileId []uint64, ip *ddns2.IP) ([]*ddns2.Provider, error) {
|
||||||
profiles := make([]*model.DDNSProfile, 0, len(profileId))
|
profiles := make([]*model.DDNSProfile, 0, len(profileId))
|
||||||
ddnsCacheLock.RLock()
|
DDNSCacheLock.RLock()
|
||||||
for _, id := range profileId {
|
for _, id := range profileId {
|
||||||
if profile, ok := ddnsCache[id]; ok {
|
if profile, ok := DDNSCache[id]; ok {
|
||||||
profiles = append(profiles, profile)
|
profiles = append(profiles, profile)
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("无法找到DDNS配置 ID %d", id)
|
return nil, fmt.Errorf("无法找到DDNS配置 ID %d", id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ddnsCacheLock.RUnlock()
|
DDNSCacheLock.RUnlock()
|
||||||
|
|
||||||
providers := make([]*ddns2.Provider, 0, len(profiles))
|
providers := make([]*ddns2.Provider, 0, len(profiles))
|
||||||
for _, profile := range profiles {
|
for _, profile := range profiles {
|
||||||
|
Loading…
Reference in New Issue
Block a user