#!/bin/sh # DifSync native Linux installer. No npm, Git, compiler or root elevation. set -eu umask 077 origin=https://difsync.com die(){ printf 'DifSync: %s\n' "$*" >&2; exit 1; } code=; enable= while [ "$#" -gt 0 ]; do case "$1" in --pair-code) [ "$#" -ge 2 ] || die "Missing pairing code"; code=$2; shift 2;; --enable-commands) enable=1; shift;; --disable-commands) enable=0; shift;; --help) echo "Usage: install-linux.sh [--pair-code CODE] [--enable-commands] [--disable-commands]"; exit 0;; *) die "Unknown option: $1";; esac done [ "$(uname -s)" = Linux ] || die "This installer is for Linux. Windows users: https://difsync.com/download/windows" case "$(uname -m)" in x86_64|amd64) arch=amd64;; aarch64|arm64) arch=arm64;; *) die "Unsupported CPU. DifSync currently supports Linux x64 and ARM64.";; esac [ -n "${HOME:-}" ] && [ -d "$HOME" ] || die "A writable home directory is required." for tool in mkdir chmod mv rm mktemp sha256sum cat sleep nohup stat id grep rmdir tr; do command -v "$tool" >/dev/null 2>&1 || die "Missing standard Linux utility: $tool"; done download(){ if command -v curl >/dev/null 2>&1; then curl -fLsS --connect-timeout 15 --max-time 120 --retry 2 "$1" -o "$2" && return; fi if command -v wget >/dev/null 2>&1; then for attempt in 1 2 3; do wget -q -T 30 -O "$2" "$1" && return; done fi return 1 } state="$HOME/.difsync-agent"; bin="$HOME/.local/bin/difsync-agent"; unit="$HOME/.config/systemd/user/difsync-agent.service" [ ! -L "$state" ] || die "Refusing a symlinked agent state directory." for f in config.json heartbeat.txt agent.log installer-settings pairing.env; do [ ! -L "$state/$f" ] || die "Refusing symlinked agent state file: $f"; done mkdir -p "$state" "$HOME/.local/bin"; chmod 700 "$state" lock="$state/install.lock"; mkdir "$lock" 2>/dev/null || die "Another installation is running. If it crashed, remove the empty $lock directory and retry." tmp=; pid=; service=0; success=0; wrotePending=0; pending="$state/pairing.env" cleanup(){ if [ "$success" = 0 ]; then if [ "$service" = 1 ]; then systemctl --user stop difsync-agent.service >/dev/null 2>&1 || :; fi if [ -n "$pid" ]; then kill "$pid" 2>/dev/null || :; fi fi [ "$wrotePending" = 0 ] || rm -f "$pending" [ -z "$tmp" ] || rm -rf "$tmp" rmdir "$lock" 2>/dev/null || : } trap cleanup EXIT trap 'exit 130' INT TERM HUP # Never replace a running native agent or stop somebody else's process. if command -v systemctl >/dev/null 2>&1 && systemctl --user is-active --quiet difsync-agent.service 2>/dev/null; then die "DifSync is already running. Check your workspace. Stop the user service before reinstalling." fi for proc in /proc/[0-9]*/comm; do [ -r "$proc" ] || continue IFS= read -r name < "$proc" 2>/dev/null || continue case "$name" in difsync-agent|difsync-agent-l) ;; node|nodejs) # Only match the old repository agent entrypoint, never arbitrary Node apps. if ! tr '\000' '\n' < "${proc%/comm}/cmdline" 2>/dev/null | grep -Eq '(^|/)agents/device-agent/src/index\.js$'; then continue; fi;; *) continue;; esac owner=$(stat -c %u "${proc%/comm}" 2>/dev/null || echo unknown) [ "$owner" != "$(id -u)" ] || die "An existing DifSync agent is running for this user. Stop it before reinstalling to avoid duplicate agents." done if [ -L "$unit" ]; then die "Refusing to replace a symlinked user service."; fi if [ -f "$unit" ] && ! grep -qx '# Managed by DifSync Linux installer' "$unit"; then die "Existing custom service found. It will not be overwritten."; fi if [ -s "$state/config.json" ]; then [ -z "$code" ] || echo "Existing pairing found; preserving it rather than replacing the account binding." code= else if [ -z "$code" ]; then printf 'Create a pairing code at https://difsync.com/devices (Add device → Linux).\n' if [ -t 0 ] || [ -t 1 ]; then printf 'Pairing code: ' >&2; IFS= read -r code &2; fi tmp=$(mktemp -d "$state/install.XXXXXXXX") printf 'Downloading native Linux %s agent…\n' "$arch" download "$origin/api/client/linux/$arch/sha256" "$tmp/sha256" || die "Could not retrieve the release checksum. Check HTTPS connectivity and CA certificates." digest=$(cat "$tmp/sha256") case "$digest" in *[!0-9a-f]*|'') die "Invalid release checksum.";; esac [ "${#digest}" = 64 ] || die "Invalid release checksum length." download "$origin/download/linux-$arch" "$tmp/agent" || die "Could not download the agent. Check your connection and retry." printf '%s %s\n' "$digest" "$tmp/agent" | sha256sum -c - >/dev/null || die "Download verification failed. No binary was installed." chmod 700 "$tmp/agent" [ ! -L "$bin" ] || die "Refusing to replace a symlinked agent." mv -f "$tmp/agent" "$bin" before=$(cat "$state/heartbeat.txt" 2>/dev/null || :) # No secrets are written to the service definition. The short-lived pairing # environment is private and removed after the first verified heartbeat. [ ! -L "$pending" ] || die "Refusing symlinked pairing file." printf 'DIFSYNC_PAIR_CODE=%s\n' "$code" > "$pending" wrotePending=1 chmod 600 "$pending" if command -v systemctl >/dev/null 2>&1 && systemctl --user show-environment >/dev/null 2>&1; then mkdir -p "$HOME/.config/systemd/user" cat > "$tmp/unit" <> "$state/agent.log" 2>&1 & pid=$! fi i=0 while [ "$i" -lt 45 ]; do after=$(cat "$state/heartbeat.txt" 2>/dev/null || :) if [ -n "$after" ] && [ "$after" != "$before" ] && [ -s "$state/config.json" ]; then success=1; break fi if [ -n "$pid" ] && ! kill -0 "$pid" 2>/dev/null; then break; fi sleep 1; i=$((i+1)) done [ "$success" = 1 ] || die "No verified heartbeat. Check the pairing code, network and agent log (journalctl --user -u difsync-agent or $state/agent.log). If pairing expired, generate a fresh code." printf 'DIFSYNC_ENABLE_COMMANDS=%s\n' "$enable" > "$state/installer-settings" rm -f "$pending" echo "DifSync installed and connected. Open https://difsync.com/devices" if [ "$service" = 1 ]; then systemctl --user enable difsync-agent.service >/dev/null 2>&1 && echo "Auto-start enabled for your user session." || echo "Connected; could not enable auto-start." echo "Status: systemctl --user status difsync-agent.service" echo "For startup before login, the host administrator may enable user lingering." else echo "Background agent started (PID $pid). This host has no usable user service manager; automatic startup after reboot/logout is not guaranteed." echo "Log: $state/agent.log" fi