-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refresh heartbeat monitor branch (#5)
- Loading branch information
Showing
5 changed files
with
125 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Allow including of external files | ||
disable=SC1091 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |