I built a chat server using Rust.

I wanted to put the server on the internet so that my friends could use it with me! So I did just that; I created a Virtual Machine on Azure, connected to the VM via SSH, downloaded rustup, compiled my code into a binary, and ran the server on the address 127.0.0.1:8080.

But I couldn’t connect to the server from my laptop :(

Connection refused

I went on azure and found the Public IP Address of the Virtual Machine that I created, 12.34.567.890.

I then tried testing the connection to the server using telnet:

telnet 12.34.567.890 8080

But I got the following error:

Trying 12.34.567.890...
telnet: connect to address 12.34.567.890: Connection refused
telnet: Unable to connect to remote host

Loopback Address

After struggling with the connection issue for a while, I googled something along the lines of…

Azure VM - Can’t connect to a TCP server that listens on a specific port

And I found this post which helpfully explains the following:

It is not only listening on a specific port but also at a specific address. You don’t want that. You are binding the server socket to IPAddress.Loopback, i.e. 127.0.0.1, which means it won’t accept any connections from outside its local host.

Bind it to 0.0.0.0.

Which lead me to learn about what a Loopback Address is…

Basically, it is an address that is meant to provide a way for a computer to talk to itself (and not the outside world). Since it doesn’t expose itself to the outside world, it is not accessible from other computers.

First contact

But even binding the server to 0.0.0.0:8080 is not enough; we must go deeper.

At this point the server is correctly bound to the IP Address 12.34.567.890, however, the port 8080 is not open to network traffic.

We must follow “configure inbound and outbound network traffic” to do two things:

  1. Open port 8080 to inbound network traffic
  2. Open port 8080 to outbound network traffic

I created rules for each that say “Allow inbound/outbound traffic from any source to any destination on port 8080”.

Success!

At this point, I was able to connect to the server from my laptop using the chat client that I wrote.

Is this the right place to stop? I’m not sure! If I were to keep this VM running 24/7 I would probably leave myself vulnerable to malicious actors!

Since I am not a security expert, I will leave it to you to figure out how to secure your VMs :)