2020-10-11 14:45:13 +02:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
### Remove this function if there's nothing to clean before calling the remove script.
true
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
domain=$YNH_APP_ARG_DOMAIN
2021-03-14 09:53:57 +01:00
path_url="/"
2020-10-11 14:45:13 +02:00
admin=$YNH_APP_ARG_ADMIN
2021-12-09 12:31:02 +01:00
architecture=$YNH_ARCH
2020-10-11 14:45:13 +02:00
app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
2020-12-30 20:26:03 +01:00
ynh_script_progression --message="Validating installation parameters..." --weight=1
2020-10-11 14:45:13 +02:00
final_path=/opt/yunohost/$app
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
# Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
2020-12-30 20:26:03 +01:00
ynh_script_progression --message="Storing installation settings..." --weight=1
2020-10-11 14:45:13 +02:00
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url
ynh_app_setting_set --app=$app --key=admin --value=$admin
2021-11-03 00:09:09 +01:00
ynh_app_setting_set --app=$app --key=interface --value=$interface
2020-10-11 14:45:13 +02:00
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
2021-09-09 17:58:14 +02:00
ynh_script_progression --message="Finding an available port..." --weight=1
2020-10-11 14:45:13 +02:00
# Find an available port for WireGuard
port_wg=$(ynh_find_port --port=8095)
ynh_app_setting_set --app=$app --key=port_wg --value=$port_wg
2020-12-29 17:42:37 +01:00
# Find an available port for WireGuard UI
2021-03-08 21:30:11 +01:00
port=$(ynh_find_port --port=$(($port_wg+1)))
2020-10-11 14:45:13 +02:00
ynh_app_setting_set --app=$app --key=port --value=$port
# Open the WireGuard port
2021-09-09 17:58:14 +02:00
ynh_script_progression --message="Configuring firewall..." --weight=1
2020-10-11 14:45:13 +02:00
ynh_exec_warn_less yunohost firewall allow --no-upnp UDP $port_wg
#=================================================
# INSTALL DEPENDENCIES
#=================================================
2020-12-30 20:26:03 +01:00
ynh_script_progression --message="Installing dependencies..." --weight=7
2020-10-11 14:45:13 +02:00
2021-03-20 18:36:54 +01:00
# Add buster-backports gpg key
ynh_install_repo_gpg --key="https://ftp-master.debian.org/keys/archive-key-10.asc" --name="$app"
2021-01-11 23:16:53 +01:00
# Add buster-backports repo
2020-12-22 21:44:25 +01:00
ynh_add_repo --uri="http://deb.debian.org/debian" --suite="buster-backports" --component="main" --name="$app"
2021-01-11 23:16:53 +01:00
# Add pin-priority for wireguard packages
2020-12-23 17:13:58 +01:00
ynh_pin_repo --package="wireguard*" --pin="origin deb http://deb.debian.org/debian buster-backports main" --priority=995 --name="$app"
2020-12-22 21:44:25 +01:00
# Update the list of package with the new repo
ynh_package_update
2021-12-22 17:24:56 +01:00
ynh_add_app_dependencies --package="$pkg_dependencies"
2020-12-22 21:44:25 +01:00
2021-01-11 23:16:53 +01:00
# Remove buster-backports repo and pin-priority
2020-12-22 21:44:25 +01:00
ynh_remove_extra_repo --name=$app
2020-10-11 14:45:13 +02:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
2020-12-30 20:26:03 +01:00
ynh_script_progression --message="Setting up source files..." --weight=1
2020-10-11 14:45:13 +02:00
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
# Download, check integrity, uncompress and patch the source from app.src
2021-12-09 12:31:02 +01:00
ynh_setup_source --dest_dir="$final_path" --source_id="$architecture"
2020-10-11 14:45:13 +02:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2021-09-09 17:58:14 +02:00
ynh_script_progression --message="Configuring NGINX web server..." --weight=1
2020-10-11 14:45:13 +02:00
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# CREATE DEDICATED USER
#=================================================
2020-12-30 20:26:03 +01:00
ynh_script_progression --message="Configuring system user..." --weight=1
2020-10-11 14:45:13 +02:00
# Create a system user
ynh_system_user_create --username=$app
#=================================================
# SPECIFIC SETUP
#=================================================
# CONFIGURING WIREGUARD
#=================================================
# Create db directory for securing it later
mkdir -p $final_path/db/server
# Add interface configuration file for the Web UI
2021-11-03 00:09:09 +01:00
ynh_add_config --template="../conf/interfaces.json" --destination="$final_path/db/server/interfaces.json"
ynh_delete_file_checksum --file="$final_path/db/server/interfaces.json"
2020-10-11 14:45:13 +02:00
2021-01-01 21:49:12 +01:00
# Create WireGuard configuration directory
2020-10-11 14:45:13 +02:00
mkdir -p /etc/wireguard
#=================================================
# SETUP SYSTEMD
#=================================================
2020-12-30 20:26:03 +01:00
ynh_script_progression --message="Configuring a systemd service..." --weight=1
2020-10-11 14:45:13 +02:00
# Create a dedicated systemd config for the web UI
2021-06-20 01:52:05 +02:00
ynh_add_systemd_config --service=wireguard_ui --template=wireguard_ui.service
2020-10-11 14:45:13 +02:00
2021-12-26 11:51:25 +01:00
# Create dedicated systemd configs for starting and monitoring WireGuard's configuration
2021-12-22 10:37:22 +01:00
cp ../conf/wireguard@.service /etc/systemd/system/wireguard@.service
2021-12-26 11:51:25 +01:00
cp ../conf/wireguard@.path /etc/systemd/system/wireguard@.path
systemctl daemon-reload
2021-12-22 10:37:22 +01:00
systemctl enable --quiet wireguard@wg0.service
2021-12-26 11:51:25 +01:00
systemctl enable --quiet --now wireguard@wg0.path
2020-10-11 14:45:13 +02:00
2021-12-30 17:06:32 +01:00
#=================================================
# 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
2020-10-11 14:45:13 +02:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
# Set permissions to app files
2021-07-04 21:43:22 +02:00
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app: "$final_path"
2020-10-11 14:45:13 +02:00
2021-07-04 21:43:22 +02:00
chmod 750 /etc/wireguard
chmod -R o-rwx /etc/wireguard
chown -R $app: /etc/wireguard
2020-10-11 14:45:13 +02:00
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
2020-12-30 20:26:03 +01:00
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
2020-10-11 14:45:13 +02:00
2021-12-22 10:37:22 +01:00
yunohost service add wireguard@wg0 --description="WireGuard VPN" --needs_exposed_ports="$port_wg" --test_status="wg show | grep wg0"
2021-09-09 17:58:14 +02:00
yunohost service add wireguard_ui --description="WireGuard UI"
2020-10-11 14:45:13 +02:00
#=================================================
2021-12-21 17:57:39 +01:00
# START UI SYSTEMD SERVICE
2020-10-11 14:45:13 +02:00
#=================================================
2021-12-21 17:57:39 +01:00
ynh_script_progression --message="Starting the systemd service for the UI..." --weight=1
2020-10-11 14:45:13 +02:00
# Start a systemd service
2021-01-21 22:15:43 +01:00
ynh_systemd_action --service_name=wireguard_ui --action="start" --line_match="http server started" --log_path="systemd" --timeout=30
2020-10-11 14:45:13 +02:00
#=================================================
# SETUP SSOWAT
#=================================================
2020-12-30 20:26:03 +01:00
ynh_script_progression --message="Configuring permissions..." --weight=1
2020-10-11 14:45:13 +02:00
2021-09-09 17:58:14 +02:00
ynh_permission_update --permission="main" --remove="all_users" --add="$admin"
2020-10-11 14:45:13 +02:00
#=================================================
# RELOAD NGINX
#=================================================
2021-12-09 12:31:02 +01:00
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
2020-10-11 14:45:13 +02:00
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
2021-03-22 17:02:49 +01:00
ynh_script_progression --message="Installation of $app completed. You may need to reboot your server before being able to start the WireGuard service." --last