From b39ab65086ce26afc65878c7210e448e43bcca2e Mon Sep 17 00:00:00 2001 From: Julien VAUBOURG Date: Sat, 2 May 2015 14:54:08 +0200 Subject: [PATCH] Works with non-multissid devices --- conf/init_ynh-hotspot | 98 ++++++++++++++++++++++++------------ conf/iw_devices | 20 ++++++++ conf/iw_multissid | 22 ++++++++ conf/iw_ssids | 20 ++++++++ scripts/install | 9 ++-- scripts/remove | 5 -- sources/controller.php | 26 ++++++---- sources/views/_ssid.html.php | 2 +- 8 files changed, 153 insertions(+), 49 deletions(-) create mode 100644 conf/iw_devices create mode 100644 conf/iw_multissid create mode 100644 conf/iw_ssids diff --git a/conf/init_ynh-hotspot b/conf/init_ynh-hotspot index 2f721ae..2c49935 100644 --- a/conf/init_ynh-hotspot +++ b/conf/init_ynh-hotspot @@ -48,13 +48,25 @@ is_nat_set() { is_ip4nataddr_set() { i=${1} - ip address show dev "ssid${i}" 2> /dev/null | grep -q "${ynh_ip4_nat_prefix[${i}]}.1/24" + if [ "${i}" -eq 0 ]; then + dev=${ynh_wifi_device} + else + dev="hotspot${i}" + fi + + ip address show dev "${dev}" 2> /dev/null | grep -q "${ynh_ip4_nat_prefix[${i}]}.1/24" } is_ip6addr_set() { i=${1} - ip address show dev "ssid${i}" 2> /dev/null | grep -q "${ynh_ip6_addr[${i}]}/64" + if [ "${i}" -eq 0 ]; then + dev=${ynh_wifi_device} + else + dev="hotspot${i}" + fi + + ip address show dev "${dev}" 2> /dev/null | grep -q "${ynh_ip6_addr[${i}]}/64" } is_forwarding_set() { @@ -98,14 +110,26 @@ set_nat() { set_ip4nataddr() { i=${1} - ip address add "${ynh_ip4_nat_prefix[${i}]}.1/24" dev "ssid${i}" + if [ "${i}" -eq 0 ]; then + dev=${ynh_wifi_device} + else + dev="hotspot${i}" + fi + + ip address add "${ynh_ip4_nat_prefix[${i}]}.1/24" dev "${dev}" } set_ip6addr() { i=${1} + if [ "${i}" -eq 0 ]; then + dev=${ynh_wifi_device} + else + dev="hotspot${i}" + fi + ip address delete "${ynh_ip6_addr[${i}]}/64" dev tun0 &> /dev/null - ip address add "${ynh_ip6_addr[${i}]}/64" dev "ssid${i}" + ip address add "${ynh_ip6_addr[${i}]}/64" dev "${dev}" } set_forwarding() { @@ -116,12 +140,18 @@ set_forwarding() { start_dhcpd() { i=${1} + if [ "${i}" -eq 0 ]; then + dev=${ynh_wifi_device} + else + dev="hotspot${i}" + fi + rm -f /etc/dnsmasq.dhcpd/dhcpdv?-ssid*.conf if has_ip6delegatedprefix ${i}; then cp /etc/dnsmasq.dhcpdhcpd/dhcpdv6{.conf.tpl,-ssid${i}.conf} - sed "s||ssid${i}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf + sed "s||${dev}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf sed "s||${ynh_ip6_net[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf sed "s||${ynh_ip6_dns0[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf sed "s||${ynh_ip6_dns1[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf @@ -133,7 +163,7 @@ start_dhcpd() { sed "s||${ynh_ip4_dns0[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf sed "s||${ynh_ip4_dns1[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf - sed "s||ssid${i}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf + sed "s||${dev}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf sed "s||${ynh_ip4_nat_prefix[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf -p0 @@ -147,18 +177,12 @@ start_hostapd() { sed "s||${ynh_wifi_device}|g" -i /etc/hostapd/hostapd.conf sed "s||${ynh_wifi_channel}|g" -i /etc/hostapd/hostapd.conf - - iwconfig "${ynh_wifi_device}" | grep -q 'n *ESSID' - if [ $? -eq 0 ]; then - sed "s|||g" -i /etc/hostapd/hostapd.conf - else - sed "s||#|g" -i /etc/hostapd/hostapd.conf - fi + sed "s|||g" -i /etc/hostapd/hostapd.conf for i in $(seq 0 $((${ynh_multissid} - 1))); do cp /etc/hostapd/hostapd.conf{.tpl2,.tmp} - sed "s||ssid${i}|g" -i /etc/hostapd/hostapd.conf.tmp + sed "s||hotspot${i}|g" -i /etc/hostapd/hostapd.conf.tmp sed "s||${ynh_wifi_ssid[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp sed "s||${ynh_wifi_passphrase[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp @@ -192,13 +216,25 @@ unset_nat() { unset_ip4nataddr() { i=${1} - ip address delete "${ynh_ip4_nat_prefix[${i}]}.1/24" dev "ssid${i}" + if [ "${i}" -eq 0 ]; then + dev=${ynh_wifi_device} + else + dev="hotspot${i}" + fi + + ip address delete "${ynh_ip4_nat_prefix[${i}]}.1/24" dev "${dev}" } unset_ip6addr() { i=${1} - ip address delete "${ynh_ip6_addr[${i}]}/64" dev "ssid${i}" + if [ "${i}" -eq 0 ]; then + dev=${ynh_wifi_device} + else + dev="hotspot${i}" + fi + + ip address delete "${ynh_ip6_addr[${i}]}/64" dev "${dev}" } unset_forwarding() { @@ -372,19 +408,19 @@ case "$1" in # Set ipv4 NAT address if ! is_ip4nataddr_set ${i}; then - echo "SSID ${i}: Set IPv4 NAT address" + echo "Hotspot ${i}: Set IPv4 NAT address" set_ip4nataddr ${i} fi # Set the ipv6 address if has_ip6delegatedprefix ${i} && ! is_ip6addr_set ${i}; then - echo "SSID ${i}: Set IPv6 address" + echo "Hotspot ${i}: Set IPv6 address" set_ip6addr ${i} fi # Run DHCP servers if ! is_dhcpd_running ${i}; then - echo "SSID ${i}: Set DHCP servers (dnsmasq)" + echo "Hotspot ${i}: Set DHCP servers (dnsmasq)" start_dhcpd ${i} fi done @@ -409,17 +445,17 @@ case "$1" in for i in $(seq 0 $((${ynh_multissid} - 1))); do if is_ip4nataddr_set ${i}; then - echo "SSID ${i}: Unset IPv4 NAT address" + echo "Hotspot ${i}: Unset IPv4 NAT address" unset_ip4nataddr ${i} fi if has_ip6delegatedprefix ${i} && is_ip6addr_set ${i}; then - echo "SSID ${i}: Unset IPv6 address" + echo "Hotspot ${i}: Unset IPv6 address" unset_ip6addr ${i} fi if is_dhcpd_running ${i}; then - echo "SSID ${i}: Stop DHCP servers" + echo "Hotspot ${i}: Stop DHCP servers" stop_dhcpd ${i} fi done @@ -470,30 +506,30 @@ case "$1" in for i in $(seq 0 $((${ynh_multissid} - 1))); do if has_ip6delegatedprefix ${i}; then - echo "[INFO] SSID ${i}: IPv6 delegated prefix found" - echo "[INFO] SSID ${i}: IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}" + echo "[INFO] Hotspot ${i}: IPv6 delegated prefix found" + echo "[INFO] Hotspot ${i}: IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}" if is_ip6addr_set ${i}; then - echo "[OK] SSID ${i}: IPv6 address set" + echo "[OK] Hotspot ${i}: IPv6 address set" else - echo "[ERR] SSID ${i}: No IPv6 address set" + echo "[ERR] Hotspot ${i}: No IPv6 address set" exitcode=1 fi else - echo "[INFO] SSID ${i}: No IPv6 delegated prefix found" + echo "[INFO] Hotspot ${i}: No IPv6 delegated prefix found" fi if is_dhcpd_running ${i}; then - echo "[OK] SSID ${i}: DHCP servers set" + echo "[OK] Hotspot ${i}: DHCP servers set" else - echo "[ERR] SSID ${i}: No DHCP servers set" + echo "[ERR] Hotspot ${i}: No DHCP servers set" exitcode=1 fi if is_ip4nataddr_set ${i}; then - echo "[OK] SSID ${i}: IPv4 NAT address set" + echo "[OK] Hotspot ${i}: IPv4 NAT address set" else - echo "[ERR] SSID ${i}: No IPv4 NAT address set" + echo "[ERR] Hotspot ${i}: No IPv4 NAT address set" exitcode=1 fi done diff --git a/conf/iw_devices b/conf/iw_devices new file mode 100644 index 0000000..5274842 --- /dev/null +++ b/conf/iw_devices @@ -0,0 +1,20 @@ +#!/bin/bash + +# Wifi Hotspot app for YunoHost +# Copyright (C) 2015 Julien Vaubourg +# Contribute at https://github.com/jvaubourg/hotspot_ynh +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +echo -n $(iw dev | grep Interface | grep -v 'mon\.' | grep -v hotspot | awk '{ print $NF }') | tr ' ' \| diff --git a/conf/iw_multissid b/conf/iw_multissid new file mode 100644 index 0000000..1ed65de --- /dev/null +++ b/conf/iw_multissid @@ -0,0 +1,22 @@ +#!/bin/bash + +# Wifi Hotspot app for YunoHost +# Copyright (C) 2015 Julien Vaubourg +# Contribute at https://github.com/jvaubourg/hotspot_ynh +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +phy=$(iw "${1}" info | grep wiphy | awk '{ print $NF }') + +iw "phy${phy}" info | grep -A1 'valid interface combinations' | tail -n1 | sed 's/.*{.*AP.*}\s<=\s\(.*\),.*/\1/' diff --git a/conf/iw_ssids b/conf/iw_ssids new file mode 100644 index 0000000..4e4185e --- /dev/null +++ b/conf/iw_ssids @@ -0,0 +1,20 @@ +#!/bin/bash + +# Wifi Hotspot app for YunoHost +# Copyright (C) 2015 Julien Vaubourg +# Contribute at https://github.com/jvaubourg/hotspot_ynh +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +echo -n hotspot0 $(iw dev | grep Interface | grep hotspot | awk '{ print $NF }') | tr ' ' \| diff --git a/scripts/install b/scripts/install index 5ac568e..f3258b4 100644 --- a/scripts/install +++ b/scripts/install @@ -56,7 +56,7 @@ if [ ! $? -eq 0 ]; then fi # Install packages -packages='php5-fpm sipcalc hostapd iptables wireless-tools dnsmasq' +packages='php5-fpm sipcalc hostapd iptables iw dnsmasq' export DEBIAN_FRONTEND=noninteractive # Packaged USB Wireless Device firmwares @@ -101,7 +101,7 @@ if [ -z "${ip6_net}" ]; then # ip6_addr=$(bash ../conf/ipv6_compressed "${ip6_addr}") fi -wifi_device=$(sudo iwconfig 2>&1 | grep 802.11 | head -n1 | awk '{ print $1 }') +wifi_device=$(sudo bash ../conf/iw_devices | awk -F\| '{ print $1 }') if [ -z "${wifi_device}" ]; then echo "ERROR: No wifi interface found" >&2 @@ -125,7 +125,10 @@ sudo yunohost app setting hotspot ip4_dns1 -v 80.67.169.12 sudo yunohost app setting hotspot ip4_nat_prefix -v 10.0.242 sudo yunohost app setting hotspot vpnclient -v no -# Install IPv6 scripts +# Install custom scripts +sudo install -o root -g root -m 0755 ../conf/iw_multissid /usr/local/bin/ +sudo install -o root -g root -m 0755 ../conf/iw_devices /usr/local/bin/ +sudo install -o root -g root -m 0755 ../conf/iw_ssids /usr/local/bin/ sudo install -o root -g root -m 0755 ../conf/ipv6_expanded /usr/local/bin/ sudo install -o root -g root -m 0755 ../conf/ipv6_compressed /usr/local/bin/ diff --git a/scripts/remove b/scripts/remove index 889cd81..9836e94 100644 --- a/scripts/remove +++ b/scripts/remove @@ -48,9 +48,4 @@ sudo service nginx reload # Remove sources sudo rm -rf /var/www/wifiadmin/ -# Remove packets -# The yunohost policy is currently to not uninstall packets (dependency problems) -## sudo apt-get --assume-yes --force-yes remove hostapd iptables sipcalc wireless-tools -## sudo apt-get --assume-yes --force-yes remove firmware-atheros atmel-firmware firmware-linux-free firmware-linux-nonfree firmware-realtek firmware-ralink firmware-libertas zd1211-firmware - exit 0 diff --git a/sources/controller.php b/sources/controller.php index 6d16419..7b3ee5e 100644 --- a/sources/controller.php +++ b/sources/controller.php @@ -60,12 +60,24 @@ function ipv6_compressed($ip) { return $output[0]; } +function iw_multissid($nic) { + exec('sudo iw_multissid '.escapeshellarg($nic), $output); + + return $output[0]; +} + +function iw_devices() { + exec('sudo iw_devices', $output); + + return getArray($output[0]); +} + function getArray($str) { return explode('|', $str); } function noneValue($str) { - return $str == 'none' ? '' : $str; + return ($str == 'none') ? '' : $str; } function is_connected_through_hotspot($ip6_net, $ip4_nat_prefix) { @@ -78,22 +90,18 @@ function is_connected_through_hotspot($ip6_net, $ip4_nat_prefix) { dispatch('/', function() { $ssids = array(); + $devs = iw_devices(); $devs_list = ''; - exec('sudo iwconfig', $devs); - $wifi_device = moulinette_get('wifi_device'); $multissid = moulinette_get('multissid'); $wifi_channel = moulinette_get('wifi_channel'); foreach($devs AS $dev) { - if(preg_match('/802.11/', $dev)) { - $dev = explode(' ', $dev); - $dev = $dev[0]; + $dev_multissid = iw_multissid($dev); - $active = ($dev == $wifi_device) ? 'class="active"' : ''; - $devs_list .= "
  • $dev
  • \n"; - } + $active = ($dev == $wifi_device) ? 'class="active"' : ''; + $devs_list .= "
  • $dev
  • \n"; } $wifi_ssid = getArray(moulinette_get('wifi_ssid')); diff --git a/sources/views/_ssid.html.php b/sources/views/_ssid.html.php index 826f034..794a009 100644 --- a/sources/views/_ssid.html.php +++ b/sources/views/_ssid.html.php @@ -1,6 +1,6 @@
    >
    -

    ">

    +

    ">