修复重置订阅API的错误
Some checks failed
build / build (api, amd64, linux) (push) Failing after -50s
build / build (api, arm64, linux) (push) Failing after -51s
build / build (api.exe, amd64, windows) (push) Failing after -51s

This commit is contained in:
CN-JS-HuiBai
2026-04-18 00:34:10 +08:00
parent d341138c8a
commit 64655932b1
7 changed files with 29 additions and 17 deletions

3
.gitignore vendored
View File

@@ -5,4 +5,5 @@ development/
dist/ dist/
frontend/admin/reverse/node_modules/ frontend/admin/reverse/node_modules/
log log
sqldes sqldes
api.exe

View File

@@ -207,7 +207,7 @@ func AdminUserResetSecret(c *gin.Context) {
return return
} }
newUUID := uuid.NewString() newUUID := uuid.NewString()
newToken := strings.ReplaceAll(uuid.NewString(), "-", "") newToken := service.GenerateSubscriptionToken()
if err := database.DB.Model(&model.User{}). if err := database.DB.Model(&model.User{}).
Where("id = ?", payload.ID). Where("id = ?", payload.ID).
Updates(map[string]any{"uuid": newUUID, "token": newToken}).Error; err != nil { Updates(map[string]any{"uuid": newUUID, "token": newToken}).Error; err != nil {

View File

@@ -1,7 +1,6 @@
package handler package handler
import ( import (
"crypto/md5"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
@@ -84,12 +83,11 @@ func Register(c *gin.Context) {
} }
now := time.Now().Unix() now := time.Now().Unix()
tokenRaw := fmt.Sprintf("%x", md5.Sum([]byte(time.Now().String()+req.Email)))
user := model.User{ user := model.User{
Email: req.Email, Email: req.Email,
Password: hashedPassword, Password: hashedPassword,
UUID: uuid.New().String(), UUID: uuid.New().String(),
Token: tokenRaw[:16], Token: service.GenerateSubscriptionToken(),
CreatedAt: now, CreatedAt: now,
UpdatedAt: now, UpdatedAt: now,
} }

View File

@@ -3,12 +3,11 @@
package handler package handler
import ( import (
"crypto/md5"
"fmt"
"net/http" "net/http"
"time" "time"
"xboard-go/internal/database" "xboard-go/internal/database"
"xboard-go/internal/model" "xboard-go/internal/model"
"xboard-go/internal/service"
"xboard-go/pkg/utils" "xboard-go/pkg/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -21,8 +20,8 @@ type LoginRequest struct {
} }
type RegisterRequest struct { type RegisterRequest struct {
Email string `json:"email" binding:"required,email"` Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required,min=8"` Password string `json:"password" binding:"required,min=8"`
InviteCode *string `json:"invite_code"` InviteCode *string `json:"invite_code"`
} }
@@ -51,7 +50,7 @@ func Login(c *gin.Context) {
} }
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"token": token, "token": token,
"is_admin": user.IsAdmin, "is_admin": user.IsAdmin,
}) })
} }
@@ -78,9 +77,7 @@ func Register(c *gin.Context) {
} }
newUUID := uuid.New().String() newUUID := uuid.New().String()
// Generate a 16-character random token for compatibility token := service.GenerateSubscriptionToken()
tokenRaw := fmt.Sprintf("%x", md5.Sum([]byte(time.Now().String()+req.Email)))
token := tokenRaw[:16]
user := model.User{ user := model.User{
Email: req.Email, Email: req.Email,

View File

@@ -115,7 +115,7 @@ func UserResetSecurity(c *gin.Context) {
} }
newUUID := uuid.New().String() newUUID := uuid.New().String()
newToken := fmt.Sprintf("%x", md5.Sum([]byte(time.Now().String()+user.Email)))[:16] newToken := service.GenerateSubscriptionToken()
if err := database.DB.Model(&model.User{}). if err := database.DB.Model(&model.User{}).
Where("id = ?", user.ID). Where("id = ?", user.ID).
Updates(map[string]any{"uuid": newUUID, "token": newToken, "updated_at": time.Now().Unix()}).Error; err != nil { Updates(map[string]any{"uuid": newUUID, "token": newToken, "updated_at": time.Now().Unix()}).Error; err != nil {

View File

@@ -1,9 +1,7 @@
package service package service
import ( import (
"crypto/md5"
"encoding/json" "encoding/json"
"fmt"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -101,7 +99,7 @@ func SyncIPv6ShadowAccount(user *model.User) bool {
ipv6User.ID = 0 ipv6User.ID = 0
ipv6User.Email = ipv6Email ipv6User.Email = ipv6Email
ipv6User.UUID = uuid.New().String() ipv6User.UUID = uuid.New().String()
ipv6User.Token = fmt.Sprintf("%x", md5.Sum([]byte(time.Now().String()+ipv6Email)))[:16] ipv6User.Token = GenerateSubscriptionToken()
ipv6User.U = 0 ipv6User.U = 0
ipv6User.D = 0 ipv6User.D = 0
ipv6User.T = 0 ipv6User.T = 0

18
internal/service/token.go Normal file
View File

@@ -0,0 +1,18 @@
package service
import (
"crypto/rand"
"encoding/hex"
)
// GenerateSubscriptionToken returns a 32-character hex token compatible with
// XBoard-style subscription URLs.
func GenerateSubscriptionToken() string {
buf := make([]byte, 16)
if _, err := rand.Read(buf); err != nil {
// Fall back to zero-value encoding only in the unlikely event random
// source fails; callers still get a stable-length token.
return hex.EncodeToString(buf)
}
return hex.EncodeToString(buf)
}