handle port conflict

This commit is contained in:
shocknet-justin 2025-09-25 11:29:37 -04:00
parent cc15d1072c
commit 7bf03c3ab7
3 changed files with 61 additions and 12 deletions

View file

@ -11,7 +11,7 @@ log() {
echo -e "$(echo "$message" | sed 's/\\e\[[0-9;]*m//g')" >> "$TMP_LOG_FILE" 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" REPO="shocknet/Lightning.Pub"
BRANCH="master" BRANCH="master"
@ -31,6 +31,7 @@ log_error() {
modules=( modules=(
"utils" "utils"
"ports"
"install_lnd" "install_lnd"
"install_nodejs" "install_nodejs"
"install_lightning_pub" "install_lightning_pub"

View file

@ -89,17 +89,33 @@ install_lnd() {
# Create .lnd directory if it doesn't exist # Create .lnd directory if it doesn't exist
mkdir -p $USER_HOME/.lnd mkdir -p $USER_HOME/.lnd
# Check if lnd.conf already exists and avoid overwriting it # Ensure lnd.conf exists.
if [ -f $USER_HOME/.lnd/lnd.conf ]; then touch $USER_HOME/.lnd/lnd.conf
log "${PRIMARY_COLOR}lnd.conf already exists. Skipping creation of new lnd.conf file.${RESET_COLOR}"
else # Check for and add default settings only if the keys are missing.
cat <<EOF > $USER_HOME/.lnd/lnd.conf grep -q "^bitcoin.mainnet=" $USER_HOME/.lnd/lnd.conf || echo "bitcoin.mainnet=true" >> $USER_HOME/.lnd/lnd.conf
bitcoin.mainnet=true grep -q "^bitcoin.node=" $USER_HOME/.lnd/lnd.conf || echo "bitcoin.node=neutrino" >> $USER_HOME/.lnd/lnd.conf
bitcoin.node=neutrino grep -q "^neutrino.addpeer=" $USER_HOME/.lnd/lnd.conf || echo "neutrino.addpeer=neutrino.shock.network" >> $USER_HOME/.lnd/lnd.conf
neutrino.addpeer=neutrino.shock.network 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
fee.url=https://nodes.lightning.computer/fees/v1/btc-fee-estimates.json
EOF chmod 600 $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 fi
log "${SECONDARY_COLOR}LND${RESET_COLOR} installation and configuration completed." log "${SECONDARY_COLOR}LND${RESET_COLOR} installation and configuration completed."

32
scripts/ports.sh Normal file
View file

@ -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
}