Workaround: Issue Setting up UT2004 Behind a NAT

Post Reply
DDRRE
Posts: 3
Joined: Sat Apr 04, 2020 1:15 pm

Workaround: Issue Setting up UT2004 Behind a NAT

Post by DDRRE »

Hi,

Took some trial and error, but if you're looking to run UT2004 servers behind a NAT on Linux, and you're using ServerBehindNAT=True, you should make sure you change the source port (SNAT) on outgoing TCP master server packets, and define ut2k4port to match Port as defined in ~/.ut2004/System/UT2004.ini (usually 7777).

Anyway, here's a script I wrote that sets this up for you. Just write this into a file (like ut2004-linux-natfix.sh) and chmod it to allow execution.
It should run as the root user (or with sudo) to apply the workaround using iptables.

Code: Select all

#!/bin/bash
## UT2004 Linux server workaround script for ServerBehindNAT
## DDRRE, 2020

## Works around the UT2004 Linux server issue when operating behind a NAT with the ServerBehindNAT property set to True. iptables is used to configure SNAT for matching packets.
## Requires root privileges (iptables).

## Run this script before launching the UT2004 server binary. It will configure UT2004 master server packets to use the port specified in "ut2k4port" below, which should be set to your UT2004 query port (default is 7777).

ut2k4port=7777



## START

result1=0
result2=0
result3=0
getip () {
                                #requires result1 (and result2 and result3 for ut2004, which uses the ServerBehindNAT feature)
                                result1=0
                                result2=0
                                result3=0
                                if [[ $1 == 'ut2004' ]] && um1ip="$(dig +short ut2004master1.epicgames.com)" && um2ip="$(dig +short ut2004master2.epicgames.com)"
                                        then
                                                result2="${um1ip}"
                                                result3="${um2ip}"
                                                result1="$(ip route get "${um1ip}" | awk '{ print $7 }' | head -1)"
                                        else
                                                if [[ $1 == 'ut2004' ]]
                                                        then
                                                                result1=0
                                                                result2=0
                                                                result3=0
                                                fi
                                fi
}

natfix () {

                getip ut2004
                myip="$result1"
                master1ip="$result2"
                master2ip="$result3"
                if [ -f /tmp/DDRRE-ut2k4-ip ] && [ -f /tmp/DDRRE-ut2k4-master1 ] && [ -f /tmp/DDRRE-ut2k4-master2 ]
                        then
                                echo "Last configured IP was $(cat /tmp/cpgcmi-ut2k4-ip). Deleting UT2004 iptables SNAT rules"
                                sudo iptables -t nat -D POSTROUTING -p tcp -d $(cat /tmp/DDRRE-ut2k4-master1) -j SNAT --to-source $(cat /tmp/cpgcmi-ut2k4-ip):${ut2k4port}
                                sudo iptables -t nat -D POSTROUTING -p tcp -d $(cat /tmp/DDRRE-ut2k4-master2) -j SNAT --to-source $(cat /tmp/cpgcmi-ut2k4-ip):${ut2k4port}
                                rm -f /tmp/DDRRE-ut2k4-ip
                                rm -f /tmp/DDRRE-ut2k4-master1
                                rm -f /tmp/DDRRE-ut2k4-master2
                fi
                if ! [ "${result1}" = 0 ]
                        then
                        echo "${result1}" > /tmp/DDRRE-ut2k4-ip
                        echo "${result2}" > /tmp/DDRRE-ut2k4-master1
                        echo "${result3}" > /tmp/DDRRE-ut2k4-master2
                        echo "Configuring UT2004 server for IP address ${result1}:${ut2k4port}"
                        sudo iptables -t nat -A POSTROUTING -p tcp -d "$result2" -j SNAT --to-source ${myip}:${ut2k4port}
                        sudo iptables -t nat -A POSTROUTING -p tcp -d "$result3" -j SNAT --to-source ${myip}:${ut2k4port}

                fi
}

getip
natfix
Best,
DDRRE

Post Reply