#!/usr/bin/env bash # Resberry VPN installer. # # curl -fsSL https://raw.githubusercontent.com/rohamrajabzadeh/raspberry-vpn/main/install.sh | sudo bash # # Turns a Debian/Ubuntu/Raspberry Pi OS machine with a wired uplink and a WiFi # card into a VPN gateway: it broadcasts an access point and sends everything # that joins it through a VPN tunnel, managed from a web panel. # # Run it again at any time to upgrade; an existing installation keeps its # settings, servers and password. See `--help` for options. # # The whole script is wrapped in main() and only called on the last line, so # a download that is cut off half-way can never run a partial script. set -Eeuo pipefail REPO="rohamrajabzadeh/raspberry-vpn" # Engines are downloaded from their own projects' releases and verified # against these pinned SHA-256 values before anything is installed. SINGBOX_VERSION="1.12.9" XRAY_VERSION="24.12.31" SINGBOX_SHA256_arm64="0d571bf961c651cc5a4eaffe9715d7759edb484b90473f3fa25aaab87fa11961" SINGBOX_SHA256_amd64="519bc521e6b25f779b37738c5fca0fa3f68175b3d8e434fcacd8ea42da9e70ef" XRAY_SHA256_arm64="746333b1bf9ad8b860fe77d9f5ba94588449923519de38947eaca4112527f7b8" XRAY_SHA256_amd64="445353d406081585a5e2958ed05372e3bf90ec6e4301d5ae7177231bb554077f" CONFIG=/etc/resberry/config.yaml DATA_DIR=/var/lib/resberry STATE="$DATA_DIR/install-state" LOG=/var/log/resberry-install.log BIN_DIR=/usr/local/bin SBIN_DIR=/usr/local/sbin # Settings (flags or environment). Empty means "ask, or pick a default". RELEASE_TAG="${RESBERRY_VERSION:-}" ADMIN_USER="${RESBERRY_ADMIN_USER:-}" ADMIN_PASS="${RESBERRY_ADMIN_PASS:-}" WIFI_SSID="${RESBERRY_WIFI_SSID:-}" WIFI_PASS="${RESBERRY_WIFI_PASS:-}" COUNTRY="${RESBERRY_COUNTRY:-}" BAND="${RESBERRY_BAND:-2.4}" CHANNEL="${RESBERRY_CHANNEL:-}" PANEL_PORT="${RESBERRY_PORT:-4000}" AP_IFACE="${RESBERRY_AP_INTERFACE:-}" AP_ADDRESS="${RESBERRY_AP_ADDRESS:-}" LOCAL_DIR="${RESBERRY_LOCAL_DIR:-}" TLS=0 ASSUME_YES=0 RECONFIGURE=0 GENERATED_ADMIN_PASS=0 GENERATED_WIFI_PASS=0 MODE=install TMP="" usage() { cat <<'EOF' Resberry VPN installer Usage: curl -fsSL https://raw.githubusercontent.com/rohamrajabzadeh/raspberry-vpn/main/install.sh | sudo bash curl -fsSL .../install.sh | sudo bash -s -- [options] sudo ./install.sh [options] With no options it asks a few questions (press Enter to accept a default) and generates strong passwords for you. Options: --yes Don't ask anything; use defaults and generate passwords --admin-user NAME Panel username (default: admin) --ssid NAME WiFi network name (default: ResberryVPN) --country CC Two-letter country code for the WiFi radio (default: US) --band 2.4|5 WiFi band (default: 2.4) --channel N WiFi channel (default: 6 on 2.4 GHz, 36 on 5 GHz) --port N Panel port (default: 4000) --tls Serve the panel over HTTPS with a self-signed certificate --ap-interface IF Wireless interface to broadcast on (default: detected) --ap-address A.B.C.D/24 Gateway address and subnet for WiFi clients (default: the first free one of 192.168.50.1/24, ...) --version TAG Install a specific release (default: latest) --local-dir DIR Use resberryd, sing-box and xray from DIR instead of downloading them (offline installs, development) --reconfigure Rewrite the config of an existing installation (new secrets and credentials; servers are kept) -h, --help Show this help Passwords are never taken from the command line. To choose your own, set RESBERRY_ADMIN_PASS and RESBERRY_WIFI_PASS in the environment, or type them when asked. Every option above can also be given as an environment variable (RESBERRY_WIFI_SSID, RESBERRY_COUNTRY, ...). To remove Resberry VPN: sudo resberry-uninstall (add --purge to delete data) EOF } # --------------------------------------------------------------------------- output if [ -t 1 ]; then C_BLUE=$'\033[36m'; C_GREEN=$'\033[32m'; C_RED=$'\033[31m'; C_YELLOW=$'\033[33m'; C_BOLD=$'\033[1m'; C_OFF=$'\033[0m' else C_BLUE=""; C_GREEN=""; C_RED=""; C_YELLOW=""; C_BOLD=""; C_OFF="" fi say() { printf '%s==>%s %s\n' "$C_BLUE" "$C_OFF" "$*"; } ok() { printf '%s ok %s %s\n' "$C_GREEN" "$C_OFF" "$*"; } warn() { printf '%swarn%s %s\n' "$C_YELLOW" "$C_OFF" "$*" >&2; } die() { printf '%serror%s %s\n' "$C_RED" "$C_OFF" "$*" >&2; exit 1; } log() { printf '%s %s\n' "$(date '+%F %T')" "$*" >>"$LOG" 2>/dev/null || true; } cleanup() { [ -n "$TMP" ] && [ -d "$TMP" ] && rm -rf -- "$TMP"; return 0; } on_error() { local code=$? line=$1 printf '\n%serror%s the installer stopped at line %s (exit %s).\n' "$C_RED" "$C_OFF" "$line" "$code" >&2 if [ "$MODE" = install ] && [ -f "$STATE" ]; then printf ' The system is partly configured. Run it again to continue, or undo with:\n sudo resberry-uninstall\n' >&2 fi printf ' Details: %s\n' "$LOG" >&2 exit "$code" } # --------------------------------------------------------------------------- helpers have() { command -v "$1" >/dev/null 2>&1; } # ask VAR "Question" "default" — reads from the terminal even when the script # itself arrives on stdin (curl | bash). ask() { local var=$1 text=$2 def=${3:-} reply="" if [ "$ASSUME_YES" = 1 ] || [ ! -r /dev/tty ]; then printf -v "$var" '%s' "$def"; return 0 fi if [ -n "$def" ]; then read -r -p "$text [$def]: " reply /dev/tty printf -v "$var" '%s' "$reply" } # random_password LENGTH — letters and digits only, with the look-alike # characters removed, so it can be typed on a phone and is always valid as a # WPA passphrase. random_password() { local len=$1 out="" while [ "${#out}" -lt "$len" ]; do out="$out$(LC_ALL=C tr -dc 'abcdefghijkmnpqrstuvwxyzACDEFGHJKLMNPQRTUVWXY34679' /dev/null | head -c "$len" || true)" done printf '%s' "${out:0:len}" } state_get() { [ -f "$STATE" ] && sed -n "s/^$1=//p" "$STATE" | tail -n 1 || true; } state_set() { mkdir -p "$DATA_DIR"; touch "$STATE"; chmod 600 "$STATE" # Only the first value recorded for a key is kept: it describes the system # as it was before Resberry VPN changed it. if ! grep -q "^$1=" "$STATE"; then printf '%s=%s\n' "$1" "$2" >>"$STATE"; fi } unit_enabled() { systemctl is-enabled "$1" 2>/dev/null || true; } unit_active() { systemctl is-active "$1" 2>/dev/null || true; } fetch() { # fetch URL DEST local url=$1 dest=$2 if have curl; then curl -fsSL --retry 3 --retry-delay 2 --connect-timeout 20 -o "$dest" "$url" else wget -q --tries=3 --timeout=30 -O "$dest" "$url" fi } verify_sha256() { # verify_sha256 FILE EXPECTED LABEL local got got="$(sha256sum "$1" | awk '{print $1}')" [ "$got" = "$2" ] || die "checksum mismatch for $3 expected $2 got $got The download may be corrupted or tampered with; nothing was installed from it." } install_bin() { # install_bin SRC NAME — replace atomically, never leave a half-written binary install -m 0755 "$1" "$BIN_DIR/.$2.new" mv -f "$BIN_DIR/.$2.new" "$BIN_DIR/$2" } # --------------------------------------------------------------------------- preflight parse_args() { while [ $# -gt 0 ]; do case "$1" in --yes|-y) ASSUME_YES=1 ;; --tls) TLS=1 ;; --reconfigure) RECONFIGURE=1 ;; --admin-user) ADMIN_USER=${2:?--admin-user needs a value}; shift ;; --ssid) WIFI_SSID=${2:?--ssid needs a value}; shift ;; --country) COUNTRY=${2:?--country needs a value}; shift ;; --band) BAND=${2:?--band needs a value}; shift ;; --channel) CHANNEL=${2:?--channel needs a value}; shift ;; --port) PANEL_PORT=${2:?--port needs a value}; shift ;; --ap-interface) AP_IFACE=${2:?--ap-interface needs a value}; shift ;; --ap-address) AP_ADDRESS=${2:?--ap-address needs a value}; shift ;; --version) RELEASE_TAG=${2:?--version needs a value}; shift ;; --local-dir) LOCAL_DIR=${2:?--local-dir needs a value}; shift ;; -h|--help) usage; exit 0 ;; --pass|--admin-pass|--wifi-pass|--password) die "passwords are not accepted on the command line (other users can see it there). Set RESBERRY_ADMIN_PASS / RESBERRY_WIFI_PASS in the environment, or let the installer generate them." ;; *) die "unknown option: $1 (see --help)" ;; esac shift done } preflight_system() { [ "$(id -u)" = 0 ] || die "run as root: curl ... | sudo bash" have systemctl || die "this installer needs systemd" have apt-get || die "this installer supports Debian, Ubuntu and Raspberry Pi OS (apt-get not found)" case "$(uname -m)" in aarch64|arm64) ARCH=arm64 ;; x86_64|amd64) ARCH=amd64 ;; armv7l|armv6l) die "32-bit ARM is not supported. Install a 64-bit OS (Raspberry Pi OS 64-bit or Ubuntu Server arm64)." ;; *) die "unsupported CPU architecture: $(uname -m)" ;; esac # A 64-bit kernel with a 32-bit userland cannot run the binaries. if have dpkg; then case "$(dpkg --print-architecture)" in arm64|amd64) ;; *) die "the OS userland is $(dpkg --print-architecture), but a 64-bit system is required" ;; esac fi # os-release is read in a subshell: sourcing it here would overwrite this # script's own variables (it defines VERSION, ID, NAME, ...). OS_NAME="unknown" local os_ids="" if [ -r /etc/os-release ]; then OS_NAME="$(. /etc/os-release 2>/dev/null; printf '%s' "${PRETTY_NAME:-${ID:-unknown}}")" os_ids="$(. /etc/os-release 2>/dev/null; printf ' %s %s ' "${ID:-}" "${ID_LIKE:-}")" case "$os_ids" in *" debian "*|*" ubuntu "*|*" raspbian "*) ;; *) warn "$OS_NAME is not a tested distribution; continuing because apt and systemd are present" ;; esac fi if [ -f "$CONFIG" ] && [ "$RECONFIGURE" = 0 ]; then MODE=upgrade; fi } install_packages() { local pkg missing="" for pkg in hostapd dnsmasq nftables iw rfkill iproute2 ca-certificates curl unzip tar; do dpkg -s "$pkg" >/dev/null 2>&1 || missing="$missing $pkg" done if [ -z "$missing" ]; then # Nothing to install: an upgrade must not depend on the package mirrors. state_set hostapd_enabled "$(unit_enabled hostapd)" state_set dnsmasq_enabled "$(unit_enabled dnsmasq)" state_set dnsmasq_active "$(unit_active dnsmasq)" ok "required packages are already installed" return 0 fi say "Installing packages:$missing" export DEBIAN_FRONTEND=noninteractive local opts=(-o DPkg::Lock::Timeout=180 -o Dpkg::Options::=--force-confold -y -q) local tries=0 until apt-get "${opts[@]}" update >>"$LOG" 2>&1; do tries=$((tries + 1)); [ "$tries" -ge 3 ] && die "apt-get update failed (see $LOG). Check the internet connection and the clock (date)." sleep 5 done # Installing hostapd/dnsmasq starts them with distro defaults; note their # prior state first so uninstall can put things back. state_set hostapd_was_installed "$(dpkg -s hostapd >/dev/null 2>&1 && echo yes || echo no)" state_set dnsmasq_was_installed "$(dpkg -s dnsmasq >/dev/null 2>&1 && echo yes || echo no)" state_set hostapd_enabled "$(unit_enabled hostapd)" state_set dnsmasq_enabled "$(unit_enabled dnsmasq)" state_set dnsmasq_active "$(unit_active dnsmasq)" apt-get "${opts[@]}" install hostapd dnsmasq nftables iw rfkill iproute2 ca-certificates curl unzip tar >>"$LOG" 2>&1 \ || die "package installation failed (see $LOG)" ok "packages installed" } # iface_supports_ap IFACE — true if the interface's radio lists AP mode. iface_supports_ap() { local phy phy="$(cat "/sys/class/net/$1/phy80211/name" 2>/dev/null || true)" [ -n "$phy" ] || return 1 # Captured first, then matched: with pipefail, a reader that stops early # (grep -q) can make the whole pipeline look like it failed. local modes modes="$(iw phy "$phy" info 2>/dev/null | awk '/Supported interface modes:/{f=1;next} f&&/^\t\t \*/{print $2;next} f{f=0}' || true)" grep -qx 'AP' <<<"$modes" } detect_network() { WAN_IFACE="$(ip -4 route show default 2>/dev/null | awk '!found{for(i=1;i<=NF;i++) if($i=="dev"){print $(i+1); found=1}}' || true)" [ -n "$WAN_IFACE" ] || die "no default route found. Connect the wired uplink (Ethernet) first: the gateway needs internet access to install and to work." if [ "$MODE" = upgrade ]; then # Keep whatever the existing installation uses. [ -n "$AP_IFACE" ] || AP_IFACE="$(sed -n 's/^[[:space:]]*ap_interface:[[:space:]]*//p' "$CONFIG" | tr -d "\"'" | head -n 1)" [ -n "$AP_ADDRESS" ] || AP_ADDRESS="$(sed -n 's/^[[:space:]]*ap_address:[[:space:]]*//p' "$CONFIG" | tr -d "\"'" | head -n 1)" AP_IFACE="${AP_IFACE:-wlan0}" AP_ADDRESS="${AP_ADDRESS:-192.168.50.1/24}" fi if [ -z "$AP_IFACE" ]; then local i for i in /sys/class/net/*; do i="${i##*/}" [ -d "/sys/class/net/$i/wireless" ] || [ -e "/sys/class/net/$i/phy80211" ] || continue if iface_supports_ap "$i"; then AP_IFACE="$i"; break; fi done [ -n "$AP_IFACE" ] || die "no WiFi interface that supports access-point mode was found (checked with 'iw phy'). On a Raspberry Pi make sure WiFi is not disabled (rfkill list) and the country is set." else [[ "$AP_IFACE" =~ ^[a-zA-Z0-9_.-]{1,15}$ ]] || die "--ap-interface '$AP_IFACE' is not a valid interface name" [ -d "/sys/class/net/$AP_IFACE" ] || die "interface $AP_IFACE does not exist" iface_supports_ap "$AP_IFACE" || die "interface $AP_IFACE does not support access-point mode" fi [ "$AP_IFACE" != "$WAN_IFACE" ] || die "this machine reaches the internet over WiFi ($WAN_IFACE), and that same radio would have to become the access point. Connect it to your router by Ethernet first, then run the installer again." # Refuse to take over the interface the admin is logged in through. if [ -n "${SSH_CONNECTION:-}" ]; then local ssh_ip ssh_if ssh_ip="$(printf '%s' "$SSH_CONNECTION" | awk '{print $3}')" ssh_if="$(ip -o -4 addr show 2>/dev/null | awk -v ip="$ssh_ip" '{split($4,a,"/"); if(a[1]==ip && !f){print $2; f=1}}' || true)" if [ "$ssh_if" = "$AP_IFACE" ] && [ "$MODE" = install ]; then die "your SSH session arrives over $AP_IFACE, which is about to become the access point. Reconnect over Ethernet and run the installer again." fi fi } # subnet_in_use A.B.C — true if any route or address outside the AP interface # already covers A.B.C.0/24 (the upstream LAN, a Docker bridge, a VPN...). subnet_in_use() { local prefix=$1 addrs routes addrs="$(ip -4 -o addr show 2>/dev/null | awk -v ap="$AP_IFACE" '$2!=ap{print $4}' || true)" routes="$(ip -4 route show 2>/dev/null | awk -v ap="$AP_IFACE" '{for(i=1;i<=NF;i++) if($i=="dev" && $(i+1)==ap) next} {print $1}' || true)" grep -q "^$prefix\." <<<"$addrs"$'\n'"$routes" } choose_subnet() { if [ -n "$AP_ADDRESS" ]; then [[ "$AP_ADDRESS" =~ ^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\.([0-9]{1,3})/24$ ]] \ || die "--ap-address must look like 192.168.50.1/24 (a /24 network)" AP_PREFIX="${BASH_REMATCH[1]}" local host="${BASH_REMATCH[2]}" [ "$host" -ge 1 ] && [ "$host" -le 9 ] || die "--ap-address: the gateway's last number must be 1 to 9 (10-250 are handed out to clients)" if [ "$MODE" = install ] && subnet_in_use "$AP_PREFIX"; then die "the network $AP_PREFIX.0/24 is already in use on this machine (probably your router's LAN). Choose another with --ap-address, e.g. --ap-address 10.77.0.1/24" fi else local cand for cand in 192.168.50 192.168.150 10.77.0 10.88.0 172.30.50; do if ! subnet_in_use "$cand"; then AP_PREFIX="$cand"; break; fi done [ -n "${AP_PREFIX:-}" ] || die "could not find a free private network for the WiFi clients; pass one with --ap-address" AP_ADDRESS="$AP_PREFIX.1/24" fi AP_IP="${AP_ADDRESS%/*}" } collect_settings() { [ "$MODE" = upgrade ] && return 0 ask ADMIN_USER "Panel username" "${ADMIN_USER:-admin}" [[ "$ADMIN_USER" =~ ^[A-Za-z0-9._-]{1,32}$ ]] || die "the username may contain letters, digits, dot, dash and underscore (1-32 characters)" if [ -z "$ADMIN_PASS" ]; then ask_secret ADMIN_PASS "Panel password, at least 10 characters"; fi if [ -z "$ADMIN_PASS" ]; then ADMIN_PASS="$(random_password 20)"; GENERATED_ADMIN_PASS=1; fi [ "${#ADMIN_PASS}" -ge 10 ] && [ "${#ADMIN_PASS}" -le 256 ] || die "the panel password must be 10 to 256 characters" ask WIFI_SSID "WiFi network name" "${WIFI_SSID:-ResberryVPN}" [ -n "$WIFI_SSID" ] && [ "$(printf '%s' "$WIFI_SSID" | wc -c)" -le 32 ] || die "the WiFi name must be 1 to 32 bytes" case "$WIFI_SSID" in *[[:cntrl:]]*) die "the WiFi name contains control characters" ;; esac if [ -z "$WIFI_PASS" ]; then ask_secret WIFI_PASS "WiFi password, 8 to 63 characters"; fi if [ -z "$WIFI_PASS" ]; then WIFI_PASS="$(random_password 16)"; GENERATED_WIFI_PASS=1; fi [ "${#WIFI_PASS}" -ge 8 ] && [ "${#WIFI_PASS}" -le 63 ] || die "the WiFi password must be 8 to 63 characters" if LC_ALL=C grep -q '[^ -~]' <<<"$WIFI_PASS"; then die "the WiFi password may only contain printable English letters, digits and symbols" fi if [ -z "$COUNTRY" ]; then # The radio's current regulatory domain is the best default. COUNTRY="$(iw reg get 2>/dev/null | sed -n 's/^country \([A-Z][A-Z]\):.*/\1/p' | head -n 1 || true)" ask COUNTRY "Country code for the WiFi radio (two letters)" "${COUNTRY:-US}" fi COUNTRY="$(printf '%s' "$COUNTRY" | tr '[:lower:]' '[:upper:]')" [[ "$COUNTRY" =~ ^[A-Z]{2}$ ]] || die "the country code must be two letters, e.g. US, DE, GB" case "$BAND" in 2.4|5) ;; *) die "--band must be 2.4 or 5" ;; esac if [ -z "$CHANNEL" ]; then if [ "$BAND" = 5 ]; then CHANNEL=36; else CHANNEL=6; fi; fi [[ "$CHANNEL" =~ ^[0-9]{1,3}$ ]] || die "--channel must be a number" [[ "$PANEL_PORT" =~ ^[0-9]{1,5}$ ]] && [ "$PANEL_PORT" -ge 1 ] && [ "$PANEL_PORT" -le 65535 ] || die "--port must be 1 to 65535" local listeners listeners="$(ss -ltnH "sport = :$PANEL_PORT" 2>/dev/null || true)" if [ -n "$listeners" ] && [ "$(unit_active resberryd)" != active ]; then die "port $PANEL_PORT is already in use by another program; choose another with --port" fi } # --------------------------------------------------------------------------- download download_all() { TMP="$(mktemp -d)" if [ -n "$LOCAL_DIR" ]; then say "Using binaries from $LOCAL_DIR" local b for b in resberryd sing-box xray; do [ -f "$LOCAL_DIR/$b" ] || die "$LOCAL_DIR/$b not found" cp "$LOCAL_DIR/$b" "$TMP/$b" done ok "binaries found" return 0 fi local base asset sb_sha xr_sha xr_arch if [ -n "$RELEASE_TAG" ]; then base="https://github.com/$REPO/releases/download/$RELEASE_TAG" else base="https://github.com/$REPO/releases/latest/download"; fi asset="resberryd-linux-$ARCH" say "Downloading Resberry VPN (${RELEASE_TAG:-latest release})" fetch "$base/$asset" "$TMP/resberryd" || die "could not download $base/$asset" fetch "$base/SHA256SUMS" "$TMP/SHA256SUMS" || die "could not download the release checksums" local want want="$(awk -v f="$asset" '($2==f || $2=="*"f) && !done {print $1; done=1}' "$TMP/SHA256SUMS")" [ -n "$want" ] || die "the release has no checksum for $asset" verify_sha256 "$TMP/resberryd" "$want" "$asset" ok "resberryd verified" if [ "$ARCH" = arm64 ]; then sb_sha=$SINGBOX_SHA256_arm64; xr_sha=$XRAY_SHA256_arm64; xr_arch=arm64-v8a else sb_sha=$SINGBOX_SHA256_amd64; xr_sha=$XRAY_SHA256_amd64; xr_arch=64; fi say "Downloading sing-box $SINGBOX_VERSION" local sb="sing-box-$SINGBOX_VERSION-linux-$ARCH" fetch "https://github.com/SagerNet/sing-box/releases/download/v$SINGBOX_VERSION/$sb.tar.gz" "$TMP/sb.tgz" || die "could not download sing-box" verify_sha256 "$TMP/sb.tgz" "$sb_sha" "sing-box" tar -xzf "$TMP/sb.tgz" -C "$TMP" "$sb/sing-box" mv "$TMP/$sb/sing-box" "$TMP/sing-box" ok "sing-box verified" say "Downloading Xray $XRAY_VERSION" fetch "https://github.com/XTLS/Xray-core/releases/download/v$XRAY_VERSION/Xray-linux-$xr_arch.zip" "$TMP/xray.zip" || die "could not download Xray" verify_sha256 "$TMP/xray.zip" "$xr_sha" "Xray" unzip -oq "$TMP/xray.zip" xray -d "$TMP" ok "Xray verified" } install_binaries() { say "Installing binaries to $BIN_DIR" chmod 0755 "$TMP/resberryd" "$TMP/sing-box" "$TMP/xray" "$TMP/resberryd" version >/dev/null 2>&1 || die "the downloaded resberryd does not run on this machine" install_bin "$TMP/resberryd" resberryd install_bin "$TMP/sing-box" sing-box install_bin "$TMP/xray" xray ok "$("$BIN_DIR/resberryd" version | head -n 1)" } # --------------------------------------------------------------------------- system setup # Other network managers must leave the AP interface alone, or they fight # hostapd for the radio. Each change is recorded so uninstall can revert it. release_ap_interface() { say "Reserving $AP_IFACE for the access point" if [ "$(unit_active NetworkManager)" = active ]; then mkdir -p /etc/NetworkManager/conf.d printf '# Written by the Resberry VPN installer: hostapd owns this interface.\n[keyfile]\nunmanaged-devices=interface-name:%s\n' "$AP_IFACE" \ >/etc/NetworkManager/conf.d/99-resberry.conf state_set nm_conf created systemctl reload NetworkManager >>"$LOG" 2>&1 || nmcli general reload >>"$LOG" 2>&1 || true fi local u for u in "wpa_supplicant@$AP_IFACE.service" "netplan-wpa-$AP_IFACE.service"; do if [ "$(unit_active "$u")" = active ] || [ "$(unit_enabled "$u")" = enabled ]; then state_set "unit_$u" "$(unit_enabled "$u")" systemctl disable --now "$u" >>"$LOG" 2>&1 || true fi done if [ "$(unit_active dhcpcd)" = active ] && [ -f /etc/dhcpcd.conf ] && ! grep -q "^denyinterfaces.*\b$AP_IFACE\b" /etc/dhcpcd.conf; then printf '\n# Resberry VPN: the access point interface is configured by resberry-net\ndenyinterfaces %s\n' "$AP_IFACE" >>/etc/dhcpcd.conf state_set dhcpcd_conf modified systemctl restart dhcpcd >>"$LOG" 2>&1 || true fi # An older Resberry VPN configured the address through systemd-networkd. if [ -f /etc/systemd/network/25-resberry-wlan0.network ]; then rm -f /etc/systemd/network/25-resberry-wlan0.network networkctl reload >>"$LOG" 2>&1 || true fi rfkill unblock wifi >>"$LOG" 2>&1 || true ok "$AP_IFACE is reserved" } write_system_files() { say "Writing system configuration" install -d -m 0755 /etc/resberry install -d -m 0700 "$DATA_DIR" install -d -m 0755 /etc/hostapd /etc/dnsmasq.d "$SBIN_DIR" # resberry-net: gives the access point its address and installs a boot-time # kill switch, before hostapd lets any client associate. The daemon later # replaces the table with its full rule set; until then (and if the daemon # never starts) nothing from a WiFi client is forwarded anywhere. cat >"$SBIN_DIR/resberry-net" </dev/null || true ip addr replace "\$ADDRESS" dev "\$IFACE" || exit 1 if ! nft list table inet resberry >/dev/null 2>&1; then nft -f - </dev/null || true ip addr del "\$ADDRESS" dev "\$IFACE" 2>/dev/null || true ;; esac EOF chmod 0755 "$SBIN_DIR/resberry-net" cat >/etc/systemd/system/resberry-net.service </etc/systemd/system/resberryd.service </etc/systemd/system/hostapd.service.d/10-resberry.conf </etc/dnsmasq.d/resberry.conf </etc/sysctl.d/99-resberry.conf <<'EOF' # Generated by the Resberry VPN installer. net.ipv4.conf.all.rp_filter=2 net.ipv4.conf.default.rp_filter=2 EOF state_set ip_forward "$(cat /proc/sys/net/ipv4/ip_forward 2>/dev/null || echo 0)" sysctl -q -p /etc/sysctl.d/99-resberry.conf >>"$LOG" 2>&1 || true write_uninstaller ok "system files written" } write_config() { if [ "$MODE" = upgrade ]; then ok "keeping the existing configuration ($CONFIG)" return 0 fi say "Creating $CONFIG" local args=(init-config --config "$CONFIG" --user "$ADMIN_USER" --port "$PANEL_PORT" --ap-interface "$AP_IFACE" --ap-address "$AP_ADDRESS" --ssid "$WIFI_SSID" --country "$COUNTRY" --band "$BAND" --channel "$CHANNEL") [ "$TLS" = 1 ] && args+=(--tls) [ "$RECONFIGURE" = 1 ] && args+=(--force) RESBERRY_ADMIN_PASS="$ADMIN_PASS" RESBERRY_WIFI_PASS="$WIFI_PASS" "$BIN_DIR/resberryd" "${args[@]}" >>"$LOG" 2>&1 \ || die "could not create the configuration: $(tail -n 1 "$LOG")" chmod 600 "$CONFIG" if [ "$RECONFIGURE" = 1 ]; then # The stored WiFi settings win over the config file; drop them so the new # name and password take effect. warn "--reconfigure: WiFi settings saved in the panel are kept. Change the WiFi name or password from the panel's WiFi page." fi ok "configuration written" } start_services() { say "Starting services" systemctl daemon-reload systemctl unmask hostapd >>"$LOG" 2>&1 || true systemctl enable resberry-net.service hostapd.service dnsmasq.service resberryd.service >>"$LOG" 2>&1 systemctl restart resberry-net.service || die "could not configure $AP_IFACE (journalctl -u resberry-net)" systemctl restart dnsmasq.service >>"$LOG" 2>&1 || warn "dnsmasq did not start yet; the daemon will retry" # resberryd writes hostapd.conf from its settings and starts the access point. systemctl restart resberryd.service } verify() { say "Checking that everything is running" local i healthy=0 for i in $(seq 1 45); do if "$BIN_DIR/resberryd" healthcheck --config "$CONFIG" >/dev/null 2>&1; then healthy=1; break; fi sleep 1 done local failed=0 if [ "$healthy" = 1 ]; then ok "panel is answering"; else warn "the panel is not answering"; failed=1; fi for i in $(seq 1 20); do [ "$(unit_active hostapd)" = active ] && break sleep 1 done local apinfo apinfo="$(iw dev "$AP_IFACE" info 2>/dev/null || true)" if [ "$(unit_active hostapd)" = active ] && grep -q 'type AP' <<<"$apinfo"; then ok "access point is broadcasting on $AP_IFACE" else warn "the access point is not up"; failed=1 fi if [ "$(unit_active dnsmasq)" = active ]; then ok "DHCP server is running"; else warn "dnsmasq is not running"; failed=1; fi if nft list table inet resberry >/dev/null 2>&1; then ok "firewall and kill switch are installed"; else warn "the firewall table is missing"; failed=1; fi if [ "$failed" = 1 ]; then printf '\n%sSomething did not start.%s Recent log lines:\n' "$C_RED" "$C_OFF" >&2 journalctl -u resberryd -u hostapd -u dnsmasq -u resberry-net --no-pager -n 25 2>/dev/null | sed 's/^/ /' >&2 || true printf '\nFix the cause and run the installer again, or remove everything with: sudo resberry-uninstall\n' >&2 exit 1 fi } summary() { local scheme=http ip grep -q '^panel_tls: true' "$CONFIG" 2>/dev/null && scheme=https local port port="$(sed -n 's/^panel_port:[[:space:]]*//p' "$CONFIG" | head -n 1)"; port="${port:-$PANEL_PORT}" ip="$(ip -4 -o addr show dev "$WAN_IFACE" 2>/dev/null | awk '!f{split($4,a,"/"); print a[1]; f=1}' || true)" printf '\n%s%s Resberry VPN is %s %s\n\n' "$C_BOLD" "$C_GREEN" "$([ "$MODE" = upgrade ] && echo upgraded || echo installed)" "$C_OFF" printf ' Panel %s://%s:%s\n' "$scheme" "${ip:-}" "$port" printf ' From the WiFi %s://%s:%s\n' "$scheme" "$AP_IP" "$port" if [ "$MODE" != upgrade ]; then printf ' Username %s\n' "$ADMIN_USER" if [ "$GENERATED_ADMIN_PASS" = 1 ]; then printf ' Password %s%s%s\n' "$C_BOLD" "$ADMIN_PASS" "$C_OFF" else printf ' Password the one you chose\n' fi printf '\n WiFi network %s\n' "$WIFI_SSID" if [ "$GENERATED_WIFI_PASS" = 1 ]; then printf ' WiFi password %s%s%s\n' "$C_BOLD" "$WIFI_PASS" "$C_OFF" else printf ' WiFi password the one you chose\n' fi if [ "$GENERATED_ADMIN_PASS" = 1 ] || [ "$GENERATED_WIFI_PASS" = 1 ]; then printf '\n %sWrite the generated passwords down now: they are not stored in readable form\n and will not be shown again.%s\n' "$C_YELLOW" "$C_OFF" fi [ "$scheme" = https ] && printf '\n The certificate is self-signed, so the browser will warn once.\n' printf '\n Next: open the panel, add a subscription or a server link on the Servers\n page, and press Connect. Devices that join the WiFi then go through the VPN.\n' fi printf '\n Forgot the password? sudo resberryd reset-password --password-stdin\n' printf ' Upgrade run this installer again\n' printf ' Remove sudo resberry-uninstall\n\n' } # --------------------------------------------------------------------------- uninstaller write_uninstaller() { cat >"$SBIN_DIR/resberry-uninstall" <<'UNINSTALL' #!/usr/bin/env bash # Removes Resberry VPN and puts back what the installer changed, using the # record it kept in /var/lib/resberry/install-state. Your servers and settings # are kept unless you pass --purge. set -uo pipefail STATE=/var/lib/resberry/install-state PURGE=0 case "${1:-}" in --purge) PURGE=1 ;; "") ;; *) echo "usage: resberry-uninstall [--purge]"; exit 2 ;; esac [ "$(id -u)" = 0 ] || { echo "run as root: sudo resberry-uninstall"; exit 1; } get() { [ -f "$STATE" ] && sed -n "s/^$1=//p" "$STATE" | tail -n 1; } step() { printf '==> %s\n' "$*"; } AP_IFACE="$(sed -n 's/^IFACE="\(.*\)"$/\1/p' /usr/local/sbin/resberry-net 2>/dev/null | head -n 1)" AP_ADDRESS="$(sed -n 's/^ADDRESS="\(.*\)"$/\1/p' /usr/local/sbin/resberry-net 2>/dev/null | head -n 1)" AP_IFACE="${AP_IFACE:-wlan0}" step "Stopping services" systemctl disable --now resberryd.service 2>/dev/null systemctl stop hostapd.service 2>/dev/null step "Removing routing and firewall rules" if [ -n "$AP_ADDRESS" ]; then # 192.168.50.1/24 -> 192.168.50.0/24 (the installer only creates /24 networks) CIDR="$(printf '%s' "$AP_ADDRESS" | sed 's/\.[0-9]*\/24$/.0\/24/')" while ip rule del from "$CIDR" lookup 100 2>/dev/null; do :; done fi ip route flush table 100 2>/dev/null ip link del tun0 2>/dev/null [ -x /usr/local/sbin/resberry-net ] && /usr/local/sbin/resberry-net down nft delete table inet resberry 2>/dev/null systemctl disable --now resberry-net.service 2>/dev/null # Forwarding goes back to what it was before the install; if it was on (for # Docker, another VPN...) it stays on. case "$(get ip_forward)" in 0) sysctl -qw net.ipv4.ip_forward=0 2>/dev/null ;; esac step "Removing files" rm -f /etc/systemd/system/resberryd.service /etc/systemd/system/resberry-net.service rm -f /etc/systemd/system/hostapd.service.d/10-resberry.conf rmdir /etc/systemd/system/hostapd.service.d 2>/dev/null rm -f /etc/dnsmasq.d/resberry.conf /etc/sysctl.d/99-resberry.conf rm -f /etc/hostapd/hostapd.conf rm -f /usr/local/bin/resberryd /usr/local/bin/sing-box /usr/local/bin/xray /usr/local/sbin/resberry-net systemctl daemon-reload step "Restoring the previous state of hostapd, dnsmasq and the network manager" case "$(get hostapd_enabled)" in enabled) systemctl enable hostapd 2>/dev/null ;; masked) systemctl disable hostapd 2>/dev/null; systemctl mask hostapd 2>/dev/null ;; *) systemctl disable hostapd 2>/dev/null ;; esac case "$(get dnsmasq_enabled)" in enabled) systemctl enable dnsmasq 2>/dev/null ;; *) systemctl disable dnsmasq 2>/dev/null ;; esac if [ "$(get dnsmasq_active)" = active ]; then systemctl restart dnsmasq 2>/dev/null; else systemctl stop dnsmasq 2>/dev/null; fi if [ "$(get nm_conf)" = created ]; then rm -f /etc/NetworkManager/conf.d/99-resberry.conf systemctl reload NetworkManager 2>/dev/null fi if [ "$(get dhcpcd_conf)" = modified ] && [ -f /etc/dhcpcd.conf ]; then sed -i '/^# Resberry VPN: the access point interface/d; /^denyinterfaces '"$AP_IFACE"'$/d' /etc/dhcpcd.conf systemctl restart dhcpcd 2>/dev/null fi if [ -f "$STATE" ]; then sed -n 's/^unit_\(.*\)=enabled$/\1/p' "$STATE" | while read -r unit; do systemctl enable --now "$unit" 2>/dev/null done fi if [ "$PURGE" = 1 ]; then step "Deleting settings and data (--purge)" rm -rf /etc/resberry /var/lib/resberry /var/log/resberry-install.log echo "Resberry VPN and its data were removed." else rm -f "$STATE" echo "Resberry VPN was removed. Your settings and servers are kept in /etc/resberry and" echo "/var/lib/resberry; delete them with: sudo rm -rf /etc/resberry /var/lib/resberry" fi echo "The packages hostapd, dnsmasq, nftables and iw were left installed." rm -f /usr/local/sbin/resberry-uninstall UNINSTALL chmod 0755 "$SBIN_DIR/resberry-uninstall" } # --------------------------------------------------------------------------- main main() { parse_args "$@" trap 'on_error $LINENO' ERR trap cleanup EXIT preflight_system : >>"$LOG" 2>/dev/null || true chmod 600 "$LOG" 2>/dev/null || true log "installer started (mode=$MODE arch=$ARCH os=$OS_NAME)" printf '%sResberry VPN installer%s %s on %s\n\n' "$C_BOLD" "$C_OFF" "$([ "$MODE" = upgrade ] && echo 'upgrading the existing installation' || echo 'new installation')" "$OS_NAME" install_packages detect_network choose_subnet say "Uplink: $WAN_IFACE Access point: $AP_IFACE WiFi clients: $AP_PREFIX.0/24" collect_settings download_all install_binaries release_ap_interface write_system_files write_config start_services verify summary log "installer finished (mode=$MODE)" } main "$@"