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

remove multissid

This commit is contained in:
HgO 2023-08-20 15:22:11 +02:00
parent 4325af2a2b
commit f02097b99e
6 changed files with 191 additions and 513 deletions

View file

@ -1,7 +0,0 @@
__BSS_COMMENT__bss=__WIFI_INTERFACE__
ssid=__WIFI_SSID__
__SEC_COMMENT__wpa=2
__SEC_COMMENT__wpa_passphrase=__WIFI_PASSPHRASE__
__SEC_COMMENT__wpa_key_mgmt=WPA-PSK
__SEC_COMMENT__wpa_pairwise=TKIP
__SEC_COMMENT__rsn_pairwise=CCMP

View file

@ -1,8 +1,12 @@
interface=__WIFI_DEVICE__ interface=__WIFI_DEVICE__
hw_mode=g hw_mode=g
__N_COMMENT__ieee80211n=1
__N_COMMENT__wmm_enabled=1
macaddr_acl=0 macaddr_acl=0
auth_algs=1 auth_algs=1
ignore_broadcast_ssid=0 ignore_broadcast_ssid=0
channel=__WIFI_CHANNEL__ channel=__WIFI_CHANNEL__
ssid=__WIFI_SSID__
__SEC_COMMENT__wpa=2
__SEC_COMMENT__wpa_passphrase=__WIFI_PASSPHRASE__
__SEC_COMMENT__wpa_key_mgmt=WPA-PSK
__SEC_COMMENT__wpa_pairwise=TKIP
__SEC_COMMENT__rsn_pairwise=CCMP

View file

@ -17,65 +17,49 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
source /usr/share/yunohost/helpers
# Functions # Functions
## State functions ## State functions
has_ip6delegatedprefix() { has_ip6delegatedprefix() {
local i=${1} [[ -n "${ip6_net}" ]] && [[ "${ip6_net}" != "none" ]]
[[ -n "${ip6_net[${i}]}" ]] && [[ "${ip6_net[${i}]}" != "none" ]]
} }
ip6addrfromdelegatedprefix() { ip6addrfromdelegatedprefix() {
local i=${1} echo "${ip6_net}1"
echo "${ip6_net[${i}]}${i}001"
} }
is_nat_set() { is_nat_set() {
local gateway_interface=${1} local gateway_interface=${1}
iptables -w -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${gateway_interface}" iptables -w -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${gateway_interface}"
} }
is_ip4nataddr_set() { is_ip4nataddr_set() {
local i=${1} ip address show dev "${wifi_device}" 2>/dev/null | grep -q "${ip4_nat_prefix}.1/24"
local dev=$(devfromid "${i}")
ip address show dev "${dev}" 2>/dev/null | grep -q "${ip4_nat_prefix[${i}]}.1/24"
} }
is_ip6addr_set() { is_ip6addr_set() {
local i=${1} ip address show dev "${wifi_device}" 2>/dev/null | grep -q "$(ip6addrfromdelegatedprefix)/64"
local dev=$(devfromid "${i}")
ip address show dev "${dev}" 2>/dev/null | grep -q "$(ip6addrfromdelegatedprefix $i)/64"
} }
is_ip6firewall_set() { is_ip6firewall_set() {
local i=${1} ip6tables -w -nvL FORWARD | grep DROP | grep -q "${wifi_device}"
local dev=$(devfromid "${i}")
ip6tables -w -nvL FORWARD | grep DROP | grep -q "${dev}"
} }
is_forwarding_set() { is_forwarding_set() {
local ip6=$(sysctl net.ipv6.conf.all.forwarding | awk '{ print $NF; }') local ip6=$(sysctl net.ipv6.conf.all.forwarding | awk '{ print $NF; }')
local ip4=$(sysctl net.ipv4.conf.all.forwarding | awk '{ print $NF; }') local ip4=$(sysctl net.ipv4.conf.all.forwarding | awk '{ print $NF; }')
[ "${ip6}" -eq 1 ] && [ "${ip4}" -eq 1 ] [[ "${ip6}" -eq 1 ]] && [[ "${ip4}" -eq 1 ]]
} }
is_dhcpd6_running() { is_dhcpd6_running() {
local i=${1} [[ -e "/run/dnsmasq/dnsmasq-dhcpdv6-ssid-${wifi_device}.pid" ]] && ps -p $(cat "/run/dnsmasq/dnsmasq-dhcpdv6-ssid-${wifi_device}.pid") > /dev/null
[[ -e "/run/dnsmasq/dnsmasq-dhcpdv6-ssid${i}.pid" ]] && ps -p $(cat "/run/dnsmasq/dnsmasq-dhcpdv6-ssid${i}.pid") > /dev/null
} }
is_dhcpd4_running() { is_dhcpd4_running() {
local i=${1} [[ -e "/run/dnsmasq/dnsmasq-dhcpdv4-ssid-${wifi_device}.pid" ]] && ps -p $(cat "/run/dnsmasq/dnsmasq-dhcpdv4-ssid-${wifi_device}.pid") > /dev/null
[[ -e "/run/dnsmasq/dnsmasq-dhcpdv4-ssid${i}.pid" ]] && ps -p $(cat "/run/dnsmasq/dnsmasq-dhcpdv4-ssid${i}.pid") > /dev/null
} }
is_hostapd_running() { is_hostapd_running() {
@ -83,18 +67,39 @@ is_hostapd_running() {
} }
is_running() { is_running() {
for i in $(seq 0 $((${multissid} - 1))); do if has_ip6delegatedprefix; then
(has_ip6delegatedprefix ${i} && is_ip6addr_set ${i} \ if ! is_ip6addr_set; then
&& ([ "${ip6_firewall[${i}]}" -eq 1 ] && is_ip6firewall_set ${i} || [ "${ip6_firewall[${i}]}" -eq 0 ]) \
&& is_dhcpd6_running ${i} || ! has_ip6delegatedprefix ${i}) \
&& is_ip4nataddr_set ${i} && is_dhcpd4_running ${i}
if [ ! $? -eq 0 ]; then
return 1 return 1
fi fi
done if [[ "${ip6_firewall}" -eq 1 ]] && ! is_ip6firewall_set; then
return 1
fi
if ! is_dhcpd6_running; then
return 1
fi
fi
is_hostapd_running && is_forwarding_set && ([ -z "${new_gateway_interface}" ] || is_nat_set "${new_gateway_interface}") if ! is_ip4nataddr_set; then
return 1
fi
if ! is_dhcpd4_running; then
return 1
fi
if ! is_hostapd_running; then
return 1
fi
if ! is_forwarding_set; then
return 1
fi
if [[ -n ${new_gateway_interface} ]] && ! is_nat_set "${new_gateway_interface}"; then
return 1
fi
return 0
} }
## Setters ## Setters
@ -106,33 +111,26 @@ set_nat() {
} }
set_ipaddr() { set_ipaddr() {
local i=${1} if ! is_ip4nataddr_set; then
local dev=$(devfromid "${i}") echo "hotspot ${wifi_device}: Set IPv4 NAT address"
ip address add "${ip4_nat_prefix}.1/24" dev "${wifi_device}"
if ! is_ip4nataddr_set ${i}; then
echo "hotspot${i}: Set IPv4 NAT address"
ip address add "${ip4_nat_prefix[${i}]}.1/24" dev "${dev}"
fi fi
if has_ip6delegatedprefix ${i} && ! is_ip6addr_set ${i}; then if has_ip6delegatedprefix && ! is_ip6addr_set; then
echo "hotspot${i}: Set IPv6 address" echo "hotspot ${wifi_device}: Set IPv6 address"
ip address delete "$(ip6addrfromdelegatedprefix $i)/64" dev tun0 &>/dev/null ip address delete "$(ip6addrfromdelegatedprefix)/64" dev tun0 &>/dev/null
ip address add "$(ip6addrfromdelegatedprefix $i)/64" dev "${dev}" ip address add "$(ip6addrfromdelegatedprefix)/64" dev "${wifi_device}"
fi fi
} }
set_ipfirewall() { set_ipfirewall() {
local i=${1}
local dev=$(devfromid "${i}")
# Set ipv6 firewalling # Set ipv6 firewalling
if has_ip6delegatedprefix ${i} && [ "${ip6_firewall[${i}]}" -eq 1 ] && ! is_ip6firewall_set ${i}; then if has_ip6delegatedprefix && [[ "${ip6_firewall}" -eq 1 ]] && ! is_ip6firewall_set; then
echo "hotspot${i}: Set IPv6 firewalling" echo "hotspot ${wifi_device}: Set IPv6 firewalling"
ip6tables -w -A FORWARD -i "${dev}" -j ACCEPT ip6tables -w -A FORWARD -i "${wifi_device}" -j ACCEPT
ip6tables -w -A FORWARD -o "${dev}" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT ip6tables -w -A FORWARD -o "${wifi_device}" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
ip6tables -w -A FORWARD -o "${dev}" -j DROP ip6tables -w -A FORWARD -o "${wifi_device}" -j DROP
fi fi
} }
set_forwarding() { set_forwarding() {
@ -141,62 +139,22 @@ set_forwarding() {
} }
start_dhcpd() { start_dhcpd() {
local i=${1}
local dev=$(devfromid "${i}")
# Run DHCPv4 server # Run DHCPv4 server
if ! is_dhcpd4_running ${i}; then if ! is_dhcpd4_running; then
echo "hotspot${i}: Start the DHCPv4 server (dnsmasq)" echo "hotspot ${wifi_device}: Start the DHCPv4 server (dnsmasq)"
dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv4-ssid-${wifi_device}.conf -p0 -x /run/dnsmasq/dnsmasq-dhcpv4-ssid-${wifi_device}.pid
cp /etc/dnsmasq.dhcpd/dhcpdv4{.conf.tpl,-ssid${i}.conf}
sed "s|__WIFI_DEVICE__|${dev}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
sed "s|__IP4_DNS__|${ip4_dns[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
sed "s|__IP4_NAT_PREFIX__|${ip4_nat_prefix[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf -p0 -x /run/dnsmasq/dnsmasq-dhcpv4-ssid${i}.pid
fi fi
# Run DHCPv6 server # Run DHCPv6 server
if has_ip6delegatedprefix ${i} && ! is_dhcpd6_running ${i}; then if has_ip6delegatedprefix && ! is_dhcpd6_running; then
echo "hotspot${i}: Start the NDP and DHCPv6 server (dnsmasq)" echo "hotspot ${wifi_device}: Start the NDP and DHCPv6 server (dnsmasq)"
dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv6-ssid-${wifi_device}.conf -p0 -x /run/dnsmasq/dnsmasq-dhcpv6-ssid-${wifi_device}.pid
cp /etc/dnsmasq.dhcpd/dhcpdv6{.conf.tpl,-ssid${i}.conf}
sed "s|__WIFI_DEVICE__|${dev}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
sed "s|__IP6_DNS__|${ip6_dns[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
sed "s|__IP6_NET__|${ip6_net[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf -p0 -x /run/dnsmasq/dnsmasq-dhcpv6-ssid${i}.pid
fi fi
} }
configure_hostapd() { configure_hostapd() {
local ethaddr=$(ip link show dev "${wifi_device}" | grep link/ether | awk -F: '{ printf "02:%s:%s:%s:%s:00", $2, $3, $4, $5 }') local ethaddr=$(ip link show dev "${wifi_device}" | grep link/ether | awk -F: '{ printf "02:%s:%s:%s:%s:00", $2, $3, $4, $5 }')
ip link set addr "${ethaddr}" dev "${wifi_device}" ip link set addr "${ethaddr}" dev "${wifi_device}"
cp /etc/hostapd/hostapd.base.conf /etc/hostapd/hostapd.conf
sed "s|__WIFI_DEVICE__|${wifi_device}|g" -i /etc/hostapd/hostapd.conf
sed "s|__WIFI_CHANNEL__|${wifi_channel}|g" -i /etc/hostapd/hostapd.conf
sed "s|__N_COMMENT__||g" -i /etc/hostapd/hostapd.conf
for i in $(seq 0 $((${multissid} - 1))); do
[ "${wifi_secure[${i}]}" -eq 1 ] && local sec_comment="" || local sec_comment="#"
[ "${i}" -eq 0 ] && local bss_comment="#" || local bss_comment=""
cp /etc/hostapd/hostapd.accesspoint.conf /etc/hostapd/hostapd.conf.tmp
sed "s|__WIFI_INTERFACE__|hotspot${i}|g" -i /etc/hostapd/hostapd.conf.tmp
sed "s|__WIFI_SSID__|${wifi_ssid[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp
sed "s|__WIFI_PASSPHRASE__|${wifi_passphrase[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp
sed "s|__SEC_COMMENT__|${sec_comment}|g" -i /etc/hostapd/hostapd.conf.tmp
sed "s|__BSS_COMMENT__|${bss_comment}|g" -i /etc/hostapd/hostapd.conf.tmp
cat /etc/hostapd/hostapd.conf.tmp >>/etc/hostapd/hostapd.conf
rm /etc/hostapd/hostapd.conf.tmp
done
} }
## Unsetters ## Unsetters
@ -208,29 +166,23 @@ unset_nat() {
} }
unset_ipaddr() { unset_ipaddr() {
local i=${1} if is_ip4nataddr_set; then
local dev=$(devfromid "${i}") echo "hotspot ${wifi_device}: Unset IPv4 NAT address"
ip address delete "${ip4_nat_prefix}.1/24" dev "${wifi_device}"
if is_ip4nataddr_set ${i}; then
echo "hotspot${i}: Unset IPv4 NAT address"
ip address delete "${ip4_nat_prefix[${i}]}.1/24" dev "${dev}"
fi fi
if has_ip6delegatedprefix ${i} && is_ip6addr_set ${i}; then if has_ip6delegatedprefix && is_ip6addr_set; then
echo "hotspot${i}: Unset IPv6 address" echo "hotspot ${wifi_device}: Unset IPv6 address"
ip address delete "$(ip6addrfromdelegatedprefix $i)/64" dev "${dev}" ip address delete "$(ip6addrfromdelegatedprefix)/64" dev "${wifi_device}"
fi fi
} }
unset_ipfirewall() { unset_ipfirewall() {
local i=${1} if has_ip6delegatedprefix && [[ "${ip6_firewall}" -eq 1 ]] && is_ip6firewall_set; then
local dev=$(devfromid "${i}") echo "hotspot ${wifi_device}: Unset IPv6 firewalling"
ip6tables -w -D FORWARD -i "${wifi_device}" -j ACCEPT
if has_ip6delegatedprefix ${i} && [ "${ip6_firewall[${i}]}" -eq 1 ] && is_ip6firewall_set ${i}; then ip6tables -w -D FORWARD -o "${wifi_device}" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
echo "hotspot${i}: Unset IPv6 firewalling" ip6tables -w -D FORWARD -o "${wifi_device}" -j DROP
ip6tables -w -D FORWARD -i "${dev}" -j ACCEPT
ip6tables -w -D FORWARD -o "${dev}" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
ip6tables -w -D FORWARD -o "${dev}" -j DROP
fi fi
} }
@ -240,81 +192,23 @@ unset_forwarding() {
} }
stop_dhcpd() { stop_dhcpd() {
local i=${1} if is_dhcpd6_running; then
echo "hotspot ${wifi_device}: Stop the NDP and DHCPv6 server (dnsmasq)"
if is_dhcpd6_running ${i}; then kill $(cat /run/dnsmasq/dnsmasq-dhcpdv6-ssid-${wifi_device}.pid)
echo "hotspot${i}: Stop the NDP and DHCPv6 server (dnsmasq)" rm -f /run/dnsmasq/dnsmasq-dhcpdv6-ssid-${wifi_device}.pid
kill $(cat /run/dnsmasq/dnsmasq-dhcpdv6-ssid${i}.pid)
rm -f /run/dnsmasq/dnsmasq-dhcpdv6-ssid${1}.pid
rm -f /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
fi fi
if is_dhcpd4_running ${i}; then if is_dhcpd4_running; then
echo "hotspot${i}: Stop the DHCPv4 server (dnsmasq)" echo "hotspot ${wifi_device}: Stop the DHCPv4 server (dnsmasq)"
kill $(cat /run/dnsmasq/dnsmasq-dhcpdv4-ssid${i}.pid) kill $(cat /run/dnsmasq/dnsmasq-dhcpdv4-ssid-${wifi_device}.pid)
rm -f /run/dnsmasq/dnsmasq-dhcpdv4-ssid${1}.pid rm -f /run/dnsmasq/dnsmasq-dhcpdv4-ssid-${wifi_device}.pid
rm -f /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
fi fi
} }
stop_dhcpd4() {
:
}
stop_hostapd() { stop_hostapd() {
systemctl stop hostapd systemctl stop hostapd
} }
## Tools
ynh_setting_get() {
APP="$1" KEY="$2" python3 - <<EOF
import os, yaml, sys
app = os.environ['APP']
key = os.environ['KEY']
setting_file = "/etc/yunohost/apps/%s/settings.yml" % app
assert os.path.exists(setting_file), "Setting file %s does not exists ?" % setting_file
with open(setting_file) as f:
settings = yaml.safe_load(f)
if key in settings:
print(settings[key])
EOF
}
ynh_setting_set() {
# This is a partial copypasta of the official ynh_app_setting internal helper
# In particular, we do this instead of relying on 'yunohost app setting' for
# 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:-}" python3 - <<EOF
import os, yaml, sys
app = os.environ['APP']
key, value = os.environ['KEY'], os.environ.get('VALUE', None)
setting_file = "/etc/yunohost/apps/%s/settings.yml" % app
assert os.path.exists(setting_file), "Setting file %s does not exists ?" % setting_file
with open(setting_file) as f:
settings = yaml.load(f)
settings[key] = value
with open(setting_file, "w") as f:
yaml.safe_dump(settings, f, default_flow_style=False)
EOF
}
devfromid() {
local i=${1}
if [ "${i}" -eq 0 ]; then
echo "${wifi_device}"
else
echo "hotspot${i}"
fi
}
if [ "$1" != restart ]; then if [ "$1" != restart ]; then
# Variables # Variables
@ -322,33 +216,30 @@ if [ "$1" != restart ]; then
echo -n "Retrieving Yunohost settings... " echo -n "Retrieving Yunohost settings... "
service_enabled=$(systemctl is-enabled ynh-hotspot) service_enabled=$(systemctl is-enabled ynh-hotspot)
wifi_device=$(ynh_setting_get hotspot wifi_device) wifi_device=$(ynh_app_setting_get hotspot wifi_device)
wifi_channel=$(ynh_setting_get hotspot wifi_channel) wifi_channel=$(ynh_app_setting_get hotspot wifi_channel)
multissid=$(ynh_setting_get hotspot multissid)
IFS='|' read -a wifi_ssid <<<"$(ynh_setting_get hotspot wifi_ssid)" wifi_ssid=$(ynh_app_setting_get hotspot wifi_ssid)
IFS='|' read -a wifi_secure <<<"$(ynh_setting_get hotspot wifi_secure)" wifi_secure=$(ynh_app_setting_get hotspot wifi_secure)
IFS='|' read -a wifi_passphrase <<<"$(ynh_setting_get hotspot wifi_passphrase)" wifi_passphrase=$(ynh_app_setting_get hotspot wifi_passphrase)
IFS='|' read -a ip6_firewall <<<"$(ynh_setting_get hotspot ip6_firewall)" ip6_firewall=$(ynh_app_setting_get hotspot ip6_firewall)
IFS='|' read -a ip6_net <<<"$(ynh_setting_get hotspot ip6_net)" ip6_net=$(ynh_app_setting_get hotspot ip6_net)
IFS='|' read -a dns <<<"$(ynh_setting_get hotspot dns)" dns=$(ynh_app_setting_get hotspot dns)
IFS='|' read -a ip4_nat_prefix <<<"$(ynh_setting_get hotspot ip4_nat_prefix)" ip4_nat_prefix=$(ynh_app_setting_get hotspot ip4_nat_prefix)
for i in $(seq 0 $((${multissid} - 1))); do ip6_dns=""
ip6_dns[${i}]="" ip4_dns=""
ip4_dns[${i}]="" for ip in $(echo "${dns}" | tr ',' ' '); do
for ip in $(echo "${dns[${i}]}" | tr ',' ' '); do
if [[ "$ip" == *":"* ]]; then if [[ "$ip" == *":"* ]]; then
ip6_dns[${i}]+="[$ip]," ip6_dns+="[$ip],"
else else
ip4_dns[${i}]+="$ip," ip4_dns+="$ip,"
fi fi
done done
# Remove trailing , # Remove trailing ,
ip6_dns[${i}]="${ip6_dns[${i}]%%,}" ip6_dns="${ip6_dns%%,}"
ip4_dns[${i}]="${ip4_dns[${i}]%%,}" ip4_dns="${ip4_dns%%,}"
done
old_gateway_interface=$(ynh_setting_get hotspot gateway_interface) old_gateway_interface=$(ynh_app_setting_get hotspot gateway_interface)
new_gateway_interface=$(ip route get 1.2.3.4 | awk '{ print $5; }') new_gateway_interface=$(ip route get 1.2.3.4 | awk '{ print $5; }')
echo "OK" echo "OK"
@ -361,12 +252,12 @@ start)
if is_running; then if is_running; then
echo "Already started" echo "Already started"
exit 0 exit 0
elif [ "${service_enabled}" != "enabled" ]; then elif [[ "${service_enabled}" != "enabled" ]]; then
echo "Not starting because hotspod service is disabled" echo "Not starting because hotspod service is disabled"
exit 1 exit 1
fi fi
if [ -z "${wifi_device}" ]; then if [[ -z "${wifi_device}" ]]; then
echo "[FAIL] No wifi device selected. Make sure your wifi antenna is plugged-in / available and select it in the Hotspot admin" echo "[FAIL] No wifi device selected. Make sure your wifi antenna is plugged-in / available and select it in the Hotspot admin"
exit 1 exit 1
fi fi
@ -375,13 +266,12 @@ start)
touch /tmp/.ynh-hotspot-started touch /tmp/.ynh-hotspot-started
# Check old state of the ipv4 NAT settings # Check old state of the ipv4 NAT settings
if [ -n "${old_gateway_interface}" ] && [ "${new_gateway_interface}" != "${old_gateway_interface}" ] && is_nat_set "${old_gateway_interface}"; then if [[ -n "${old_gateway_interface}" ]] && [[ "${new_gateway_interface}" != "${old_gateway_interface}" ]] && is_nat_set "${old_gateway_interface}"; then
unset_nat "${old_gateway_interface}" unset_nat "${old_gateway_interface}"
fi fi
# Set ipv4 NAT # Set ipv4 NAT
if [ -n "${new_gateway_interface}" ] && ! is_nat_set "${new_gateway_interface}"; then if [[ -n "${new_gateway_interface}" ]] && ! is_nat_set "${new_gateway_interface}"; then
echo "Set NAT" echo "Set NAT"
set_nat "${new_gateway_interface}" set_nat "${new_gateway_interface}"
fi fi
@ -402,38 +292,20 @@ start)
fi fi
sleep 1 sleep 1
# On single SSID, the hotspot interface will be wlan0 (or similar)
# in multissid, we additionally want to make sure that at least hotspot1 started
if [ "${multissid}" -gt 1 ]; then
i=0
while ! ip link show dev "hotspot1" &>/dev/null; do
sleep 1
if [ ${i} -gt 20 ]; then
echo "Failed to see hotspot interface showing up in 'ip a'"
stop_hostapd
exit 1
fi
i=$(($i + 1))
done
fi
fi fi
# For each registred ssid set_ipaddr
for i in $(seq 0 $((${multissid} - 1))); do set_ipfirewall
set_ipaddr ${i} start_dhcpd
set_ipfirewall ${i}
start_dhcpd ${i}
done
# Update dynamic settings # Update dynamic settings
ynh_setting_set hotspot gateway_interface "${new_gateway_interface}" ynh_app_setting_set hotspot gateway_interface "${new_gateway_interface}"
;; ;;
stop) stop)
echo "[hotspot] Stopping..." echo "[hotspot] Stopping..."
rm -f /tmp/.ynh-hotspot-started rm -f /tmp/.ynh-hotspot-started
if [ -n "${old_gateway_interface}" ] && is_nat_set "${old_gateway_interface}"; then if [[ -n "${old_gateway_interface}" ]] && is_nat_set "${old_gateway_interface}"; then
echo "Unset NAT" echo "Unset NAT"
unset_nat "${old_gateway_interface}" unset_nat "${old_gateway_interface}"
fi fi
@ -441,11 +313,9 @@ stop)
echo "Unset forwarding" echo "Unset forwarding"
unset_forwarding unset_forwarding
for i in $(seq 0 $((${multissid} - 1))); do unset_ipaddr
unset_ipaddr ${i} unset_ipfirewall
unset_ipfirewall ${i} stop_dhcpd
stop_dhcpd ${i}
done
if is_hostapd_running; then if is_hostapd_running; then
echo "Stop hostapd" echo "Stop hostapd"
@ -459,12 +329,12 @@ restart)
status) status)
exitcode=0 exitcode=0
if [ "${service_enabled}" != "enabled" ]; then if [[ "${service_enabled}" != "enabled" ]]; then
echo "[FAIL] Hotspot Service disabled" echo "[FAIL] Hotspot Service disabled"
exit 1 exit 1
fi fi
if [ -z "${wifi_device}" ]; then if [[ -z "${wifi_device}" ]]; then
echo "[FAIL] No wifi device selected. Make sure your wifi antenna is plugged-in / available and select it in the Hotspot admin" echo "[FAIL] No wifi device selected. Make sure your wifi antenna is plugged-in / available and select it in the Hotspot admin"
exit 1 exit 1
fi fi
@ -474,7 +344,7 @@ status)
if is_nat_set "${new_gateway_interface}"; then if is_nat_set "${new_gateway_interface}"; then
echo "[ OK ] IPv4 NAT set" echo "[ OK ] IPv4 NAT set"
else else
if [ -z "${new_gateway_interface}" ]; then if [[ -z "${new_gateway_interface}" ]]; then
echo "[INFO] No IPv4 NAT set (no internet interface)" echo "[INFO] No IPv4 NAT set (no internet interface)"
else else
echo "[FAIL] No IPv4 NAT set" echo "[FAIL] No IPv4 NAT set"
@ -496,53 +366,51 @@ status)
exitcode=1 exitcode=1
fi fi
for i in $(seq 0 $((${multissid} - 1))); do if has_ip6delegatedprefix; then
if has_ip6delegatedprefix ${i}; then echo "[INFO] hotspot ${wifi_device}: IPv6 delegated prefix found"
echo "[INFO] hotspot${i}: IPv6 delegated prefix found" echo "[INFO] hotspot ${wifi_device}: IPv6 address computed from the delegated prefix: $(ip6addrfromdelegatedprefix)"
echo "[INFO] hotspot${i}: IPv6 address computed from the delegated prefix: $(ip6addrfromdelegatedprefix $i)"
if is_ip6addr_set ${i}; then if is_ip6addr_set; then
echo "[ OK ] hotspot${i}: IPv6 address set" echo "[ OK ] hotspot ${wifi_device}: IPv6 address set"
else else
echo "[FAIL] hotspot${i}: No IPv6 address set" echo "[FAIL] hotspot ${wifi_device}: No IPv6 address set"
exitcode=1 exitcode=1
fi fi
if is_ip6firewall_set ${i}; then if is_ip6firewall_set; then
echo "[ OK ] hotspot${i}: IPv6 firewalling set" echo "[ OK ] hotspot ${wifi_device}: IPv6 firewalling set"
else else
if [ "${ip6_firewall[${i}]}" -eq 1 ]; then if [[ "${ip6_firewall}" -eq 1 ]]; then
echo "[FAIL] hotspot${i}: No IPv6 firewalling set" echo "[FAIL] hotspot ${wifi_device}: No IPv6 firewalling set"
else else
echo "[INFO] hotspot${i}: No IPv6 firewalling set" echo "[INFO] hotspot ${wifi_device}: No IPv6 firewalling set"
fi fi
exitcode=1 exitcode=1
fi fi
if is_dhcpd6_running ${i}; then if is_dhcpd6_running; then
echo "[ OK ] hotspot${i}: NDP and DHCPv6 server (dnsmasq) are running" echo "[ OK ] hotspot ${wifi_device}: NDP and DHCPv6 server (dnsmasq) are running"
else else
echo "[FAIL] hotspot${i}: NDP and DHCPv6 server (dnsmasq) are not running" echo "[FAIL] hotspot ${wifi_device}: NDP and DHCPv6 server (dnsmasq) are not running"
exitcode=1 exitcode=1
fi fi
else else
echo "[INFO] hotspot${i}: No IPv6 delegated prefix found" echo "[INFO] hotspot ${wifi_device}: No IPv6 delegated prefix found"
fi fi
if is_dhcpd4_running ${i}; then if is_dhcpd4_running; then
echo "[ OK ] hotspot${i}: DHCPv4 server (dnsmasq) is running" echo "[ OK ] hotspot ${wifi_device}: DHCPv4 server (dnsmasq) is running"
else else
echo "[FAIL] hotspot${i}: DHCPv4 (dnsmasq) is not running" echo "[FAIL] hotspot ${zifi_device}: DHCPv4 (dnsmasq) is not running"
exitcode=1 exitcode=1
fi fi
if is_ip4nataddr_set ${i}; then if is_ip4nataddr_set; then
echo "[ OK ] hotspot${i}: IPv4 NAT address set" echo "[ OK ] hotspot ${wifi_device}: IPv4 NAT address set"
else else
echo "[FAIL] hotspot${i}: No IPv4 NAT address set" echo "[FAIL] hotspot ${wifi_device}: No IPv4 NAT address set"
exitcode=1 exitcode=1
fi fi
done
exit ${exitcode} exit ${exitcode}
;; ;;

View file

@ -19,7 +19,7 @@ name = "Configuration"
visible = "no_antenna" visible = "no_antenna"
[main.service.status] [main.service.status]
ask = "The status of your VPN is unknown." ask = "The status of your Hotspot is unknown."
type = "alert" type = "alert"
style = "info" style = "info"
visible = "! no_antenna" visible = "! no_antenna"
@ -52,194 +52,58 @@ name = "Configuration"
visible = "! no_antenna" visible = "! no_antenna"
help = "Changing the channel may help with signal strength depending on neighbour WiFis" help = "Changing the channel may help with signal strength depending on neighbour WiFis"
[main.service.multissid] [main.hotspot]
ask = "Number of hotspots to broadcast" name = "Hotspot"
type = "select"
choices.1 = "1"
choices.2 = "2"
choices.3 = "3"
visible = "! no_antenna"
[main.hotspot1]
name = "Hotspot 1"
optional = false optional = false
visible = "! no_antenna" visible = "! no_antenna"
[main.hotspot1.wifi_ssid__1] [main.hotspot.wifi_ssid]
ask = "Name (SSID)" ask = "Name (SSID)"
type = "string" type = "string"
bind = "array_settings()"
pattern.regexp = '^[\w \-]{1,32}$' pattern.regexp = '^[\w \-]{1,32}$'
pattern.error = "SSID in this app are limited to letter, number space, dash and underscores." pattern.error = "SSID in this app are limited to letter, number space, dash and underscores."
[main.hotspot1.wifi_secure__1] [main.hotspot.wifi_secure]
ask = "Secure" ask = "Secure"
type = "boolean" type = "boolean"
bind = "array_settings()"
[main.hotspot1.wifi_passphrase__1] [main.hotspot.wifi_passphrase]
ask = "Password (WPA2)" ask = "Password (WPA2)"
type = "string" type = "string"
bind = "array_settings()"
redact = true redact = true
optional = true optional = true
visible = "wifi_secure__1" visible = "wifi_secure"
pattern.regexp = '^[a-zA-Z0-9]{8,63}$' pattern.regexp = '^[a-zA-Z0-9]{8,63}$'
pattern.error = "Only printable alphanumeric characters are permitted in your password. Maximal size 63 chars" pattern.error = "Only printable alphanumeric characters are permitted in your password. Maximal size 63 chars"
[main.hotspot1.advanced__1] [main.hotspot.advanced]
ask = "Advanced settings" ask = "Advanced settings"
type = "boolean" type = "boolean"
bind = "array_settings()"
[main.hotspot1.ip4_nat_prefix__1] [main.hotspot.ip4_nat_prefix]
ask = "IPv4 NAT prefix (/24)" ask = "IPv4 NAT prefix (/24)"
type = "string" type = "string"
bind = "array_settings()" visible = "advanced"
visible = "advanced__1"
pattern.regexp = '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' 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" pattern.error = "Please provide a private /24 range in the format xxx.xxx.xxx"
[main.hotspot1.ip6_net__1] [main.hotspot.ip6_net]
ask = "IPv6 delegated prefix" ask = "IPv6 delegated prefix"
type = "string" type = "string"
bind = "array_settings()"
optional = true optional = true
visible = "advanced__1" visible = "advanced"
pattern.regexp = '^[0-9a-fA-F:]+$' pattern.regexp = '^[0-9a-fA-F:]+$'
pattern.error = "Please provide a valid IPv6 Prefix" pattern.error = "Please provide a valid IPv6 Prefix"
[main.hotspot1.ip6_firewall__1] [main.hotspot.ip6_firewall]
ask = "IPv6 firewall" ask = "IPv6 firewall"
type = "boolean" type = "boolean"
bind = "array_settings()" visible = "advanced"
visible = "advanced__1"
[main.hotspot1.dns__1] [main.hotspot.dns]
ask = "DNS resolvers" ask = "DNS resolvers"
type = "tags" type = "tags"
bind = "array_settings()" visible = "advanced"
visible = "advanced__1"
pattern.regexp = '^([0-9.]{7,15}|[0-9a-fA-F:]+)$'
pattern.error = "Not an ip"
[main.hotspot2]
name = "Hotspot 2"
visible = "! no_antenna && multissid >= 2"
[main.hotspot2.wifi_ssid__2]
ask = "Name (SSID)"
type = "string"
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.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.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.error = "Not an ip"
[main.hotspot3]
name = "Hotspot 3"
visible = "! no_antenna && multissid >= 3"
[main.hotspot3.wifi_ssid__3]
ask = "Name (SSID)"
type = "string"
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.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.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" pattern.error = "Not an ip"

View file

@ -92,112 +92,49 @@ EOF
echo "value: '$(ynh_app_setting_get $app wifi_device)'" echo "value: '$(ynh_app_setting_get $app wifi_device)'"
} }
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 # SPECIFIC VALIDATORS FOR TOML SHORT KEYS
#================================================= #=================================================
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
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() { validate__wifi_ssid() {
local wifi_ssid_var="wifi_ssid__$1" if [[ -z "${wifi_ssid}" ]]
if [ "$multissid" -ge "$1" ] && [[ -z "${!wifi_ssid_var}" ]]
then then
echo 'SSID required' echo 'SSID required'
fi fi
if ! is_unique wifi_ssid
then
echo 'All Wifi names must be unique'
fi
} }
validate__wifi_passphrase() { validate__wifi_passphrase() {
local wifi_secure_var="wifi_secure__$1" if [[ "${wifi_secure}" == "1" ]] && [[ -z "${wifi_passphrase}" ]]
local wifi_passphrase_var="wifi_passphrase__$1"
if [ "$multissid" -ge "$1" ] && [[ "${!wifi_secure_var}" == "1" ]] && [[ -z "${!wifi_passphrase_var}" ]]
then then
echo 'In WPA2 secure mode, you need to provide a passphrase' echo 'In WPA2 secure mode, you need to provide a passphrase'
fi fi
} }
validate__ip4_nat_prefix() { validate__ip4_nat_prefix() {
local ip4_nat_prefix_var="ip4_nat_prefix__$1" if [[ -z "${ip4_nat_prefix}" ]]
if [ "$multissid" -ge "$1" ] && [[ -z "${!ip4_nat_prefix_var}" ]]
then then
echo 'Private IPv4 nat prefix required' echo 'Private IPv4 nat prefix required'
fi fi
if ! is_unique ip4_nat_prefix
then
echo 'All IPv4 prefix must be unique'
fi
} }
validate__dns() { validate__dns() {
local dns_var="dns__$1" if ! echo "${dns}" | grep -q "\."
local ip6_net_var="dns__$1"
if [ "$multissid" -ge "$1" ] && ! echo "${!dns_var}" | grep -q "\."
then then
echo 'IPv4 DNS required' echo 'IPv4 DNS required'
fi fi
if [ "$multissid" -ge "$1" ] && [[ -n "${!ip6_net_var}" ]] && ! echo "${!dns_var}" | grep -q ":" if [[ -n "${ip6_net}" ]] && ! echo "${dns}" | grep -q ":"
then then
echo 'IPv6 DNS required' echo 'IPv6 DNS required'
fi 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 # SPECIFIC SETTERS FOR TOML SHORT KEYS
#================================================= #=================================================
set__array_settings() {
local short_setting="${1%%__*}"
local index="${1#*__}"
local type="${types[$1]}"
local value="${!1}"
if [[ "$type" == "string" ]] && [ "$multissid" -lt "$index" ]
then
value=""
fi
local values="$(ynh_app_setting_get $app $short_setting | awk "BEGIN{FS=OFS=\"|\"} {\$${index}=\"${value}\"}"1)"
ynh_app_setting_set --app=$app --key=$short_setting --value="$values"
ynh_print_info --message="Configuration key '$short_setting' edited in app settings"
}
#================================================= #=================================================
# OVERWRITING VALIDATE STEP # OVERWRITING VALIDATE STEP
#================================================= #=================================================
ynh_app_config_validate() {
_ynh_app_config_validate
}
#================================================= #=================================================
# OVERWRITING APPLY STEP # OVERWRITING APPLY STEP
@ -210,6 +147,19 @@ ynh_app_config_apply() {
_ynh_app_config_apply _ynh_app_config_apply
if [ "${wifi_secure}" -eq 1 ]; then
local sec_comment=""
else
local sec_comment="#"
fi
ynh_add_config --template="/etc/hostapd/hostapd.base.conf" --destination="/etc/hostapd/hostapd-${wifi_device}.conf"
ynh_add_config --template="/etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl" --destination"/etc/dnsmasq.dhcpdv4-ssid-${wifi_device}.conf"
if [[ -n "${ip6_net}" ]] && [[ "${ip6_net}" != "none" ]]; then
ynh_add_config --template="/etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl" --destination"/etc/dnsmasq.dhcpdv6-ssid-${wifi_device}.conf"
fi
# Start vpn client # Start vpn client
ynh_print_info --message="Starting hotspot service if needed" ynh_print_info --message="Starting hotspot service if needed"
/usr/local/bin/ynh-hotspot start /usr/local/bin/ynh-hotspot start

View file

@ -132,7 +132,6 @@ fi
hot_reload_usb_wifi_cards hot_reload_usb_wifi_cards
wifi_device=$(iw_devices | awk -F\| '{ print $1 }') wifi_device=$(iw_devices | awk -F\| '{ print $1 }')
ynh_app_setting_set --app=$app --key=multissid --value=1
ynh_app_setting_set --app=$app --key=ssid_nb --value=1 ynh_app_setting_set --app=$app --key=ssid_nb --value=1
ynh_app_setting_set --app=$app --key=wifi_ssid --value="${wifi_ssid}" ynh_app_setting_set --app=$app --key=wifi_ssid --value="${wifi_ssid}"
ynh_app_setting_set --app=$app --key=wifi_secure --value=1 ynh_app_setting_set --app=$app --key=wifi_secure --value=1