| """Class to configure wireless settings.""" |
| |
| import time |
| |
| from antlion.controllers.ap_lib import hostapd_constants |
| from antlion.controllers.openwrt_lib.network_settings import ( |
| SERVICE_DNSMASQ, |
| ServiceManager, |
| ) |
| from antlion.controllers.openwrt_lib.wireless_config import WirelessConfig |
| |
| LEASE_FILE = "/tmp/dhcp.leases" |
| OPEN_SECURITY = "none" |
| PSK1_SECURITY = "psk" |
| PSK_SECURITY = "psk2" |
| WEP_SECURITY = "wep" |
| ENT_SECURITY = "wpa2" |
| OWE_SECURITY = "owe" |
| SAE_SECURITY = "sae" |
| SAEMIXED_SECURITY = "sae-mixed" |
| ENABLE_RADIO = "0" |
| DISABLE_RADIO = "1" |
| ENABLE_HIDDEN = "1" |
| RADIO_2G = "radio1" |
| RADIO_5G = "radio0" |
| |
| |
| class WirelessSettingsApplier(object): |
| """Class for wireless settings. |
| |
| Attributes: |
| ssh: ssh object for the AP. |
| service_manager: Object manage service configuration |
| wireless_configs: a list of |
| antlion.controllers.openwrt_lib.wireless_config.WirelessConfig. |
| channel_2g: channel for 2G band. |
| channel_5g: channel for 5G band. |
| """ |
| |
| def __init__( |
| self, ssh, configs, channel_2g, channel_5g, radio_2g=RADIO_2G, radio_5g=RADIO_5G |
| ): |
| """Initialize wireless settings. |
| |
| Args: |
| ssh: ssh connection object. |
| configs: a list of |
| antlion.controllers.openwrt_lib.wireless_config.WirelessConfig. |
| channel_2g: channel for 2G band. |
| channel_5g: channel for 5G band. |
| """ |
| self.ssh = ssh |
| self.service_manager = ServiceManager(ssh) |
| self.wireless_configs: list[WirelessConfig] = configs |
| self.channel_2g = channel_2g |
| self.channel_5g = channel_5g |
| self.radio_2g = radio_2g |
| self.radio_5g = radio_5g |
| |
| def apply_wireless_settings(self): |
| """Configure wireless settings from a list of configs.""" |
| default_2g_iface = f"default_{self.radio_2g}" |
| default_5g_iface = f"default_{self.radio_5g}" |
| |
| # set channels for 2G and 5G bands |
| self.ssh.run(f"uci set wireless.{self.radio_2g}.channel='{self.channel_2g}'") |
| self.ssh.run(f"uci set wireless.{self.radio_5g}.channel='{self.channel_5g}'") |
| if self.channel_5g == 165: |
| self.ssh.run(f"uci set wireless.{self.radio_5g}.htmode='VHT20'") |
| elif self.channel_5g == 132 or self.channel_5g == 136: |
| self.ssh.run("iw reg set ZA") |
| self.ssh.run(f"uci set wireless.{self.radio_5g}.htmode='VHT40'") |
| |
| if self.channel_2g == 13: |
| self.ssh.run("iw reg set AU") |
| |
| # disable default OpenWrt SSID |
| self.ssh.run(f"uci set wireless.{default_2g_iface}.disabled='{DISABLE_RADIO}'") |
| self.ssh.run(f"uci set wireless.{default_5g_iface}.disabled='{DISABLE_RADIO}'") |
| |
| # Enable radios |
| self.ssh.run(f"uci set wireless.{self.radio_2g}.disabled='{ENABLE_RADIO}'") |
| self.ssh.run(f"uci set wireless.{self.radio_5g}.disabled='{ENABLE_RADIO}'") |
| |
| for config in self.wireless_configs: |
| # configure open network |
| if config.security == OPEN_SECURITY: |
| if config.band == hostapd_constants.BAND_2G: |
| self.ssh.run( |
| f"uci set wireless.{default_2g_iface}.ssid='{config.ssid}'" |
| ) |
| self.ssh.run( |
| f"uci set wireless.{default_2g_iface}.disabled='{ENABLE_RADIO}'" |
| ) |
| if config.hidden: |
| self.ssh.run( |
| "uci set wireless.%s.hidden='%s'" |
| % (default_2g_iface, ENABLE_HIDDEN) |
| ) |
| elif config.band == hostapd_constants.BAND_5G: |
| self.ssh.run( |
| f"uci set wireless.{default_5g_iface}.ssid='{config.ssid}'" |
| ) |
| self.ssh.run( |
| f"uci set wireless.{default_5g_iface}.disabled='{ENABLE_RADIO}'" |
| ) |
| if config.hidden: |
| self.ssh.run( |
| "uci set wireless.%s.hidden='%s'" |
| % (default_5g_iface, ENABLE_HIDDEN) |
| ) |
| continue |
| |
| self.ssh.run(f"uci set wireless.{config.name}='wifi-iface'") |
| if config.band == hostapd_constants.BAND_2G: |
| self.ssh.run(f"uci set wireless.{config.name}.device='{self.radio_2g}'") |
| else: |
| self.ssh.run(f"uci set wireless.{config.name}.device='{self.radio_5g}'") |
| self.ssh.run(f"uci set wireless.{config.name}.network='{config.iface}'") |
| self.ssh.run(f"uci set wireless.{config.name}.mode='ap'") |
| self.ssh.run(f"uci set wireless.{config.name}.ssid='{config.ssid}'") |
| self.ssh.run( |
| f"uci set wireless.{config.name}.encryption='{config.security}'" |
| ) |
| if ( |
| config.security == PSK_SECURITY |
| or config.security == SAE_SECURITY |
| or config.security == PSK1_SECURITY |
| or config.security == SAEMIXED_SECURITY |
| ): |
| self.ssh.run(f"uci set wireless.{config.name}.key='{config.password}'") |
| elif config.security == WEP_SECURITY: |
| self.ssh.run( |
| "uci set wireless.%s.key%s='%s'" |
| % (config.name, config.wep_key_num, config.wep_key) |
| ) |
| self.ssh.run( |
| f"uci set wireless.{config.name}.key='{config.wep_key_num}'" |
| ) |
| elif config.security == ENT_SECURITY: |
| self.ssh.run( |
| "uci set wireless.%s.auth_secret='%s'" |
| % (config.name, config.radius_server_secret) |
| ) |
| self.ssh.run( |
| "uci set wireless.%s.auth_server='%s'" |
| % (config.name, config.radius_server_ip) |
| ) |
| self.ssh.run( |
| "uci set wireless.%s.auth_port='%s'" |
| % (config.name, config.radius_server_port) |
| ) |
| if config.ieee80211w: |
| self.ssh.run( |
| f"uci set wireless.{config.name}.ieee80211w='{config.ieee80211w}'" |
| ) |
| if config.hidden: |
| self.ssh.run(f"uci set wireless.{config.name}.hidden='{ENABLE_HIDDEN}'") |
| |
| self.ssh.run("uci commit wireless") |
| self.ssh.run(f"cp {LEASE_FILE} {LEASE_FILE}.tmp") |
| |
| def cleanup_wireless_settings(self): |
| """Reset wireless settings to default.""" |
| self.ssh.run("wifi down") |
| self.ssh.run("rm -f /etc/config/wireless") |
| self.ssh.run("wifi config") |
| if self.channel_5g == 132: |
| self.ssh.run("iw reg set US") |
| self.ssh.run(f"cp {LEASE_FILE}.tmp {LEASE_FILE}") |
| self.service_manager.restart(SERVICE_DNSMASQ) |
| time.sleep(9) |