1
0
mirror of https://github.com/billz/raspap-webgui.git synced 2023-10-10 13:37:24 +02:00
raspap-webgui/installers/servicestart.sh

158 lines
4.4 KiB
Bash
Raw Normal View History

2019-03-06 20:14:04 +01:00
#!/bin/bash
# When wireless client AP or Bridge mode is enabled, this script handles starting
# up network services in a specific order and timing to avoid race conditions.
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=raspapd
DESC="Service control for RaspAP"
CONFIGFILE="/etc/raspap/hostapd.ini"
DAEMONPATH="/lib/systemd/system/raspapd.service"
2020-03-15 12:02:20 +01:00
OPENVPNENABLED=$(pidof openvpn | wc -l)
positional=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-i | --interface)
interface="$2"
shift # past argument
shift # past value
;;
-s | --seconds)
seconds="$2"
shift
shift
;;
-a | --action)
action="$2"
shift
shift
;;
esac
done
set -- "${positional[@]}"
2019-03-06 20:14:04 +01:00
2021-10-19 20:23:19 +02:00
HOSTAPD_CONF="/etc/hostapd/hostapd.conf"
2019-03-06 20:14:04 +01:00
2021-10-19 20:23:19 +02:00
new_country_code=$(curl -s -H "Authorization: Bearer 9da1eb466ed052" https://ipinfo.io/json | jq -r ".country // empty")
old_country_code=$(grep ^country_code $HOSTAPD_CONF | cut -d "=" -f 2)
2020-03-20 10:05:33 +01:00
2021-10-19 20:23:19 +02:00
if [[ ! -z "$new_country_code" ]] && [[ "$old_country_code" != "$new_country_code" ]]; then
sudo sed -i "s/country_code=$old_country_code/country_code=$new_country_code/" /etc/hostapd/hostapd.conf
echo "Updated country code: $new_country_code"
fi
old_ssid=$(grep ^ssid $HOSTAPD_CONF | cut -d "=" -f 2)
2021-10-19 19:30:47 +02:00
rpi_serial=$(cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2)
new_ssid="isobox-$rpi_serial"
if [[ "$old_ssid" != "$new_ssid" ]]; then
2021-10-19 20:23:19 +02:00
sed -i "s/ssid=$old_ssid/ssid=$new_ssid/" $HOSTAPD_CONF
echo "Updated ssid: $new_ssid"
fi
2021-10-19 19:30:47 +02:00
old_hostname=$(hostname)
2021-10-19 20:23:19 +02:00
new_hostname="isobox-$rpi_serial"
2021-10-19 19:30:47 +02:00
if [[ "$old_hostname" != "$new_hostname" ]]; then
echo $new_hostname >/etc/hostname
sed -i "s/$old_hostname/$new_hostname/" /etc/hosts
hostname $new_hostname
2021-10-19 20:23:19 +02:00
echo "Updated hostname: $new_hostname"
fi
echo "Stopping network services..."
if [ $OPENVPNENABLED -eq 1 ]; then
systemctl stop openvpn-client@client
fi
systemctl stop systemd-networkd
systemctl stop hostapd.service
systemctl stop dnsmasq.service
systemctl stop dhcpcd.service
if [ "${action}" = "stop" ]; then
echo "Services stopped. Exiting."
exit 0
2021-10-19 19:30:47 +02:00
fi
if [ -f "$DAEMONPATH" ] && [ ! -z "$interface" ]; then
2020-03-10 09:42:08 +01:00
echo "Changing RaspAP Daemon --interface to $interface"
sed -i "s/\(--interface \)[[:alnum:]]*/\1$interface/" "$DAEMONPATH"
fi
if [ -r "$CONFIGFILE" ]; then
declare -A config
2019-05-01 00:19:34 +02:00
while IFS=" = " read -r key value; do
config["$key"]="$value"
done <"$CONFIGFILE"
2020-03-10 09:42:08 +01:00
if [ "${config[BridgedEnable]}" = 1 ]; then
if [ "${interface}" = "br0" ]; then
2020-03-22 12:47:47 +01:00
echo "Stopping systemd-networkd"
systemctl stop systemd-networkd
2020-03-10 09:42:08 +01:00
echo "Restarting eth0 interface..."
ip link set down eth0
ip link set up eth0
2020-03-15 12:02:20 +01:00
echo "Removing uap0 interface..."
iw dev uap0 del
echo "Enabling systemd-networkd"
systemctl start systemd-networkd
systemctl enable systemd-networkd
2020-03-10 09:42:08 +01:00
fi
else
echo "Disabling systemd-networkd"
systemctl disable systemd-networkd
ip link ls up | grep -q 'br0' &>/dev/null
if [ $? == 0 ]; then
echo "Removing br0 interface..."
ip link set down br0
ip link del dev br0
fi
2020-03-10 09:42:08 +01:00
if [ "${config[WifiAPEnable]}" = 1 ]; then
if [ "${interface}" = "uap0" ]; then
2020-03-29 17:08:33 +02:00
ip link ls up | grep -q 'uap0' &>/dev/null
2020-03-29 17:08:33 +02:00
if [ $? == 0 ]; then
echo "Removing uap0 interface..."
iw dev uap0 del
fi
2020-03-10 09:42:08 +01:00
echo "Adding uap0 interface to ${config[WifiManaged]}"
iw dev ${config[WifiManaged]} interface add uap0 type __ap
# Bring up uap0 interface
ifconfig uap0 up
fi
fi
2019-04-19 13:35:03 +02:00
fi
fi
2019-03-06 20:14:04 +01:00
# Start services, mitigating race conditions
echo "Starting network services..."
2019-03-06 20:14:04 +01:00
systemctl start hostapd.service
sleep "${seconds}"
2019-03-06 20:14:04 +01:00
systemctl start dhcpcd.service
sleep "${seconds}"
2019-03-06 20:14:04 +01:00
systemctl start dnsmasq.service
2020-03-15 12:02:20 +01:00
if [ $OPENVPNENABLED -eq 1 ]; then
systemctl start openvpn-client@client
fi
# @mp035 found that the wifi client interface would stop every 8 seconds
# for about 16 seconds. Reassociating seems to solve this
if [ "${config[WifiAPEnable]}" = 1 ]; then
echo "Reassociating wifi client interface..."
sleep "${seconds}"
wpa_cli -i ${config[WifiManaged]} reassociate
fi
2019-03-07 23:14:27 +01:00
echo "RaspAP service start DONE"