Esempio n. 1
0
func installDroneDrop() {
	c, err := ftp.Connect("192.168.1.1:21")
	defer c.Quit()

	if err != nil {
		log.Fatal(err)
	}

	log.Println("Pushing dronedrop...")
	for _, asset := range []string{"/ardrone_commander", "/configure_drone_drop.sh", "/uninstall_drone_drop.sh"} {
		if b, err := os.Open(droneDropSupportDir() + asset); err != nil {
			log.Fatal(err)
		} else {
			if err := c.Stor(asset, b); err != nil {
				log.Fatal("Error pushing", asset, err)
			}
		}
	}

	for _, asset := range []string{"/rcS.stock", "/rcS.dronedrop", "/usb.ids.stock", "/usb.ids.dronedrop"} {
		if b, err := Asset("support/drone_drop" + asset); err != nil {
			log.Fatal(err)
		} else {
			if err := c.Stor(asset, bytes.NewBuffer(b)); err != nil {
				log.Fatal("Error pushing", asset, err)
			}
		}
	}

	log.Println("Configuring dronedrop...")
	sendCommand("sh /data/video/configure_drone_drop.sh\n")
	log.Println("Rebooting drone...")
}
Esempio n. 2
0
func (u *Uploader) connect(once bool) error {
	for u.conn == nil {
		u.log.Println("Connecting")
		u.setStatus(STATUS_CONNECTING, nil)
		conn, err := ftp.Connect(u.Addr)
		if err == nil {
			u.log.Println("Logging in", u.User)
			err = conn.Login(u.User, u.Pass)
			if err == nil {
				u.log.Println("Login successful")
				u.conn = conn
				u.setStatus(STATUS_CONNECTED, nil)
				break
			} else {
				u.log.Println("Login failed:", err)
			}
		} else {
			u.log.Println("Connection failed:", err)
		}
		u.setStatus(STATUS_ERROR, err)
		if once {
			return err
		}
		select {
		case <-time.After(FTP_RECONNECT_DELAY):
		case <-u.chquit:
			return ErrQuit
		}
	}
	return nil
}
Esempio n. 3
0
// Try to connect as long as the server is not obsolete
// This function does not return errors as high-load FTPs
// do likely need hundreds of connection retries
func (elem *Ftp) ConnectLoop() {
	for !elem.Obsolete {
		conn, err := ftp.Connect(elem.Url)
		if err == nil {
			elem.Conn = conn
			break
		}

		elem.crawler.Log.Print(err)
		time.Sleep(2 * time.Second)
	}
}
Esempio n. 4
0
func (f *FTP) connect() error {
	var err error
	if f.connection, err = ftp.Connect(f.config.Host); err != nil {
		return err
	}

	if err = f.connection.Login(f.config.User, f.config.Pass); err != nil {
		f.Disconnect()
		return err
	}

	return nil
}
Esempio n. 5
0
func (n *ftpFetcher) Fetch(resource string) error {
	n.data = GetCachedFile(resource)
	if n.data != nil {
		return nil
	}

	furl, err := url.Parse(resource)
	if err != nil {
		return err
	}

	if !strings.Contains(furl.Host, ":") {
		furl.Host = furl.Host + ":21"
	}
	ftpCli, err := ftp.Connect(furl.Host)
	if err != nil {
		return err
	}
	defer ftpCli.Quit()

	fusername := "******"
	fpassword := "******"

	if furl.User != nil {
		passwd, haspass := furl.User.Password()
		if haspass {
			fpassword = passwd
		}
		fusername = furl.User.Username()
	}

	err = ftpCli.Login(fusername, fpassword)
	if err != nil {
		return err
	}
	defer ftpCli.Logout()

	resp, err := ftpCli.Retr(furl.Path)
	if err != nil {
		return err
	}

	n.data, err = ioutil.ReadAll(resp)
	resp.Close()

	PutCachedFile(resource, n.data)
	return err
}
Esempio n. 6
0
func cmd_ftp(c *command) bool {
	c.assertLeastNArg(2)

	listName, uri := c.flags.Arg(0), c.flags.Arg(1)
	if v, ok := namedFtp[uri]; ok {
		uri = v
	}
	loc, err := url.Parse(uri)
	if err != nil {
		pef("Could not parse URL '%s': %s", uri, err)
		return false
	}
	if loc.User == nil {
		loc.User = url.UserPassword("anonymous", "anonymous")
	}
	if !strings.Contains(loc.Host, ":") {
		loc.Host += ":21"
	}

	conn, err := ftp.Connect(loc.Host)
	if err != nil {
		pef("Could not connect to '%s': %s", loc.Host, err)
		return false
	}

	pass, _ := loc.User.Password()
	if err := conn.Login(loc.User.Username(), pass); err != nil {
		pef("Authentication failed for '%s': %s", loc.Host, err)
		return false
	}

	namePath := sf("%s/%s.list.gz", loc.Path, listName)
	r, err := conn.Retr(namePath)
	if err != nil {
		pef("Could not retrieve '%s' from '%s': %s", namePath, loc.Host, err)
		return false
	}

	if _, err := io.Copy(os.Stdout, r); err != nil {
		pef("Could not write '%s' to stdout: %s", listName, err)
		return false
	}

	// Don't even bother trying to close the connection.
	return true
}
Esempio n. 7
0
func (fs *FTPStorage) Put(hash string, done chan error) (w io.WriteCloser, e error) {
	returningNormally := false
	//make sure we don't flood the FTP server
	fs.connectionsChan <- 0
	defer func() {
		if !returningNormally {
			<-fs.connectionsChan
		}
	}()

	connection, err := ftp.Connect(fs.server + ":" + strconv.Itoa(fs.port))
	if err != nil {
		return nil, err
	}
	defer func() {
		if !returningNormally {
			connection.Quit()
		}
	}()

	err = connection.Login(fs.username, fs.password)
	if err != nil {
		return nil, err
	}

	err = connection.ChangeDir(fs.directory)
	if err != nil {
		return nil, err
	}

	reader, writer := io.Pipe()

	go func() {
		err := connection.Stor(hash, reader)
		if err != nil {
			reader.CloseWithError(err)
		}
		<-fs.connectionsChan
		connection.Quit()
		done <- err
	}()

	returningNormally = true
	return writer, nil
}
Esempio n. 8
0
func (fs *FTPStorage) Get(hash string) (io.ReadCloser, error) {
	fs.connectionsChan <- 0
	defer func() { <-fs.connectionsChan }()

	connection, err := ftp.Connect(fs.server + ":" + strconv.Itoa(fs.port))
	if err != nil {
		return nil, err
	}
	defer connection.Quit()

	err = connection.Login(fs.username, fs.password)
	if err != nil {
		return nil, err
	}

	err = connection.ChangeDir(fs.directory)
	if err != nil {
		return nil, err
	}

	return connection.Retr(hash)
}
Esempio n. 9
0
func (f *FtpBase) Conn() (err error) {
	conn, err := ftp.Connect(f.ip)
	if err != nil {
		log.Info("ftp.Connect[%s] %s", f.ip, err)
		return err
	}

	err = conn.Login(f.usr, f.pwd)
	if err != nil {
		log.Info("ftp.Login[%s:%s] %s", f.usr, f.pwd, err)
		return err
	}

	f.conn = conn
	f.homePath, err = conn.CurrentDir()
	if err != nil {
		log.Info("conn.CurrentDir(): [%s][%s:%s] %s", f.ip, f.usr, f.pwd, err)
		return err
	}

	return nil
}
Esempio n. 10
0
func (rt *FTPRoundTripper) getConnection(hostport string) (conn *ftp.ServerConn, err error) {
	rt.lock.Lock()
	conns, ok := rt.idleConnections[hostport]
	if ok && len(conns) > 0 {
		conn = conns[0]
		rt.idleConnections[hostport] = conns[1:]
		rt.lock.Unlock()
		return
	}
	rt.lock.Unlock()

	conn, err = ftp.Connect(hostport)
	if err != nil {
		return nil, err
	}

	err = conn.Login("anonymous", "anonymous")
	if err != nil {
		conn.Quit()
		return nil, err
	}

	return conn, nil
}
Esempio n. 11
0
func main() {
	c, err := ftp.Connect("42.96.206.158:21")
	if err != nil {
		fmt.Println(err)
	}

	err = c.Login("samsong", "tco99312^")
	if err != nil {
		fmt.Println(err)
	}

	err = c.ChangeDir("/www")
	if err != nil {
		fmt.Println(err)
	}

	dir, err := c.CurrentDir()
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("当前目录:" + dir)
	}

	list, err := c.List("/www")
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("目录列表:")
	fmt.Println("=====================================================================")
	for _, info := range list {
		fmt.Println(info.Name)
	}
	c.Quit()

}
Esempio n. 12
0
func main() {

	goopt.Description = func() string {
		return "Example program for using the goopt flag library."
	}
	goopt.Version = "1.0"
	goopt.Summary = "checker.exe -h 127.0.0.1 -u user -p 123qwe -d /dir -f file1"
	goopt.Parse(nil)

	fmt.Printf("FTP agrs info server_ip[%v]]\n", *server_ip)
	fmt.Printf("FTP agrs info server_port[%v]\n", *server_port)
	fmt.Printf("FTP agrs info username[%v]\n", *username)
	fmt.Printf("FTP agrs info password[%v]\n", *password)
	fmt.Printf("FTP agrs info dir[%v]\n", *dir)
	fmt.Printf("FTP agrs info file[%s]\n", *file)

	c, err := ftp.Connect("localhost:21")
	if err != nil {
		fmt.Printf("FTP Connect return : err\n")
		return
	}

	fmt.Printf("FTP Connect to server\n")

	err = c.Login(*username, *password)
	if err != nil {
		fmt.Printf("FTP Login return : ", err)
		return
	}

	fmt.Printf("FTP Login PASS!\n")

	err = c.ChangeDir(*dir)
	if err != nil {
		fmt.Printf("FTP ChangeDir return : ", err)
		return
	}

	fmt.Printf("FTP ChangeDir PASS!\n")

	entries := []*ftp.Entry{}

	entries, err = c.List(".")
	if err != nil {
		fmt.Printf("FTP List return : ", err)
		return
	}

	fmt.Printf("FTP List PASS!\n")

	for i, _ := range entries {

		fmt.Printf("    List Name[%s]\n", entries[i].Name)
		fmt.Printf("    List Type[%v]\n", entries[i].Type)
		fmt.Printf("    List Size[%d]\n", entries[i].Size)
		fmt.Printf("    List Time[%v]\n", entries[i].Time)

		if *file == entries[i].Name {
			fmt.Printf("OK , find the file[%s] in the FTP Server!\n", *file)

			c.Quit()
			fmt.Printf("FTP Quit!\n")
			return
		}
	}

	fmt.Printf("Sorry , find the file[%s] in the FTP Server!\n", *file)
	c.Quit()
	fmt.Printf("FTP Quit!\n")
	return
}
Esempio n. 13
0
// Scan an FTP repository and index its files
func (s *scan) ScanFTP(ftpURL, identifier string, stop chan bool) (err error) {
	if !strings.HasPrefix(ftpURL, "ftp://") {
		log.Error("%s does not start with ftp://", ftpURL)
		return
	}

	ftpurl, err := url.Parse(ftpURL)
	if err != nil {
		return err
	}

	host := ftpurl.Host
	if !strings.Contains(host, ":") {
		host += ":21"
	}

	if isStopped(stop) {
		return scanAborted
	}

	c, err := ftp.Connect(host)
	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.Info("[%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("[%s] ftp error %s", identifier, err.Error())
	}

	prefixDir, err := c.CurrentDir()
	if err != nil {
		return fmt.Errorf("[%s] ftp error %s", identifier, 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 = s.walkFtp(c, files, prefix+"/", stop)
	if err != nil {
		return fmt.Errorf("[%s] ftp error %s", identifier, err.Error())
	}

	// Connect to the database
	s.redis = NewRedis()
	conn, err := s.redis.connect()
	if err != nil {
		log.Error("[%s] Redis: %s", identifier, err)
		return err
	}
	defer conn.Close()

	conn.Send("MULTI")

	filesKey := fmt.Sprintf("MIRROR_%s_FILES", identifier)
	filesTmpKey := fmt.Sprintf("MIRROR_%s_FILES_TMP", identifier)

	// Remove any left over
	conn.Send("DEL", filesTmpKey)

	count := 0
	for _, f := range files {
		f.path = strings.TrimPrefix(f.path, prefix)

		if os.Getenv("DEBUG") != "" {
			fmt.Printf("%s\n", f.path)
		}

		// Add all the files to a temporary key
		conn.Send("SADD", filesTmpKey, f.path)

		// Mark the file as being supported by this mirror
		rk := fmt.Sprintf("FILEMIRRORS_%s", f.path)
		conn.Send("SADD", rk, identifier)

		// Save the size of the current file found on this mirror
		ik := fmt.Sprintf("FILEINFO_%s_%s", identifier, f.path)
		conn.Send("HSET", ik, "size", f.size)

		// Publish update
		conn.Send("PUBLISH", MIRROR_FILE_UPDATE, fmt.Sprintf("%s %s", identifier, f.path))

		count++
	}

	_, err = conn.Do("EXEC")
	if err != nil {
		return err
	}

	// Get the list of files no more present on this mirror
	toremove, err := redis.Values(conn.Do("SDIFF", filesKey, filesTmpKey))
	if err != nil {
		return err
	}

	// Remove this mirror from the given file SET
	if len(toremove) > 0 {
		conn.Send("MULTI")
		for _, e := range toremove {
			log.Info("[%s] Removing %s from mirror", identifier, e)
			conn.Send("SREM", fmt.Sprintf("FILEMIRRORS_%s", e), identifier)
			conn.Send("DEL", fmt.Sprintf("FILEINFO_%s_%s", identifier, e))
			conn.Send("PUBLISH", MIRROR_FILE_UPDATE, fmt.Sprintf("%s %s", identifier, e))
		}
		_, err = conn.Do("EXEC")
		if err != nil {
			return err
		}
	}

	// Finally rename the temporary sets containing the list
	// of files for this mirror to the production key
	if count > 0 {
		_, err = conn.Do("RENAME", filesTmpKey, filesKey)
		if err != nil {
			return err
		}
	} else {
		_, _ = conn.Do("DEL", filesKey)
	}

	sinterKey := fmt.Sprintf("HANDLEDFILES_%s", identifier)

	// Count the number of files known on the remote end
	common, _ := redis.Int64(conn.Do("SINTERSTORE", sinterKey, "FILES", filesKey))

	s.setLastSync(conn, identifier)
	log.Info("[%s] Indexed %d files (%d known)", identifier, count, common)

	return nil
}