From 0b7a473d8b7979ed8189862ff9bd5980e349d0d1 Mon Sep 17 00:00:00 2001
From: zavviodotcom <30752771+zavviodotcom@users.noreply.github.com>
Date: Sun, 4 Mar 2018 11:36:15 -0700
Subject: [PATCH] network updates
disable dhcp server in client mode, support connection to network with no encryption
---
01-network | 213 ++++++++++++++++++++++++++++++
network | 377 +++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 590 insertions(+)
create mode 100644 01-network
create mode 100644 network
diff --git a/01-network b/01-network
new file mode 100644
index 0000000..fa3a8e1
--- /dev/null
+++ b/01-network
@@ -0,0 +1,213 @@
+#!/bin/sh
+
+source /etc/fang_hacks.cfg
+source /media/mmcblk0p2/data/etc/profile
+
+NETWORK_MODE=${NETWORK_MODE:-0}
+DISABLE_CLOUD=${DISABLE_CLOUD:-0}
+
+kill_network()
+{
+ if [ -e "/var/run/wpa_supplicant.pid" -o -d "/var/run/wpa_supplicant" ]; then
+ echo "Terminating wpa_supplicant"
+ killall wpa_supplicant
+ fi
+
+ if [ -e "/var/run/hostapd.pid" -o -d "/var/run/hostapd" ]; then
+ echo "Terminating hostapd"
+ killall hostapd
+ fi
+ if [ -e "/var/run/udhcpc.pid" ]; then
+ echo "Terminating udhcpc"
+ killall udhcpc
+ fi
+ if [ -e "/var/run/udhcpd.pid" ]; then
+ echo "Terminating udhcpd"
+ killall udhcpd
+ fi
+}
+
+start()
+{
+ echo "Starting Network..."
+ rc=0
+ case $NETWORK_MODE in
+ 0)
+ echo "Cloud mode: network unmanaged"
+ if [ "$DISABLE_CLOUD" -eq 1 ]; then
+ echo "WARNING: DISABLE_CLOUD is enabled!"
+ echo "WARNING: Network will NOT be operational!"
+ rc=1
+ fi
+
+ if [ -n "$try_recover" ]; then
+ ssid="$(cat /etc/config/.wifissid)"
+ key="$(cat /etc/config/.wifipasswd)"
+ echo "Trying to recover from failed connection (ssid: $ssid)..."
+ do_connect 0 "$ssid" "$key"
+ rc=$?
+ fi
+ ;;
+ 1)
+ echo "WiFi Client mode: using wpa_supplicant.conf"
+ CFG_WPA="${CFG_WPA:-/media/mmcblk0p2/data/etc/wpa_supplicant.conf}"
+ if [ ! -e "$CFG_WPA" ]; then echo "File not found: $CFG_WPA"; return 1; fi
+ kill_network
+
+ wpa_supplicant -B -i wlan0 \
+ -c /media/mmcblk0p2/data/etc/wpa_supplicant.conf \
+ -P /var/run/wpa_supplicant.pid 2>&1
+ rc=$?
+ if [ $rc -eq 0 ]; then
+ udhcpc -i wlan0 -p /var/run/udhcpc.pid -b 2>&1
+ rc=$?
+ if [ $rc -ne 0 ]; then
+ echo "Failed to start udhcpc"
+ fi
+ else
+ echo "Failed to start wpa_supplicant"
+ fi
+ ;;
+ 2)
+ echo "Access point mode: using hostapd.conf"
+ CFG_AP="${CFG_AP:-/media/mmcblk0p2/data/etc/hostapd.conf}"
+ CFG_DHCPD="${CFG_DHCPD:-/media/mmcblk0p2/data/etc/udhcpd.conf}"
+ if [ ! -e "$CFG_AP" ]; then echo "File not found: $CFG_AP"; return 1; fi
+ if [ ! -e "$CFG_DHCPD" ]; then echo "File not found: $CFG_AP"; return 1; fi
+ ap_addr="$(cat $CFG_DHCPD | grep ^opt.*router | awk '{print $3}')"
+ ap_ssid="$(cat $CFG_AP | grep ^ssid= | cut -d'=' -f2)"
+ if expr "$ap_addr" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
+ kill_network
+ ifconfig wlan0 $ap_addr 2>&1
+ if [ $? -ne 0 ]; then
+ echo "Failed to set IP $ap_addr"; return 1;
+ fi
+ hostapd -B -P /var/run/hostapd.pid /media/mmcblk0p2/data/etc/hostapd.conf 2>&1
+ if [ $? -ne 0 ]; then
+ echo "Failed to start hostapd"; return 1;
+ fi
+ udhcpd -f /media//mmcblk0p2/data/etc/udhcpd.conf >>/tmp/hacks.log 2>&1 &
+ if [ $? -ne 0 ]; then
+ echo "Failed to start udhcpd"; return 1;
+ fi
+ echo "Hotspot '$ap_addr' online"
+ else
+ echo "Invalid IP: $ap_addr"
+ rc=1
+ fi
+ ;;
+ *)
+ echo "Unsupported NETWORK_MODE($NETWORK_MODE)"
+ ;;
+ esac
+ return $rc
+}
+
+stop()
+{
+ echo "For safety reasons, the network cannot be stopped."
+ echo "You would be locked out!"
+}
+
+connect()
+{
+ # If connection fails, run start() to hopefully restore connection
+ try_recover=1
+ do_connect "$@"
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ echo "Connection failed: trying to recover..."
+ start
+ fi
+ return $ret
+}
+
+do_connect()
+{
+ # temporary connect to ssid
+ if [ -z "$2" ]; then
+ echo "Usage: $0 connect [ssid] [passphrase]"
+ return 1
+ fi
+ cat > /tmp/wpa_tmp.conf << EOF
+ctrl_interface=/var/run/wpa_supplicant
+update_config=1
+EOF
+echo "network={" >> /tmp/wpa_tmp.conf
+#echo "bssid=$4" >> /tmp/wpa_tmp.conf
+printf "ssid=\"" >> /tmp/wpa_tmp.conf
+echo "$2"\" | tr '+' ' ' >> /tmp/wpa_tmp.conf
+if [ -z "$3" ]; then
+echo "key_mgmt=NONE" >> /tmp/wpa_tmp.conf
+else
+echo "psk=\"$3\"" >> /tmp/wpa_tmp.conf
+fi
+echo "}" >> /tmp/wpa_tmp.conf
+ if [ -e "/var/run/wpa_supplicant.pid" -o -d "/var/run/wpa_supplicant" ]; then
+ echo "Terminating wpa_supplicant"
+ killall wpa_supplicant
+ fi
+ if [ -e "/var/run/udhcpc.pid" ]; then
+ echo "Terminating udhcpc"
+ killall udhcpc
+ fi
+ if [ -e "/var/run/udhcpd.pid" ]; then
+ echo "Terminating udhcpd"
+ killall udhcpd
+ fi
+ if pidof hostapd >/dev/null; then
+ echo "Terminating hostapd"
+ killall hostapd
+ fi
+ echo "Connecting to '$2'..."
+ wpa_supplicant -B -i wlan0 -c /tmp/wpa_tmp.conf -P /var/run/wpa_supplicant.pid >/dev/null
+ if [ $? -eq 0 ]; then
+ id="$(wpa_cli -iwlan0 add_network)"
+ status="$(wpa_cli -iwlan0 set_network $id ssid \"$2\")"
+ if [ "$status" != "OK" ]; then echo "wpa_cli ssid failed: $status"; return 1; fi
+ status="$(wpa_cli -iwlan0 set_network $id scan_ssid 1)"
+ if [ "$status" != "OK" ]; then echo "wpa_cli scan_ssid failed: $status"; return 1; fi
+ if [ -z "$3" ]; then
+ status="$(wpa_cli -iwlan0 set_network $id key_mgmt NONE)"
+ if [ "$status" != "OK" ]; then echo "wpa_cli key_mgmt failed: $status"; return 1; fi
+ else
+ status="$(wpa_cli -iwlan0 set_network $id key_mgmt WPA-PSK)"
+ if [ "$status" != "OK" ]; then echo "wpa_cli key_mgmt failed: $status"; return 1; fi
+ status="$(wpa_cli -iwlan0 set_network $id psk \"$3\")"
+ if [ "$status" != "OK" ]; then echo "wpa_cli psk failed: $status"; return 1; fi
+ fi
+ status="$(wpa_cli -iwlan0 enable_network $id)"
+ if [ "$status" != "OK" ]; then echo "wpa_cli psk failed: $status"; return 1; fi
+ else
+ echo "Failed to start wpa_supplicant"
+ return 1
+ fi
+
+ udhcpc -i wlan0 -p /var/run/udhcpc.pid -b 2>&1
+ if [ $? -ne 0 ]; then
+ echo "Failed to start udhcpc"
+ return 1
+ fi
+ echo "Success"
+ return 0
+}
+
+status()
+{
+ case $NETWORK_MODE in
+ 0) echo "Cloud" ;;
+ 1) echo "Client" ;;
+ 2) echo "AP" ;;
+ *) echo "NOK"; return 1; ;;
+ esac
+ return 0
+}
+
+if [ $# -eq 0 ]; then
+ start
+else
+ case $1 in start|stop|status|connect|kill_network)
+ $1 "$@"
+ ;;
+ esac
+fi
diff --git a/network b/network
new file mode 100644
index 0000000..95fcb0c
--- /dev/null
+++ b/network
@@ -0,0 +1,377 @@
+#!/bin/sh
+
+echo "Content-type: text/html"
+echo ""
+source func.cgi
+if [ -e "/etc/fang_hacks.cfg" ]; then source /etc/fang_hacks.cfg; fi
+PATH="/bin:/sbin:/usr/bin:/media/mmcblk0p2/data/bin:/media/mmcblk0p2/data/sbin:/media/mmcblk0p2/data/usr/bin"
+
+CFG_MODE="${NETWORK_MODE:-0}"
+CFG_CLIENT="/media/mmcblk0p2/data/etc/wpa_supplicant.conf"
+CFG_AP="/media/mmcblk0p2/data/etc/hostapd.conf"
+CFG_DHCPD="/media/mmcblk0p2/data/etc/udhcpd.conf"
+
+
+if [ -n "$F_action" ]; then
+ case "$F_action" in
+ connect)
+ client_ssid=$(printf '%b' "${F_ssid//%/\\x}")
+ client_key=$(printf '%b' "${F_key//%/\\x}")
+ ACTION_MSG="$(/media/mmcblk0p2/data/etc/scripts/01-network connect "$client_ssid" "$client_key")"
+ ACTION_RC=$?
+ CFG_MODE=1
+ ;;
+ apply)
+ ACTION_RC=1
+ apply_ssid=$(printf '%b' "${F_ssid//%/\\x}")
+ apply_key=$(printf '%b' "${F_key//%/\\x}")
+ case "$F_mode" in
+ 0)
+ # apply cloud settings
+ if [ -n "$apply_ssid" -a -n "${apply_key}" ]; then
+ ACTION_MSG="Updating cloud settings..."
+ echo -n "$apply_ssid" > /etc/config/.wifissid
+ echo -n "$apply_key" > /etc/config/.wifipasswd
+ ACTION_RC=0
+ CFG_MODE=0
+ fi
+ ;;
+ 1)
+ # apply wpa_supplicant settings
+ if [ -n "$apply_ssid" -a -n "${apply_key}" ]; then
+ if [ -e "$CFG_CLIENT" ]; then
+ ACTION_MSG="Updating wpa_supplicant..."
+ sed -i.bak 's/^\(\sssid\).*/\1="'${apply_ssid}'"/; s/^\(\spsk\).*/\1="'${apply_key}'"/' "$CFG_CLIENT" 2>&1
+ ACTION_RC=$?
+ CFG_MODE=1
+ else
+ ACTION_MSG="$CFG_CLIENT doesn't exist!"
+ fi
+ fi
+ ;;
+ 2)
+ # apply hostapd settings
+ if [ -n "$apply_ssid" -a -n "$apply_key" ]; then
+ if [ -e "$CFG_AP" ]; then
+ ACTION_MSG="Updating hostapd..."
+ sed -i.bak 's/^\(ssid\).*/\1='${apply_ssid}'/; s/^\(wpa_passphrase\).*/\1='${apply_key}'/' "$CFG_AP" 2>&1
+ ACTION_RC=$?
+ CFG_MODE=2
+ else
+ ACTION_MSG="$CFG_AP doesn't exist!"
+ fi
+ fi
+ ;;
+ esac
+ if [ $ACTION_RC -eq 0 ]; then
+ # apply network mode
+ sed -i.bak 's/^NETWORK_MODE=[0-9]/NETWORK_MODE='$CFG_MODE'/' /etc/fang_hacks.cfg
+ ACTION_MSG="$ACTION_MSG
Applying network mode $CFG_MODE"
+ echo "Restarting Network..."
+ ACTION_MSG="$ACTION_MSG
$(/media/mmcblk0p2/data/etc/scripts/01-network start 2>&1)" + fi + ;; + esac +fi + +if [ -e "$CFG_CLIENT" ]; then + CFG_SSID="$(cat "$CFG_CLIENT")" +fi + +cat << EOF + + + +
$ACTION_MSG
Interfaces:+
$(ifconfig; iwconfig)
Routes:+
$(route)
DNS:+ + +EOF + +
$(cat /etc/resolv.conf)