diff --git a/scripts/build_appimage.py b/scripts/build_appimage.py index a03823e..811c207 100755 --- a/scripts/build_appimage.py +++ b/scripts/build_appimage.py @@ -187,8 +187,78 @@ APPDIR="$(dirname "$(readlink -f "$0")")" DATA_DIR="${MOON_I2P_DATA_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/moon-i2p-browser}" PROFILE_DIR="${MOON_I2P_PROFILE_DIR:-$DATA_DIR/profile}" DOWNLOAD_DIR="${MOON_I2P_DOWNLOAD_DIR:-$HOME/Downloads/moon-i2p}" +LOG_FILE="$DATA_DIR/router.log" +LOCK_FILE="$DATA_DIR/instance.lock" -mkdir -p "$DATA_DIR" "$PROFILE_DIR/extensions" "$DOWNLOAD_DIR" +mkdir -p "$DATA_DIR" + +PYTHON_BIN="$(command -v python3 || true)" + +abort_already_running() { + local detail="$1" + local msg="Moon I2P Browser is already running ($detail). Close the existing window before relaunching." + echo "$msg" >&2 + if [[ "${MOON_I2P_NO_NOTIFY:-0}" != "1" ]] && [[ -n "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]] && command -v notify-send >/dev/null 2>&1; then + notify-send -a "Moon I2P Browser" \ + "Moon I2P Browser is already running" \ + "Close the existing Moon I2P Browser window before relaunching ($detail)." \ + >/dev/null 2>&1 || true + fi + exit 1 +} + +# Guard 1: Exclusive AppRun instance lock (append mode so held lockfiles are never truncated). +exec 9>>"$LOCK_FILE" +if command -v flock >/dev/null 2>&1; then + if ! flock -n 9; then + EXISTING_PID="$(tr -dc '0-9' < "$LOCK_FILE" 2>/dev/null || true)" + if [[ -n "$EXISTING_PID" ]]; then + abort_already_running "PID $EXISTING_PID" + fi + abort_already_running "instance lock held" + fi +fi + +# Guard 2: Detect an already-running Firefox on this profile (e.g. legacy AppImage without instance.lock). +if [[ -n "$PYTHON_BIN" && -f "$PROFILE_DIR/.parentlock" ]]; then + EXISTING_FF_PID="$("$PYTHON_BIN" -c ' +import fcntl, os, struct, sys +p = sys.argv[1] +try: + fd = os.open(p, os.O_RDONLY) +except OSError: + sys.exit(0) +try: + fl = struct.pack("hhqqi4x", fcntl.F_WRLCK, os.SEEK_SET, 0, 0, 0) + res = fcntl.fcntl(fd, fcntl.F_GETLK, fl) + l_type, _, _, _, l_pid = struct.unpack("hhqqi4x", res) + if l_type != fcntl.F_UNLCK and l_pid > 0: + print(l_pid) +except OSError: + pass +finally: + os.close(fd) +' "$PROFILE_DIR/.parentlock" 2>/dev/null || true)" + if [[ -n "$EXISTING_FF_PID" ]]; then + abort_already_running "Firefox PID $EXISTING_FF_PID" + fi +fi + +if [[ -L "$PROFILE_DIR/lock" ]]; then + LOCK_TARGET="$(readlink "$PROFILE_DIR/lock" 2>/dev/null || true)" + if [[ "$LOCK_TARGET" == *":+"* ]]; then + LOCK_PID="${LOCK_TARGET##*:+}" + if [[ "$LOCK_PID" =~ ^[0-9]+$ ]] && kill -0 "$LOCK_PID" 2>/dev/null; then + if ! LOCK_CMDLINE="$(tr '\0' ' ' < "/proc/$LOCK_PID/cmdline" 2>/dev/null)" || printf '%s' "$LOCK_CMDLINE" | grep -qi "firefox"; then + abort_already_running "Firefox PID $LOCK_PID" + fi + fi + fi +fi + +printf '%s\n' "$$" > "$LOCK_FILE" + +mkdir -p "$PROFILE_DIR/extensions" "$DOWNLOAD_DIR" # Ensure the bundled extension XPI is installed in the browser profile. cp -f "$APPDIR/usr/share/moon-i2p/moon-i2p-torrent@local.xpi" \ @@ -215,7 +285,6 @@ EOF done fi -PYTHON_BIN="$(command -v python3 || true)" if [[ -n "$PYTHON_BIN" ]]; then read -r PROXY_PORT API_PORT SAM_PORT < <("$PYTHON_BIN" -c ' import socket @@ -273,10 +342,13 @@ EOF ROUTER_BIN="$APPDIR/usr/bin/moon-i2p-router" ROUTER_PID="" FF_PID="" +ACTIVE_PORTS_WRITTEN=0 cleanup() { local rc=$? - rm -f "$DATA_DIR/active_ports.json" + if [[ "$ACTIVE_PORTS_WRITTEN" == "1" ]]; then + rm -f "$DATA_DIR/active_ports.json" + fi if [[ -n "$FF_PID" ]] && kill -0 "$FF_PID" 2>/dev/null; then kill -TERM "$FF_PID" 2>/dev/null || true wait "$FF_PID" 2>/dev/null || true @@ -296,6 +368,18 @@ cleanup() { } trap cleanup EXIT INT TERM +LOG_MAX_BYTES="${MOON_I2P_LOG_MAX_BYTES:-5242880}" +[[ "$LOG_MAX_BYTES" =~ ^[0-9]+$ ]] || LOG_MAX_BYTES=5242880 +if [[ -f "$LOG_FILE" ]]; then + LOG_SIZE="$(stat -c%s "$LOG_FILE" 2>/dev/null || wc -c < "$LOG_FILE")" + LOG_SIZE="${LOG_SIZE//[[:space:]]/}" + if [[ "$LOG_SIZE" =~ ^[0-9]+$ ]] && (( LOG_SIZE >= LOG_MAX_BYTES )); then + mv -f "$LOG_FILE" "$LOG_FILE.1" + fi +fi +printf '\n=== Moon I2P Browser start %s appdir=%s proxy=%s api=%s sam=%s ===\n' \ + "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$APPDIR" "$PROXY_PORT" "$API_PORT" "$SAM_PORT" >> "$LOG_FILE" + "$ROUTER_BIN" \ --public \ --debug \ @@ -303,12 +387,15 @@ trap cleanup EXIT INT TERM --http-proxy "127.0.0.1:$PROXY_PORT" \ --torrent-api "127.0.0.1:$API_PORT" \ --download-dir "$DOWNLOAD_DIR" \ - >> "$DATA_DIR/router.log" 2>&1 & + 9>&- >> "$LOG_FILE" 2>&1 & ROUTER_PID=$! -cat > "$DATA_DIR/active_ports.json" < "$ACTIVE_PORTS_TMP" <&- & FF_PID=$! wait "$FF_PID" '''