func (f *FTP) Deliver(r io.Reader) error { if f.Timeout == 0 { // Default to 5 second timeout f.Timeout = 5 } conn, err := jftp.DialTimeout(f.Address, time.Duration(f.Timeout)*time.Second) if err != nil { return err } // Login err = conn.Login(f.Username, f.Password) if err != nil { return err } var path string // Determine the path if len(f.Path) > 0 { path = f.Path } else if f.PathFunc != nil { path = f.PathFunc() } else { return errors.New("Missing path and path func") } // Create the path if necessary... if strings.Contains(path, "/") { dir := filepath.Dir(path) spl := strings.Split(dir, "/") for _, folder := range spl { files, err := conn.NameList(folder) if err != nil { return err } if len(files) == 0 { // Folder does not exist. Create err = conn.MakeDir(folder) if err != nil { return err } } err = conn.ChangeDir(folder) if err != nil { return err } } path = filepath.Base(path) } return conn.Stor(path, r) }
// connect and login to ftp-server func (v *FTPConn) FTPLogin(host_port, user, password string, timeout int) error { serconn, err := ftp.DialTimeout(host_port, time.Duration(timeout)*time.Second) if err != nil { CLog.PrintLog(true, "Error connect to ftp-server: ", host_port, ". ", err) return err } v.ServerConn = *serconn if err := v.ServerConn.Login(user, password); err != nil { CLog.PrintLog(true, "Error login to ftp-server for user name: ", user, ". ", err) return err } return nil }
func NewFtpClient(cfg FtpCfg) (*MyFtpClient, error) { addr := fmt.Sprintf("%s:%d", cfg.host, cfg.port) conn, err := ftp.DialTimeout(addr, 5*time.Second) if err != nil { log.Printf("Error connecting: %s", err) return nil, err } err = conn.Login(cfg.user, cfg.pw) if err != nil { log.Printf("Error login in: %s", err) conn.Quit() return nil, err } return &MyFtpClient{conn, true}, nil }
//Upload uplaod local log file to ftp server func Upload(ftpLogFile, localLogFile, ftpFinishFile, localFinishFile, basePath, domain, custom string) error { client, err := ftp.DialTimeout(Config.FtpServer, 10*time.Second) if err != nil { Log.Errorf("第一次连接ftp服务器失败: %v", err) if custom == "yunduan" { for i := 0; i < 2; i++ { client, err = ftp.DialTimeout(backupftpServer[0]+":5021", 10*time.Second) if err != nil { Log.Errorf("第二次连接ftp服务器失败: %v", err) client, err = ftp.DialTimeout(backupftpServer[1]+":5021", 10*time.Second) if err != nil { Log.Errorf("第三次连接ftp服务器失败: %v", err) return err } } } } } err = client.Login(Config.FtpUser, Config.FtpPass) if err != nil { Log.Errorf("登录ftp服务器失败: %v", err) return err } file, err := os.Open(localLogFile) if err != nil { Log.Errorf("打开本地文件[%v]失败: %v", localLogFile, err) return err } defer file.Close() dirList := strings.Split(basePath, "/") err = client.ChangeDir("/") if err != nil { Log.Errorf("ftp服务器上切换至目录[/]失败: %v", err) return err } for i := 1; i < len(dirList); i++ { err := client.MakeDir(dirList[i]) if err != nil { if strings.Contains(err.Error(), "550") { } else { Log.Errorf("ftp服务器中创建目录[%v]失败:%v", dirList[i], err) return err } } err = client.ChangeDir(dirList[i]) if err != nil { if strings.Contains(err.Error(), "550") { } else { Log.Errorf("ftp服务器上切换至目录[%v]失败: %v", dirList[i], err) return err } } } err = client.Stor(ftpLogFile, file) if err != nil { Log.Errorf("上传文件[%v]到ftp服务器失败: %v", localLogFile, err) return err } if custom == "yunduan" { err = client.Rename(ftpLogFile, strings.Replace(ftpLogFile, ".tmp", "", 1)) if err != nil { Log.Errorf("重命名ftp服务器中的文件 %v 为 %v 失败:%v", ftpLogFile, ftpLogFile+".tmp", err) return err } } if custom == "mangguo" { finishFile, err := os.Open(localFinishFile) if err != nil { return err } defer finishFile.Close() err = client.Stor(ftpFinishFile, finishFile) if err != nil { Log.Errorf("上传文件[%v]到ftp服务器失败: %v", localFinishFile, err) return err } } return nil }
func (f *FTPScanner) Scan(scanurl, identifier string, conn redis.Conn, stop chan bool) error { if !strings.HasPrefix(scanurl, "ftp://") { return fmt.Errorf("%s does not start with ftp://", scanurl) } ftpurl, err := url.Parse(scanurl) if err != nil { return err } host := ftpurl.Host if !strings.Contains(host, ":") { host += ":21" } if utils.IsStopped(stop) { return ScanAborted } c, err := ftp.DialTimeout(host, 5*time.Second) if err != nil { return err } defer c.Quit() username, password := "******", "anonymous" if ftpurl.User != nil { username = ftpurl.User.Username() pass, hasPassword := ftpurl.User.Password() if hasPassword { password = pass } } err = c.Login(username, password) if err != nil { return err } log.Infof("[%s] Requesting file list via ftp...", identifier) var files []*filedata = make([]*filedata, 0, 1000) err = c.ChangeDir(ftpurl.Path) if err != nil { return fmt.Errorf("ftp error %s", err.Error()) } prefixDir, err := c.CurrentDir() if err != nil { return fmt.Errorf("ftp error %s", err.Error()) } if os.Getenv("DEBUG") != "" { _ = prefixDir //fmt.Printf("[%s] Current dir: %s\n", identifier, prefixDir) } prefix := ftpurl.Path // Remove the trailing slash prefix = strings.TrimRight(prefix, "/") files, err = f.walkFtp(c, files, prefix+"/", stop) if err != nil { return fmt.Errorf("ftp error %s", err.Error()) } count := 0 for _, fd := range files { fd.path = strings.TrimPrefix(fd.path, prefix) if os.Getenv("DEBUG") != "" { fmt.Printf("%s\n", fd.path) } f.scan.ScannerAddFile(*fd) count++ } return nil }