修复重置订阅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

View File

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

View File

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

View File

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

View File

@@ -115,7 +115,7 @@ func UserResetSecurity(c *gin.Context) {
}
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{}).
Where("id = ?", user.ID).
Updates(map[string]any{"uuid": newUUID, "token": newToken, "updated_at": time.Now().Unix()}).Error; err != nil {

View File

@@ -1,9 +1,7 @@
package service
import (
"crypto/md5"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
@@ -101,7 +99,7 @@ func SyncIPv6ShadowAccount(user *model.User) bool {
ipv6User.ID = 0
ipv6User.Email = ipv6Email
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.D = 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)
}