mirror of
https://github.com/YunoHost-Apps/hotspot_ynh.git
synced 2024-09-03 19:25:53 +02:00
211 lines
5.1 KiB
Bash
211 lines
5.1 KiB
Bash
#!/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)
|
|
|
|
#=================================================
|
|
# SPECIFIC GETTERS FOR TOML SHORT KEY
|
|
#=================================================
|
|
|
|
get__no_antenna() {
|
|
if [[ $(iw_devices) == "" ]]
|
|
then
|
|
echo "value: true"
|
|
else
|
|
echo "value: false"
|
|
fi
|
|
}
|
|
|
|
get__status() {
|
|
local service_enabled=$(ynh_app_setting_get $app service_enabled)
|
|
if systemctl is-active hostapd -q
|
|
then
|
|
if [ $service_enabled -eq 1 ]
|
|
then
|
|
cat << EOF
|
|
style: success
|
|
ask:
|
|
en: |-
|
|
Your Hotspot is running :)
|
|
EOF
|
|
else
|
|
cat << EOF
|
|
style: warning
|
|
ask:
|
|
en: Your Hotspot is running, but it shouldn't !
|
|
EOF
|
|
fi
|
|
elif [ $service_enabled -eq 1 ]
|
|
then
|
|
cat << EOF
|
|
style: danger
|
|
ask:
|
|
en: |-
|
|
Your Hotspot is down ! Here are errors logged in the last 5 minutes
|
|
\`\`\`
|
|
$(journalctl -u hostapd -n10 -o cat | sed 's/^/ /g')
|
|
\`\`\`
|
|
EOF
|
|
else
|
|
cat << EOF
|
|
style: info
|
|
ask:
|
|
en: Your Hotspot is down as expected.
|
|
EOF
|
|
|
|
fi
|
|
}
|
|
|
|
get__wifi_device() {
|
|
if [[ $(iw_devices) == "" ]]
|
|
then
|
|
echo "choices: []"
|
|
else
|
|
cat << EOF
|
|
choices:
|
|
EOF
|
|
for device in $(iw_devices | sed "s/|/ /g")
|
|
do
|
|
echo " $device: $device"
|
|
done
|
|
fi
|
|
|
|
echo "value: '$(ynh_app_setting_get $app wifi_device)'"
|
|
}
|
|
|
|
get__dns() {
|
|
ip6_net=$(ynh_app_setting_get --app=$app --key=ip6_net)
|
|
ip6_dns=$(ynh_app_setting_get --app=$app --key=ip6_dns | tr -d '[]')
|
|
ip4_nat_prefix=$(ynh_app_setting_get --app=$app --key=ip4_nat_prefix)
|
|
ip4_dns=$(ynh_app_setting_get --app=$app --key=ip4_dns)
|
|
|
|
if [[ -n ${ip6_net} ]] && [[ -z ${ip6_dns} ]]; then
|
|
ip6_dns="${ip6_net}1"
|
|
fi
|
|
|
|
if [[ -n ${ip4_nat_prefix} ]] && [[ -z ${ip4_dns} ]]; then
|
|
ip4_dns="${ip4_nat_prefix}.1"
|
|
fi
|
|
|
|
echo "value: ${ip4_dns},${ip6_dns}"
|
|
}
|
|
|
|
#=================================================
|
|
# SPECIFIC VALIDATORS FOR TOML SHORT KEYS
|
|
#=================================================
|
|
|
|
validate__wifi_ssid() {
|
|
if [[ -z "${wifi_ssid}" ]]
|
|
then
|
|
echo 'SSID required'
|
|
fi
|
|
}
|
|
|
|
validate__wifi_passphrase() {
|
|
if [[ "${wifi_secure}" == "1" ]] && [[ -z "${wifi_passphrase}" ]]
|
|
then
|
|
echo 'In WPA2 secure mode, you need to provide a passphrase'
|
|
fi
|
|
}
|
|
|
|
validate__ip4_nat_prefix() {
|
|
if [[ -z "${ip4_nat_prefix}" ]]
|
|
then
|
|
echo 'Private IPv4 nat prefix required'
|
|
fi
|
|
}
|
|
|
|
validate__dns() {
|
|
if ! echo "${dns}" | grep -q "\."
|
|
then
|
|
echo 'IPv4 DNS required'
|
|
fi
|
|
if [[ -n "${ip6_net}" ]] && ! echo "${dns}" | grep -q ":"
|
|
then
|
|
echo 'IPv6 DNS required'
|
|
fi
|
|
}
|
|
|
|
#=================================================
|
|
# SPECIFIC SETTERS FOR TOML SHORT KEYS
|
|
#=================================================
|
|
|
|
set__dns() {
|
|
ip6_dns=""
|
|
ip4_dns=""
|
|
for ip in $(echo "${dns}" | tr ',' ' '); do
|
|
if [[ "$ip" == *":"* ]]; then
|
|
ip6_dns+="[$ip],"
|
|
else
|
|
ip4_dns+="$ip,"
|
|
fi
|
|
done
|
|
# Remove trailing ,
|
|
ip6_dns="${ip6_dns%%,}"
|
|
ip4_dns="${ip4_dns%%,}"
|
|
|
|
if [[ -n ${ip6_net} ]] && [[ -z ${ip6_dns} ]]; then
|
|
ip6_dns="${ip6_net}1"
|
|
fi
|
|
|
|
if [[ -n ${ip4_nat_prefix} ]] && [[ -z ${ip4_dns} ]]; then
|
|
ip4_dns="${ip4_nat_prefix}.1"
|
|
fi
|
|
|
|
ynh_app_setting_set $app ip6_dns "${ip6_dns}"
|
|
ynh_app_setting_set $app ip4_dns "${ip4_dns}"
|
|
}
|
|
|
|
#=================================================
|
|
# OVERWRITING VALIDATE STEP
|
|
#=================================================
|
|
|
|
#=================================================
|
|
# OVERWRITING APPLY STEP
|
|
#=================================================
|
|
ynh_app_config_apply() {
|
|
# Stop vpn client
|
|
ynh_print_info --message="Stopping hotspot in order to edit files"
|
|
/usr/local/bin/${service_name} stop
|
|
|
|
_ynh_app_config_apply
|
|
|
|
if [ "${wifi_secure}" -eq 1 ]; then
|
|
local sec_comment=""
|
|
else
|
|
local sec_comment="#"
|
|
fi
|
|
|
|
ynh_add_config --template="/etc/hostapd/$app/hostapd.conf.tpl" --destination="/etc/hostapd/$app/hostapd.conf"
|
|
ynh_add_config --template="/etc/dnsmasq.$app/dhcpdv4.conf.tpl" --destination="/etc/dnsmasq.$app/dhcpdv4.conf"
|
|
|
|
if [[ -n "${ip6_net}" ]] && [[ "${ip6_net}" != "none" ]]; then
|
|
ynh_add_config --template="/etc/dnsmasq.$app/dhcpdv6.conf.tpl" --destination="/etc/dnsmasq.$app/dhcpdv6.conf"
|
|
fi
|
|
|
|
# Start hotspot
|
|
ynh_print_info --message="Starting hotspot service if needed"
|
|
/usr/local/bin/${service_name} start
|
|
|
|
}
|
|
|
|
ynh_app_config_run $1
|