Skip to content

Instantly share code, notes, and snippets.

@typhartez
Created April 3, 2020 13:36
Show Gist options
  • Select an option

  • Save typhartez/23ec3b7345fe37611ad310762246a6aa to your computer and use it in GitHub Desktop.

Select an option

Save typhartez/23ec3b7345fe37611ad310762246a6aa to your computer and use it in GitHub Desktop.
Local Opensim with NAT Loopback on Linux

Prepare things

Gather important information about the local network and public IP. Example:

  • local IP: 192.168.1.1 (network 192.168.1.0/24)
  • external IP: 88.88.88.88 (easier with fixed IP, but works with dynamic ones)
  • Opensim port: 9000 (example in standalone)
  • Region port: 9000 (and any additionnal ones...)

IP addresses can be obtained with the natloopback script below.

Forward ports on the modem

Make rules on the router to forward ports

  • Public TCP port 9000 -> 192.168.1.1:9000
  • Public UDP port 9000 -> 192.168.1.1:9000

Restart the router!

DNS

Register a domain name (even with a dynamic dns), let's use myworld.example.com for this example

myworld.example.com -> 88.88.88.88

Setup /etc/hosts

Ensure that in local the domain name points to the local network in

/etc/hosts

192.168.1.1    myworld.example.com

Setup iptables rules

Information taken on OpenSimulator web site.

/usr/local/bin/natloopback

#!/bin/bash

if [[ $(id -u) != "0" ]]; then
  echo "run this as root..." >&2
  exit 1
fi

ipt=/usr/bin/iptables
myaddr=$(ip addr|grep -Po '(?!(inet 127.\d.\d.1))(inet \K(\d{1,3}\.){3}\d{1,3}\/\d{1,2})')

INTERNET_IP=$(curl -s ifconfig.me)
SERVER_IP=$(echo $myaddr|awk '{sub(/\/.*$/, ""); print}')
LAN_NET=$(echo $myaddr|awk -v FS='.' -v OFS='.' '{sub(/^[0-9]+/, "0", $4);print}')

if [[ $1 = addr ]]; then
  cat <<EOF

INTERNET_IP $INTERNET_IP
SERVER_IP   $SERVER_IP
LAN_NET     $LAN_NET

EOF
elif [[ $1 = list ]]; then
  $ipt -t nat -L -n
elif [[ $1 = stop ]]; then
  $ipt -t nat -F
  $ipt -t nat -X
elif [[ -n $1 ]]; then
  PORT=$1
  $ipt -t nat -I PREROUTING -d $INTERNET_IP -p tcp --dport $PORT --jump DNAT --to-destination $SERVER_IP
  $ipt -t nat -I PREROUTING -d $INTERNET_IP -p udp --dport $PORT --jump DNAT --to-destination $SERVER_IP

  $ipt -t nat -I POSTROUTING -s $LAN_NET -d $SERVER_IP -p tcp --dport $PORT --jump SNAT --to-source $INTERNET_IP
  $ipt -t nat -I POSTROUTING -s $LAN_NET -d $SERVER_IP -p udp --dport $PORT --jump SNAT --to-source $INTERNET_IP
else
  cat >&2 <<EOF
usage: natloopback {port}|stop|list|addr
  {port}  port number to enable nat loopback on
  stop    remove all nat rules (warning: not only for those defined by this script)
  list    list active nat rules
  addr    give important network information
EOF
fi

Run those rules as root each time the machine is started or the IP address changes, or manually

$ sudo /usr/local/bin/natloopback 9000

This script has several commands.

Remove all nat rules:

$ sudo /usr/local/bin/natloopback stop

List nat rules:

$ sudo /usr/local/bin/natloopback list

Gather network information:

$ sudo /usr/local/bin/natloopback addr

INTERNET_IP  88.88.88.88
SERVER_IP    192.168.1.1
LAN_NET      192.168.1.0/24

Opensim configuration

In all .ini files, whenever a host is needed, specify the domain name

OpenSim.ini

[Const]
  Host = "myworld.example.com"
  Port = 9042
  URI = "http://${Const|Host}:${Const|Port}"
  Name = MyWorld

[Network]
  http_listener_port = ${Const|Port}
  ExternalHostNameForLSL= "${Const|Host}"

[GridInfoService]
  login = ${Const|URI}/

; all other references to port, hostname or URI can be made with initial constants defined in the `[Const]` section

In Regions.ini, give also the domain name for external name:

Regions.ini

[MyWorld]
RegionUUID = 00b0861a-a717-4f95-aece-a83af7f12741
Location = 1000,1000
InternalAddress = 0.0.0.0
InternalPort = 9000
ResolveAdress = False
ExternalHostName = myworld.example.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment