Files
qortal-DevNet-scripts/add-public-ip-to-settings-mac.sh

113 lines
3.4 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
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
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
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
}
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
if ! command -v python3 >/dev/null 2>&1; then
echo "Error: python3 not found; cannot update settings.json."
exit 1
fi
export PUBLIC_IP LISTEN_PORT API_PORT DEV_PROXY_PORT LISTEN_DATA_PORT SET_PORTS
python3 - <<'PY'
import json
import os
import sys
settings_path = "settings.json"
with open(settings_path, "r", encoding="utf-8") as fh:
data = json.load(fh)
public_ip = os.environ["PUBLIC_IP"]
listen_port = int(os.environ["LISTEN_PORT"])
api_port = int(os.environ["API_PORT"])
dev_proxy_port = int(os.environ["DEV_PROXY_PORT"])
listen_data_port = int(os.environ["LISTEN_DATA_PORT"])
set_ports = os.environ.get("SET_PORTS", "false").lower() == "true"
data["ourExternalIpAddress"] = public_ip
if set_ports:
data["apiPort"] = api_port
data["listenPort"] = listen_port
data["devProxyPort"] = dev_proxy_port
data["listenDataPort"] = listen_data_port
with open(settings_path, "w", encoding="utf-8") as fh:
json.dump(data, fh, indent=2)
fh.write("\n")
PY
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