fix: enhance AP and client mode switching with NetworkManager support
This commit is contained in:
@ -11,6 +11,7 @@ AP_ADDRESS="192.168.4.1/24"
|
|||||||
AP_GATEWAY="192.168.4.1"
|
AP_GATEWAY="192.168.4.1"
|
||||||
AP_DHCP_RANGE_START="192.168.4.20"
|
AP_DHCP_RANGE_START="192.168.4.20"
|
||||||
AP_DHCP_RANGE_END="192.168.4.150"
|
AP_DHCP_RANGE_END="192.168.4.150"
|
||||||
|
AP_CONNECTION_NAME="MPVJ-AP"
|
||||||
|
|
||||||
set_wifi_country() {
|
set_wifi_country() {
|
||||||
if command -v raspi-config > /dev/null 2>&1; then
|
if command -v raspi-config > /dev/null 2>&1; then
|
||||||
@ -59,7 +60,69 @@ EOF
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
configure_access_point_stack() {
|
cleanup_legacy_access_point_stack() {
|
||||||
|
rm -f /etc/NetworkManager/conf.d/99-mpvj-unmanage-wlan0.conf
|
||||||
|
rm -f /etc/systemd/system/mpvj-ap-mode.service
|
||||||
|
systemctl disable mpvj-ap-mode.service hostapd.service dnsmasq.service 2>/dev/null || true
|
||||||
|
systemctl stop mpvj-ap-mode.service hostapd.service dnsmasq.service 2>/dev/null || true
|
||||||
|
|
||||||
|
if [ -f /etc/dhcpcd.conf ]; then
|
||||||
|
sed -i '/# BEGIN MPVJ AP/,/# END MPVJ AP/d' /etc/dhcpcd.conf
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
configure_networkmanager_access_point() {
|
||||||
|
mkdir -p /etc/systemd/system /etc/NetworkManager/conf.d
|
||||||
|
|
||||||
|
cleanup_legacy_access_point_stack
|
||||||
|
|
||||||
|
cat <<EOF > /etc/NetworkManager/conf.d/90-mpvj-wifi-powersave.conf
|
||||||
|
[connection]
|
||||||
|
wifi.powersave = 2
|
||||||
|
EOF
|
||||||
|
|
||||||
|
cat <<EOF > /etc/sysctl.d/90-mpvj-ap.conf
|
||||||
|
net.ipv4.ip_forward=1
|
||||||
|
EOF
|
||||||
|
|
||||||
|
systemctl enable NetworkManager.service
|
||||||
|
systemctl restart NetworkManager.service
|
||||||
|
|
||||||
|
nmcli radio wifi on || true
|
||||||
|
nmcli connection delete "$AP_CONNECTION_NAME" 2>/dev/null || true
|
||||||
|
nmcli device set wlan0 managed yes || true
|
||||||
|
nmcli device set wlan0 autoconnect yes || true
|
||||||
|
nmcli device disconnect wlan0 2>/dev/null || true
|
||||||
|
|
||||||
|
nmcli connection add type wifi ifname wlan0 con-name "$AP_CONNECTION_NAME" autoconnect yes ssid "$WIFI_SSID"
|
||||||
|
nmcli connection modify "$AP_CONNECTION_NAME" connection.interface-name wlan0
|
||||||
|
nmcli connection modify "$AP_CONNECTION_NAME" connection.autoconnect yes connection.autoconnect-priority 100
|
||||||
|
nmcli connection modify "$AP_CONNECTION_NAME" 802-11-wireless.mode ap 802-11-wireless.band bg 802-11-wireless.powersave 2
|
||||||
|
nmcli connection modify "$AP_CONNECTION_NAME" ipv4.method shared ipv4.addresses "$AP_ADDRESS" ipv6.method disabled
|
||||||
|
nmcli connection modify "$AP_CONNECTION_NAME" wifi-sec.key-mgmt wpa-psk wifi-sec.psk "$WIFI_PASS"
|
||||||
|
|
||||||
|
cat <<EOF > /etc/systemd/system/mpvj-nm-ap.service
|
||||||
|
[Unit]
|
||||||
|
Description=Bring up MPVJ NetworkManager hotspot on boot
|
||||||
|
After=NetworkManager.service
|
||||||
|
Wants=NetworkManager.service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=oneshot
|
||||||
|
RemainAfterExit=yes
|
||||||
|
ExecStart=/usr/bin/bash -lc 'nmcli radio wifi on || true; nmcli device set wlan0 managed yes || true; nmcli device set wlan0 autoconnect yes || true; nmcli connection up "$AP_CONNECTION_NAME" ifname wlan0 || (nmcli connection reload && nmcli connection up "$AP_CONNECTION_NAME" ifname wlan0)'
|
||||||
|
ExecStop=/usr/bin/bash -lc 'nmcli connection down "$AP_CONNECTION_NAME" 2>/dev/null || true'
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
|
||||||
|
systemctl daemon-reload
|
||||||
|
systemctl enable mpvj-nm-ap.service avahi-daemon.service
|
||||||
|
systemctl restart mpvj-nm-ap.service
|
||||||
|
}
|
||||||
|
|
||||||
|
configure_legacy_access_point_stack() {
|
||||||
mkdir -p /etc/hostapd /etc/systemd/system /etc/NetworkManager/conf.d
|
mkdir -p /etc/hostapd /etc/systemd/system /etc/NetworkManager/conf.d
|
||||||
|
|
||||||
cat <<EOF > /etc/hostapd/hostapd.conf
|
cat <<EOF > /etc/hostapd/hostapd.conf
|
||||||
@ -132,6 +195,16 @@ EOF
|
|||||||
systemctl disable wpa_supplicant.service wpa_supplicant@wlan0.service 2>/dev/null || true
|
systemctl disable wpa_supplicant.service wpa_supplicant@wlan0.service 2>/dev/null || true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configure_access_point_stack() {
|
||||||
|
if command -v nmcli > /dev/null 2>&1 && systemctl list-unit-files NetworkManager.service --no-legend 2>/dev/null | grep -q '^NetworkManager\.service'; then
|
||||||
|
echo "Using NetworkManager hotspot configuration for wlan0..."
|
||||||
|
configure_networkmanager_access_point
|
||||||
|
else
|
||||||
|
echo "Using hostapd/dnsmasq hotspot configuration for wlan0..."
|
||||||
|
configure_legacy_access_point_stack
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
if [ -z "$REPO_URL" ]; then
|
if [ -z "$REPO_URL" ]; then
|
||||||
echo "Usage: $0 <REPO_URL>"
|
echo "Usage: $0 <REPO_URL>"
|
||||||
exit 1
|
exit 1
|
||||||
|
|||||||
@ -5,15 +5,21 @@ set -euo pipefail
|
|||||||
echo "Switching to AP mode..."
|
echo "Switching to AP mode..."
|
||||||
|
|
||||||
rfkill unblock wlan || true
|
rfkill unblock wlan || true
|
||||||
systemctl stop wpa_supplicant.service wpa_supplicant@wlan0.service 2>/dev/null || true
|
|
||||||
|
|
||||||
|
if command -v nmcli > /dev/null 2>&1 && nmcli connection show MPVJ-AP > /dev/null 2>&1; then
|
||||||
|
nmcli radio wifi on || true
|
||||||
|
nmcli device set wlan0 managed yes || true
|
||||||
|
nmcli device set wlan0 autoconnect yes || true
|
||||||
|
nmcli connection up MPVJ-AP ifname wlan0
|
||||||
|
else
|
||||||
|
systemctl stop wpa_supplicant.service wpa_supplicant@wlan0.service 2>/dev/null || true
|
||||||
ip link set wlan0 down || true
|
ip link set wlan0 down || true
|
||||||
ip addr flush dev wlan0 || true
|
ip addr flush dev wlan0 || true
|
||||||
ip link set wlan0 up
|
ip link set wlan0 up
|
||||||
ip address add 192.168.4.1/24 dev wlan0
|
ip address add 192.168.4.1/24 dev wlan0
|
||||||
|
|
||||||
systemctl start mpvj-ap-mode.service
|
systemctl start mpvj-ap-mode.service
|
||||||
systemctl restart hostapd.service
|
systemctl restart hostapd.service
|
||||||
systemctl restart dnsmasq.service
|
systemctl restart dnsmasq.service
|
||||||
|
fi
|
||||||
|
|
||||||
echo "AP mode active on wlan0 at 192.168.4.1"
|
echo "AP mode active on wlan0 at 192.168.4.1"
|
||||||
|
|||||||
@ -4,12 +4,16 @@ set -euo pipefail
|
|||||||
|
|
||||||
echo "Switching to client mode..."
|
echo "Switching to client mode..."
|
||||||
|
|
||||||
|
if command -v nmcli > /dev/null 2>&1 && nmcli connection show MPVJ-AP > /dev/null 2>&1; then
|
||||||
|
nmcli connection down MPVJ-AP 2>/dev/null || true
|
||||||
|
nmcli device set wlan0 autoconnect yes || true
|
||||||
|
nmcli device connect wlan0 2>/dev/null || true
|
||||||
|
else
|
||||||
systemctl stop mpvj-ap-mode.service 2>/dev/null || true
|
systemctl stop mpvj-ap-mode.service 2>/dev/null || true
|
||||||
systemctl stop hostapd.service dnsmasq.service
|
systemctl stop hostapd.service dnsmasq.service
|
||||||
|
|
||||||
ip addr flush dev wlan0 || true
|
ip addr flush dev wlan0 || true
|
||||||
ip link set wlan0 up || true
|
ip link set wlan0 up || true
|
||||||
|
|
||||||
systemctl start wpa_supplicant.service || systemctl start wpa_supplicant@wlan0.service
|
systemctl start wpa_supplicant.service || systemctl start wpa_supplicant@wlan0.service
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Client mode active. wlan0 returned to wpa_supplicant."
|
echo "Client mode active. wlan0 returned to wpa_supplicant."
|
||||||
|
|||||||
Reference in New Issue
Block a user