package toolbox import ( "errors" "fmt" "os" "os/user" "path" "path/filepath" "strings" "time" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/systemctl" ) type Ftp struct { DefaultUser string DefaultGroup string } type FtpClient interface { Status() (bool, bool) Operate(operate string) error LoadList() ([]FtpList, error) UserAdd(username, path, passwd string) error UserDel(username string) error SetPasswd(username, passwd string) error Reload() error LoadLogs() ([]FtpLog, error) } func NewFtpClient() (*Ftp, error) { userItem, err := user.LookupId("1000") if err == nil { groupItem, err := user.LookupGroupId(userItem.Gid) if err != nil { return nil, err } return &Ftp{DefaultUser: userItem.Username, DefaultGroup: groupItem.Name}, err } if err.Error() != user.UnknownUserIdError(1000).Error() { return nil, err } groupItem, err := user.LookupGroupId("1000") if err == nil { stdout2, err := cmd.Execf("useradd -u 1000 -g %s %s", groupItem.Name, "1panel") if err != nil { return nil, errors.New(stdout2) } return &Ftp{DefaultUser: "1panel", DefaultGroup: groupItem.Name}, nil } if err.Error() != user.UnknownGroupIdError("1000").Error() { return nil, err } stdout, err := cmd.Exec("groupadd -g 1000 1panel") if err != nil { return nil, errors.New(string(stdout)) } stdout2, err := cmd.Exec("useradd -u 1000 -g 1panel 1panel") if err != nil { return nil, errors.New(stdout2) } return &Ftp{DefaultUser: "1panel", DefaultGroup: "1panel"}, nil } func (f *Ftp) Status() (bool, bool) { isActive, _ := systemctl.IsActive("pure-ftpd.service") isExist, _ := systemctl.IsExist("pure-ftpd.service") return isActive, isExist } func (f *Ftp) Operate(operate string) error { switch operate { case "start", "restart", "stop": stdout, err := cmd.Execf("systemctl %s pure-ftpd.service", operate) if err != nil { return fmt.Errorf("%s the pure-ftpd.service failed, err: %s", operate, stdout) } return nil default: return fmt.Errorf("not support such operation: %v", operate) } } func (f *Ftp) UserAdd(username, passwd, path string) error { std, err := cmd.Execf("pure-pw useradd %s -u %s -d %s <