1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/wireguard_ynh.git synced 2024-09-03 20:35:58 +02:00
wireguard_ynh/scripts/install
2023-09-23 23:13:18 +02:00

118 lines
4.4 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
interface=$(ip route | awk '/default/ { print $5 }' | head -n1)
main_domain=$(cat /etc/yunohost/current_host)
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_app_setting_set --app=$app --key=interface --value=$interface
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=1
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$install_dir"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring NGINX web server..." --weight=1
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# SPECIFIC SETUP
#=================================================
# CONFIGURING WIREGUARD
#=================================================
# Create db directory for securing it later
mkdir -p $install_dir/db/server
# Add interface configuration file for the Web UI
ynh_add_config --template="../conf/interfaces.json" --destination="$install_dir/db/server/interfaces.json"
ynh_delete_file_checksum --file="$install_dir/db/server/interfaces.json"
# Add configuration for the Web UI
ynh_add_config --template="../conf/wireguard-ui.env" --destination="$install_dir/wireguard-ui.env"
# Create WireGuard configuration directory
mkdir -p /etc/wireguard
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..." --weight=1
# Create a dedicated systemd config for the web UI
ynh_add_systemd_config --service=wireguard_ui --template=wireguard_ui.service
# Create dedicated systemd configs for starting and monitoring WireGuard's configuration
cp ../conf/wireguard@.service /etc/systemd/system/wireguard@.service
cp ../conf/wireguard@.path /etc/systemd/system/wireguard@.path
systemctl daemon-reload
systemctl enable --quiet wireguard@wg0.service
systemctl enable --quiet --now wireguard@wg0.path
#=================================================
# ENABLE PORT FORWARDING
#=================================================
ynh_script_progression --message="Enabling port forwarding..." --weight=1
ynh_add_config --template="../conf/sysctl.conf" --destination="/etc/sysctl.d/$app.conf"
sysctl -p /etc/sysctl.d/$app.conf
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
# Set permissions to app files
chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app: "$install_dir"
chmod 750 /etc/wireguard
chmod -R o-rwx /etc/wireguard
chown -R $app: /etc/wireguard
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
yunohost service add wg-quick@wg0 --description="WireGuard VPN" --needs_exposed_ports="$port_wg" --test_status="wg show | grep wg0"
yunohost service add wireguard_ui --description="WireGuard UI"
#=================================================
# START UI SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting the systemd service for the UI..." --weight=1
# Start a systemd service
ynh_systemd_action --service_name=wireguard_ui --action="start" --line_match="http server started" --log_path="systemd" --timeout=30
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed. You may need to reboot your server before being able to start the WireGuard service." --last