package service import ( "context" "encoding/json" "errors" "fmt" "sea-vhost/internal/auth" "sea-vhost/internal/isolation" "sea-vhost/internal/panel" "sea-vhost/internal/scope" "sea-vhost/internal/store" ) var ErrOwnershipIncomplete = errors.New("归属变更未完成,主机管理已锁定,请按原目标和策略恢复") type OwnershipCredentials struct { HostPassword string `json:"hostPassword,omitempty"` FTPPassword string `json:"ftpPassword,omitempty"` DatabasePassword string `json:"databasePassword,omitempty"` } // Ownership changes never rename a resource namespace, Unix identity or directory. // Persist staged credentials before native mutations so an explicit recovery can // repeat those mutations with the same passwords after a timeout or restart. func (s *Projects) ChangeHostOwner(ctx context.Context, id, version, target uint64, mode string, stop, resume bool) (OwnershipCredentials, error) { var result OwnershipCredentials if mode != "preserve" && mode != "reset" { return result, errors.New("请选择保留或重置凭据") } err := s.withHostOwnershipChange(ctx, id, func() error { hosts, e := s.store.ListAdminProjects(ctx) if e != nil { return e } var host *store.AdminProject for i := range hosts { if hosts[i].ID == id { host = &hosts[i] break } } if host == nil { return store.ErrNotFound } if host.OwnershipVersion != version { return errors.New("主机归属已变化,请重新预览") } if !host.ManagementLocked && ((host.OwnerTenantID == nil && target == 0) || (host.OwnerTenantID != nil && *host.OwnerTenantID == target)) { return nil } ctx = scope.WithProject(ctx, id, host.NodeID) var manager panel.CredentialManager var sessions isolation.CredentialSessions if mode == "reset" { var ok bool manager, ok = s.panel.(panel.CredentialManager) if !ok { return errors.New("面板尚未支持凭据重置") } sessions, ok = s.isolation.(isolation.CredentialSessions) if !ok { return errors.New("节点尚未支持旧连接注销") } } label := fmt.Sprintf("ownership-change:%d:%d", id, version) var cipher []byte if !host.ManagementLocked && mode == "reset" { result.HostPassword, e = randomPassword() if e != nil { return e } result.FTPPassword, e = randomPanelPassword() if e != nil { return e } result.DatabasePassword, e = randomPanelPassword() if e != nil { return e } raw, _ := json.Marshal(result) cipher, e = s.credentials.Seal(label, string(raw)) if e != nil { return e } } pending, e := s.store.BeginOwnershipChange(ctx, id, version, target, mode, stop, resume, cipher) if e != nil { return e } // From this point any failure is deliberately fail-closed and recoverable. finish := func() error { if mode == "reset" { raw, e := s.credentials.Open(label, pending.CredentialsCipher) if e != nil { return e } if e = json.Unmarshal([]byte(raw), &result); e != nil { return e } if result.HostPassword == "" || result.FTPPassword == "" || result.DatabasePassword == "" { return errors.New("staged credentials missing") } } if stop { sites, e := s.store.ListSites(ctx, host.TenantID, id) if e != nil { return e } for _, site := range sites { if site.PanelSiteID == 0 { continue } e = s.panel.SetSiteState(ctx, site.PanelSiteID, site.Domain, false) if e != nil && site.Runtime != "PHP" { plan, _, planErr := s.store.PlanAndUsage(ctx, host.TenantID) if planErr != nil { return planErr } if terminateErr := s.isolation.TerminateRuntime(ctx, isolation.RuntimeProcessSpec{Project: isolation.ProjectSpec{TenantID: host.TenantID, HostName: host.HostName, RunPath: site.RunPath, StorageBytes: plan.StorageBytes, ProcessLimit: uint32(site.ProcessLimit)}}); terminateErr != nil { return terminateErr } e = s.panel.SetSiteState(ctx, site.PanelSiteID, site.Domain, false) } if e != nil { return e } if site.Runtime == "PHP" { spec, e := s.phpIdentitySpec(ctx, host.Project, site) if e != nil { return e } if e = s.isolation.SetPHPIdentityState(ctx, spec, false); e != nil { return e } } if site.Primary { e = s.store.SetProjectState(ctx, host.TenantID, id, "stopped") } else { e = s.store.SetSiteState(ctx, host.TenantID, id, site.ID, "stopped") } if e != nil { return e } } } var hash string var ftpCipher, dbCipher []byte if mode == "reset" { ftps, e := s.store.ListFTPResources(ctx, host.TenantID, &id) if e != nil { return e } var ftp *store.FTPResource for i := range ftps { if ftps[i].ProjectID == id { ftp = &ftps[i] break } } dbs, e := s.store.ListDatabaseResources(ctx, host.TenantID, &id) if e != nil { return e } var database *store.DatabaseResource for i := range dbs { if dbs[i].ProjectID == id { database = &dbs[i] break } } if ftp == nil || database == nil { return errors.New("native resources missing") } if e = manager.SetFTPPassword(ctx, ftp.PanelID, ftp.Username, result.FTPPassword); e != nil { return e } if e = manager.SetDatabasePassword(ctx, database.PanelID, database.Username, result.DatabasePassword); e != nil { return e } plan, _, e := s.store.PlanAndUsage(ctx, host.TenantID) if e != nil { return e } if e = sessions.DisconnectCredentialSessions(ctx, isolation.ProjectSpec{TenantID: host.TenantID, HostName: host.HostName, RunPath: host.RunPath, StorageBytes: plan.StorageBytes, ProcessLimit: uint32(host.ProcessLimit)}); e != nil { return e } hash, e = auth.HashPassword(result.HostPassword) if e != nil { return e } ftpCipher, e = s.credentials.Seal(credentialLabel("ftp", id), result.FTPPassword) if e != nil { return e } dbCipher, e = s.credentials.Seal(credentialLabel("database", id), result.DatabasePassword) if e != nil { return e } } return s.store.CompleteOwnershipChange(ctx, id, version, target, mode, hash, ftpCipher, dbCipher) } if e = finish(); e != nil { return ErrOwnershipIncomplete } return nil }) if err != nil { return OwnershipCredentials{}, err } return result, nil }