package service import ( "context" "errors" "sea-vhost/internal/isolation" "sea-vhost/internal/panel" "sea-vhost/internal/scope" "sea-vhost/internal/store" ) func (s *Projects) ManageFTP(ctx context.Context, u store.User, id uint64, action string) (string, error) { p, e := s.authorizedProject(ctx, u, id) if e != nil { return "", e } ctx = scope.WithProject(ctx, p.ID, p.NodeID) if action != "disable" { if e = s.ensureTenantWritable(ctx, p.TenantID); e != nil { return "", e } } rows, e := s.store.ListFTPResources(ctx, p.TenantID, &id) if e != nil || len(rows) != 1 { return "", errors.New("FTP 账号不存在") } ftp := rows[0] plan, _, e := s.store.PlanAndUsage(ctx, p.TenantID) if e != nil { return "", e } agent, ok := s.isolation.(isolation.FTPManager) if !ok { return "", errors.New("节点未支持 FTP 管理") } spec := isolation.ProjectSpec{TenantID: p.TenantID, HostName: p.HostName, RunPath: p.RunPath, StorageBytes: plan.StorageBytes, ProcessLimit: uint32(p.ProcessLimit)} switch action { case "enable", "disable": enabled := action == "enable" if e = s.panel.SetFTPState(ctx, ftp.PanelID, ftp.Username, enabled); e != nil { return "", e } if e = s.store.SetFTPEnabled(ctx, p.ID, enabled); e != nil { _ = s.panel.SetFTPState(context.WithoutCancel(ctx), ftp.PanelID, ftp.Username, ftp.Enabled) return "", e } if !enabled { if e = agent.DisconnectFTP(ctx, spec); e != nil { return "", errors.New("FTP 已停用,旧连接断开失败,请稍后重试") } } return "", nil case "reset": adapter, ok := s.panel.(panel.CredentialManager) if !ok { return "", errors.New("面板未支持改密") } stored, e := s.store.Credentials(ctx, p.TenantID, p.ID) if e != nil { return "", e } old, e := s.credentials.Open(credentialLabel("ftp", p.ID), stored.FTPPasswordCipher) if e != nil { return "", e } password, e := randomPanelPassword() if e != nil { return "", e } cipher, e := s.credentials.Seal(credentialLabel("ftp", p.ID), password) if e != nil { return "", e } if e = adapter.SetFTPPassword(ctx, ftp.PanelID, ftp.Username, password); e != nil { return "", e } if e = s.store.SetFTPPasswordCipher(ctx, p.ID, cipher); e != nil { _ = adapter.SetFTPPassword(context.WithoutCancel(ctx), ftp.PanelID, ftp.Username, old) return "", e } if e = agent.DisconnectFTP(ctx, spec); e != nil { return password, errors.New("密码已更新,旧连接断开失败,请停用后重新启用 FTP") } return password, nil default: return "", errors.New("不支持的 FTP 操作") } }