Fix IPv6 formatting in SERVER_UDP_PORTS and add more placeholders (#700)

This commit is contained in:
Geoff Bourne authored and GitHub committed 2026-11-01 11:10:33 -06:00
1 parent fbd61c3666
commit 56e335f883
2 files changed
+133 -48

No files matched your search

+29 -1
View File
@@ -194,12 +194,40 @@ During the handshake, BDS sends the client a list of addresses and UDP ports. Th
- IMPORTANT: to access over the internet you need to use the public ip in the `SERVER_UDP_PORTS`, and forward all the ports.
Use an IPv4 or IPv6 literal, not a hostname.
To use the public IP address of the host, use the literal string `<public_ip>`. At startup, the image replaces it with the result of `curl ifconfig.me`.
Use a range with at least one port for each player (`max-players`, default 10).
Publish the TCP port and the full UDP range.
For an example, see [examples/nethernet/compose.yml](examples/nethernet/compose.yml).
### Using placeholder for public IP
Use one of the following placeholders in `SERVER_UDP_PORTS` to have the container resolve your public IP at startup using https://www.ipify.org/:
- `<public_ip>` : resolves to either IPv4 or IPv6, whichever is resolved by the universal lookup
- `<public_ipv4>` : resolves to IPv4 only
- `<public_ipv6>` : resolves to IPv6 only
### From Mojang's docs regarding SERVER_UDP_PORTS
```
Configures UDP client transport ports.
This property is ignored when `transport=raknet`.
If not set, UDP ports are allocated from the operating system's ephemeral port range.
Two modes are supported:
1. Single UDP port shared by all clients.
2. UDP port range, where client connections are distributed across a range of ports but will reuse ports if the range is exhausted.
Both modes support either the same local and public port values or a public:local mapping. Ranges on each side of `:` must be the same length.
Optionally, specify the public IP address using IP:public:local (IPv4) or [IPv6]:public:local (IPv6).
Both port values are required with an IP prefix, even when they are the same.
Examples:
server-udp-ports=19489 (Single UDP port shared by all clients, local and public values are the same)
server-udp-ports=29489:19489 (Single UDP port shared by all clients, public port:29489 must map to local port:19489)
server-udp-ports=19400-19500 (UDP port range, local and public values are the same)
server-udp-ports=21200-21300:19400-19500 (UDP port range, public port range: 21200-21300 must map to local port range: 19400-19500)
server-udp-ports=12.19.20.18:19489:19489 (Single UDP port with public IP, 12.19.20.18:19489 must map to the server's local UDP port 19489)
server-udp-ports=[2001:db8::2021:26:03]:21200-21300:19400-19500
(UDP port range with public IP, [2001:db8::2021:26:03]:21200-21300 must map to the server's local UDP ports 19400-19500)
```
### Firewall
On the host firewall and on all network firewalls between the clients and the server, allow these inbound connections:
+104 -47
View File
@@ -21,13 +21,18 @@ function isTrue() {
return 1
}
function logInfo() {
msg="${1?}"
echo "INFO $msg"
}
function logWarn() {
msg=${1?}
msg="${1?}"
echo "WARN $msg" >&2
}
function logError() {
msg=${1?}
msg="${1?}"
echo "ERROR $msg" >&2
}
@@ -166,6 +171,102 @@ function extractVersionFromUrl() {
fi
}
function isIPv6() {
ip=${1:?}
if [[ $ip =~ ^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}$ ]]; then
return 0
else
return 1
fi
}
function formatIpPart() {
ip=${1:?}
if isIPv6 "$ip"; then
echo "[$ip]"
else
echo "$ip"
fi
}
## @param placeholder The placeholder to replace (e.g., <public_ip>, <public_ipv4>, <public_ipv6>)
## @param publicIp The public IP address to replace the placeholder with. If empty, which will be the case if ipify is unable to resolve the requested address, the function will log an error and exit.
function replaceIpPlaceholder() {
local placeholder="${1:?}"
local publicIp="$2"
if [[ -z "$publicIp" ]]; then
logError "Unable to resolve public IP for ${placeholder}"
exit 1
fi
logInfo "Replacing ${placeholder} with ${publicIp} in SERVER_UDP_PORTS"
SERVER_UDP_PORTS="${SERVER_UDP_PORTS//${placeholder}/$(formatIpPart "$publicIp")}"
export SERVER_UDP_PORTS
}
## Evaluates the SERVER_UDP_PORTS environment variable and replaces the following placeholders:
## - <public_ip> with the public IP address (IPv4 or IPv6) of the server
## - <public_ipv4> with the public IPv4 address of the server
## - <public_ipv6> with the public IPv6 address of the server
## If one of the specific placeholders does not resolve, then the startup will exit
function manageServerUdpPorts() {
if [[ "$SERVER_UDP_PORTS" == *"<public_ip>"* ]]; then
replaceIpPlaceholder "<public_ip>" "$(curl -s https://api64.ipify.org)"
elif [[ "$SERVER_UDP_PORTS" == *"<public_ipv4>"* ]]; then
replaceIpPlaceholder "<public_ipv4>" "$(curl -s https://api.ipify.org)"
elif [[ "$SERVER_UDP_PORTS" == *"<public_ipv6>"* ]]; then
replaceIpPlaceholder "<public_ipv6>" "$(curl -s https://api6.ipify.org)"
fi
}
function manageProperties() {
manageServerUdpPorts
if [[ -n "$VARIABLES" ]]; then
echo "Setting variables"
mkdir -p config/default
# Try to parse VARIABLES as JSON
if echo "$VARIABLES" | jq empty >/dev/null 2>&1; then
# VARIABLES is valid JSON
echo "$VARIABLES" | jq '.' > "config/default/variables.json"
else
# VARIABLES is not valid JSON, attempt to parse as custom format
echo "VARIABLES is not valid JSON, attempting to parse as custom format"
# Parse the VARIABLES using custom format (key:value,key:value)
# Note: Values should not contain unescaped commas or colons
jq -n --arg vars "$VARIABLES" '
$vars
| split("[,\\n]+"; "")
| map(
select(length>0) |
split("=") as $kv |
{ ($kv[0]): ($kv[1] | fromjson? // $kv[1]) }
)
| add
' > "config/default/variables.json"
fi
fi
if [ -f server.properties ]; then
# prevent issue with bind mounted server.properties which can not be moved (sed tries to move the file when '-i' is used)
_SERVER_PROPERTIES=$(sed '/^white-list=.*/d' server.properties) #Removes white-list= line from server.properties
echo "${_SERVER_PROPERTIES}" > server.properties
fi
export ALLOW_LIST
# update server.properties with environment settings
set-property --file server.properties --bulk /etc/bds-property-definitions.json
}
#######################################################################################################################
# MAIN
#######################################################################################################################
echo "Image info: $(paste -d, -s /etc/image.properties)"
debugCurlArgs=()
@@ -445,51 +546,7 @@ if [[ -n "$ALLOW_LIST_USERS" || -n "$WHITE_LIST_USERS" ]]; then
fi
fi
if [[ "$SERVER_UDP_PORTS" == *"<public_ip>"* ]]; then
echo "Replacing <public_ip> in $SERVER_UDP_PORTS"
public_ip=$(curl -s ifconfig.me)
echo "Public IP resolved to: $public_ip"
SERVER_UDP_PORTS="${SERVER_UDP_PORTS//<public_ip>/$public_ip}"
export SERVER_UDP_PORTS
fi
if [[ -n "$VARIABLES" ]]; then
echo "Setting variables"
mkdir -p config/default
# Try to parse VARIABLES as JSON
if echo "$VARIABLES" | jq empty >/dev/null 2>&1; then
# VARIABLES is valid JSON
echo "$VARIABLES" | jq '.' > "config/default/variables.json"
else
# VARIABLES is not valid JSON, attempt to parse as custom format
echo "VARIABLES is not valid JSON, attempting to parse as custom format"
# Parse the VARIABLES using custom format (key:value,key:value)
# Note: Values should not contain unescaped commas or colons
jq -n --arg vars "$VARIABLES" '
$vars
| split("[,\\n]+"; "")
| map(
select(length>0) |
split("=") as $kv |
{ ($kv[0]): ($kv[1] | fromjson? // $kv[1]) }
)
| add
' > "config/default/variables.json"
fi
fi
if [ -f server.properties ]; then
# prevent issue with bind mounted server.properties which can not be moved (sed tries to move the file when '-i' is used)
_SERVER_PROPERTIES=$(sed '/^white-list=.*/d' server.properties) #Removes white-list= line from server.properties
echo "${_SERVER_PROPERTIES}" > server.properties
fi
export ALLOW_LIST
# update server.properties with environment settings
set-property --file server.properties --bulk /etc/bds-property-definitions.json
manageProperties
export LD_LIBRARY_PATH=.