Skip to content

Commit 3637436

Browse files
Add port conflict checks for HTTPS and VPN in installer script
1 parent 50957c3 commit 3637436

1 file changed

Lines changed: 85 additions & 21 deletions

File tree

scripts/dappnode_install.sh

Lines changed: 85 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,37 @@ DNCORE_COMPOSE_ARGS=()
3737
log() {
3838
# LOGFILE is created after dir bootstrap; until then we just print to stdout.
3939
if [[ -n "${LOGFILE:-}" && -d "${LOGS_DIR:-}" ]]; then
40-
printf '%s\n' "$*" | tee -a "$LOGFILE"
40+
printf '%s\n' "[INFO] $*" | tee -a "$LOGFILE"
4141
else
42-
printf '%s\n' "$*"
42+
printf '%s\n' "[INFO] $*"
4343
fi
4444
}
4545

4646
warn() {
47-
log "[WARN] $*"
47+
# LOGFILE is created after dir bootstrap; until then we just print to stdout.
48+
if [[ -n "${LOGFILE:-}" && -d "${LOGS_DIR:-}" ]]; then
49+
printf '%s\n' "[WARN] $*" | tee -a "$LOGFILE"
50+
else
51+
printf '%s\n' "[WARN] $*"
52+
fi
53+
}
54+
55+
error() {
56+
# LOGFILE is created after dir bootstrap; until then we just print to stdout.
57+
if [[ -n "${LOGFILE:-}" && -d "${LOGS_DIR:-}" ]]; then
58+
printf '%s\n' "[ERROR] $*" | tee -a "$LOGFILE"
59+
else
60+
printf '%s\n' "[ERROR] $*"
61+
fi
4862
}
4963

5064
die() {
51-
log "[ERROR] $*"
65+
# LOGFILE is created after dir bootstrap; until then we just print to stdout.
66+
if [[ -n "${LOGFILE:-}" && -d "${LOGS_DIR:-}" ]]; then
67+
printf '%s\n' "[ERROR] $*" | tee -a "$LOGFILE"
68+
else
69+
printf '%s\n' "[ERROR] $*"
70+
fi
5271
exit 1
5372
}
5473

@@ -523,35 +542,79 @@ bootstrap_filesystem() {
523542
touch "${LOGFILE}" || true
524543
}
525544

526-
# Check if port 80 is in use (necessary for HTTPS)
527-
# Returns IS_PORT_USED=true only if port 80 or 443 is used by something OTHER than our HTTPS container
528-
is_port_used() {
529-
# Check if port 80 or 443 is in use at all
530-
local port80_used port443_used
531-
if command -v lsof >/dev/null 2>&1; then
532-
lsof -i -P -n | grep ":80 (LISTEN)" &>/dev/null && port80_used=true || port80_used=false
533-
lsof -i -P -n | grep ":443 (LISTEN)" &>/dev/null && port443_used=true || port443_used=false
545+
# Generic helper: returns 0 if a process is bound to the given port, 1 if not.
546+
# Usage: is_port_listening <port> [tcp|udp]
547+
# tcp (default): matches TCP sockets in LISTEN state
548+
# udp: matches any process bound to the UDP port
549+
is_port_listening() {
550+
local port="$1"
551+
local proto="${2:-tcp}"
552+
if [[ "$proto" == "udp" ]]; then
553+
lsof -i "udp:${port}" -P -n 2>/dev/null | grep -q .
534554
else
555+
lsof -i "tcp:${port}" -P -n 2>/dev/null | grep -q "(LISTEN)"
556+
fi
557+
}
558+
559+
# Check if ports 80/443 are occupied by something other than our own HTTPS container.
560+
# Sets HTTPS_PORTS_BLOCKED=true/false.
561+
check_https_ports_conflict() {
562+
if ! command -v lsof >/dev/null 2>&1; then
535563
warn "lsof not found; assuming ports 80/443 are in use (HTTPS will be skipped)"
536-
IS_PORT_USED=true
564+
HTTPS_PORTS_BLOCKED=true
537565
return
538566
fi
539567

540-
if [ "$port80_used" = false ] && [ "$port443_used" = false ]; then
541-
IS_PORT_USED=false
568+
if ! is_port_listening 80 && ! is_port_listening 443; then
569+
HTTPS_PORTS_BLOCKED=false
542570
return
543571
fi
544572

545-
# If either port is in use, check if it's our HTTPS container
573+
# Port 80 or 443 is in use; check if it's our own HTTPS container
546574
if docker ps --format '{{.Names}}' 2>/dev/null | grep -q "^DAppNodeCore-https.dnp.dappnode.eth$"; then
547-
# Port 80 or 443 is used by our HTTPS container, so we consider it "not used" for package determination
548-
IS_PORT_USED=false
575+
# Our own HTTPS container already holds the port — not a conflict
576+
HTTPS_PORTS_BLOCKED=false
549577
else
550578
# Port 80 or 443 is used by something else
551-
IS_PORT_USED=true
579+
HTTPS_PORTS_BLOCKED=true
552580
fi
553581
}
554582

583+
# Check that ports required by VPN/Wireguard are not already in use by another process.
584+
# Must be called after PKGS is populated. Exits with a helpful error on conflict.
585+
check_vpn_ports_conflict() {
586+
if ! command -v lsof >/dev/null 2>&1; then
587+
return # cannot check; proceed and let the container report a bind error
588+
fi
589+
590+
local pkg
591+
for pkg in "${PKGS[@]}"; do
592+
case "$pkg" in
593+
WIREGUARD)
594+
if is_port_listening 51820 udp; then
595+
error "Port 51820/UDP is already in use on this host."
596+
error "This port is required by the Wireguard package and must be free before installing."
597+
error "Free up port 51820 and re-run the installer, or — if you do not need VPN"
598+
error "connectivity — consider using --minimal instead (advanced users only)."
599+
exit 1
600+
fi
601+
;;
602+
VPN)
603+
local vpn_blocked=()
604+
is_port_listening 1194 udp && vpn_blocked+=(1194/UDP)
605+
is_port_listening 8092 tcp && vpn_blocked+=(8092/TCP)
606+
if [[ ${#vpn_blocked[@]} -gt 0 ]]; then
607+
error "Port(s) ${vpn_blocked[*]} are already in use on this host."
608+
error "These ports are required by the OpenVPN package and must be free before installing."
609+
error "Free up the port(s) and re-run the installer, or — if you do not need VPN"
610+
error "connectivity — consider using --minimal instead (advanced users only)."
611+
exit 1
612+
fi
613+
;;
614+
esac
615+
done
616+
}
617+
555618
# Determine packages to be installed
556619
determine_packages() {
557620
# Explicit package list override from flag/env always has top priority.
@@ -649,8 +712,8 @@ determine_packages() {
649712

650713
# Default mode (no --packages/--minimal/--lite): install full package set.
651714
# HTTPS is included only when ports 80/443 are available.
652-
is_port_used
653-
if [ "$IS_PORT_USED" == "true" ]; then
715+
check_https_ports_conflict
716+
if [ "$HTTPS_PORTS_BLOCKED" == "true" ]; then
654717
PKGS=(BIND IPFS VPN WIREGUARD DAPPMANAGER WIFI NOTIFICATIONS PREMIUM)
655718
else
656719
PKGS=(HTTPS BIND IPFS VPN WIREGUARD DAPPMANAGER WIFI NOTIFICATIONS PREMIUM)
@@ -710,6 +773,7 @@ resolve_packages() {
710773
# If such variable with 'dev:'' suffix is used, then the component is built from specified branch or commit.
711774
# you can also specify an IPFS version like /ipfs/<cid>:<version> (the exact version is required).
712775
determine_packages
776+
check_vpn_ports_conflict
713777
for comp in "${PKGS[@]}"; do
714778
ver="${comp}_VERSION"
715779
log "Processing $comp: ${!ver-}"

0 commit comments

Comments
 (0)