mirror of
https://github.com/YunoHost-Apps/hotspot_ynh.git
synced 2024-09-03 19:25:53 +02:00
183 lines
4.3 KiB
Text
183 lines
4.3 KiB
Text
|
#!/bin/bash
|
||
|
|
||
|
#=================================================
|
||
|
# GENERIC STARTING
|
||
|
#=================================================
|
||
|
# IMPORT GENERIC HELPERS
|
||
|
#=================================================
|
||
|
|
||
|
source _common.sh
|
||
|
source /usr/share/yunohost/helpers
|
||
|
|
||
|
#=================================================
|
||
|
# MANAGE SCRIPT FAILURE
|
||
|
#=================================================
|
||
|
|
||
|
# Exit if an error occurs during the execution of the script
|
||
|
ynh_abort_if_errors
|
||
|
|
||
|
#=================================================
|
||
|
# RETRIEVE ARGUMENTS
|
||
|
#=================================================
|
||
|
|
||
|
final_path=$(ynh_app_setting_get $app final_path)
|
||
|
|
||
|
set_right() {
|
||
|
if [ -f $1 ]
|
||
|
then
|
||
|
chown $app:$app $1
|
||
|
chmod go=--- $1
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
#=================================================
|
||
|
# SPECIFIC GETTERS FOR TOML SHORT KEY
|
||
|
#=================================================
|
||
|
|
||
|
get__status() {
|
||
|
local service_enabled=$(ynh_app_setting_get $app service_enabled)
|
||
|
if ip route get 1.2.3.4 | grep -q tun0
|
||
|
then
|
||
|
if [ $service_enabled -eq 1 ]
|
||
|
then
|
||
|
cat << EOF
|
||
|
style: success
|
||
|
ask:
|
||
|
en: |-
|
||
|
Your VPN is running :)
|
||
|
|
||
|
**IPv4:** \`$(curl https://ip.yunohost.org --silent)\`
|
||
|
**IPv6:** \`$(curl https://ip6.yunohost.org --silent)\`
|
||
|
EOF
|
||
|
else
|
||
|
cat << EOF
|
||
|
style: warning
|
||
|
ask:
|
||
|
en: Your VPN is running, but it shouldn't !
|
||
|
EOF
|
||
|
fi
|
||
|
elif [ $service_enabled -eq 1 ]
|
||
|
then
|
||
|
cat << EOF
|
||
|
style: danger
|
||
|
ask:
|
||
|
en: |-
|
||
|
Your VPN is down ! Here are errors logged in the last 5 minutes
|
||
|
\`\`\`
|
||
|
$(journalctl -u openvpn@client -p0..3 --since "- 20 minutes" -o cat | sed 's/^/ /g' | tail -n 15)
|
||
|
\`\`\`
|
||
|
EOF
|
||
|
else
|
||
|
cat << EOF
|
||
|
style: info
|
||
|
ask:
|
||
|
en: Your VPN is down has expected.
|
||
|
EOF
|
||
|
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
get__login_user() {
|
||
|
if [ -s /etc/openvpn/keys/credentials ]
|
||
|
then
|
||
|
echo "$(sed -n 1p /etc/openvpn/keys/credentials)"
|
||
|
else
|
||
|
echo ""
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
get__login_passphrase() {
|
||
|
if [ -s /etc/openvpn/keys/credentials ]
|
||
|
then
|
||
|
echo "$(sed -n 2p /etc/openvpn/keys/credentials)"
|
||
|
else
|
||
|
echo ""
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
|
||
|
#=================================================
|
||
|
# SPECIFIC VALIDATORS FOR TOML SHORT KEYS
|
||
|
#=================================================
|
||
|
validate__login_user() {
|
||
|
|
||
|
if grep -q '^\s*auth-user-pass' ${config_file}
|
||
|
then
|
||
|
if [[ -z "${login_user}" ]]
|
||
|
then
|
||
|
echo 'A Username is needed with this configuration file'
|
||
|
fi
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
#=================================================
|
||
|
# SPECIFIC SETTERS FOR TOML SHORT KEYS
|
||
|
#=================================================
|
||
|
set__login_user() {
|
||
|
if [ -n "${login_user}" ]
|
||
|
then
|
||
|
echo "${login_user}\n${login_passphrase}" > /etc/openvpn/keys/credentials
|
||
|
set_right /etc/openvpn/keys/credentials
|
||
|
else
|
||
|
echo "" > /etc/openvpn/keys/credentials
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
set__login_passphrase() {
|
||
|
:
|
||
|
}
|
||
|
|
||
|
#=================================================
|
||
|
# OVERWRITING VALIDATE STEP
|
||
|
#=================================================
|
||
|
read_cube() {
|
||
|
tmp_dir=$(dirname "$1")
|
||
|
setting_value="$(jq --raw-output ".$2" "$1")"
|
||
|
if [[ "$setting_value" == "null" ]]
|
||
|
then
|
||
|
setting_value=''
|
||
|
# Save file in tmp dir
|
||
|
elif [[ "$2" == "crt_"* ]]
|
||
|
then
|
||
|
if [ -n "${setting_value}" ]
|
||
|
then
|
||
|
echo "${setting_value}" | sed 's/|/\n/g' > $tmp_dir/$2
|
||
|
setting_value="$tmp_dir/$2"
|
||
|
fi
|
||
|
fi
|
||
|
echo $setting_value
|
||
|
}
|
||
|
ynh_app_config_validate() {
|
||
|
_ynh_app_config_validate
|
||
|
}
|
||
|
|
||
|
#=================================================
|
||
|
# OVERWRITING APPLY STEP
|
||
|
#=================================================
|
||
|
ynh_app_config_apply() {
|
||
|
|
||
|
# Stop vpn client
|
||
|
ynh_print_info --message="Stopping vpnclient in order to edit files"
|
||
|
touch /tmp/.ynh-vpnclient-stopped
|
||
|
/usr/local/bin/ynh-vpnclient stop
|
||
|
|
||
|
chown $app:$app /etc/openvpn/keys
|
||
|
chmod go=--- /etc/openvpn/keys
|
||
|
|
||
|
_ynh_app_config_apply
|
||
|
|
||
|
set_right /etc/openvpn/client.conf
|
||
|
set_right /etc/openvpn/keys/ca-server.crt
|
||
|
set_right /etc/openvpn/keys/user.crt
|
||
|
set_right /etc/openvpn/keys/user.key
|
||
|
set_right /etc/openvpn/keys/user_ta.key
|
||
|
|
||
|
# Start vpn client
|
||
|
ynh_print_info --message="Starting vpnclient service if needed"
|
||
|
/usr/local/bin/ynh-vpnclient start
|
||
|
rm -f /tmp/.ynh-vpnclient-stopped
|
||
|
|
||
|
}
|
||
|
|
||
|
ynh_app_config_run $1
|