1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/hotspot_ynh.git synced 2024-09-03 19:25:53 +02:00

[fix] Config panel

This commit is contained in:
ljf 2021-09-20 18:01:28 +02:00
parent e0fe74fbbe
commit bb2f1b8b13
8 changed files with 207 additions and 121 deletions

View file

@ -19,7 +19,7 @@
dhcp-range=interface:<TPL:WIFI_DEVICE>,<TPL:IP4_NAT_PREFIX>.2,<TPL:IP4_NAT_PREFIX>.254,4h
# Send DHCPv4 option.
dhcp-option=option:dns-server,<TPL:IP4_DNS0>,<TPL:IP4_DNS1>
dhcp-option=option:dns-server,<TPL:IP4_DNS>
# Set the DHCP server to authoritative mode. In this mode it will barge in
# and take over the lease for any client which broadcasts on the network,

View file

@ -20,4 +20,4 @@
dhcp-range=interface:<TPL:WIFI_DEVICE>,<TPL:IP6_NET>,slaac,64,4h
# Send DHCPv6 option. Note [] around IPv6 addresses.
dhcp-option=option6:dns-server,[<TPL:IP6_DNS0>],[<TPL:IP6_DNS1>]
dhcp-option=option6:dns-server,<TPL:IP6_DNS>

View file

@ -19,7 +19,7 @@
dhcp-range=interface:<TPL:WIFI_DEVICE>,<TPL:IP4_NAT_PREFIX>.2,<TPL:IP4_NAT_PREFIX>.254,4h
# Send DHCPv4 option.
dhcp-option=option:dns-server,<TPL:IP4_DNS0>,<TPL:IP4_DNS1>
dhcp-option=option:dns-server,<TPL:IP4_DNS>
# Set the DHCP server to authoritative mode. In this mode it will barge in
# and take over the lease for any client which broadcasts on the network,

View file

@ -20,4 +20,4 @@
dhcp-range=interface:<TPL:WIFI_DEVICE>,<TPL:IP6_NET>,slaac,64,4h
# Send DHCPv6 option. Note [] around IPv6 addresses.
dhcp-option=option6:dns-server,[<TPL:IP6_DNS0>],[<TPL:IP6_DNS1>]
dhcp-option=option6:dns-server,<TPL:IP6_DNS>

View file

@ -140,8 +140,7 @@ start_dhcpd6() {
sed "s|<TPL:WIFI_DEVICE>|${dev}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
sed "s|<TPL:IP6_NET>|${ynh_ip6_net[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
sed "s|<TPL:IP6_DNS0>|${ynh_ip6_dns0[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
sed "s|<TPL:IP6_DNS1>|${ynh_ip6_dns1[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
sed "s|<TPL:IP6_DNS>|${ynh_ip6_dns[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf -p0
}
@ -152,8 +151,7 @@ start_dhcpd4() {
cp /etc/dnsmasq.dhcpd/dhcpdv4{.conf.tpl,-ssid${i}.conf}
sed "s|<TPL:IP4_DNS0>|${ynh_ip4_dns0[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
sed "s|<TPL:IP4_DNS1>|${ynh_ip4_dns1[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
sed "s|<TPL:IP4_DNS>|${ynh_ip4_dns[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
sed "s|<TPL:WIFI_DEVICE>|${dev}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
sed "s|<TPL:IP4_NAT_PREFIX>|${ynh_ip4_nat_prefix[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
@ -253,6 +251,7 @@ ynh_setting_get() {
setting=${2}
grep "^${setting}:" "/etc/yunohost/apps/${app}/settings.yml" | sed s/^[^:]\\+:\\s*[\"\']\\?// | sed s/\\s*[\"\']\$//
# '"
}
ynh_setting_set() {
@ -262,7 +261,7 @@ ynh_setting_set() {
# performance reasons (it takes a few second to run every yunohost commands)
# and to remove the need for the infamous '--need-lock' option/issue.
app="$1" key="$2" value="${3:-}" python2.7 - <<EOF
app="$1" key="$2" value="${3:-}" python3 - <<EOF
import os, yaml, sys
app = os.environ['app']
key, value = os.environ['key'], os.environ.get('value', None)
@ -304,11 +303,22 @@ if [ "$1" != restart ]; then
IFS='|' read -a ynh_ip6_addr <<< "$(ynh_setting_get hotspot ip6_addr)"
IFS='|' read -a ynh_ip6_firewall <<< "$(ynh_setting_get hotspot ip6_firewall)"
IFS='|' read -a ynh_ip6_net <<< "$(ynh_setting_get hotspot ip6_net)"
IFS='|' read -a ynh_ip6_dns0 <<< "$(ynh_setting_get hotspot ip6_dns0)"
IFS='|' read -a ynh_ip6_dns1 <<< "$(ynh_setting_get hotspot ip6_dns1)"
IFS='|' read -a ynh_ip4_dns0 <<< "$(ynh_setting_get hotspot ip4_dns0)"
IFS='|' read -a ynh_ip4_dns1 <<< "$(ynh_setting_get hotspot ip4_dns1)"
IFS='|' read -a ynh_dns <<< "$(ynh_setting_get hotspot dns)"
IFS='|' read -a ynh_ip4_nat_prefix <<< "$(ynh_setting_get hotspot ip4_nat_prefix)"
for i in $(seq 0 $((${ynh_multissid} - 1))); do
ynh_ip6_dns[${i}]=""
ynh_ip4_dns[${i}]=""
for ip in ; do
if [[ "$ip" == *":"* ]]
then
ynh_ip6_dns[${i}]+="[$ip],"
else
ynh_ip4_dns[${i}]+="$ip,"
fi
done
ynh_ip6_dns[${i}]="${ynh_ip6_dns[${i}]%%*,}"
ynh_ip4_dns[${i}]="${ynh_ip4_dns[${i}]%%*,}"
done
old_internet_device=$(ynh_setting_get hotspot internet_device)
new_internet_device=$(ip route | awk '/default via/ { print $NF; }')

View file

@ -7,24 +7,28 @@ name = "Auto-configuration"
name = "Service"
optional = false
[main.service.no_antenna]
ask = "No wifi antenna has been detected! If you use a wifi USB dongle and that this one is not listed here, try to unplug and replug it, then reload this page."
type = "alert"
style = "danger"
visible = "no_antenna"
[main.service.status]
ask = "The status of your VPN is unknown."
type = "alert"
style = "info"
visible = "! no_antenna"
[main.service.service_enabled]
ask = "Enable Hotspot"
type = "boolean"
[main.service.doc]
ask = "You need to select a wifi antenna interface. If you use a wifi USB dongle and that this one is not listed here, try to unplug and replug it, then reload this page."
type = "alert"
style = "warning"
visible = "! no_antenna"
[main.service.wifi_device]
ask = "Device"
type = "select"
choices.wlan0 = "wlan0"
visible = "! no_antenna"
[main.service.wifi_channel]
ask = "Channel"
@ -40,160 +44,196 @@ name = "Auto-configuration"
choices.9 = "9"
choices.10 = "10"
choices.11 = "11"
visible = "! no_antenna"
[main.service.ssid_nb]
[main.service.multissid]
ask = "Hotspot number"
type = "number"
min = 1
max = 3
type = "select"
choices.1 = "1"
choices.2 = "2"
choices.3 = "3"
visible = "! no_antenna"
[main.hotspot1]
name = "Hotspot 1"
optional = false
visible = "! no_antenna"
[main.hotspot1.wifi_ssid__1]
ask = "Name (SSID)"
type = "string"
pattern.regexp = '^[\w \-]{1,32}$"
pattern.error = 'SSID in this app are limited to letter, number space, dash and underscores."
bind = "array_settings()"
pattern.regexp = '^[\w \-]{1,32}$'
pattern.error = "SSID in this app are limited to letter, number space, dash and underscores."
[main.hotspot1.wifi_secure__1]
ask = "Secure"
type = "boolean"
bind = "array_settings()"
[main.hotspot1.wifi_passphrase__1]
ask = "Password (WPA2)"
type = "string"
bind = "array_settings()"
redact = true
optional = true
visible = "wifi_secure__1"
pattern.regexp = '^[a-zA-Z0-9]{8,63}$'
pattern.error = "Only printable alphanumeric characters are permitted in your password. Maximal size 63 chars"
[main.hotspot1.advanced__1]
ask = "Advanced settings"
type = "boolean"
bind = "array_settings()"
[main.hotspot1.ip4_nat_prefix__1]
ask = "IPv4 NAT prefix (/24)"
type = "string"
bind = "array_settings()"
visible = "advanced__1"
pattern.regexp = "^[0-9.]{7,15}$"
pattern.error = "Please provide a valid IP"
pattern.regexp = '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'
pattern.error = "Please provide a private /24 range in the format xxx.xxx.xxx"
[main.hotspot1.ip6_net__1]
ask = "IPv6 delegated prefix"
type = "string"
bind = "array_settings()"
optional = "true"
visible = "advanced__1"
pattern.regexp = "^[0-9a-fA-F:]+$"
pattern.regexp = '^[0-9a-fA-F:]+$'
pattern.error = "Please provide a valid IPv6 Prefix"
[main.hotspot1.ip6_firewall__1]
ask = "IPv6 firewall"
type = "boolean"
bind = "array_settings()"
visible = "advanced__1"
[main.hotspot1.dns__1]
ask = "DNS resolvers"
type = "tags"
bind = "array_settings()"
visible = "advanced__1"
pattern.regexp = "^([0-9.]{7,15}|[0-9a-fA-F:]+)$"
pattern.regexp = '^([0-9.]{7,15}|[0-9a-fA-F:]+)$'
pattern.error = "Not an ip"
[main.hotspot2]
name = "Hotspot 2"
visible = "ssid_nb >= 2"
visible = "! no_antenna && multissid >= 2"
[main.hotspot2.wifi_ssid__2]
ask = "Name (SSID)"
type = "string"
pattern.regexp = '^[\w \-]{1,32}$"
pattern.error = 'SSID in this app are limited to letter, number space, dash and underscores."
bind = "array_settings()"
pattern.regexp = '^[\w \-]{1,32}$'
pattern.error = "SSID in this app are limited to letter, number space, dash and underscores."
[main.hotspot2.wifi_secure__2]
ask = "Secure"
type = "boolean"
bind = "array_settings()"
[main.hotspot2.wifi_passphrase__2]
ask = "Password (WPA2)"
type = "string"
bind = "array_settings()"
redact = true
visible = "wifi_secure__2"
pattern.regexp = '^[a-zA-Z0-9]{8,63}$'
pattern.error = "Only printable alphanumeric characters are permitted in your password. Maximal size 63 chars"
[main.hotspot2.advanced__2]
ask = "Advanced settings"
type = "boolean"
bind = "array_settings()"
[main.hotspot2.ip4_nat_prefix__2]
ask = "IPv4 NAT prefix (/24)"
type = "string"
bind = "array_settings()"
visible = "advanced__2"
pattern.regexp = "^[0-9.]{7,15}$"
pattern.regexp = '^[0-9.]{7,15}$'
pattern.error = "Please provide a valid IP"
[main.hotspot2.ip6_net__2]
ask = "IPv6 delegated prefix"
type = "string"
bind = "array_settings()"
visible = "advanced__2"
pattern.regexp = "^[0-9a-fA-F:]+$"
pattern.regexp = '^[0-9a-fA-F:]+$'
pattern.error = "Please provide a valid IPv6 Prefix"
[main.hotspot2.ip6_firewall__2]
ask = "IPv6 firewall"
type = "boolean"
bind = "array_settings()"
visible = "advanced__2"
[main.hotspot2.dns__2]
ask = "DNS resolvers"
type = "tags"
bind = "array_settings()"
visible = "advanced__2"
pattern.regexp = "^([0-9.]{7,15}|[0-9a-fA-F:]+)$"
pattern.regexp = '^([0-9.]{7,15}|[0-9a-fA-F:]+)$'
pattern.error = "Not an ip"
[main.hotspot3]
name = "Hotspot 3"
visible = "ssid_nb >= 3"
visible = "! no_antenna && multissid >= 3"
[main.hotspot3.wifi_ssid__3]
ask = "Name (SSID)"
type = "string"
pattern.regexp = '^[\w \-]{1,32}$"
pattern.error = 'SSID in this app are limited to letter, number space, dash and underscores."
bind = "array_settings()"
pattern.regexp = '^[\w \-]{1,32}$'
pattern.error = "SSID in this app are limited to letter, number space, dash and underscores."
[main.hotspot3.wifi_secure__3]
ask = "Secure"
type = "boolean"
bind = "array_settings()"
[main.hotspot3.wifi_passphrase__3]
ask = "Password (WPA2)"
type = "string"
bind = "array_settings()"
redact = true
visible = "wifi_secure__3"
pattern.regexp = '^[a-zA-Z0-9]{8,63}$'
pattern.error = "Only printable alphanumeric characters are permitted in your password. Maximal size 63 chars"
[main.hotspot3.advanced__3]
ask = "Advanced settings"
type = "boolean"
bind = "array_settings()"
[main.hotspot3.ip4_nat_prefix__3]
ask = "IPv4 NAT prefix (/24)"
type = "string"
bind = "array_settings()"
visible = "advanced__3"
pattern.regexp = "^[0-9.]{7,15}$"
pattern.regexp = '^[0-9.]{7,15}$'
pattern.error = "Please provide a valid IP"
[main.hotspot3.ip6_net__3]
ask = "IPv6 delegated prefix"
type = "string"
bind = "array_settings()"
visible = "advanced__3"
pattern.regexp = "^[0-9a-fA-F:]+$"
pattern.regexp = '^[0-9a-fA-F:]+$'
pattern.error = "Please provide a valid IPv6 Prefix"
[main.hotspot3.ip6_firewall__3]
ask = "IPv6 firewall"
type = "boolean"
bind = "array_settings()"
visible = "advanced__3"
[main.hotspot3.dns__3]
ask = "DNS resolvers"
type = "tags"
bind = "array_settings()"
visible = "advanced__3"
pattern.regexp = "^([0-9.]{7,15}|[0-9a-fA-F:]+)$"
pattern.regexp = '^([0-9.]{7,15}|[0-9a-fA-F:]+)$'
pattern.error = "Not an ip"

View file

@ -22,21 +22,26 @@ ynh_abort_if_errors
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__no_antenna() {
if [[ $(iw_devices) == "" ]]
then
echo "value: false"
else
cat << EOF
value: false
ask: Wifi antenna correctly detected
style: info
EOF
fi
}
get__status() {
local service_enabled=$(ynh_app_setting_get $app service_enabled)
if ip route get 1.2.3.4 | grep -q tun0
if systemctl is-active hostapd -q
then
if [ $service_enabled -eq 1 ]
then
@ -44,16 +49,13 @@ get__status() {
style: success
ask:
en: |-
Your VPN is running :)
**IPv4:** \`$(curl https://ip.yunohost.org --silent)\`
**IPv6:** \`$(curl https://ip6.yunohost.org --silent)\`
Your Hotspot is running :)
EOF
else
cat << EOF
style: warning
ask:
en: Your VPN is running, but it shouldn't !
en: Your Hotspot is running, but it shouldn't !
EOF
fi
elif [ $service_enabled -eq 1 ]
@ -62,91 +64,139 @@ EOF
style: danger
ask:
en: |-
Your VPN is down ! Here are errors logged in the last 5 minutes
Your Hotspot 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)
$(journalctl -u hostapd -n10 -o cat | sed 's/^/ /g')
\`\`\`
EOF
else
cat << EOF
style: info
ask:
en: Your VPN is down has expected.
en: Your Hotspot is down has expected.
EOF
fi
}
get__login_user() {
if [ -s /etc/openvpn/keys/credentials ]
get__wifi_device() {
if [[ $(iw_devices) == "" ]]
then
echo "$(sed -n 1p /etc/openvpn/keys/credentials)"
echo "choices: []"
else
echo ""
cat << EOF
choices:
EOF
for device in $(iw_devices)
do
echo " $device: $device"
done
fi
}
get__login_passphrase() {
if [ -s /etc/openvpn/keys/credentials ]
then
echo "$(sed -n 2p /etc/openvpn/keys/credentials)"
else
echo ""
fi
get__array_settings() {
local short_setting="${1%%__*}"
local index="${1#*__}"
IFS='|' read -a values <<< "$(ynh_app_setting_get $app $short_setting)"
echo "value: \"${values[$(($index - 1))]:-}\""
}
#=================================================
# SPECIFIC VALIDATORS FOR TOML SHORT KEYS
#=================================================
validate__login_user() {
if grep -q '^\s*auth-user-pass' ${config_file}
is_unique() {
local short_setting="$1"
local short_setting__1="$1__1"
local short_setting__2="$1__2"
local short_setting__3="$1__3"
if [[ "${!short_setting__1}" == "${!short_setting__2}" ]]
then
if [[ -z "${login_user}" ]]
then
echo 'A Username is needed with this configuration file'
fi
return 1
elif [ "$multissid" -ge "3" ] && [[ "${!short_setting__1}" == "${!short_setting__3}" ]]
then
return 1
elif [ "$multissid" -ge "3" ] && [[ "${!short_setting__2}" == "${!short_setting__3}" ]]
then
return 1
fi
return 0
}
validate__wifi_ssid() {
local wifi_ssid_var="wifi_ssid__$1"
if [ "$multissid" -ge "$1" ] && [[ -z "${!wifi_ssid_var}" ]]
then
echo 'SSID required'
fi
if ! is_unique wifi_ssid
then
echo 'All Wifi names must be unique'
fi
}
validate__wifi_passphrase() {
local wifi_secure_var="wifi_secure__$1"
local wifi_passphrase_var="wifi_passphrase__$1"
if [ "$multissid" -ge "$1" ] && [[ "${!wifi_secure_var}" == "1" ]] && [[ -z "${!wifi_passphrase_var}" ]]
then
echo 'In WPA2 secure mode, you need to provide a passphrase'
fi
}
validate__ip4_nat_prefix() {
local ip4_nat_prefix_var="ip4_nat_prefix__$1"
if [ "$multissid" -ge "$1" ] && [[ -z "${!ip4_nat_prefix_var}" ]]
then
echo 'Private IPv4 nat prefix required'
fi
if ! is_unique ip4_nat_prefix
then
echo 'All IPv4 prefix must be unique'
fi
}
validate__dns() {
local dns_var="dns__$1"
local ip6_net_var="dns__$1"
if [ "$multissid" -ge "$1" ] && ! echo "${!dns_var}" | grep -q "\."
then
echo 'IPv4 DNS required'
fi
if [ "$multissid" -ge "$1" ] && [[ -n "${!ip6_net_var}" ]] && ! echo "${!dns_var}" | grep -q ":"
then
echo 'IPv6 DNS required'
fi
}
validate__array_settings() {
local short_setting="${1%%__*}"
local index="${1#*__}"
if type -t validate__$short_setting | grep -q '^function$' 2>/dev/null;
then
validate__$short_setting $index
fi
}
#=================================================
# SPECIFIC SETTERS FOR TOML SHORT KEYS
#=================================================
set__login_user() {
if [ -n "${login_user}" ]
set__array_settings() {
local short_setting="${1%%__*}"
local index="${1#*__}"
local type="${types[$1]}"
local value="${!1}"
if [[ "$type" == "string" ]] && [ "$multissid" -lt "$index" ]
then
echo "${login_user}\n${login_passphrase}" > /etc/openvpn/keys/credentials
set_right /etc/openvpn/keys/credentials
else
echo "" > /etc/openvpn/keys/credentials
value=""
fi
local values="$(ynh_app_setting_get $app $short_setting | awk 'BEGIN{FS=OFS="|"} {$'$index'="'${!1}'"}'1)"
ynh_app_setting_set --app=$app --key=$short_setting --value="$values"
ynh_print_info --message="Configuration key '$short_setting' edited in app settings"
}
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
}
@ -157,25 +207,14 @@ ynh_app_config_validate() {
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
ynh_print_info --message="Stopping hotspot in order to edit files"
/usr/local/bin/ynh-hotspot 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_print_info --message="Starting hotspot service if needed"
/usr/local/bin/ynh-hotspot start
}

View file

@ -180,10 +180,7 @@ ynh_app_setting_set --app=$app --key=wifi_channel --value=6
ynh_app_setting_set --app=$app --key=ip6_addr --value="${ip6_addr}"
ynh_app_setting_set --app=$app --key=ip6_firewall --value=1
ynh_app_setting_set --app=$app --key=ip6_net --value="${ip6_net}"
ynh_app_setting_set --app=$app --key=ip6_dns0 --value=2001:913::8
ynh_app_setting_set --app=$app --key=ip6_dns1 --value=2001:910:800::12
ynh_app_setting_set --app=$app --key=ip4_dns0 --value=80.67.188.188
ynh_app_setting_set --app=$app --key=ip4_dns1 --value=80.67.169.12
ynh_app_setting_set --app=$app --key=dns --value="2001:913::8,2001:910:800::12,80.67.188.188,80.67.169.12"
ynh_app_setting_set --app=$app --key=ip4_nat_prefix --value=10.0.242
ynh_app_setting_set --app=$app --key=vpnclient --value=no