Expose localhost to Internet using btunnel

expose localhost to internet

Exposing your app running in any port of your localhost to the internet is very easy using btunnel. This tool gives you power to not only expose an http app, but helps you to share your local filesystem as well as any TCP app to the internet accessible from anywhere in the world. Another primary advantage of btunnel is it's ease of use - exposing localhost to internet with a single command is really a game changer for developers. Yet, with btunnel, you have some really adavanced features like

  • geo-location based tunnel endpoint
  • basic authentication to secure your app
  • multiple tunnels exposure through config file

to name a few which also makes it suitable for large scale development and production workload.

Why do you need to expose localhost to internet?

There are many instances where you would want to expose localhost to the internet :

  1. Development :

    • Quickly protyping your idea
    • Test it across different devices
    • Testing from different geographical locations
  2. Collaboration :

    • Sharing your development with your collaborator from different locations
    • Pitching a proof of concept to collaborators and investors
    • Having a POC app hosted for some open-source projects in public repository
  3. Production :

    • Don't go for the hassle for all the cloud jargon when you don't need any
    • Secure your database only inside enclosed private network
    • Much cheaper to reuse your own hardware than to pay hefty fee to the cloud providers
  4. Webhooks :

    • Developing and testing and webhook is a tedius job
    • Many payment gateways relies upon webhook backend to reflect the payment information, i.e. Stripe
    • During development, your webhook server should be exposed to internet and used in the service. This greatly reduce dev, debugging and validation effort.

In many cases, small businesses have to pay high fees for cloud framework when reusing an existing hardware lying around is much more optimal. Similarly, you can create your own robust compute architecture and go crazy with the microservice configurations without even bothering about the compatibility and configuraton of different cloud providers. Modern day mini-computers (such as rasperry-pi) are also perfectly capable to run an app and can handle fairly large request frequency (5000 per minute based upon app complexity).

Install btunnel

Prerequisites

Before starting with the process of exposing localhost to internet, first you need to register and login at btunnel website to get an API_KEY which is required for authentication.

Install btunnel

Go to btunnel website and download the cli-tool btunnel for your OS flavor. Then extract the .tar.gz or .zip file to get the executable binary. Go to the directory through terminal (*Nix) or cmd (Windows) using cd command where the btunnel binary is extracted.

To get an overview of the subcommands, run the following

For *Nix (Mac and Linux)

$ ./btunnel --help

For Windows

C:\Users\user\Desktop\btunnel-dir> btunnel --help

How to expose localhost to internet

To expose an http app running in your localhost at port PORT, use the "http" command as shown below. The default port picked up by btunnel is 8000. This gives you a randomly generated url which is globally accessible through the internet.

To get a list of option, run the following command -

btunnel http --help

Running Example App

btunnel http --port 8000 --key <API_KEY>
  Name: -> John Doe
  Email: -> john.doe@example.com
  TimeOut: -> Never
  Web Monitoring: -> http://localhost:7140

    http   https://abcd.btunnel.co.in -> http://localhost:8000 Fri Feb  9 14:57:02 2024                                             
                                                                                                                                    
    http   2024/02/09 - 20:27:11 | 200 |    2.027519ms |   49.37.161.239 | GET      "/movies"                                       
    http   2024/02/09 - 20:27:11 | 200 |    1.625393ms |   49.37.161.239 | GET      "/latest"                                       
    http   2024/02/09 - 20:27:13 | 200 |    1.237355ms |   49.37.161.239 | GET      "/playerinfo?q=63cfcca5dbe96360f40b2f95"        
    http   2024/02/09 - 20:27:14 | 200 |   61.965661ms |   49.37.161.239 | GET      "/subs?id=63cfcca5dbe96360f40b2f95&episode=0"   
    http   2024/02/09 - 20:27:15 | 206 |  7.155068815s |   49.37.161.239 | GET      "/video?id=652b95e1c7cfaf093da9ad57&episode=0"  
                                                                                                                                    
                                                                                                                                    
  Press q or Esc to exit 

Sharing exposed localhost

After exposing your app, a temporary url is shown in your terminal. In the above example, the url https://abcd.btunnel.co.in, which will be something else in your case. Now your app is live and exposed to the internet for public access through this url. You can share this address to anyone to showcase your app. This means, you don't need to setup any public cloud and directly handle all the traffic from your computer only.

How to have fixed url

Btunnel lets you to register a subdomain of your own choice and then use that one to create the url that will expose your localhost. This helps you to avoid generating random urls each time. You can use this fixed url to globally expose your app or use a CNAME entry in your DNS server to map this url to your own registered domain. This way, you can serve an app running in your localhost from your domain as a real production app under your domain name.

Failsafe

Btunnel client auto reconnects if a tunnel gets disconnected due to some network issue. Btunnel keeps retrying for three times in row to establish a connection before decaling it as a failure. This gives you a fairly strong failsafe against accidental connection drop.

Let's get started to expose some of your own app to the internet - Register now

SEE ALSO