Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions src/vorta/borg/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,32 @@ def prepare(cls, profile):
if n_backup_folders == 0 and '--paths-from-command' not in extra_cmd_options:
ret['message'] = trans_late('messages', 'Add some folders to back up first.')
return ret

network_status_monitor = get_network_status_monitor()
current_wifi = network_status_monitor.get_current_wifi()
if current_wifi is not None:
wifi_is_disallowed = WifiSettingModel.select().where(
(WifiSettingModel.ssid == current_wifi)
& (WifiSettingModel.allowed == False) # noqa
& (WifiSettingModel.profile == profile)
network_status_monitor = get_network_status_monitor()

wifi_list = network_status_monitor.get_all_wifi_ssids()
clean_wifi_list = []
for w in wifi_list:
if w:
try:
clean_wifi_list.append(str(w))
except Exception:
pass
wifi_list = clean_wifi_list
print(f"DEBUG: all connected wifi: {wifi_list}")
if wifi_list:
disallowed_wifi = WifiSettingModel.select().where(
(WifiSettingModel.ssid.in_(wifi_list))
& (WifiSettingModel.allowed == False)
& (WifiSettingModel.profile == profile.id)
)
if wifi_is_disallowed.count() > 0 and profile.repo.is_remote_repo():
count = disallowed_wifi.count()
print(f"DEBUG: disallowed wifi count: {count}")
if count > 0:
ret['message'] = trans_late('messages', 'Current Wifi is not allowed.')
return ret

if (
profile.repo.is_remote_repo()
and profile.dont_run_on_metered_networks
and network_status_monitor.is_network_metered()
):
ret['message'] = trans_late('messages', 'Not running backup over metered connection.')
return ret

else:
print("DEBUG: no wifi → skipping restriction")
ret['profile'] = profile
ret['repo'] = profile.repo

Expand All @@ -150,7 +155,6 @@ def prepare(cls, profile):
'Your current Borg version does not support ZStd compression.',
)
return ret

cmd = [
'borg',
'create',
Expand Down
77 changes: 64 additions & 13 deletions src/vorta/network_status/network_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from PyQt6 import QtDBus
from PyQt6.QtCore import QObject, QVersionNumber, pyqtSignal, pyqtSlot
from PyQt6.QtDBus import QDBusArgument

from vorta.network_status.abc import NetworkStatusMonitor, SystemWifiInfo

Expand Down Expand Up @@ -36,24 +37,49 @@ def is_network_active(self) -> bool:
except DBusException:
logger.exception("Failed to check connectivity state. Assuming connected")
return True
def get_all_wifi_ssids(self) -> list[str]:
wifi_ssids = []

try:
active_paths = self._nm.get_active_connections_paths()

for path in active_paths:
try:
active_connection = self._nm.get_active_connection_info(path)

if active_connection.type != "802-11-wireless":
continue

settings = self._nm.get_settings(active_connection.connection)
ssid = self._get_ssid_from_settings(settings)

if ssid:
wifi_ssids.append(ssid)

except Exception as e:
continue

return wifi_ssids

except Exception as e:
return []

def get_current_wifi(self) -> str | None:
# Only check the primary connection. VPN over WiFi will still show the WiFi as Primary Connection.
# We don't check all active connections, as NM won't disable WiFi when connecting a cable.
try:
active_connection_path = self._nm.get_primary_connection_path()
if not active_connection_path:
connection_path = self._nm.get_primary_connection_path()
if not connection_path:
return None
active_connection = self._nm.get_active_connection_info(active_connection_path)
if active_connection.type == '802-11-wireless':
settings = self._nm.get_settings(active_connection.connection)
ssid = self._get_ssid_from_settings(settings)
if ssid:
return ssid
except DBusException:
logger.exception("Failed to get currently connected WiFi network, assuming none")
return None

active_connection = self._nm.get_active_connection_info(connection_path)

if active_connection.type != "802-11-wireless":
return None

settings = self._nm.get_settings(active_connection.connection)
return self._get_ssid_from_settings(settings)

except Exception:
return None
def get_known_wifis(self) -> list[SystemWifiInfo]:
wifis: list[SystemWifiInfo] = []
try:
Expand Down Expand Up @@ -135,6 +161,31 @@ def get_system_nm_adapter(cls) -> NetworkManagerDBusAdapter:
if not nm_adapter.isValid():
raise UnsupportedException("Can't connect to NetworkManager")
return nm_adapter

def get_active_connections_paths(self) -> list[str]:
try:
iface = QtDBus.QDBusInterface(
self.BUS_NAME,
self.NM_PATH,
"org.freedesktop.DBus.Properties",
self._bus,
)

reply = iface.call("Get", self.INTERFACE_NAME, "ActiveConnections")

if not reply.arguments():
return []

paths = reply.arguments()[0]

print("DEBUG raw:", paths)
print("DEBUG type:", type(paths))

return [str(p) for p in paths]

except Exception:
logger.exception("Failed to get active connections")
return []

@pyqtSlot("unsigned int")
def networkStateChanged(self, state: int) -> None:
Expand Down