Files
3m-ui/backend/internal/security/credentials.go
T
dzx941 5b4cb63a73 fix: align listener generation with mihomo official spec
- Port now supports mihomo ports format (single/range/list)
- Add explicit RoutingMark field support
- Auto-propagate UDP flag for vmess/vless/trojan/anytls/trusttunnel
- Update validator to accept port ranges
- Sync visual config and client converter port types
- Rename module from dzx941 to kazeyukiro
- Fix missing ExpiresAt in JWTClaims
- Fix nodeDTO Port type to string
- Fix frontend tsconfig and add missing dependencies
2026-08-16 09:21:42 +08:00

68 lines
1.5 KiB
Go

package security
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
"io"
)
var credentialKey []byte
// InitCredentialKey initializes the key used to encrypt recoverable Mihomo
// credentials. The caller should provide a secret from server configuration.
func InitCredentialKey(secret string) {
sum := sha256.Sum256([]byte(secret))
credentialKey = sum[:]
}
func Encrypt(plain string) (string, error) {
if len(credentialKey) == 0 {
return "", errors.New("credential encryption key is not initialized")
}
block, err := aes.NewCipher(credentialKey)
if err != nil {
return "", err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}
ciphertext := gcm.Seal(nonce, nonce, []byte(plain), nil)
return base64.RawStdEncoding.EncodeToString(ciphertext), nil
}
func Decrypt(encoded string) (string, error) {
if len(credentialKey) == 0 {
return "", errors.New("credential encryption key is not initialized")
}
raw, err := base64.RawStdEncoding.DecodeString(encoded)
if err != nil {
return "", err
}
block, err := aes.NewCipher(credentialKey)
if err != nil {
return "", err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
n := gcm.NonceSize()
if len(raw) < n {
return "", errors.New("invalid encrypted credential")
}
plain, err := gcm.Open(nil, raw[:n], raw[n:], nil)
if err != nil {
return "", err
}
return string(plain), nil
}