Using the hosts file on Windows with ports

As you might or might not know I’m running Profit Trailer (PT) on a VPS. To be safe the GUI is not accessible to the outside world, but only through an SSH tunnel. It has been irritating me for a while that the URLs are not readable, e.g. localhost:8081. I wanted readable URLs without ports in them. Today I finally figured out how to do this using a combination of host file and the Windows networking tool netsh.

I am showing my settings here, so adjust accordingly for your own situation.

Notes

  1. All commands need to be run as administrator
  2. If you are running an antivirus program (in my case Avira) chances are it will block updating the hosts file. I needed temporarily needed to disable this in Avira. In Avira uncheck the option Protect Windows hosts file from changes under Configuration > General > Security.AviraDon’t forget to re-enable protection again once you have updated the hosts file.

Update hosts file

Open the hosts file with notepad (don’t forget to do this as administrator).

notepad C:\Windows\System32\drivers\etc\hosts

Add the following entries to the hosts file. The IP address can be any local IP address as long as it is unique. You can use the command netstat -a -n -p TCP to check which IP addresses are in use.

127.0.0.2 binance.pt
127.0.0.3 poloniex.pt
127.0.0.4 bittrex.pt
127.0.1.2 binance.ptm
127.0.1.3 poloniex.ptm
127.0.1.4 bittrex.ptm

Save and close the hosts file.

Tip: re-enable hosts file protection if you disabled it.

Setup network redirect

Execute the following network configuration commands from the command line (don’t forget to do this as administrator).

netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.0.0.2 connectport=8081 connectaddress=localhost
netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.0.0.3 connectport=8082 connectaddress=localhost
netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.0.0.4 connectport=8083 connectaddress=localhost
netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.0.1.2 connectport=8101 connectaddress=localhost
netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.0.1.3 connectport=8102 connectaddress=localhost
netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.0.1.4 connectport=8103 connectaddress=localhost

You can check that the entries have added with the command:

netsh interface portproxy show v4tov4

You can remove the entries with the following command:

netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.0.0.2
netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.0.0.3
netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.0.0.4
netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.0.1.2
netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.0.1.3
netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.0.1.4

Testing

In your browser enter the following URL.

http://binance.pt

If all went well you can now see the Profit Trailer GUI with a readable URL. How cool is that?

Advertisements

2 thoughts on “Using the hosts file on Windows with ports

  1. Network at work blocks me from accessing these dashboards because it is seen as an application (due to portnumbers in url i guess). Any way this is possible too on ubuntu? Would you mind writing an article about that too?

  2. Not sure I understand what you mean. The whole idea behind my setup is that you do not need to open any ports on the VPS – other than the port SSH is running on – to the outside world. Request from the client are tunneling through SSH to the server. For the server it is a local request.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s