Exemplo n.º 1
0
    def get_known_wifis(self):
        from vorta.models import WifiSettingModel
        plist_path = '/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist'

        try:
            plist_file = open(plist_path, 'rb')
            wifis = plistlib.load(plist_file).get('KnownNetworks')
        except xml.parsers.expat.ExpatError:
            logger.error('Unable to parse list of Wifi networks.')
            return None

        result = []
        if wifis is not None:
            for wifi in wifis.values():
                raw_last_connected = wifi.get('LastConnected', None)
                last_connected = None if not raw_last_connected \
                    else format_date_time(raw_last_connected, WifiSettingModel.last_connected.formats)
                ssid = wifi.get('SSIDString', None)

                if ssid is None:
                    continue

                result.append(
                    SystemWifiInfo(ssid=ssid, last_connected=last_connected))

        return result
Exemplo n.º 2
0
    def get_known_wifis(self) -> List[SystemWifiInfo]:
        wifis = []
        try:
            connections_paths = self._nm.get_connections_paths()
        except DBusException:
            logger.exception("Failed to list connections")
            return wifis

        for connection_path in connections_paths:
            try:
                settings = self._nm.get_settings(connection_path)
            except DBusException:
                logger.warning("Couldn't load settings for %s",
                               connection_path,
                               exc_info=True)
            else:
                ssid = self._get_ssid_from_settings(settings)
                if ssid:
                    timestamp = settings['connection'].get('timestamp')
                    wifis.append(
                        SystemWifiInfo(
                            ssid=ssid,
                            last_connected=timestamp
                            and datetime.utcfromtimestamp(timestamp),
                        ))
        return wifis
Exemplo n.º 3
0
    def get_known_wifis(self):
        """
        Listing all known Wifi networks isn't possible any more from macOS 11. Instead we
        just return the current Wifi.
        """
        wifis = []
        current_wifi = self.get_current_wifi()
        if current_wifi is not None:
            wifis.append(SystemWifiInfo(ssid=current_wifi, last_connected=dt.now()))

        return wifis
Exemplo n.º 4
0
def test_get_known_wifis_with_never_used_connection(nm_monitor):
    nm_monitor._nm.get_connections_paths.return_value = ['/org/freedesktop/NetworkManager/Settings/12']
    nm_monitor._nm.get_settings.return_value = {
        'connection': {},
        '802-11-wireless': {'ssid': [84, 69, 83, 84]},
    }

    result = nm_monitor.get_known_wifis()

    assert result == [SystemWifiInfo(
        ssid='TEST',
        last_connected=None,
    )]
Exemplo n.º 5
0
def test_get_known_wifis(nm_monitor):
    nm_monitor._nm.get_connections_paths.return_value = ['/org/freedesktop/NetworkManager/Settings/12']
    nm_monitor._nm.get_settings.return_value = {
        'connection': {'timestamp': 1597303736},
        '802-11-wireless': {'ssid': [84, 69, 83, 84]},
    }

    result = nm_monitor.get_known_wifis()

    assert result == [SystemWifiInfo(
        ssid='TEST',
        last_connected=datetime(2020, 8, 13, 7, 28, 56),
    )]
Exemplo n.º 6
0
def test_get_known_wifis_partial_failure(nm_monitor):
    nm_monitor._nm.get_connections_paths.return_value = [
        '/org/freedesktop/NetworkManager/Settings/12',
        '/org/freedesktop/NetworkManager/Settings/42',
    ]
    nm_monitor._nm.get_settings.side_effect = [
        DBusException("Test"),
        {
            'connection': {},
            '802-11-wireless': {
                'ssid': [84, 69, 83, 84]
            },
        },
    ]

    result = nm_monitor.get_known_wifis()

    assert result == [SystemWifiInfo(
        ssid='TEST',
        last_connected=None,
    )]