ftp: Add FTP over TLS support

This commit is contained in:
Gary Kim 2019-05-01 00:16:08 +08:00 committed by Nick Craig-Wood
parent 66b3795eb8
commit 2890b69c48

View File

@ -2,6 +2,7 @@
package ftp package ftp
import ( import (
"crypto/tls"
"io" "io"
"net/textproto" "net/textproto"
"os" "os"
@ -46,6 +47,10 @@ func init() {
Help: "FTP password", Help: "FTP password",
IsPassword: true, IsPassword: true,
Required: true, Required: true,
}, {
Name: "tls",
Help: "Use FTP over TLS, leave blank for (false)",
Default: false,
}, { }, {
Name: "concurrency", Name: "concurrency",
Help: "Maximum number of FTP simultaneous connections, 0 for unlimited", Help: "Maximum number of FTP simultaneous connections, 0 for unlimited",
@ -62,6 +67,7 @@ type Options struct {
User string `config:"user"` User string `config:"user"`
Pass string `config:"pass"` Pass string `config:"pass"`
Port string `config:"port"` Port string `config:"port"`
TLS bool `config:"tls"`
Concurrency int `config:"concurrency"` Concurrency int `config:"concurrency"`
} }
@ -120,7 +126,14 @@ func (f *Fs) Features() *fs.Features {
// Open a new connection to the FTP server. // Open a new connection to the FTP server.
func (f *Fs) ftpConnection() (*ftp.ServerConn, error) { func (f *Fs) ftpConnection() (*ftp.ServerConn, error) {
fs.Debugf(f, "Connecting to FTP server") fs.Debugf(f, "Connecting to FTP server")
c, err := ftp.DialTimeout(f.dialAddr, fs.Config.ConnectTimeout) ftpConfig := []ftp.DialOption{ftp.DialWithTimeout(fs.Config.ConnectTimeout)}
if f.opt.TLS {
tlsConfig := &tls.Config{
ServerName: f.opt.Host,
}
ftpConfig = append(ftpConfig, ftp.DialWithTLS(tlsConfig))
}
c, err := ftp.Dial(f.dialAddr, ftpConfig...)
if err != nil { if err != nil {
fs.Errorf(f, "Error while Dialing %s: %s", f.dialAddr, err) fs.Errorf(f, "Error while Dialing %s: %s", f.dialAddr, err)
return nil, errors.Wrap(err, "ftpConnection Dial") return nil, errors.Wrap(err, "ftpConnection Dial")
@ -203,7 +216,11 @@ func NewFs(name, root string, m configmap.Mapper) (ff fs.Fs, err error) {
} }
dialAddr := opt.Host + ":" + port dialAddr := opt.Host + ":" + port
u := "ftp://" + path.Join(dialAddr+"/", root) protocol := "ftp://"
if opt.TLS {
protocol = "ftps://"
}
u := protocol + path.Join(dialAddr+"/", root)
f := &Fs{ f := &Fs{
name: name, name: name,
root: root, root: root,