Files
SingBox-Gopanel/internal/middleware/node_auth.go
CN-JS-HuiBai 1ed31b9292
All checks were successful
build / build (api, amd64, linux) (push) Successful in -47s
build / build (api, arm64, linux) (push) Successful in -48s
build / build (api.exe, amd64, windows) (push) Successful in -47s
first commit
2026-04-17 09:49:16 +08:00

51 lines
1.1 KiB
Go

//go:build ignore
package middleware
import (
"net/http"
"xboard-go/internal/database"
"xboard-go/internal/model"
"github.com/gin-gonic/gin"
)
func NodeAuth() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.Query("token")
nodeID := c.Query("node_id")
nodeType := c.Query("node_type")
if token == "" || nodeID == "" {
c.JSON(http.StatusUnauthorized, gin.H{"message": "缺少认证信息"})
c.Abort()
return
}
// Check server_token from settings
var setting model.Setting
if err := database.DB.Where("name = ?", "server_token").First(&setting).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "系统配置错误"})
c.Abort()
return
}
if token != setting.Value {
c.JSON(http.StatusUnauthorized, gin.H{"message": "无效的Token"})
c.Abort()
return
}
// Get node info
var node model.Server
if err := database.DB.Where("id = ? AND type = ?", nodeID, nodeType).First(&node).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"message": "节点不存在"})
c.Abort()
return
}
c.Set("node", &node)
c.Next()
}
}