150 lines
4.8 KiB
Bash
Executable File
150 lines
4.8 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
if command -v jq > /dev/null 2>&1; then
|
|
USE_JQ=true
|
|
else
|
|
USE_JQ=false
|
|
if command -v python3 > /dev/null 2>&1; then
|
|
echo "Warning: jq not found. Using python3 fallback."
|
|
else
|
|
echo "Error: neither jq nor python3 found. Please install one of them."
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
if [ ! -f settings.json ]; then
|
|
echo "settings.json not found. Downloading default settings.json..."
|
|
if command -v wget > /dev/null 2>&1; then
|
|
wget -qO settings.json https://cloud.qortal.org/s/QortalDevNetDefaultSettingsFile/download/default-settings.json
|
|
elif command -v curl > /dev/null 2>&1; then
|
|
curl -s -o settings.json https://cloud.qortal.org/s/QortalDevNetDefaultSettingsFile/download/default-settings.json
|
|
else
|
|
echo "Neither wget nor curl found. Please install one of them."
|
|
exit 1
|
|
fi
|
|
echo "Downloaded default settings.json"
|
|
fi
|
|
|
|
PORT_START="${QORTAL_PORT_START:-23391}"
|
|
PORT_START_NUM=$(printf '%s' "$PORT_START" | tr -d ' \n\r')
|
|
if ! echo "$PORT_START_NUM" | grep -Eq '^[0-9]+$'; then
|
|
echo "Error: invalid port start '${PORT_START}'."
|
|
exit 1
|
|
fi
|
|
PORT_START="$PORT_START_NUM"
|
|
API_PORT=$PORT_START
|
|
LISTEN_PORT=$((PORT_START + 1))
|
|
DEV_PROXY_PORT=$((PORT_START + 2))
|
|
LISTEN_DATA_PORT=$((PORT_START + 3))
|
|
|
|
SET_PORTS=false
|
|
if [ "$PORT_START" -ne 23391 ]; then
|
|
SET_PORTS=true
|
|
fi
|
|
|
|
get_public_ip() {
|
|
local ip=""
|
|
local url=""
|
|
local urls="https://api.ipify.org https://ipv4.icanhazip.com https://checkip.amazonaws.com https://ifconfig.me https://canhazip.com"
|
|
local curl_opts="-fsS --max-time 8 -A Mozilla/5.0"
|
|
|
|
for url in $urls; do
|
|
if command -v curl > /dev/null 2>&1; then
|
|
ip="$(curl $curl_opts "$url" 2>/dev/null || true)"
|
|
elif command -v wget > /dev/null 2>&1; then
|
|
ip="$(wget -qO- "$url" || true)"
|
|
else
|
|
return 1
|
|
fi
|
|
ip="$(echo "$ip" | tr -d ' \n\r')"
|
|
if echo "$ip" | grep -Eq '^[0-9]{1,3}(\.[0-9]{1,3}){3}$'; then
|
|
echo "$ip"
|
|
return 0
|
|
fi
|
|
done
|
|
|
|
if command -v dig > /dev/null 2>&1; then
|
|
ip="$(dig +short myip.opendns.com @resolver1.opendns.com | head -n 1 | tr -d ' \n\r')"
|
|
if echo "$ip" | grep -Eq '^[0-9]{1,3}(\.[0-9]{1,3}){3}$'; then
|
|
echo "$ip"
|
|
return 0
|
|
fi
|
|
fi
|
|
|
|
return 1
|
|
}
|
|
|
|
# Get public IP address
|
|
echo "Obtaining public IP address..."
|
|
if ! PUBLIC_IP="$(get_public_ip)"; then
|
|
echo "Warning: Could not obtain a valid public IPv4 address; please manually set 'ourExternalIpAddress' (find it at https://www.whatismyipaddress.com)."
|
|
exit 0
|
|
fi
|
|
|
|
echo "Public IP obtained: $PUBLIC_IP"
|
|
|
|
if [ "$USE_JQ" = true ]; then
|
|
echo "Updating settings.json using jq..."
|
|
jq --arg ip "$PUBLIC_IP" \
|
|
--argjson setPorts "$SET_PORTS" \
|
|
--argjson apiPort "$API_PORT" \
|
|
--argjson listenPort "$LISTEN_PORT" \
|
|
--argjson devProxyPort "$DEV_PROXY_PORT" \
|
|
--argjson listenDataPort "$LISTEN_DATA_PORT" \
|
|
'
|
|
.ourExternalIpAddress = $ip
|
|
| (if $setPorts then .apiPort = $apiPort
|
|
| .listenPort = $listenPort
|
|
| .devProxyPort = $devProxyPort
|
|
| .listenDataPort = $listenDataPort
|
|
else . end)
|
|
' \
|
|
settings.json > settings.json.tmp && mv settings.json.tmp settings.json || {
|
|
echo "Error: failed to update settings.json using jq."
|
|
rm -f settings.json.tmp
|
|
exit 1
|
|
}
|
|
else
|
|
echo "Updating settings.json using python3..."
|
|
PUBLIC_IP="$PUBLIC_IP" \
|
|
SET_PORTS="$SET_PORTS" \
|
|
API_PORT="$API_PORT" \
|
|
LISTEN_PORT="$LISTEN_PORT" \
|
|
DEV_PROXY_PORT="$DEV_PROXY_PORT" \
|
|
LISTEN_DATA_PORT="$LISTEN_DATA_PORT" \
|
|
python3 - <<'PY'
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
settings_path = "settings.json"
|
|
try:
|
|
with open(settings_path, "r", encoding="utf-8") as fh:
|
|
data = json.load(fh)
|
|
except Exception as exc:
|
|
print(f"Error: failed to parse settings.json: {exc}")
|
|
sys.exit(1)
|
|
|
|
data["ourExternalIpAddress"] = os.environ["PUBLIC_IP"]
|
|
if os.environ.get("SET_PORTS", "false").lower() == "true":
|
|
data["apiPort"] = int(os.environ["API_PORT"])
|
|
data["listenPort"] = int(os.environ["LISTEN_PORT"])
|
|
data["devProxyPort"] = int(os.environ["DEV_PROXY_PORT"])
|
|
data["listenDataPort"] = int(os.environ["LISTEN_DATA_PORT"])
|
|
|
|
with open(settings_path, "w", encoding="utf-8") as fh:
|
|
json.dump(data, fh, indent=2)
|
|
fh.write("\n")
|
|
PY
|
|
if [ $? -ne 0 ]; then
|
|
echo "Error: failed to update settings.json using python3."
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
if [ "$SET_PORTS" = true ]; then
|
|
echo "Successfully updated settings.json with ourExternalIpAddress=${PUBLIC_IP} and ports ${API_PORT}-${LISTEN_DATA_PORT}."
|
|
else
|
|
echo "Successfully updated settings.json with ourExternalIpAddress=${PUBLIC_IP}."
|
|
fi
|