SSH Tunneling — Exposing local applications to the internet!!!
Suppose you’re working for a client or implementing an OAuth. You need your application to be available publicly on the internet. And, if you’re working with OAuth, it requires you to register your redirect callback URL with TLS support. In both these scenarios, if you don’t have any server set up already, you need a way around. There can be other scenarios. In all those scenarios, you somewhat require to push your local codebase to the server. And if you don’t have a server right now, you can use a few open-source or freemium services to achieve the goal. I’m going to list a few services below. There are a lot. And, you can even implement the same thing of your own if you have a VPS set up. Moreover, none of them are bound to any specific programming language. Thus if you’re a PHP or Python developer or whatever language you code, you can use any of the following services to host your localhost application to go online.
How does it work?
It’ll be a long story but long story short. In the following services, they use ssh tunneling behind the scene. Which is a method for creating an encrypted ssh connection between the server and the client through ports. Read more about ssh tunneling.
localtunnel
To use this service, you’ll need to have Node.js installed on your host machine. Firstly, you’ll need to install a package globally using the following command.
npm install -g localtunnel
Make sure that the Node.js binaries are available from your terminal. Next, you need to run the following command to make your local application publicly available.
lt --port PORT# PORT is the specific port your application is running.
# if your application runs on port 8090
# then it'd be lt --port 8090
If the command ran successfully, then it’ll output an URL in the terminal. And if you hit that URL, you’ll get to see your application from the internet. And you can share that URL too.
If you want to specify the subdomain, you can do that as well. It’ll be like
lt --port 8090 --subdomain cool-funky-name
If the subdomain is available, then you’ll get an URL with the specified subdomain name. Check the documentation to know more.
expose
Expose does a similar thing as before. But it’s built with PHP. So, you’ll need to have PHP runtime in your host machine to use expose. To install expose in your host machine, run the following command.
composer global require beyondcode/expose
This will install expose globally, on your host machine. And, also make sure that your php binaries are available from your terminal. If not available, find where the composer installs the global packages. By default, it installs in your home's composer directory [~/.composer
]. So, $HOME/.composer/vendor/bin
should be added to your PATH
variable.
Next, to use this service, you’ll need to register yourself with beyondco.de. Then from the dashboard, you’ll get your auth token
.
Next, you’ll need to set up your auth token.
expose token YOUR-AUTH-TOKEN-HERE
After successfully setting up your token, you can run the following command to expose your local site to the internet.
expose share localhost:PORT
PORT is the port number your application is running on. If your application is running on port 8090, then it’ll be like the following.
expose share localhost:8090
If the command ran without any issue, it’ll then show you an URL in the terminal. And your application is available from the internet with that URL.
You can also ask for a subdomain with this command. If available then it will you generate the URL with that subdomain rather than a random string.
expose share localhost:PORT --subdomain=cool-funky-name
Check their documentation for more. You can also share with a limited number of people with basic auth.
StaqLab-tunnel
Unlike localtunnel or expose, staqlab tunnel doesn’t require any runtime to use their service. You’ll need to download a binary from their website and start using their service. Head to their website and download the required binary. You can move the binary to a place where it’s available from everywhere in your terminal. Otherwise, if it’s in your current directory, then you’ll have to use it as ./staqlab-tunnel
command.
To tunnel your local application to the internet. You need to run the following
./staqlab-tunnel PORT# PORT is the port number your application is listening to
# if the application is listening to 8090, then the command will be
# ./staqlab-tunnel 8090
Upon success, an URL will be shown as output to your terminal. And your application is exposed to that URL.
You can also specify your desired subdomain name. To do so run,
./staqlab-tunnel PORT hostname=cool-funky-name
Your application will be hosted on that subdomain rather than a random subdomain.
ngrok
ngrok (pronounced as en-grok) requires authentication through their website. After registering yourself on their website, from the dashboard you’ll get an auth token. And you’ll need to download a binary from their website. After downloading the binary, you can use the service like below.
# First set the auth token
./ngrok authtoken YOUR-AUTH-TOKEN
# Replace YOUR-AUTH-TOKEN with your real token./ngrok http PORT# PORT is the port number that your application listens to.
# If it's on 8090, it should be then like,# ./ngrok http 8090
If the command doesn’t show any error, then it’ll show you an URL for your publicly available website. ngrok doesn’t provide a subdomain for free-tier service. You’ll have to spend some money if you need subdomains. Check their documentation for more.
localhost.run
localhost.run doesn’t require any binary or any runtime dependency. It solely depends on the ssh command. To use their service, all you need to do is run the following command.
ssh -R 80:localhost:PORT ssh.localhost.run# The 80 at the beginning should remain untouched.
# PORT is the port number your application is listening to
# if your application listens to 8090
# then the command becomes like
# ssh -R 80:localhost:8090 ssh.localhost.run
If you need to dig more, know about ssh remote port forward. This service also doesn’t provide subdomains free of cost.
teleconsole
teleconsole is interesting. It starts an SSH server on your host machine and lets you share your terminal with other users. They mentioned in their documentation that if you’re sharing the credential/id with others, you know that the guy on the other end is trustworthy as he has full control of your computer.
To use teleconsole, you’ll need to use their binary. You can download their latest binary from HERE. Next, you can run the following command.
./teleconsole
That’ it. It’ll then give you an ID. The guy on the other end will need the ID to access your computer. And he’ll use the following command. And he’ll also need to use the executable on his computer.
./teleconsole join SESSION-ID
If you want to expose your application, you can run like,
./teleconsole -f localhost:PORT# PORT is the port number your application is running on.
# if the application's port is 8090
# then it'll be like
# ./teleconsole -f localhost:8090
And, when the guest user uses the join command, he’ll be shown a message that the host has invited you to access port PORT on their machine via localhost:9000. So, if he opens the browser and goes to localhost:9000
he’ll get to view your application.
Otherwise, if he knows the exact port the application is running, he can specify it when joining.
./teleconsole -L 9000:localhost:PORT join SESSION-ID
# Change the PORT with the port your application listens to.
To know more, check their documentation.
tunnelto.dev
For tunnelto.dev you need to download their binary or install it globally. Visit their website to download it. And also you need an API key. You’ll get your API key in the dashboard after you complete the registration.
./tunnelto set-auth --key YOUR-AUTH-KEY
# Set your auth key./tunnelto -p PORT
# PORT is the port number your application is running to.
After running the above command, if nothing went wrong then you’ll be shown an URL. You can then visit your application with that URL.
You can also use a subdomain. To do so, you can run the following command.
./tunnelto -p PORT -s subdomain
If the subdomain is available, then you’ll be given a new URL with that subdomain.
telebit
Telebit requires you to install the globally in your system. To do so, you need to run the following command.
curl https://get.telebit.io/ | bash
It’ll then install whatever it requires. After installing successfully, it’ll let you claim a subdomain. You’ll need to provide your email. After the setup is successful, then you can run the following commands
telebit http PORT
# PORT is the port number you application is listening to.
And after it’s successfully set up, you can visit the given URL to view your application. You can also share your whole directory using,
telebit http /path/to/your/directory/
You can also do other stuff with telebit. Check the documentation for more.
Can I use it with docker?
Please, docker has nothing to do with this. Your application is containerized. Your reverse proxy may be listening on port 80. Your host machine’s 8000 (or whatever port it is) is forwarding all the traffic to the container’s 80 port. Then 8000 is the port you need to replace in the above examples. Because your host machine doesn’t know anything about container’s 80. It only cares about 8000.
Caveat
Your application will be available online if your host machine remains connected to the services. As soon as the connection gets interrupted between your computer and the remote service due to power failure or internet unavailability, the URLs will be broken and wouldn’t work.
Happy coding and sharing. ❤