Skip to content

Commit

Permalink
Refresh heartbeat monitor branch (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmens authored Oct 27, 2023
1 parent e047f0e commit 32ed303
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 88 deletions.
2 changes: 2 additions & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Allow including of external files
disable=SC1091
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# rpi-umpx-decoder
This repository contains the MicroMPX set-up for [ZuidWest FM](https://www.zuidwestfm.nl/) in the Netherlands. It uses a Rapsberry Pi 4 and a [HiFiBerry DAC2 Pro XLR
This repository contains the MicroMPX set-up for [ZuidWest FM](https://www.zuidwestfm.nl/) in the Netherlands. It uses a Rapsberry Pi 3B or 4 and a [HiFiBerry DAC2 Pro XLR
](https://www.hifiberry.com/shop/boards/hifiberry-dac2-pro-xlr/) as audio output. It downloads the most recent version of the MicroMPX decoder from Thimeo, which is managed by systemd as service.

# How to prepare the Rapsberry Pi
- Install Raspberry Pi OS Lite 11 (bullseye) 64-bit
- Install Raspberry Pi OS Lite 12 (Bookworm) 64-bit
- Ensure you are root by running `sudo su`
- Download and run the install script with the command `/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/oszuidwest/rpi-umpx-decoder/main/setup.sh)"`

Expand All @@ -12,11 +12,6 @@ This repository contains the MicroMPX set-up for [ZuidWest FM](https://www.zuidw
- If you want monitoring via UptimeRobot, add the contents of `allowlist.txt` to the `Whitelist=` section of hte MicroMPX configuration file which is at `/home/micrompx/.MicroMPX_Decoder.rc`

# How to add the Raspberry Pi to the VPN
- Download `https://raw.githubusercontent.com/oszuidwest/rpi-umpx-decoder/main/vpn.sh`
- Fill in the variables at the top of `vpn.sh`
- Run `chmod +x vpn.sh` to make the script executable
- Run `./vpn.sh` and reboot
- Download and run the VPN script with the command `/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/oszuidwest/rpi-umpx-decoder/main/vpn.sh)"`
- Check with `ip a` if you have an interface named `wg0` with the correct IP
- If the `wg0` interface is not showing, enable debugging with `modprobe wireguard && echo module wireguard +p > /sys/kernel/debug/dynamic_debug/control` and `tail -f /var/log/syslog` to look for errors

[WIP]
2 changes: 1 addition & 1 deletion micrompx.service
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=MicroMPX Decoder
After=network.target
Documentation=http://zuidwestfm.nl/
Documentation=https://www.zuidwestfm.nl/

[Service]
Type=simple
Expand Down
69 changes: 31 additions & 38 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
#!/usr/bin/env bash

# Set some colors
readonly GREEN='\033[1;32m'
readonly RED='\033[1;31m'
readonly YELLOW='\033[0;33m'
readonly BLUE='\033[1;34m'
readonly NC='\033[0m' # No Color
# Start with a clean terminal
clear

# Download the functions library
if ! curl -s -o /tmp/functions.sh https://raw.githubusercontent.com/oszuidwest/bash-functions/main/common-functions.sh; then
echo -e "*** Failed to download functions library. Please check your network connection! ***"
exit 1
fi

# Source the functions file
source /tmp/functions.sh

# Set color variables
set_colors

# Check if running as root
are_we_root

# Check if this is Linux
is_this_linux
is_this_os_64bit

# Check if we are running on a Raspberry Pi 3 or newer
check_rpi_model 3

# Something fancy for the sysadmin
clear
cat << "EOF"
______ _ ___ __ _ ______ __ __
|___ / (_) | \ \ / / | | | ____| \/ |
Expand All @@ -19,25 +36,7 @@ cat << "EOF"
EOF

# Hi!
echo -e "${GREEN}⎎ MicroMPX Setup for Raspberry Pi 4${NC}\n\n"

# Function that checks if this is a supported platform
check_platform() {
if ! grep -q "Raspberry Pi 4" /proc/device-tree/model > /dev/null; then
echo -e "${RED}** NOT RUNNING ON A RASPBERRY PI 4 **${NC}"
echo -e "${YELLOW}This script is only tested on a Raspberry Pi 4. Press Enter to continue anyway...${NC}"
read -r
fi
}

# Check if running as root
if [[ "$(id -u)" -ne 0 ]]; then
echo -e "${RED}This script must be run as root. Please run 'sudo su' first.${NC}"
exit 1
fi

# Check if we are running on a Raspberry PI 4
check_platform
echo -e "${GREEN}⎎ MicroMPX Setup for Raspberry Pi${NC}\n\n"

# Check and stop micrompx service if running
echo -e "${BLUE}►► Checking and stopping MicroMPX service if running...${NC}"
Expand All @@ -52,15 +51,10 @@ echo -e "${BLUE}►► Expanding filesystem...${NC}"
raspi-config --expand-rootfs > /dev/null

# Timezone configuration
echo -e "${BLUE}►► Setting timezone to Europe/Amsterdam...${NC}"
ln -fs /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime > /dev/null
dpkg-reconfigure -f noninteractive tzdata > /dev/null
set_timezone Europe/Amsterdam

# Update the OS
echo -e "${BLUE}►► Updating all packages...${NC}"
apt -qq -y update > /dev/null 2>&1
apt -qq -y full-upgrade > /dev/null 2>&1
apt -qq -y autoremove > /dev/null 2>&1
update_os silent

# Add user for micrompx
echo -e "${BLUE}►► Adding micrompx user if it doesn't exist...${NC}"
Expand All @@ -77,26 +71,25 @@ else
fi

# Install dependencies for micrompx
echo -e "${BLUE}►► Installing dependencies...${NC}"
apt -qq -y install libasound2 > /dev/null 2>&1
install_packages silent libasound2

# Download micrompx from Thimeo
echo -e "${BLUE}►► Downloading and installing MicroMPX...${NC}"
mkdir -p /opt/micrompx > /dev/null
wget -q https://www.stereotool.com/download/MicroMPX_Decoder_ARM64 -O /opt/micrompx/MicroMPX_Decoder > /dev/null
curl -s -o /opt/micrompx/MicroMPX_Decoder https://download.thimeo.com/MicroMPX_Decoder_ARM64
chmod +x /opt/micrompx/MicroMPX_Decoder > /dev/null
setcap CAP_NET_BIND_SERVICE=+eip /opt/micrompx/MicroMPX_Decoder > /dev/null

# Add service
echo -e "${BLUE}►► Installing MicroMPX service...${NC}"
rm -f /etc/systemd/system/micrompx.service > /dev/null
wget -q https://raw.githubusercontent.com/oszuidwest/rpi-umpx-decoder/main/micrompx.service -O /etc/systemd/system/micrompx.service > /dev/null
curl -s -o /etc/systemd/system/micrompx.service https://raw.githubusercontent.com/oszuidwest/rpi-umpx-decoder/main/micrompx.service
systemctl daemon-reload > /dev/null
systemctl enable micrompx > /dev/null

# Disable only the hdmi audio so we can use the minijack for monitoring
echo -e "${BLUE}►► Disabling onboard audio...${NC}"
readonly CONFIG_FILE="/boot/config.txt"
readonly CONFIG_FILE="/boot/firmware/config.txt"
sed -i '/dtoverlay=vc4-fkms-v3d/ { /audio=off/! s/$/,audio=off/ }' "$CONFIG_FILE" > /dev/null
sed -i '/dtoverlay=vc4-kms-v3d/ { /noaudio/! s/$/,noaudio/ }' "$CONFIG_FILE" > /dev/null

Expand Down
129 changes: 88 additions & 41 deletions vpn.sh
Original file line number Diff line number Diff line change
@@ -1,58 +1,105 @@
#!/bin/bash

# Variables (Replace these with the actual values)
readonly SERVER_PUBLIC_IP="<server_ip>"
readonly SERVER_PUBLIC_KEY="<server_public_key>"
readonly NETWORK="172.16.0.0/24"
readonly RASPBERRY_ADDRESS="172.16.0.2/24" # Assume .1 is the endpoint
# Start with a clean terminal
clear

# Only change these paths if you know what you're doing
readonly PRIVATE_KEY_PATH="/etc/wireguard/privatekey"
readonly PUBLIC_KEY_PATH="/etc/wireguard/publickey"

# Check if running as root
if [[ "$(id -u)" -ne 0 ]]; then
echo "This script must be run as root. Please run 'sudo su' first."
# Download the functions library
if ! curl -f -Ss -o /tmp/functions.sh https://raw.githubusercontent.com/oszuidwest/bash-functions/main/common-functions.sh; then
echo -e "*** Failed to download functions library. Please check your network connection! ***"
exit 1
fi

# Check if WireGuard is installed, if not, install it
if ! command -v wg >/dev/null 2>&1; then
echo "WireGuard is not installed. Updating system and installing WireGuard..."
apt update -qq -y && apt install -qq -y wireguard
fi
# Source the functions file
source /tmp/functions.sh

# Check if the server keys exist. If not, generate them
if [[ -f "$PRIVATE_KEY_PATH" ]] && [[ -f "$PUBLIC_KEY_PATH" ]]; then
echo "Server keys already exist. No action required."
else
echo "Server keys are missing. Generating new keys..."
rm -f "$PRIVATE_KEY_PATH" "$PUBLIC_KEY_PATH"
umask 077
wg genkey | tee "$PRIVATE_KEY_PATH" | wg pubkey > "$PUBLIC_KEY_PATH"
# Set color variables
set_colors

# Start with a clean terminal
clear

# Check if running as root
are_we_root

# Check if this is Linux
is_this_linux
is_this_os_64bit

# Check if we are running on a Raspberry Pi 3 or newer
check_rpi_model 3

# Ask for input for variables
ask_user "SERVER_PUBLIC_IP" "127.0.0.1" "Enter the ip-address of the Wireguard server" "str"
ask_user "SERVER_PUBLIC_KEY" "GQ4G7V+uRFRbqzYTgNHLd58o+RNPUW99L7Nc7mTt2Hs=" "Enter the public key of the Wirguard server" "str"
ask_user "NETWORK" "172.18.1.0/24" "Enter the network range you want to allow to connect" "str"
ask_user "RASPBERRY_ADDRESS" "172.18.1.2/32" "Enter the private ip-address this device should have" "str"

# Paths
WIREGUARD_PATH="/etc/wireguard"
PRIVATE_KEY_PATH="${WIREGUARD_PATH}/privatekey"
PUBLIC_KEY_PATH="${WIREGUARD_PATH}/publickey"
CONFIGURATION_PATH="${WIREGUARD_PATH}/wg0.conf"

# Ensure WireGuard is installed
install_packages silent wireguard

# Generate server keys if they do not exist
if [[ ! -f $PRIVATE_KEY_PATH || ! -f $PUBLIC_KEY_PATH ]]; then
echo "Server keys are missing. Generating new keys..."
umask 077
if ! wg genkey | tee "$PRIVATE_KEY_PATH" | wg pubkey > "$PUBLIC_KEY_PATH"; then
echo "Error: Failed to generate keys."
exit 1
fi
fi

# Read the generated private key
GENERATED_PRIVATE_KEY="$(cat $PRIVATE_KEY_PATH)"
# Read the generated keys
GENERATED_PRIVATE_KEY=$(<"$PRIVATE_KEY_PATH")
GENERATED_PUBLIC_KEY=$(<"$PUBLIC_KEY_PATH")

# Backup old configuration file if it exists
if [[ -f $CONFIGURATION_PATH ]]; then
mv "$CONFIGURATION_PATH" "${CONFIGURATION_PATH}_old_$(date +%Y%m%d%H%M%S)"
fi

# Create WireGuard configuration file
echo "Creating WireGuard configuration file..."
bash -c "cat > /etc/wireguard/wg0.conf << EOL
# Create the WireGuard configuration file
cat >"$CONFIGURATION_PATH" <<EOL
[Interface]
Address = ${RASPBERRY_ADDRESS}
PrivateKey = ${GENERATED_PRIVATE_KEY}
Address = $RASPBERRY_ADDRESS
PrivateKey = $GENERATED_PRIVATE_KEY
[Peer]
PublicKey = ${SERVER_PUBLIC_KEY}
Endpoint = ${SERVER_PUBLIC_IP}:51820
AllowedIPs = ${NETWORK}
PublicKey = $SERVER_PUBLIC_KEY
Endpoint = $SERVER_PUBLIC_IP:51820
AllowedIPs = $NETWORK
PersistentKeepalive = 25
EOL"
EOL

# Bring up the WireGuard interface
wg-quick up wg0
# Ensure the WireGuard configuration file is readable and not empty
if [[ ! -r $CONFIGURATION_PATH || ! -s $CONFIGURATION_PATH ]]; then
echo "Error: The WireGuard configuration file is not readable or is empty."
exit 1
fi

# Enable the WireGuard interface on boot
systemctl enable wg-quick@wg0
# Check if the WireGuard interface needs to be enabled on boot
if ! systemctl is-enabled --quiet wg-quick@wg0
then
echo -e "${BLUE}►► Enabling the wg0 interface on boot...${NC}"
systemctl enable wg-quick@wg0
fi

# Bring up the WireGuard interface
if ip link show wg0 &> /dev/null; then
echo -e "${BLUE}►► Restarting wg0...${NC}"
wg-quick down wg0
wg-quick up wg0
else
echo -e "${BLUE}►► Bringing wg0 up...${NC}"
wg-quick up wg0
fi

echo "WireGuard VPN configuration completed!"
# Fin
echo -e "\n${GREEN}✓ Success!${NC}"
echo -e "There should now be an interface named ${BOLD}wg0${NC} on this machine."
echo -e "The IP of the WireGuard interface is ${BOLD}$RASPBERRY_ADDRESS${NC}"
echo -e "The public key to put in the server is ${BOLD}$GENERATED_PUBLIC_KEY${NC}\n"

0 comments on commit 32ed303

Please sign in to comment.