freedombone-utils-wifi 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Wifi functions
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. WIFI_CHANNEL=2
  30. WIFI_INTERFACE=wlan0
  31. WIFI_TYPE='wpa2-psk'
  32. WIFI_SSID=
  33. WIFI_PASSPHRASE=
  34. WIFI_HOTSPOT='no'
  35. WIFI_NETWORKS_FILE=~/${PROJECT_NAME}-wifi.cfg
  36. # repo for atheros AR9271 wifi driver
  37. ATHEROS_WIFI_REPO="https://github.com/qca/open-ath9k-htc-firmware.git"
  38. function default_network_config {
  39. echo '# This file describes the network interfaces available on your system' > /etc/network/interfaces
  40. echo '# and how to activate them. For more information, see interfaces(5).' >> /etc/network/interfaces
  41. echo 'source /etc/network/interfaces.d/*' >> /etc/network/interfaces
  42. }
  43. # Debian stretch has a problem where the formerly predictable wlan0 and eth0
  44. # device names get assigned random names. This is a hacky workaround.
  45. # Also adding net.ifnames=0 to kernel options on bootloader may work.
  46. function enable_predictable_device_names {
  47. test_predictable=$(ls -la /etc/udev/rules.d/80-net-setup-link.rules)
  48. if [[ "$test_predictable" != *"/dev/null" ]]; then
  49. ln -s /dev/null /etc/udev/rules.d/80-net-setup-link.rules
  50. update-initramfs -u
  51. fi
  52. }
  53. function wifi_is_running {
  54. wifi_state=$(wpa_cli status)
  55. if [[ "$wifi_state" == *"COMPLETED"* ]]; then
  56. echo "1"
  57. else
  58. echo "0"
  59. fi
  60. }
  61. function wifi_static_network_interface {
  62. NETWORK_IS_STATIC=0
  63. read_config_param "NETWORK_IS_STATIC"
  64. if [ ${NETWORK_IS_STATIC} -eq 0 ]; then
  65. echo '#this line must always be here' > /etc/network/interfaces.d/static
  66. echo 'iface default inet dhcp' >> /etc/network/interfaces.d/static
  67. else
  68. read_config_param "LOCAL_NETWORK_STATIC_IP_ADDRESS"
  69. read_config_param "ROUTER_IP_ADDRESS"
  70. { echo 'iface default inet static';
  71. echo " address ${LOCAL_NETWORK_STATIC_IP_ADDRESS}";
  72. echo ' netmask 255.255.255.0';
  73. echo " gateway ${ROUTER_IP_ADDRESS}"; } > /etc/network/interfaces.d/static
  74. fi
  75. }
  76. function setup_wifi_atheros {
  77. if [[ $(running_as_root) == "0" ]]; then
  78. return
  79. fi
  80. apt-get -yq install firmware-linux-free
  81. atheros_drivers_file=drivers/firmware-ath9k-htc.deb
  82. if [ ! -f $atheros_drivers_file ]; then
  83. if [ ! -f ~/freedombone/$atheros_drivers_file ]; then
  84. return
  85. else
  86. atheros_drivers_file=~/freedombone/$atheros_drivers_file
  87. fi
  88. else
  89. atheros_drivers_file=$(pwd)/$atheros_drivers_file
  90. fi
  91. dpkg -i "$atheros_drivers_file"
  92. reset_usb_devices
  93. update_wifi_adaptors
  94. if [ "$IFACE" ]; then
  95. wpa_action "${IFACE}" stop
  96. wpa_cli -i "${IFACE}" terminate
  97. #ifconfig $IFACE up
  98. ifup "$IFACE"
  99. fi
  100. }
  101. function setup_wifi {
  102. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  103. return
  104. fi
  105. if [ ! $WIFI_SSID ]; then
  106. return
  107. fi
  108. if [ ${#WIFI_SSID} -lt 2 ]; then
  109. return
  110. fi
  111. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  112. return
  113. fi
  114. HOTSPOT='no'
  115. if [[ $WIFI_HOTSPOT != 'no' ]]; then
  116. HOTSPOT='yes'
  117. fi
  118. if [ -f "$WIFI_NETWORKS_FILE" ]; then
  119. "${PROJECT_NAME}-wifi" --networks "$WIFI_NETWORKS_FILE"
  120. mark_complete "${FUNCNAME[0]}"
  121. return
  122. fi
  123. if [[ $WIFI_TYPE != 'none' ]]; then
  124. if [ ! $WIFI_PASSPHRASE ]; then
  125. echo $'No wifi passphrase was given'
  126. return
  127. fi
  128. if [ ${#WIFI_PASSPHRASE} -lt 2 ]; then
  129. echo $'Wifi passphrase was too short'
  130. return
  131. fi
  132. "${PROJECT_NAME}-wifi" -s "$WIFI_SSID" -t "$WIFI_TYPE" -p "$WIFI_PASSPHRASE" --hotspot "$HOTSPOT" --networks "$WIFI_NETWORKS_FILE"
  133. else
  134. "${PROJECT_NAME}-wifi" -s "$WIFI_SSID" -t "$WIFI_TYPE" --hotspot "$HOTSPOT" --networks "$WIFI_NETWORKS_FILE"
  135. fi
  136. mark_completed "${FUNCNAME[0]}"
  137. }
  138. # ath9k_htc driver
  139. function install_atheros_wifi {
  140. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  141. return
  142. fi
  143. if [ "$INSTALLING_ON_BBB" != "yes" ]; then
  144. return
  145. fi
  146. if [[ $ENABLE_BATMAN != "yes" ]]; then
  147. return
  148. fi
  149. if [ -d "$INSTALL_DIR/open-ath9k-htc-firmware" ]; then
  150. return
  151. fi
  152. # have drivers already been installed ?
  153. if [ -f /lib/firmware/htc_9271.fw ]; then
  154. return
  155. fi
  156. apt-get -yq install build-essential cmake git m4 texinfo
  157. if [ ! -d "$INSTALL_DIR" ]; then
  158. mkdir -p "$INSTALL_DIR"
  159. fi
  160. cd "$INSTALL_DIR" || exit 24678246
  161. if [ ! -d "$INSTALL_DIR/open-ath9k-htc-firmware" ]; then
  162. function_check git_clone
  163. if ! git_clone "$ATHEROS_WIFI_REPO" "$INSTALL_DIR/open-ath9k-htc-firmware"; then
  164. rm -rf "$INSTALL_DIR/open-ath9k-htc-firmware"
  165. exit 74283
  166. fi
  167. fi
  168. cd "$INSTALL_DIR/open-ath9k-htc-firmware" || exit 24682468
  169. git checkout 1.4.0
  170. if ! make toolchain; then
  171. rm -rf "$INSTALL_DIR/open-ath9k-htc-firmware"
  172. exit 24820
  173. fi
  174. if ! make firmware; then
  175. rm -rf "$INSTALL_DIR/open-ath9k-htc-firmware"
  176. exit 63412
  177. fi
  178. if ! cp target_firmware/*.fw /lib/firmware/; then
  179. exit 74681
  180. fi
  181. mark_completed "${FUNCNAME[0]}"
  182. }
  183. function update_wifi_adaptors {
  184. IFACE=
  185. IFACE_SECONDARY=
  186. for i in $(seq 10 -1 0); do
  187. ifdown --force "wlan${i}" 2> /dev/null
  188. done
  189. for i in $(seq 10 -1 0); do
  190. if grep -q "wlan${i}" /proc/net/dev; then
  191. if [ ! $IFACE ]; then
  192. IFACE="wlan${i}"
  193. else
  194. IFACE_SECONDARY="wlan${i}"
  195. return
  196. fi
  197. fi
  198. done
  199. }
  200. function wifi_get_psk {
  201. ssid=$1
  202. passphrase=$2
  203. psk=$(wpa_passphrase "$ssid" "$passphrase" | grep 'psk=' | sed -n 2p | awk -F '=' '{print $2}')
  204. echo "$psk"
  205. }
  206. function hotspot_off {
  207. if [ ! -f /etc/hostapd/hostapd.conf ]; then
  208. return
  209. fi
  210. systemctl stop hostapd
  211. rm /etc/hostapd/hostapd.conf
  212. if [ -f /etc/network/interfaces_original_static ]; then
  213. cp /etc/network/interfaces_original_static /etc/network/interfaces.d/static
  214. else
  215. if [ -f /etc/network/interfaces.d/static ]; then
  216. rm /etc/network/interfaces.d/static
  217. fi
  218. fi
  219. if [ -f /etc/network/interfaces_original_wifi ]; then
  220. cp /etc/network/interfaces_original_wifi /etc/network/interfaces.d/wifi
  221. else
  222. if [ -f /etc/network/interfaces.d/wifi ]; then
  223. rm /etc/network/interfaces.d/wifi
  224. fi
  225. fi
  226. if [ -f /etc/network/interfaces.d/bridge ]; then
  227. rm /etc/network/interfaces.d/bridge
  228. fi
  229. default_network_config
  230. wpa_action ${WIFI_INTERFACE} stop
  231. wpa_cli -i ${WIFI_INTERFACE} terminate
  232. systemctl restart network-manager
  233. }
  234. function hotspot_on {
  235. if [ ! -f /etc/default/hostapd ]; then
  236. echo $'/etc/default/hostapd was not found'
  237. exit 67241
  238. fi
  239. if [ ${#WIFI_PASSPHRASE} -lt 8 ]; then
  240. echo $'Wifi hotspot passphrase is too short'
  241. exit 25719
  242. fi
  243. sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
  244. { echo '### Wireless network name ###';
  245. echo "interface=$WIFI_INTERFACE";
  246. echo '';
  247. echo '### Set your bridge name ###';
  248. echo 'bridge=br0';
  249. echo '';
  250. echo 'driver=nl80211';
  251. echo "country_code=UK";
  252. echo "ssid=$WIFI_SSID";
  253. echo 'hw_mode=g';
  254. echo 'channel=6';
  255. echo 'wpa=2';
  256. echo "wpa_passphrase=$WIFI_PASSPHRASE";
  257. echo '';
  258. echo '## Key management algorithms ##';
  259. echo 'wpa_key_mgmt=WPA-PSK';
  260. echo '';
  261. echo '## Set cipher suites (encryption algorithms) ##';
  262. echo '## TKIP = Temporal Key Integrity Protocol';
  263. echo '## CCMP = AES in Counter mode with CBC-MAC';
  264. echo 'wpa_pairwise=TKIP';
  265. echo 'rsn_pairwise=CCMP';
  266. echo '';
  267. echo '## Shared Key Authentication ##'
  268. echo 'auth_algs=1';
  269. echo '';
  270. echo '## Accept all MAC address ###';
  271. echo 'macaddr_acl=0'; } > /etc/hostapd/hostapd.conf
  272. if [ ! -f /etc/network/interfaces_original_static ]; then
  273. if [ -f /etc/network/interfaces.d/static ]; then
  274. cp /etc/network/interfaces.d/static /etc/network/interfaces_original_static
  275. fi
  276. fi
  277. if [ ! -f /etc/network/interfaces_original_wifi ]; then
  278. if [ -f /etc/network/interfaces.d/wifi ]; then
  279. cp /etc/network/interfaces.d/wifi /etc/network/interfaces_original_wifi
  280. fi
  281. fi
  282. default_network_config
  283. echo "allow-hotplug $WIFI_INTERFACE" > /etc/network/interfaces.d/wifi
  284. echo "iface $WIFI_INTERFACE inet manual" >> /etc/network/interfaces.d/wifi
  285. echo 'allow-hotplug eth0' >> /etc/network/interfaces.d/static
  286. echo 'iface eth0 inet manual' >> /etc/network/interfaces.d/static
  287. echo 'iface br0 inet static' >> /etc/network/interfaces.d/bridge
  288. echo " bridge_ports $WIFI_INTERFACE eth0" >> /etc/network/interfaces.d/bridge
  289. systemctl restart network-manager
  290. ifup $WIFI_INTERFACE
  291. systemctl restart hostapd
  292. }
  293. function wifi_store_original_network_settings {
  294. if [ ! -f /etc/network/interfaces_original ]; then
  295. if ! grep -q "# wifi enabled" /etc/network/interfaces; then
  296. cp /etc/network/interfaces /etc/network/interfaces_original
  297. fi
  298. fi
  299. }
  300. function wifi_original_network_settings {
  301. remove_config_param "NETWORK_IS_STATIC"
  302. if [ -f /etc/network/interfaces.d/static ]; then
  303. rm /etc/network/interfaces.d/static
  304. fi
  305. if [ -f /etc/network/interfaces.d/wifi ]; then
  306. rm /etc/network/interfaces.d/wifi
  307. fi
  308. if [ -f /etc/network/interfaces.d/bridge ]; then
  309. rm /etc/network/interfaces.d/bridge
  310. fi
  311. }
  312. function wifi_wpa2_psk {
  313. ssid=$1
  314. passphrase=$2
  315. wifi_store_original_network_settings
  316. echo 'allow-hotplug eth0' > /etc/network/interfaces.d/static
  317. echo 'iface eth0 inet dhcp' >> /etc/network/interfaces.d/static
  318. echo "allow-hotplug ${WIFI_INTERFACE}" > /etc/network/interfaces.d/wifi
  319. echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces.d/wifi
  320. echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces.d/wifi
  321. wifi_static_network_interface
  322. wpa_passphrase "$ssid" "$passphrase" > "$WIFI_CONFIG"
  323. systemctl restart network-manager
  324. ifup $WIFI_INTERFACE
  325. }
  326. function wifi_none {
  327. ssid=$1
  328. wifi_store_original_network_settings
  329. echo 'allow-hotplug eth0' > /etc/network/interfaces.d/static
  330. echo 'iface eth0 inet dhcp' >> /etc/network/interfaces.d/static
  331. echo "allow-hotplug ${WIFI_INTERFACE}" > /etc/network/interfaces.d/wifi
  332. echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces.d/wifi
  333. echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces.d/wifi
  334. wifi_static_network_interface
  335. { echo 'ctrl_interface=/run/wpa_supplicant';
  336. echo 'update_config=1';
  337. echo 'eapol_version=1';
  338. echo '';
  339. echo 'network={'; } > "$WIFI_CONFIG"
  340. if [[ "${ssid}" != $'any' && "${ssid}" != $'all' && "${ssid}" != $'open' ]]; then
  341. echo " ssid=\"${ssid}\"" >> "$WIFI_CONFIG"
  342. fi
  343. echo ' key_mgmt=NONE' >> "$WIFI_CONFIG"
  344. echo '}' >> "$WIFI_CONFIG"
  345. systemctl restart network-manager
  346. ifup $WIFI_INTERFACE
  347. }
  348. function networks_from_file {
  349. if [ ! -f "$WIFI_NETWORKS_FILE" ]; then
  350. exit 4
  351. fi
  352. if [[ $(config_param_exists "WIFI_INTERFACE") == "0" ]]; then
  353. exit 5
  354. fi
  355. read_config_param "WIFI_INTERFACE"
  356. wifi_store_original_network_settings
  357. echo 'allow-hotplug eth0' > /etc/network/interfaces.d/static
  358. echo 'iface eth0 inet dhcp' >> /etc/network/interfaces.d/static
  359. { echo "allow-hotplug ${WIFI_INTERFACE}";
  360. echo "iface ${WIFI_INTERFACE} inet manual";
  361. echo " wpa-roam $WIFI_CONFIG"; } > /etc/network/interfaces.d/wifi
  362. wifi_static_network_interface
  363. # remove wpa_supplicant.conf if it exists
  364. if [ -f "$WIFI_CONFIG" ]; then
  365. rm -f "$WIFI_CONFIG"
  366. fi
  367. { echo 'ctrl_interface=/run/wpa_supplicant';
  368. echo 'update_config=1';
  369. echo 'eapol_version=1';
  370. echo ''; } > "$WIFI_CONFIG"
  371. ctr=0
  372. while read -r line
  373. do
  374. if [ ${#line} -gt 1 ]; then
  375. if [[ "$line" != '#'* ]]; then
  376. if [ $ctr -eq 0 ]; then
  377. WIFI_SSID="$line"
  378. fi
  379. if [ $ctr -eq 1 ]; then
  380. WIFI_TYPE="$line"
  381. if [[ $WIFI_TYPE == $'none' || $WIFI_TYPE == $'open' ]]; then
  382. echo 'network={' >> "$WIFI_CONFIG"
  383. if [[ "${WIFI_SSID}" != $'any' && "${WIFI_SSID}" != $'all' && "${WIFI_SSID}" != $'open' ]]; then
  384. echo " ssid=\"${WIFI_SSID}\"" >> "$WIFI_CONFIG"
  385. fi
  386. echo ' key_mgmt=NONE' >> "$WIFI_CONFIG"
  387. echo '}' >> "$WIFI_CONFIG"
  388. ctr=0
  389. continue
  390. fi
  391. fi
  392. if [ $ctr -eq 2 ]; then
  393. WIFI_PASSPHRASE="$line"
  394. wpa_passphrase "$WIFI_SSID" "$WIFI_PASSPHRASE" >> "$WIFI_CONFIG"
  395. ctr=0
  396. continue
  397. fi
  398. ctr=$((ctr + 1))
  399. fi
  400. fi
  401. done < "$WIFI_NETWORKS_FILE"
  402. systemctl restart network-manager
  403. #ifconfig ${WIFI_INTERFACE} up
  404. ifup $WIFI_INTERFACE
  405. }
  406. function wifi_networks_file_header {
  407. { echo $'# Add wifi networks as follows:';
  408. echo '#';
  409. echo $'# MySSID';
  410. echo $'# wpa2-psk';
  411. echo $'# myWifiPassphrase';
  412. echo '#';
  413. echo $'# AnotherSSID';
  414. echo $'# none';
  415. echo '#'; } > "$WIFI_NETWORKS_FILE"
  416. }
  417. function create_networks_interactive {
  418. remove_config_param "WIFI_INTERFACE"
  419. update_wifi_adaptors
  420. if [ ! "$IFACE" ]; then
  421. # Don't try to configure wifi if there are no adaptors
  422. return
  423. fi
  424. if [ -f "$WIFI_NETWORKS_FILE" ]; then
  425. rm "$WIFI_NETWORKS_FILE"
  426. fi
  427. # By default connect to any open wifi
  428. WIFI_SSID=''
  429. WIFI_TYPE=$'open'
  430. wifi_ctr=0
  431. wifi_networks_done=
  432. wifi_settings_defined=
  433. while [ ! $wifi_networks_done ]
  434. do
  435. data=$(mktemp 2>/dev/null)
  436. wifistr=$"\\nTo use this system via wifi (eg. USB dongle) enter the details below, otherwise just select Ok:"
  437. if [ -f /root/.wifi-only ]; then
  438. wifistr=$"\\nEnter your wifi login details below."
  439. fi
  440. dialog --backtitle $"Freedombone Configuration" \
  441. --title $"Wifi Settings ${wifi_ctr}" \
  442. --form "$wifistr" 13 65 4 \
  443. $"SSID (can be 'any'):" 1 1 "$WIFI_SSID" 1 24 30 30 \
  444. $"Type (open/wpa2-psk):" 2 1 "$WIFI_TYPE" 2 24 10 10 \
  445. $"Passphrase:" 3 1 "$WIFI_PASSPHRASE" 3 24 50 50 \
  446. 2> "$data"
  447. sel=$?
  448. case $sel in
  449. 1) rm -f "$data"
  450. return;;
  451. 255) rm -f "$data"
  452. return;;
  453. esac
  454. WIFI_SSID=$(sed -n 1p < "$data")
  455. WIFI_TYPE=$(sed -n 2p < "$data")
  456. WIFI_PASSPHRASE=$(sed -n 3p < "$data")
  457. rm -f "$data"
  458. # If the wifi-only indicator is present then wifi details must
  459. # be specified, otherwise they're optional
  460. if [ ! -f /root/.wifi-only ]; then
  461. # if these fields are empty then there are no more wifi networks
  462. if [ ${#WIFI_SSID} -lt 2 ]; then
  463. wifi_networks_done='yes'
  464. continue
  465. fi
  466. if [ ${#WIFI_TYPE} -lt 2 ]; then
  467. wifi_networks_done='yes'
  468. continue
  469. fi
  470. fi
  471. if [ ! -f "$WIFI_NETWORKS_FILE" ]; then
  472. wifi_networks_file_header
  473. fi
  474. # update the wifi networks file
  475. { echo '';
  476. echo "$WIFI_SSID";
  477. echo "$WIFI_TYPE"; } >> "$WIFI_NETWORKS_FILE"
  478. if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
  479. echo "$WIFI_PASSPHRASE" >> "$WIFI_NETWORKS_FILE"
  480. fi
  481. if [ ${#WIFI_SSID} -gt 1 ]; then
  482. if [ ${#WIFI_TYPE} -gt 1 ]; then
  483. if [[ "${WIFI_TYPE}" == $'none' || "${WIFI_TYPE}" == $'open' ]]; then
  484. write_config_param "WIFI_INTERFACE" "$WIFI_INTERFACE"
  485. return
  486. else
  487. if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
  488. write_config_param "WIFI_INTERFACE" "$WIFI_INTERFACE"
  489. return
  490. fi
  491. fi
  492. fi
  493. fi
  494. # clear values
  495. WIFI_SSID=
  496. WIFI_PASSPHRASE=
  497. wifi_ctr=$((wifi_ctr + 1))
  498. done
  499. }
  500. function disable_wifi {
  501. if [[ ${1} == 'yes' || ${1} == 'y' ]]; then
  502. hotspot_off
  503. if [ -f /etc/network/interfaces.d/static ]; then
  504. rm /etc/network/interfaces.d/static
  505. fi
  506. if [ -f /etc/network/interfaces.d/wifi ]; then
  507. rm /etc/network/interfaces.d/wifi
  508. fi
  509. if [ -f /etc/network/interfaces.d/bridge ]; then
  510. rm /etc/network/interfaces.d/bridge
  511. fi
  512. default_network_config
  513. remove_config_param "WIFI_INTERFACE"
  514. wpa_action ${WIFI_INTERFACE} stop
  515. wpa_cli -i ${WIFI_INTERFACE} terminate
  516. systemctl restart network-manager
  517. else
  518. networks_from_file
  519. fi
  520. }
  521. function count_wlan {
  522. # counts the number of wlan devices
  523. ctr=0
  524. for i in $(seq 0 1 10); do
  525. if grep -q "wlan${i}" /proc/net/dev; then
  526. ctr=$((ctr + 1))
  527. fi
  528. done
  529. echo $ctr
  530. }
  531. function wifi_exists {
  532. if grep -q "wlan" /proc/net/dev; then
  533. echo "1"
  534. else
  535. echo "0"
  536. fi
  537. }
  538. function remove_wifi_startup_script {
  539. remove_config_param "WIFI_INTERFACE"
  540. systemd_file=/etc/systemd/system/wifistart.service
  541. if [ -f $systemd_file ]; then
  542. systemctl stop wifistart
  543. systemctl disable wifistart
  544. rm $systemd_file
  545. fi
  546. if [ -f "$HOME/${PROJECT_NAME}-wifi.cfg" ]; then
  547. rm "$HOME/${PROJECT_NAME}-wifi.cfg"
  548. fi
  549. }
  550. function create_wifi_startup_script {
  551. systemd_file=/etc/systemd/system/wifistart.service
  552. { echo '[Unit]';
  553. echo 'Description=WifiStartup (Start wifi networking)';
  554. echo 'After=syslog.target';
  555. echo 'After=network.target';
  556. echo 'After=remote-fs.target';
  557. echo '';
  558. echo '[Service]';
  559. echo 'Type=simple';
  560. echo 'User=root';
  561. echo 'Group=root';
  562. echo 'WorkingDirectory=/root';
  563. echo "ExecStart=/usr/local/bin/${PROJECT_NAME}-wifi --wait 5 2> /dev/null";
  564. echo '';
  565. echo '[Install]';
  566. echo 'WantedBy=multi-user.target'; } > $systemd_file
  567. systemctl enable wifistart
  568. systemctl daemon-reload
  569. }
  570. function remove_wifi_startup_script {
  571. systemd_file=/etc/systemd/system/wifistart.service
  572. if [ -f $systemd_file ]; then
  573. systemctl disable wifistart
  574. systemctl daemon-reload
  575. rm $systemd_file
  576. fi
  577. }
  578. # NOTE: deliberately no exit 0