Skip to content

Commit

Permalink
network updates
Browse files Browse the repository at this point in the history
disable dhcp server in client mode, support connection to network with no encryption
  • Loading branch information
zavviodotcom committed Mar 4, 2018
1 parent 63e5d43 commit 0b7a473
Show file tree
Hide file tree
Showing 2 changed files with 590 additions and 0 deletions.
213 changes: 213 additions & 0 deletions 01-network
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 0b7a473

Please sign in to comment.