package controller import ( "fmt" "strings" "ezops/model" "ezops/pkg/crypto" "gorm.io/gorm" ) func (h *ServerHandler) loadImportCredential(id *uint, wantType string) (model.Credential, string, bool, error) { return loadImportCredentialByDB(h.db, h.crypt, id, wantType) } func (h *CloudSyncHandler) loadImportCredential(id *uint, wantType string) (model.Credential, string, bool, error) { return loadImportCredentialByDB(h.db, h.crypt, id, wantType) } func loadImportCredentialByDB(db *gorm.DB, crypt *crypto.SecretBox, id *uint, wantType string) (model.Credential, string, bool, error) { var cred model.Credential if id == nil || *id == 0 { return cred, "", false, nil } if err := db.First(&cred, *id).Error; err != nil { return cred, "", false, fmt.Errorf("凭据不存在") } if cred.SecretType != wantType { return cred, "", false, fmt.Errorf("请选择 %s 类型凭据", strings.ToUpper(wantType)) } plain, err := crypt.Decrypt(cred.Content) if err != nil { return cred, "", false, fmt.Errorf("凭据解密失败") } return cred, plain, true, nil } func normalizeImportOSType(osType, osName string) string { raw := strings.ToLower(strings.TrimSpace(osType + " " + osName)) if strings.Contains(raw, "windows") || strings.Contains(raw, "win") { return "windows" } return "linux" } // normalizeServerRemoteAccess 补齐历史导入数据中缺失的 os_type/protocol 字段 // Logan.Li func normalizeServerRemoteAccess(server *model.Server) { if server == nil { return } if server.OSType != "" && server.Protocol != "" { if server.OSType == "windows" && server.Protocol != "rdp" { server.Protocol = "rdp" } if server.Protocol == "rdp" && server.OSType == "" { server.OSType = "windows" } if server.RDPPort == 0 { server.RDPPort = 3389 } return } osType := normalizeImportOSType(server.OSType, server.OS) if osType == "windows" { server.OSType = "windows" server.Protocol = "rdp" if server.RDPPort == 0 { server.RDPPort = 3389 } return } if server.OSType == "" { server.OSType = "linux" } if server.Protocol == "" { server.Protocol = "ssh" } } // isRDPServer 判断资产是否应走 RDP 图形会话 // Logan.Li func isRDPServer(server model.Server) bool { normalizeServerRemoteAccess(&server) return server.Protocol == "rdp" || server.OSType == "windows" }