Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: support only communication encrypted #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions ftp.go
Expand Up @@ -17,14 +17,24 @@ import (
// RePwdPath is the default expression for matching files in the current working directory
var RePwdPath = regexp.MustCompile(`\"(.*)\"`)

// StatusError is a trivial implementation of error
type StatusError struct {
s string
}

func (e StatusError) Error() string {
return e.s
}

// FTP is a session for File Transfer Protocol
type FTP struct {
conn net.Conn

addr string

debug bool
tlsconfig *tls.Config
debug bool
tlsconfig *tls.Config
dataEncryption bool

reader *bufio.Reader
writer *bufio.Writer
Expand Down Expand Up @@ -237,9 +247,12 @@ func (ftp *FTP) AuthTLS(config *tls.Config) error {
}

if _, err := ftp.cmd(StatusOK, "PROT P"); err != nil {
if _, ok := err.(StatusError); ok {
return nil
}
return err
}

ftp.dataEncryption = true
return nil
}

Expand Down Expand Up @@ -371,7 +384,7 @@ func (ftp *FTP) newConnection(port int) (conn net.Conn, err error) {
return
}

if ftp.tlsconfig != nil {
if ftp.dataEncryption && ftp.tlsconfig != nil {
conn = tls.Client(conn, ftp.tlsconfig)
}

Expand Down