1
votes

Is possible to create a tunnel with ngrok for an ftp server created using a node cli script that runs from localhost?

UPDATE
I'm using this code but not able to start the server and connect

#!/usr/bin/env node

/**
 * 
 */
const path = require('path');
const ngrok = require('ngrok');
const FtpServer = require('ftp-srv');

const www = path.format({dir: __dirname, base: '/shared'})

const ftpServer = new FtpServer({
    url: 'ftp://127.0.0.1:21',
    anonymous: true,
    greeting: 'Hello user!'
});

ftpServer.on('login', (data, resolve, reject) => {
    console.log(data);
    resolve({root: www});
});

ftpServer.listen().then( () => {
    console.log('Server is running');
    ngrok.connect({proto: 'tcp', addr: 21});
});

I get this error

{"name":"ftp-srv","hostname":"host.local","pid":38965,"level":40,"msg":"Passive URL not set. Passive connections not available.","time":"2021-03-07T21:34:50.077Z","v":0}
{"name":"ftp-srv","hostname":"host.local","pid":38965,"level":50,"err":{"message":"listen EACCES: permission denied 127.0.0.1:21","name":"Error","stack":"Error: listen EACCES: permission denied 127.0.0.1:21\n    at Server.setupListenHandle [as _listen2] (node:net:1278:21)\n    at listenInCluster (node:net:1343:12)\n    at doListen (node:net:1480:7)\n    at processTicksAndRejections (node:internal/process/task_queues:81:21)","code":"EACCES"},"msg":"[Event] error","time":"2021-03-07T21:34:50.105Z","v":0}
Unhandled rejection Error: listen EACCES: permission denied 127.0.0.1:21
    at Server.setupListenHandle [as _listen2] (node:net:1278:21)
    at listenInCluster (node:net:1343:12)
    at doListen (node:net:1480:7)
    at processTicksAndRejections (node:internal/process/task_queues:81:21)

I need to start a node http server first? how I will connect to the tunnel that will have this address tcp://2.tcp.ngrok.io:11653 ?

1
Yes ngrok creates tunnels and supports different port forwarding. A little bit more detail is needed actually answer your question though. What are you trying to do? What have your tried? What is your final preferred result? - Brettski
I will update the question with the code. Basically I want to create a cli script that when run will create an ftp server that will be accessible from the net to the peoples who have the link using an ngrok tunnel to upload/download files. - newbiedev
The message, Error: listen EACCES: permission denied 127.0.0.1:21 looks to me that your operating system is not allowing the server to listen on port 21. This is not uncommon. You may need to move it to a different port, like 21021 or whatever - Brettski
You are on the correct track here. You start up the FTP server, then connect ngrok to it pointing at the local port you need ngrok to route traffic to. It doesn't need to be TCP21. If you are on the free plan of ngrok it will return a different address to connect to publicly each time you setup a tunnel. - Brettski

1 Answers

1
votes

Based on the error you provided it appears your operating system is not allowing the application to listen on TCP port 21. This is not uncommon (security reasons). My suggestion is to use a different port, e.g. 21021 for the FTP server to listed to. Once the server starts then have ngrok forward traffic to that port through the tunnel.

If you are on the free plan of ngrok, each time you establish a tunnel the address to that tunnel will change. So the first time it may be, tcp://2.tcp.ngrok.io:11653, the next time it will change, for example to, tcp://2.tcp.ngrok.io:13859.