diff --git a/scripts/install.sh b/scripts/install.sh index 9ee57bb9..d08e88f2 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -11,7 +11,7 @@ log() { echo -e "$(echo "$message" | sed 's/\\e\[[0-9;]*m//g')" >> "$TMP_LOG_FILE" } -SCRIPT_VERSION="0.2.0" +SCRIPT_VERSION="0.2.1" REPO="shocknet/Lightning.Pub" BRANCH="master" @@ -31,10 +31,7 @@ log_error() { modules=( "utils" - "check_homebrew" # NOTE: Used for macOS, which is untested/unsupported - "install_rsync_mac" # NOTE: Used for macOS, which is untested/unsupported - "create_launchd_plist" # NOTE: Used for macOS, which is untested/unsupported - "start_services_mac" # NOTE: Used for macOS, which is untested/unsupported + "ports" "install_lnd" "install_nodejs" "install_lightning_pub" @@ -94,8 +91,7 @@ log "Detected OS: $OS" log "Detected ARCH: $ARCH" if [ "$OS" = "Mac" ]; then - log "Handling macOS specific setup" - handle_macos || log_error "macOS setup failed" 1 + log_error "macOS is not currently supported by this install script. Please use a Linux-based system." 1 else # Explicit kickoff log for LND so the flow is clear in the install log log "${PRIMARY_COLOR}Installing${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR}..." diff --git a/scripts/install_lnd.sh b/scripts/install_lnd.sh index 34aa7c78..81061d07 100755 --- a/scripts/install_lnd.sh +++ b/scripts/install_lnd.sh @@ -89,17 +89,33 @@ install_lnd() { # Create .lnd directory if it doesn't exist mkdir -p $USER_HOME/.lnd - # Check if lnd.conf already exists and avoid overwriting it - if [ -f $USER_HOME/.lnd/lnd.conf ]; then - log "${PRIMARY_COLOR}lnd.conf already exists. Skipping creation of new lnd.conf file.${RESET_COLOR}" - else - cat < $USER_HOME/.lnd/lnd.conf -bitcoin.mainnet=true -bitcoin.node=neutrino -neutrino.addpeer=neutrino.shock.network -fee.url=https://nodes.lightning.computer/fees/v1/btc-fee-estimates.json -EOF - chmod 600 $USER_HOME/.lnd/lnd.conf + # Ensure lnd.conf exists. + touch $USER_HOME/.lnd/lnd.conf + + # Check for and add default settings only if the keys are missing. + grep -q "^bitcoin.mainnet=" $USER_HOME/.lnd/lnd.conf || echo "bitcoin.mainnet=true" >> $USER_HOME/.lnd/lnd.conf + grep -q "^bitcoin.node=" $USER_HOME/.lnd/lnd.conf || echo "bitcoin.node=neutrino" >> $USER_HOME/.lnd/lnd.conf + grep -q "^neutrino.addpeer=" $USER_HOME/.lnd/lnd.conf || echo "neutrino.addpeer=neutrino.shock.network" >> $USER_HOME/.lnd/lnd.conf + grep -q "^fee.url=" $USER_HOME/.lnd/lnd.conf || echo "fee.url=https://nodes.lightning.computer/fees/v1/btc-fee-estimates.json" >> $USER_HOME/.lnd/lnd.conf + + chmod 600 $USER_HOME/.lnd/lnd.conf + + # Port conflict resolution. + local lnd_port=9735 + if ! is_port_available $lnd_port; then + # The port is occupied. We should intervene if our service is either in a failed state + # or not active at all (which covers fresh installs and failure loops). + if systemctl --user -q is-failed lnd.service 2>/dev/null || ! systemctl --user -q is-active lnd.service 2>/dev/null; then + log "Port $lnd_port is occupied and LND service is not healthy. Attempting to resolve by finding a new port." + lnd_port_new=$(find_available_port $lnd_port) + log "Configuring LND to use new port $lnd_port_new." + + sed -i '/^listen=/d' $USER_HOME/.lnd/lnd.conf + echo "listen=0.0.0.0:$lnd_port_new" >> $USER_HOME/.lnd/lnd.conf + log "LND configuration updated. The service will be restarted by the installer." + else + log "Port $lnd_port is in use by a healthy LND service (assumed to be our own). No changes will be made." + fi fi log "${SECONDARY_COLOR}LND${RESET_COLOR} installation and configuration completed." diff --git a/scripts/ports.sh b/scripts/ports.sh new file mode 100644 index 00000000..89ca1b4b --- /dev/null +++ b/scripts/ports.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# --- Port Management --- + +# Finds an available TCP port, starting from a given base port. +# If the base port is in use, it increments until a free port is found. +# +# @param base_port The starting port number. +# @return The first available port number. +find_available_port() { + local port=$1 + while ! is_port_available "$port"; do + log "Port $port is in use. Checking next port..." >&2 + port=$((port + 1)) + done + echo "$port" +} + +# Checks if a given TCP port is available. +# +# @param port The port number to check. +# @return 0 if the port is available, 1 otherwise. +is_port_available() { + local port=$1 + ! lsof -iTCP:"$port" -sTCP:LISTEN -P -t >/dev/null 2>&1 +}