diff --git a/api/crypto/crypto.go b/api/crypto/crypto.go index 8a6ad90f..5ed59363 100644 --- a/api/crypto/crypto.go +++ b/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 diff --git a/internal/sign/crypto.go b/internal/crypto/crypto.go similarity index 97% rename from internal/sign/crypto.go rename to internal/crypto/crypto.go index 0ca117a3..023d8e79 100644 --- a/internal/sign/crypto.go +++ b/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 } diff --git a/internal/crypto/errors.go b/internal/crypto/errors.go index 4cbac414..200a6cd6 100644 --- a/internal/crypto/errors.go +++ b/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") ) diff --git a/internal/kernel/boot.go b/internal/kernel/boot.go index 5a7eb57f..f5a2fd2a 100644 --- a/internal/kernel/boot.go +++ b/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) diff --git a/internal/middleware/encrypted_params.go b/internal/middleware/encrypted_params.go index d2a2b877..381df944 100644 --- a/internal/middleware/encrypted_params.go +++ b/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 diff --git a/internal/sign/errors.go b/internal/sign/errors.go deleted file mode 100644 index 25e77969..00000000 --- a/internal/sign/errors.go +++ /dev/null @@ -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") -)