DEV Community

Cover image for Create your own tunnelling like ngrok does

Posted on

Create your own tunnelling like ngrok does

If you're here is because you don't want to use ngrok or you are looking for an alternative to it, which is pretty valid because if somebody did that, why you don't?

Well, this story begins with me trying to make my local development environment public and here is where ngrok came to the rescue, this tool works really well, but with some limitations, like having a random subdomain every time you restart your application or using more than one connection, are the things you will need to deal with unless you don't pay for it.

So basically everything works behind ssh tunnelling connections, and if you want to create your own ngrok clone, mainly you will need a VPS server running Linux preferentially.

Now try to connect your local computer to the VPS server through SSH ( in this case).

$ ssh root@
Enter fullscreen mode Exit fullscreen mode

There are some different ways to get this done, every connection string depends on the service provider you're using, could need to type your password or could be using a .pem key but in the end, will have the same result.

With your VPS session opened, just type the next command in your terminal, if you have issues with this command try to open the sshd_config file and write at the very end "GatewayPorts yes".

$ echo "GatewayPorts yes" >> /etc/ssh/sshd_config
By default, OpenSSH only allows connecting to remote forwarded ports from the server host. However, the GatewayPorts option in the server configuration file sshd_config can be used to control this.

Restart the SSH service and exit from your session

$ service ssh restart
$ exit
Enter fullscreen mode Exit fullscreen mode

Now you should be able to run a command similar to this one, and guess what!!

$ ssh -R 8080:localhost:3000 root@
Enter fullscreen mode Exit fullscreen mode

Voilá, this will open the port 8080 on the VPS server and will forward it to the port 3000 on our local machine, basically means you can type on your browser and effectively will redirect the traffic to your local computer.

I know you want more, say.. subdomains and https connections? yeah, why not, let's finish this.

The next part of this mini how-to is creating a subdomain and link it with your public VPS IP, but I'm will leave this to you, again .. this depends on what service provider you're using.

At this point, you have your VPS server running and ready to forward from source to destination ports, let's assume you have your subdomain created as well, now the next step will be setting up your web server with a reverse proxy tweak. In my case I decided to use Nginx, it's my favourite and it's really simple to set up, actually, my current configuration is similar to this:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;
    location / {
Enter fullscreen mode Exit fullscreen mode

And again, works! my VPS server receives traffic from my subdomain and redirects to which is the same server but through SSH tunnelling.

If we test again typing in the browser address bar should work and redirect again all petitions to your local computer.

This still needs to work with SSL and to complete this step I recommend you to install certbot and follow the instructions, it's pretty straight forward so I don't have a doubt you will get it.

This is my first article as a non-native English-speaker, so be kind and patient.

Happy Coding! :D

Top comments (2)

lecabel profile image

Hi, I am trying to have the same as you did but I don't have a VPS server. I have to forward to a Sinatra application from Amazon Alexa services. I used ngrok and it works fine. I would like now to create the tunneling myself. I have a duckdns domain and Cerbot installation both working ok but I am not able to redirect external request from Alexa voice service to port 4567 (Sinatra). Could you please help ? thanks

advaitt17 profile image

What when we terminate the SSH window?