up
This commit is contained in:
@@ -7,21 +7,19 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Server ServerConfig
|
||||
Database DatabaseConfig
|
||||
JWT JWTConfig
|
||||
Email EmailConfig
|
||||
Upload UploadConfig
|
||||
Site SiteConfig
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Port string
|
||||
Mode string
|
||||
Server struct {
|
||||
Port string `yaml:"port"`
|
||||
Mode string `yaml:"mode"`
|
||||
} `yaml:"server"`
|
||||
Database DatabaseConfig `yaml:"database"`
|
||||
JWT JWTConfig `yaml:"jwt"`
|
||||
Email EmailConfig `yaml:"email"`
|
||||
Upload UploadConfig `yaml:"upload"`
|
||||
Site SiteConfig `yaml:"site"`
|
||||
Security SecurityConfig `yaml:"security"`
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Type string
|
||||
Path string
|
||||
}
|
||||
|
||||
@@ -42,13 +40,17 @@ type UploadConfig struct {
|
||||
}
|
||||
|
||||
type SiteConfig struct {
|
||||
Title string `mapstructure:"title"`
|
||||
Description string `mapstructure:"description"`
|
||||
BaseURL string `mapstructure:"base_url"`
|
||||
ICP string `mapstructure:"icp"`
|
||||
Copyright string `mapstructure:"copyright"`
|
||||
Logo string `mapstructure:"logo"`
|
||||
Favicon string `mapstructure:"favicon"`
|
||||
Title string
|
||||
Description string
|
||||
BaseURL string
|
||||
Logo string
|
||||
Favicon string
|
||||
Copyright string
|
||||
ICP string
|
||||
}
|
||||
|
||||
type SecurityConfig struct {
|
||||
EncryptKey string
|
||||
}
|
||||
|
||||
func LoadConfig() (*Config, error) {
|
||||
|
79
internal/utils/crypto.go
Normal file
79
internal/utils/crypto.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
type DeviceValidateResponse struct {
|
||||
Status string `json:"status"` // 设备状态
|
||||
LicenseType string `json:"license_type"` // 授权类型
|
||||
ExpireTime string `json:"expire_time"` // 过期时间
|
||||
StartCount int `json:"start_count"` // 启动次数
|
||||
MaxUses int `json:"max_uses"` // 最大使用次数
|
||||
Timestamp int64 `json:"timestamp"` // 时间戳
|
||||
Signature string `json:"signature"` // 签名
|
||||
}
|
||||
|
||||
// EncryptResponse 加密设备验证响应
|
||||
func EncryptResponse(data DeviceValidateResponse, key []byte) (string, error) {
|
||||
// 序列化数据
|
||||
plaintext, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 创建随机IV
|
||||
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
||||
iv := ciphertext[:aes.BlockSize]
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 加密
|
||||
stream := cipher.NewCFBEncrypter(block, iv)
|
||||
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
|
||||
|
||||
// 返回base64编码的密文
|
||||
return base64.StdEncoding.EncodeToString(ciphertext), nil
|
||||
}
|
||||
|
||||
// DecryptResponse 解密设备验证响应
|
||||
func DecryptResponse(encrypted string, key []byte) (*DeviceValidateResponse, error) {
|
||||
ciphertext, err := base64.StdEncoding.DecodeString(encrypted)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(ciphertext) < aes.BlockSize {
|
||||
return nil, errors.New("密文太短")
|
||||
}
|
||||
|
||||
iv := ciphertext[:aes.BlockSize]
|
||||
ciphertext = ciphertext[aes.BlockSize:]
|
||||
|
||||
stream := cipher.NewCFBDecrypter(block, iv)
|
||||
stream.XORKeyStream(ciphertext, ciphertext)
|
||||
|
||||
var response DeviceValidateResponse
|
||||
if err := json.Unmarshal(ciphertext, &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
@@ -28,6 +28,8 @@ func InitDB(config *DatabaseConfig) (*gorm.DB, error) {
|
||||
|
||||
&model.DeviceModel{},
|
||||
|
||||
&model.DeviceLog{},
|
||||
|
||||
&model.PasswordResetToken{},
|
||||
|
||||
&model.Captcha{},
|
||||
|
@@ -36,33 +36,22 @@ func TestDB(t *testing.T) *gorm.DB {
|
||||
// TestConfig 创建测试配置
|
||||
func TestConfig() *Config {
|
||||
return &Config{
|
||||
Server: ServerConfig{
|
||||
Server: struct {
|
||||
Port string `yaml:"port"`
|
||||
Mode string `yaml:"mode"`
|
||||
}{
|
||||
Port: "8080",
|
||||
Mode: "test",
|
||||
},
|
||||
Database: DatabaseConfig{
|
||||
Type: "sqlite3",
|
||||
Path: ":memory:",
|
||||
},
|
||||
JWT: JWTConfig{
|
||||
Secret: "test-secret",
|
||||
Expire: "24h",
|
||||
},
|
||||
Email: EmailConfig{
|
||||
Host: "smtp.example.com",
|
||||
Port: 587,
|
||||
Username: "test@example.com",
|
||||
Password: "test-password",
|
||||
},
|
||||
Upload: UploadConfig{
|
||||
Path: "./test-uploads",
|
||||
},
|
||||
Site: SiteConfig{
|
||||
Title: "Test Site",
|
||||
Description: "Test Description",
|
||||
BaseURL: "http://localhost:8080",
|
||||
ICP: "Test ICP",
|
||||
Copyright: "Test Copyright",
|
||||
Security: SecurityConfig{
|
||||
EncryptKey: "test-32-byte-encrypt-key-here1234567",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user