Browse Source

fix: user curd panic after install

Jacky 4 months ago
parent
commit
d83272d5b8

+ 2 - 2
api/crypto/crypto.go

@@ -4,13 +4,13 @@ import (
 	"net/http"
 
 	"github.com/0xJacky/Nginx-UI/api"
-	"github.com/0xJacky/Nginx-UI/internal/sign"
+	"github.com/0xJacky/Nginx-UI/internal/crypto"
 	"github.com/gin-gonic/gin"
 )
 
 // GetPublicKey generates a new ED25519 key pair and registers it in the cache
 func GetPublicKey(c *gin.Context) {
-	sign, err := sign.GetCryptoParams()
+	sign, err := crypto.GetCryptoParams()
 	if err != nil {
 		api.ErrHandler(c, err)
 		return

+ 2 - 2
internal/sign/crypto.go → internal/crypto/crypto.go

@@ -1,4 +1,4 @@
-package sign
+package crypto
 
 import (
 	"crypto/rand"
@@ -44,7 +44,7 @@ func GenerateRSAKeyPair() (privateKeyPEM, publicKeyPEM []byte, err error) {
 // GetCryptoParams registers a new key pair in the cache if it doesn't exist
 // otherwise, it returns the existing nonce and public key
 func GetCryptoParams() (sign *Sign, err error) {
-	// Check if key pair exists in cache
+	// Check if the key pair exists in then cache
 	if sign, ok := cache.Get(CacheKey); ok {
 		return sign.(*Sign), nil
 	}

+ 1 - 0
internal/crypto/errors.go

@@ -6,4 +6,5 @@ var (
 	e                     = cosy.NewErrorScope("crypto")
 	ErrPlainTextEmpty     = e.New(50001, "plain text is empty")
 	ErrCipherTextTooShort = e.New(50002, "cipher text is too short")
+	ErrTimeout            = e.New(40401, "request timeout")
 )

+ 9 - 6
internal/kernel/boot.go

@@ -3,6 +3,10 @@ package kernel
 import (
 	"crypto/rand"
 	"encoding/hex"
+	"mime"
+	"path"
+	"runtime"
+
 	"github.com/0xJacky/Nginx-UI/internal/analytic"
 	"github.com/0xJacky/Nginx-UI/internal/cache"
 	"github.com/0xJacky/Nginx-UI/internal/cert"
@@ -17,10 +21,8 @@ import (
 	"github.com/uozi-tech/cosy"
 	sqlite "github.com/uozi-tech/cosy-driver-sqlite"
 	"github.com/uozi-tech/cosy/logger"
+	cModel "github.com/uozi-tech/cosy/model"
 	cSettings "github.com/uozi-tech/cosy/settings"
-	"mime"
-	"path"
-	"runtime"
 )
 
 func Boot() {
@@ -73,12 +75,13 @@ func recovery() {
 }
 
 func InitDatabase() {
+	cModel.ResolvedModels()
 	// Skip install
 	if settings.NodeSettings.SkipInstallation {
 		skipInstall()
 	}
 
-	if "" != cSettings.AppSettings.JwtSecret {
+	if cSettings.AppSettings.JwtSecret != "" {
 		db := cosy.InitDB(sqlite.Open(path.Dir(cSettings.ConfPath), settings.DatabaseSettings))
 		model.Use(db)
 		query.Init(db)
@@ -88,7 +91,7 @@ func InitDatabase() {
 }
 
 func InitNodeSecret() {
-	if "" == settings.NodeSettings.Secret {
+	if settings.NodeSettings.Secret == "" {
 		logger.Info("Secret is empty, generating...")
 		uuidStr := uuid.New().String()
 		settings.NodeSettings.Secret = uuidStr
@@ -102,7 +105,7 @@ func InitNodeSecret() {
 }
 
 func InitCryptoSecret() {
-	if "" == settings.CryptoSettings.Secret {
+	if settings.CryptoSettings.Secret == "" {
 		logger.Info("Secret is empty, generating...")
 
 		key := make([]byte, 32)

+ 2 - 2
internal/middleware/encrypted_params.go

@@ -6,7 +6,7 @@ import (
 	"io"
 	"net/http"
 
-	"github.com/0xJacky/Nginx-UI/internal/sign"
+	"github.com/0xJacky/Nginx-UI/internal/crypto"
 	"github.com/gin-gonic/gin"
 	"github.com/uozi-tech/cosy"
 )
@@ -30,7 +30,7 @@ func EncryptedParams() gin.HandlerFunc {
 		}
 
 		// 2. Decrypt the parameters (implement your decryption logic)
-		decryptedData, err := sign.Decrypt(encryptedReq.EncryptedParams)
+		decryptedData, err := crypto.Decrypt(encryptedReq.EncryptedParams)
 		if err != nil {
 			c.AbortWithStatusJSON(http.StatusBadRequest, ErrDecryptionFailed)
 			return

+ 0 - 12
internal/sign/errors.go

@@ -1,12 +0,0 @@
-package sign
-
-import "github.com/uozi-tech/cosy"
-
-var (
-	e                        = cosy.NewErrorScope("sign")
-	ErrTimeout               = e.New(40401, "request timeout")
-	ErrInvalidNonce          = e.New(50000, "invalid nonce")
-	ErrDecodePrivateKey      = e.New(50001, "failed to decode private key")
-	ErrInvalidSign           = e.New(50002, "invalid signature")
-	ErrEncryptedDataTooShort = e.New(50003, "encrypted data too short")
-)