diff --git a/scripts/install.sh b/scripts/install.sh index d67ffb90..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,6 +31,7 @@ log_error() { modules=( "utils" + "ports" "install_lnd" "install_nodejs" "install_lightning_pub" diff --git a/scripts/install_lnd.sh b/scripts/install_lnd.sh index 34aa7c78..a8a12f2e 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 for fresh installs + if [ $lnd_status -eq 0 ]; then + local lnd_port=9735 + if ! is_port_available $lnd_port; then + if ! systemctl --user -q is-active lnd.service >/dev/null 2>&1; then + log "Port $lnd_port is in use by another process. Finding an alternative port." + lnd_port_new=$(find_available_port $lnd_port) + log "Configuring LND to use port $lnd_port_new." + + # Remove any existing listen entry and add the new one. + sed -i '/^listen=/d' $USER_HOME/.lnd/lnd.conf + echo "listen=:$lnd_port_new" >> $USER_HOME/.lnd/lnd.conf + else + log "Port $lnd_port is in use, but it seems to be by our own lnd service. No changes made." + fi + 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..223d21e0 --- /dev/null +++ b/scripts/ports.sh @@ -0,0 +1,32 @@ +#!/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..." + 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 + if [[ "$OS" == "Mac" ]]; then + # Use lsof on macOS + ! lsof -iTCP:"$port" -sTCP:LISTEN -P -t >/dev/null + else + # Use ss on Linux + ! ss -lntu | awk '{print $5}' | grep -q ":$port$" + fi +}