fix: backend respect TRANSPORT_ENCRYPTION config for model/exchange updates

This commit is contained in:
tinkle-community
2025-12-09 19:30:00 +08:00
parent 23277fc39f
commit 6546264df9

View File

@@ -1196,9 +1196,10 @@ func (s *Server) handleGetModelConfigs(c *gin.Context) {
c.JSON(http.StatusOK, safeModels) c.JSON(http.StatusOK, safeModels)
} }
// handleUpdateModelConfigs Update AI model configurations (encrypted data only) // handleUpdateModelConfigs Update AI model configurations (supports both encrypted and plain text based on config)
func (s *Server) handleUpdateModelConfigs(c *gin.Context) { func (s *Server) handleUpdateModelConfigs(c *gin.Context) {
userID := c.GetString("user_id") userID := c.GetString("user_id")
cfg := config.Get()
// Read raw request body // Read raw request body
bodyBytes, err := c.GetRawData() bodyBytes, err := c.GetRawData()
@@ -1207,7 +1208,19 @@ func (s *Server) handleUpdateModelConfigs(c *gin.Context) {
return return
} }
// Parse encrypted payload var req UpdateModelConfigRequest
// Check if transport encryption is enabled
if !cfg.TransportEncryption {
// Transport encryption disabled, accept plain JSON
if err := json.Unmarshal(bodyBytes, &req); err != nil {
logger.Infof("❌ Failed to parse plain JSON request: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request format"})
return
}
logger.Infof("📝 Received plain text model config (UserID: %s)", userID)
} else {
// Transport encryption enabled, require encrypted payload
var encryptedPayload crypto.EncryptedPayload var encryptedPayload crypto.EncryptedPayload
if err := json.Unmarshal(bodyBytes, &encryptedPayload); err != nil { if err := json.Unmarshal(bodyBytes, &encryptedPayload); err != nil {
logger.Infof("❌ Failed to parse encrypted payload: %v", err) logger.Infof("❌ Failed to parse encrypted payload: %v", err)
@@ -1235,13 +1248,13 @@ func (s *Server) handleUpdateModelConfigs(c *gin.Context) {
} }
// Parse decrypted data // Parse decrypted data
var req UpdateModelConfigRequest
if err := json.Unmarshal([]byte(decrypted), &req); err != nil { if err := json.Unmarshal([]byte(decrypted), &req); err != nil {
logger.Infof("❌ Failed to parse decrypted data: %v", err) logger.Infof("❌ Failed to parse decrypted data: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse decrypted data"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse decrypted data"})
return return
} }
logger.Infof("🔓 Decrypted model config data (UserID: %s)", userID) logger.Infof("🔓 Decrypted model config data (UserID: %s)", userID)
}
// Update each model's configuration // Update each model's configuration
for modelID, modelData := range req.Models { for modelID, modelData := range req.Models {
@@ -1293,9 +1306,10 @@ func (s *Server) handleGetExchangeConfigs(c *gin.Context) {
c.JSON(http.StatusOK, safeExchanges) c.JSON(http.StatusOK, safeExchanges)
} }
// handleUpdateExchangeConfigs Update exchange configurations (encrypted data only) // handleUpdateExchangeConfigs Update exchange configurations (supports both encrypted and plain text based on config)
func (s *Server) handleUpdateExchangeConfigs(c *gin.Context) { func (s *Server) handleUpdateExchangeConfigs(c *gin.Context) {
userID := c.GetString("user_id") userID := c.GetString("user_id")
cfg := config.Get()
// Read raw request body // Read raw request body
bodyBytes, err := c.GetRawData() bodyBytes, err := c.GetRawData()
@@ -1304,7 +1318,19 @@ func (s *Server) handleUpdateExchangeConfigs(c *gin.Context) {
return return
} }
// Parse encrypted payload var req UpdateExchangeConfigRequest
// Check if transport encryption is enabled
if !cfg.TransportEncryption {
// Transport encryption disabled, accept plain JSON
if err := json.Unmarshal(bodyBytes, &req); err != nil {
logger.Infof("❌ Failed to parse plain JSON request: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request format"})
return
}
logger.Infof("📝 Received plain text exchange config (UserID: %s)", userID)
} else {
// Transport encryption enabled, require encrypted payload
var encryptedPayload crypto.EncryptedPayload var encryptedPayload crypto.EncryptedPayload
if err := json.Unmarshal(bodyBytes, &encryptedPayload); err != nil { if err := json.Unmarshal(bodyBytes, &encryptedPayload); err != nil {
logger.Infof("❌ Failed to parse encrypted payload: %v", err) logger.Infof("❌ Failed to parse encrypted payload: %v", err)
@@ -1332,13 +1358,13 @@ func (s *Server) handleUpdateExchangeConfigs(c *gin.Context) {
} }
// Parse decrypted data // Parse decrypted data
var req UpdateExchangeConfigRequest
if err := json.Unmarshal([]byte(decrypted), &req); err != nil { if err := json.Unmarshal([]byte(decrypted), &req); err != nil {
logger.Infof("❌ Failed to parse decrypted data: %v", err) logger.Infof("❌ Failed to parse decrypted data: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse decrypted data"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse decrypted data"})
return return
} }
logger.Infof("🔓 Decrypted exchange config data (UserID: %s)", userID) logger.Infof("🔓 Decrypted exchange config data (UserID: %s)", userID)
}
// Update each exchange's configuration // Update each exchange's configuration
for exchangeID, exchangeData := range req.Exchanges { for exchangeID, exchangeData := range req.Exchanges {