Skip to content

Instantly share code, notes, and snippets.

@mchoiruln
Last active February 20, 2019 15:01
Show Gist options
  • Save mchoiruln/1bdee12eefa295e1d485a6da79287fe4 to your computer and use it in GitHub Desktop.
Save mchoiruln/1bdee12eefa295e1d485a6da79287fe4 to your computer and use it in GitHub Desktop.
Using port number for Windows host

SOURCE

I managed to achieve this by using Windows included Networking tool netsh.

The hosts file is for host name resolution only, so a combination of the two did the trick for me.

Example

Overview
example.app:80
 |                           <--Link by Hosts File
 +--> 127.65.43.21:80
       |                     <--Link by netsh Utility
       +--> localhost:8081
Actions
  • Started my server on localhost:8081
  • Added my "local DNS" in the hosts file as a new line
    • 127.65.43.21 example.app
      • Any free address in the network 127.0.0.0/8 (127.x.x.x) can be used.
      • Note: I am assuming 127.65.43.21:80 is not occupied by another service.
      • You can check with netstat -a -n -p TCP | grep "LISTENING"
    • added the following network configuration with netsh command utility
      • netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.65.43.21 connectport=8081 connectaddress=127.0.0.1
    • I can now access the server at http://example.app
Notes
  • netsh portproxy needs ipv6 libraries even only to use v4tov4, typically they will also be included by default, otherwise install them using the following command: netsh interface ipv6 install

You can see the entry you have added with the command:
netsh interface portproxy show v4tov4
You can remove the entry with the following command:
netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.65.43.21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment