Firewall – Communicating between two servers sitting behind their respective NATs

firewallnat;socket

I'm building an web app (Server A) that communicates with a remote server (Server B) that is connected to the internet.

Server B resides behind a NAT and is listening to communication on 3 non-standard ports.

In a typical scenario, in order for me to deploy multiple remote servers across different networks and to have it communicate with the parent web app (Server A), each of these servers have to have the routers manually configured to forward the respective ports to a static ip in which Server B is configured.

I'm looking for a way to solve this problem of having to configure each router individually and manually for each deployment. Port forwarding has to be done because Server A has to communicate with Server B and has to initiate the conversation.

I'm asking if there's a way for Server B to initiate the communication rather than the other way round so that port forwarding doesn't need to occur.

Server B would just need to call home, open up what ever sockets/ports necessarily (this is where I need your help for information) and all communication would be done via these opened ports without Server A ever needing to know the ip of Server B, and having to talk to the router to handle the forwarding.

Is this scenario possible?

Edit:
Added some more details:

Basically I have created a webapp (Server A) that talks to multiple 3D print servers that has an REST API exposed. The print server API servers are all listening on a non-standard port of 8721.

Right now, manually on my webapp I am configuring manually to point to each print server via their external ip address and port-forwarded port. As you can probably tell this is going to create a shitload of problems as most of these external ip addresses are going to change due to most home/consumer networks not having a static ip.

This means that I have to find out everytime the ip address is changed and manually modify my records on my webapp.

I imagine (and hypothesize) that one of the best way to solve this is to not let the webapp worry about where each of these print servers are located but rather let the print servers themselves tell the webserver where they are.

I'm trying to solve 2 problems:

  1. The external ip addresses being dynamic and not static (as mentioned above)
  2. Port-forwarding/firewall imposed by the home networks. In the event that there are multiple print servers in a home network, this means that in order to reach each server individually, the home router needs to know how to route each unique port which needs to be configured on both the webapp and the home router side to the right print server (as all of them are listening on 8721).

Sorry for the lengthy post. I', still relatively new to the whole socket programming part of things and I'm hoping that someone would be able to point me to where I should be looking to find out more information on how to do the above.

Best Answer

Yes, this is possible. This can be automated easily with UPnP port forwarding, if enabled on the NAT host/device, if security is not a consideration. If you do not want to use UPnP, for example, because security is a consideration, you can achieve this using another API to access your NAT devices. UPnP is very common in home routers.

You may wish to use another server to communicate the IP addresses and port numbers of Server A and Server B at a static IP address or FQDN that is directly accessible on a static port. This can be done by storing the data in a text file, for example.

As @Evan said, without specifics about what you are really trying to achieve, this is all I can say.