0

i'm not sure about the right terminology, but try to redirect Traffic from Server A to Server B. I have the following Resources:

  • A DNS Record Type A pointing to the Server A's IP address, e.g.: abc.com
  • Server A Ubuntu, with a static IP of e.g.: 00.00.00.01, no ports are listening.
  • Server B Debian, with a dynamic IP of lets say: 00.00.00.02, which is listening on port 123
  • Incoming Requests from the internet on abc.com:123

Server A is a server without much processing power, but has a static IP. Server B is a powerfull server, but has a dynamic IP. Both operate from different Networks.

My plan is that Server B frequently tells Server A it's current IP address and store it on Server A's Drive in a file.

If there is a request on Server A at the Port 123, it should use the previously stored IP address and redirect the traffic to it.

What i previously tried was to use IP tables and redirect the traffic. I'm not very familiar iptables, but found the following commands on the internet which made sense to me:

sudo sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g' /etc/sysctl.conf # enable forwarding
sudo sysctl net.ipv4.ip_forward # apply changes
sudo iptables -t nat -A PREROUTING -p tcp --dport 123 -j DNAT --to-destination 00.00.00.02:123
sudo iptables -t nat -A POSTROUTING -p tcp -d 00.00.00.02 --dport 123 -j SNAT --to-source 00.00.00.01

Afterwards i restarted the iptables service. As no error's were produced, i tried use traceroute to check if my requests on the domain would be sent to Server B. which they didn't.

traceroute abc.com 123

i do know that nothing is listening on port 123 on Server A, but thought thats fine as iptables would redirect the traffic anyways to Server B.

Maybe someone has an idea.

2
  • Have you come across Dynamic DNS (DDNS)? This is the usual way of handling your situation Commented Dec 18, 2021 at 16:16
  • 1
    thx, that was a hint in the right direction. My DNS Provider does not have this functionality. BUT it has a REST Api, and i can just use a crontab to UPDATE the IP Adress in the DNS Record via shell script every 30 minutes.
    – TheDomis4
    Commented Dec 18, 2021 at 17:33

1 Answer 1

0

As @roaima pointed out:

Have you come across Dynamic DNS (DDNS)? This is the usual way of handling your situation

And you said your DNS provider has a REST API; perfect!

You can simply only check every 10s or so whether B's public IP has changed, and then only update when it's necessary.

That way you both don't cause updates when nothing has happened, and you get a low latency on updating the public IP address when it does.

A script like this would work:

#!/bin/bash

lastip=""

while true
do
  currip=$(dig +short myip.opendns.com resolver1.opendns.com)
  if [ "${lastip}" = "${currip}" ]
  then
    sleep 10
  else
    curl WHATEVERNEEDSTOBEDONE_TO_UPDATE_DNS
    lastip="${currip}"
    sleep 300
  fi
done

As roaima points out, of course your DNS provider must have a sufficiently low DNS TTL (i.e. it must mark the responses it gives as expiring quickly). However, my experience is that it's not rare that providers offer things like 5 to 20 minute minimum TTLs, and that they are configurable. So, I'd recommend you try.

The rather short sleep interval is due to it being rather "cheap" to query your own IP address using openDNS' myip service, and it being always better to update early, no matter how long the update will take to propagate to the clients.

4
  • That won't work unless the TTL for the DNS records is really low (like 60 seconds or less). I've yet to come across a DNS provider that will permit such a low value. The usual approach is to get a DDNS record from a specialist provider and then CNAME your website to the DDNS domain Commented Dec 18, 2021 at 18:12
  • @roaima I hope I sensibly address that in my last paragraph. Commented Dec 18, 2021 at 18:17
  • Only partially. If you've a TTL of an hour, a typical value, then you've an average of 30 minutes and a worst case of fractionally under an hour before the update will be noticed by the end user Commented Dec 18, 2021 at 19:00
  • yep, but if you've got a TTL of an hour, and only update every 30 min, then your worst case becomes 1h30min - 𝜀; so, doing the update early is kind of always desirable. Commented Dec 18, 2021 at 19:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .