1

I'm currently using github.com/jlaffaye/ftp on Go to send files.

I'm trying to connect to an FTP and upload a zipped file of about 700MB.

I connect to server properly and change the working dir but when I'm about to call the Stor function, it responds with "connection refused" and drops.

This is the code:

    ftpFile="hola.txt"
    fileOpen, err := os.Open(ftpFile) // For read access.
    if err != nil {
        fmt.Println("error "+ftpFile)
        panic(err)
    } else {
        fmt.Println("abierto "+ftpFile)
    }

    reader:=bufio.NewReader(fileOpen)

    err = client.Stor(ftpDir+ftpFile,reader)
    if err != nil {
        fmt.Println("error en stor "+ftpDir+ftpFile)
        panic(err)
    } else {
        fmt.Println("subiendo "+ftpFile)
    }
    defer fileOpen.Close()

I'm opening a file with os.Open then read it with bufio.NewReader and pass it to the Stor function but it disconnects. The FTP is good as I was connected to it via FileZilla, the zipfile or txt file (in this code example) are both good and I believe I'm missing the shot when it comes to the bufio.NewReader but I can't find a working example of reading a file and using this goftp library.

Update: Here's the logfile

220 Microsoft FTP Service
USER *user goes here*
331 Password required
PASS *plain text password goes here*
230 User logged in.
FEAT
211-Extended features supported:
 LANG EN*
 UTF8
 AUTH TLS;TLS-C;SSL;TLS-P;
 PBSZ
 PROT C;P;
 CCC
 HOST
 SIZE
 MDTM
 REST STREAM
211 END
TYPE I
200 Type set to I.
OPTS UTF8 ON
200 OPTS UTF8 command successful - UTF8 encoding now ON.
CWD /web-import/pre/
250 CWD command successful.
PWD
257 "/web-import/pre" is current directory.
PASV
227 Entering Passive Mode (x,x,x,x,x,x).

Update: Looks like someone had the same problem some weeks ago: goftp - 229 Entering Extended Passive Mode now I'll be looking for solutions and will post here.

Update: Changed the script to connect to a CentOS server and got the same error. Now that the FTP server is discarded then there are 2 culprits: goftp package and the script itself.

Update: Tried again with a simpler script following example at documentation and failed again at the same spot. Had to report issue on developer's Github https://github.com/jlaffaye/ftp/issues/272

Update: I connected via terminal and opened FTP from command line. I typed "ls" by mistake and got this error

227 Entering Passive Mode (x,x,x,x,x,x).
ftp: connect: Connection refused

Last Update: After coding and debugging in the end it wasn't the goftp package neither the server neither the client. It was my firewall that blocked my ftp from going PASV. I whitelisted the ip and it worked perfectly.

  • Connection refused means that the data connection could not be established. This is for example the case when there is some firewall in between which would need traffic inspection to detect and allow the ports for the data connection - but does not get these since the FTP connection is encrypted. Unfortunately it is unknown if this is the case and it is unknown how Filezilla worked (for example without encryption). – Steffen Ullrich Sep 06 '22 at 20:30
  • Hello @steffenullrich The debug logfile shows it connects successfully. It logs in, change directories, and drops when I try to upload the file. I connected with plain FTP using FileZilla and had no problems. – Rush Alvarado Sep 06 '22 at 21:57
  • Check that FileZilla is connecting using [Passive mode](https://wiki.filezilla-project.org/Network_Configuration) (I believe it defaults to trying Passive and, if that fails, switches to active). You may need to [enable logging](https://wiki.filezilla-project.org/Logs) to check. `jlaffaye/ftp` [only supports passive](https://github.com/jlaffaye/ftp/issues/127). – Brits Sep 06 '22 at 22:37
  • @brits changed FileZilla to passive only and it accepted the file. I still believe this is something more related to Go than to FTP but I can't figure out what's wrong. – Rush Alvarado Sep 06 '22 at 23:45
  • The log you included shows `PASV` rather than `EPSV`? [This](https://go.dev/play/p/V-zZM2EGjE_t) worked fine for me (testing locally with [ftpdmin](https://www.sentex.ca/~mwandel/ftpdmin/) this does not support `EPSV` but I can see an attempt made to use that which is not in your logs....). – Brits Sep 07 '22 at 00:28
  • @Brits I changed the script again and tried to connect to a CentOS server I have and I got the same error. – Rush Alvarado Sep 07 '22 at 00:49
  • @RushAlvarado: *"The debug logfile shows it connects successfully."* - that's only for the control connection. FTP creates a new data connection for each data transfer (file transfer, directory listing, ... - but not things like directory change) using a dynamically allocated port - this is the target given in the response to PASV. And this is the connection which fails both with goftp as with the command line FTP client you've used. If you say that FileZilla successfully handles listing and transfer then it would be interesting to get the logile from FileZilla. – Steffen Ullrich Sep 07 '22 at 04:35
  • @SteffenUllrich and Brits. thank you both for your replies. In the end it wasn't the script or the server. It was the firewall that was preventing me from transferring files on PASV. – Rush Alvarado Sep 07 '22 at 05:25
  • @RushAlvarado just out of interest if the problem was the firewall then how did Filezilla work? (do you have an application specific firewall blocking outgoing traffic?). – Brits Sep 07 '22 at 05:59
  • @Brits I used FileZilla from my PC and I was trying to upload files from a server to another server. The problem was on the origin server that blocked outgoing transfers of FTP on PASV. The IP of the destination server was whitelisted and that made the trick. Also, thank you for your comments. – Rush Alvarado Sep 07 '22 at 12:01

0 Answers0