3

I am using https://www.npmjs.com/package/basic-ftp basic ftp package to connect to ftps server. I have tried my other extension but failed to connect to ftps server below is my code

const ftp = require("basic-ftp")

example();

async function example() {
    const client = new ftp.Client()
    client.ftp.verbose = true
    try {
        await client.access({
            host: "ftp.xxxx.xxxxx",
            user: "xxxx@xxxx.xx",
            password: "xxxxxxx",
            secure :true
        })
        await client.ensureDir("/my/remote/directory")
        console.log(await client.list())
        await client.uploadFrom("temp/readme.txt", "readme.txt")
       // await client.downloadTo("README_COPY.md", "README_FTP.md")
    }
    catch(err) {
        console.log(err)
    }
    client.close()
}

but giving me a error

Connected to xxx.xxx.xx.xxx:21
< 220 Service ready for new user.
Login security: No encryption
> USER xx@xxx.xx
< 331 User name okay, need password for xxxx@xxx.xx.
> PASS ###
< 530 Box: Smartest Energy does not allow regular FTP; use FTPS instead. (Both "
explicit" and "implicit" FTPS are supported.)
{ FTPError: 530 Box: Smartest Energy does not allow regular FTP; use FTPS instea
d. (Both "explicit" and "implicit" FTPS are supported.)
    at FTPContext._onControlSocketData (D:\node\basicftp\node_modules\basic-ftp\
dist\FtpContext.js:276:39)
    at Socket.socket.on.data (D:\node\basicftp\node_modules\basic-ftp\dist\FtpCo
ntext.js:121:44)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:265:13)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) name
: 'FTPError', code: 530 }

Please help Thanks in advance

Somnath Rokade
  • 655
  • 1
  • 9
  • 27

2 Answers2

5

You will require to connect Explicit FTPS over TLS. to connect to ftps over tls you will need to pass the following options:

const fs = require('fs');

async function example() {
  const client = new ftp.Client()
  client.ftp.verbose = true
  try {
    const secureOptions = {
      // Necessary only if the server requires client certificate authentication.
      key: fs.readFileSync('client-key.pem'),
      cert: fs.readFileSync('client-cert.pem'),

      // Necessary only if the server uses a self-signed certificate.
      ca: [fs.readFileSync('server-cert.pem')],

      // Necessary only if the server's cert isn't for "localhost".
      checkServerIdentity: () => { return null; },
    };
    await client.access({
      host: "ftp.xxxx.xxxxx",
      user: "xxxx@xxxx.xx",
      password: "xxxxxxx",
      secure: true,
      secureOptions: secureOptions
    })
    await client.ensureDir("/my/remote/directory")
    console.log(await client.list())
    await client.uploadFrom("temp/readme.txt", "readme.txt")
    // await client.downloadTo("README_COPY.md", "README_FTP.md")
  }
  catch (err) {
    console.log(err)
  }
  client.close()
}
Rashomon
  • 5,962
  • 4
  • 29
  • 67
shubham
  • 414
  • 2
  • 3
  • you specified the certificates as required ?and added the secureOptions flag in the options too right? – shubham Dec 13 '19 at 05:53
  • are your sure that you specified the correct paths of the certificates to be used? – shubham Dec 13 '19 at 05:53
  • Yes. added the secureOptions flag in the options too. When I connect using filezilla then certificate doesn't require so that I haven't provided. just kept the checkServerIdentity: () => { return null; }, option in const secureOptions – Somnath Rokade Dec 13 '19 at 05:59
  • i suggest trying the following library then : https://www.npmjs.com/package/ftps – shubham Dec 13 '19 at 06:02
  • How Can I generate these files 1)server-cert.pem 2) client-cert.pem 3)client-key.pem – Somnath Rokade Dec 13 '19 at 09:14
  • I haved configured npmjs.com/package/ftps but its having certificate issue as well. Please suggest me how to get the local certificate – Somnath Rokade Dec 13 '19 at 11:46
  • can you comment the line client.close() and debug the basic-ftp implementation? ideally it should have worked without any certificates. – shubham Dec 13 '19 at 12:08
  • Thanks for your comment I tried with your suggestion but still error occurred. Now there is no error but nothing happens Showing below as the output D:\node\basicftp>node index.js Connected to xxx.xxx.xx.xxx:21 < 220 Service ready for new user. > AUTH TLS < 234 Command AUTH okay; starting TLS connection. – Somnath Rokade Dec 13 '19 at 13:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204204/discussion-between-somnath-rokade-and-shubham). – Somnath Rokade Dec 13 '19 at 13:10
  • Concise code with comments. Nice. – Terrance00 Jul 25 '23 at 21:59
0

After trying to get this working with basic-ftp, i just tried https://www.npmjs.com/package/ssh2-sftp-clientand it worked immediately.

user1961312
  • 89
  • 1
  • 9