How to expose your PC to the internet without a public IP address?
Internet is fascinating: keeps everyone connected, made work from home a reality, and made the availability of information cheap and fast, yet the basic concepts of the internet are very simple. It’s just some shared files. Every website or web app you visit is just some files stored somewhere in another computer in the network. When someone enters the URL, a request is sent to the particular computer and the computer sends back the needed files to the one who made the request and it is viewed in the browser. Even if the basic concept is that simple, the internet just impacted the world on a very very large scale. It was really a revolution.
While it is able to request a file from another computer in the network, can we share our files with another computer? Internet is just a distributed network, so we might also be able to share files with another computer. Suppose you developed a cool web app and you want to showcase this to your friends or maybe the outside world. It could be done with the help of hosting providers where you just upload the files of your web app and it is run on a server provided by the provider.
Hosting on other platforms is great if your app is ready to be in production since it is more secure and the machines on which you host your app will have much higher performance and also the internet connection speed will be very fast.
But there are times when you just want to simply showcase your app to someone and not make it available for everyone or you may want to get feedback on your app simultaneously while developing. Hosting on a platform and learning how to host it on a hosting platform may be too much at this stage. Moreover, there are no completely free hosting solutions for dynamic web apps. Either there will be a limit in the amount of usage or you have to pay a fee.
There is a perfect solution for this and it is connecting your PC directly to the internet so that you can make the server on your machine listen to requests from the internet and do not need to host. Anyone who is able to reach the server in your machine can send requests to it or communicate with it. So, how do we do it? For that, let’s get into some basics of networking.
An IP address is analogous to a physical address. Suppose if you want to send a courier to your friend, you provide the address of his home. Same as that IP address helps to identify machines in the network. When you type a URL in the browser, the URL is translated to the corresponding IP address with the help of DNS (Domain Name System) and the request is sent to the particular IP address. The world is currently using the version 4 IP address known as the IPV4 address
IPV4 address is the 32-bit numerical address. There can be 4 billion possible IPV4 addresses. So, if we assign an IPV4 address to each machine, the machine can be uniquely identified from the whole network and can be communicated with. If that was the case yet, everyone could expose their PC very easily to the internet but that’s not the case now.
Even if IPV4 addresses can have 4 billion values, it has exhausted as the internet had grown rapidly. So, as of now only ISP’s and others like data centers or hosting providers have unique IPV4 addresses. It’s a little bit difficult to have a unique IP for ourselves. These unique addresses are known as public IP.
Private IP addresses are the ones that your router uses to identify the machines in a local network. So an ISP may assign a public IP address to your router and then the router will have an IP address pool. Each machine connected to the router will be given an IP address from the pool. These IP addresses assigned by the router will be private and are known as private IP addresses. The router itself will also be having a private IP address which is known as the default gateway. A machine in the local network sends the request to this default gateway IP.
NAT(Network Address Translation)
NAT maps the public IP to the corresponding private IP, that is when a machine from the local network sends a request to a server through the internet, it first sends the request to the private IP of the router, then NAT maps the private IP to the public IP of the router. When the server sends a response back to the machine, it sends the request to the public IP of the router and NAT maps the public IP of the router to the private IP of the sender.
So, there is no need for a public IP for every device if the device shares the same local network. This also increases security as no one can connect directly to a device behind NAT. If your router has a public IP, you can expose your PC to the internet by port-forwarding which is very simple.
The problem is that sometimes even the router will not have a public IP as the router is just behind another NAT which means that the router itself is a device in a larger local network. Instead of assigning public IP for every router, the ISP may use a NAT and assign a private IP to the router. So there will be a local network of routers just under the ISP and under each router, there will be a local network of computers or mobile phones. This is intended for increased security and also to decrease the usage of the public IP.
Exposing PC to the internet without a public IP
This is my application running on a Nodejs server at port 3000. This application is on my PC.
To expose a PC to the internet without a public IP:
- Download and install OpenVPN. In my ubuntu 20.04, it was preinstalled.
- Goto https://portmap.io/
- Create an account(it’s free)
- Click on the configurations tab
- Add a new configuration, select the protocol, give a configuration name and then save the configuration and download the configuration file.
- Goto mapping rules tab and create a new mapping rule.
- Set the protocol as ‘http’
- On the ‘Port on your PC’ field provide the port on which the server is running on your machine. In this case, 3000 as my server is running on 3000.
- Save the rule.
- Now open a terminal and navigate to the location where the configuration file is downloaded and enter the command
sudo openvpn --config <Your downloaded config file name>
If the ‘initialization sequence completed’ message appears you are set to go.
Now, the OpenVPN sets up a tunnel from your machine to the portmap.io website. Send the URL which was generated by the portmap.io after saving the mapping rule to your friends and when they click on it a request is sent to the portmap.io to the specified port in the URL and the portmap.io redirects that URL to the specified port in your PC.
Now you can connect communicate with your server from anywhere in the world!!!