138 lines
3.9 KiB
Bash
Executable File
138 lines
3.9 KiB
Bash
Executable File
#!/bin/bash
|
|
set -euo pipefail
|
|
IFS=$'\n\t'
|
|
|
|
PUBLISH_SHARE_TOKEN="PublishDevNetIPsHere"
|
|
PUBLISH_WEBDAV_URL="https://cloud.qortal.org/public.php/webdav"
|
|
PORT_START_DEFAULT=23391
|
|
PORT_START="${QORTAL_PORT_START:-$PORT_START_DEFAULT}"
|
|
LISTEN_PORT=""
|
|
|
|
need_cmd() { command -v "$1" >/dev/null 2>&1; }
|
|
|
|
is_valid_port() {
|
|
local port="$1"
|
|
if ! echo "$port" | grep -Eq '^[0-9]+$'; then
|
|
return 1
|
|
fi
|
|
if (( port < 1 || port > 65535 )); then
|
|
return 1
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
read_listen_port_from_settings() {
|
|
local settings_path=""
|
|
local port=""
|
|
local candidate=""
|
|
|
|
for settings_path in "./settings.json" "${HOME}/qortal/settings.json"; do
|
|
if [[ -f "$settings_path" ]]; then
|
|
if need_cmd jq; then
|
|
candidate="$(jq -r '.listenPort // empty' "$settings_path" 2>/dev/null || true)"
|
|
else
|
|
candidate="$(sed -nE 's/.*"listenPort"[[:space:]]*:[[:space:]]*([0-9]+).*/\1/p' "$settings_path" | head -n 1)"
|
|
fi
|
|
candidate="$(echo "$candidate" | tr -d ' \n\r')"
|
|
if is_valid_port "$candidate"; then
|
|
port="$candidate"
|
|
break
|
|
fi
|
|
fi
|
|
done
|
|
|
|
if [[ -n "$port" ]]; then
|
|
echo "$port"
|
|
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 need_cmd curl; then
|
|
ip="$(curl $curl_opts "$url" 2>/dev/null || true)"
|
|
elif need_cmd wget; 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 need_cmd dig; 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
|
|
}
|
|
|
|
publish_public_ip() {
|
|
local ip="$1"
|
|
local port="$2"
|
|
local host_name="$3"
|
|
local tmp_file="$4"
|
|
local remote_name="${host_name}.txt"
|
|
local url="${PUBLISH_WEBDAV_URL}/${remote_name}"
|
|
local alt_name=""
|
|
local alt_url=""
|
|
local http_code=""
|
|
|
|
printf '%s:%s\n' "$ip" "$port" > "$tmp_file"
|
|
|
|
if need_cmd curl; then
|
|
http_code="$(curl -sS -u "${PUBLISH_SHARE_TOKEN}:" -T "$tmp_file" -o /dev/null -w "%{http_code}" "$url" || true)"
|
|
if [ "$http_code" != "200" ] && [ "$http_code" != "201" ] && [ "$http_code" != "204" ]; then
|
|
alt_name="${host_name}-$(date -u +'%Y%m%d_%H%M%S').txt"
|
|
alt_url="${PUBLISH_WEBDAV_URL}/${alt_name}"
|
|
http_code="$(curl -sS -u "${PUBLISH_SHARE_TOKEN}:" -T "$tmp_file" -o /dev/null -w "%{http_code}" "$alt_url" || true)"
|
|
if [ "$http_code" != "200" ] && [ "$http_code" != "201" ] && [ "$http_code" != "204" ]; then
|
|
echo "Publish failed with HTTP ${http_code}"
|
|
return 1
|
|
fi
|
|
fi
|
|
elif need_cmd wget; then
|
|
wget --method=PUT --body-file="$tmp_file" --user="$PUBLISH_SHARE_TOKEN" --password="" -qO- "$url" >/dev/null
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
HOST_NAME="$(hostname -s 2>/dev/null || hostname)"
|
|
TMP_FILE="$(mktemp -t devnet-ip.XXXXXX)"
|
|
trap 'rm -f "$TMP_FILE"' EXIT
|
|
|
|
SETTINGS_LISTEN_PORT="$(read_listen_port_from_settings || true)"
|
|
if [[ -n "$SETTINGS_LISTEN_PORT" ]]; then
|
|
LISTEN_PORT="$SETTINGS_LISTEN_PORT"
|
|
else
|
|
PORT_START_NUM="$(echo "$PORT_START" | tr -d ' \n\r')"
|
|
if ! echo "$PORT_START_NUM" | grep -Eq '^[0-9]+$'; then
|
|
PORT_START_NUM="$PORT_START_DEFAULT"
|
|
fi
|
|
PORT_START="$PORT_START_NUM"
|
|
if (( PORT_START < 1024 || PORT_START > 65532 )); then
|
|
PORT_START="$PORT_START_DEFAULT"
|
|
fi
|
|
LISTEN_PORT=$((PORT_START + 1))
|
|
fi
|
|
|
|
PUBLIC_IP="$(get_public_ip || true)"
|
|
if [[ -z "$PUBLIC_IP" ]]; then
|
|
echo "Warning: could not determine public IP to publish."
|
|
exit 0
|
|
fi
|
|
publish_public_ip "$PUBLIC_IP" "$LISTEN_PORT" "$HOST_NAME" "$TMP_FILE"
|
|
echo "Published ${PUBLIC_IP}:${LISTEN_PORT} for ${HOST_NAME} to ${PUBLISH_WEBDAV_URL}/${HOST_NAME}.txt"
|