Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
NM: Calculate WG gateway using eduvpn-common
Browse files Browse the repository at this point in the history
  • Loading branch information
jwijenbergh committed Aug 12, 2024
1 parent 36f8ee8 commit 24ec736
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion eduvpn/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ def search_custom(self, query: str) -> Iterator[Any]:
class Application:
def __init__(self, variant: ApplicationVariant, common: EduVPN) -> None:
self.variant = variant
self.nm_manager = nm.NMManager(variant)
self.nm_manager = nm.NMManager(variant, common)
self.common = common
directory = variant.config_prefix
self.config = Configuration.load(directory)
Expand Down
13 changes: 7 additions & 6 deletions eduvpn/nm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import enum
import ipaddress
import logging
import os
import time
Expand All @@ -12,7 +11,7 @@
from tempfile import mkdtemp
from typing import Any, Callable, Optional, TextIO, Tuple

from eduvpn_common.main import Jar
from eduvpn_common.main import EduVPN, Jar
from gi.repository.Gio import Cancellable, Task # type: ignore

from eduvpn.ovpn import Ovpn
Expand Down Expand Up @@ -81,15 +80,16 @@ def from_active_state(cls, state: "NM.ActiveConnectionState") -> "ConnectionStat

# A manager for a manager :-)
class NMManager:
def __init__(self, variant: ApplicationVariant):
def __init__(self, variant: ApplicationVariant, common_lib: EduVPN):
self.variant = variant
self.proxy = None
try:
self._client = NM.Client.new(None)
self.wg_gateway_ip: Optional[ipaddress.IPv4Address] = None
self.wg_gateway_ip: Optional[str] = None
except Exception:
self._client = None
self.cancel_jar = Jar(lambda x: x.cancel())
self.common_lib = common_lib

@property
def client(self) -> "NM.Client":
Expand Down Expand Up @@ -304,7 +304,7 @@ def failover_endpoint_ip(self) -> Optional[str]:
if not self.wg_gateway_ip:
_logger.debug("no wg gateway ip found in failover endpoint")
return None
return str(self.wg_gateway_ip)
return self.wg_gateway_ip
else:
_logger.debug(f"Unknown protocol: {protocol}")
return None
Expand Down Expand Up @@ -452,7 +452,8 @@ def start_wireguard_connection( # noqa: C901
addr = ip_interface(ip.strip())
if addr.version == 4:
if not self.wg_gateway_ip:
self.wg_gateway_ip = addr.network[1]
net_str = str(addr.network)
self.wg_gateway_ip = self.common_lib.calculate_gateway(net_str)
ipv4s.append(NM.IPAddress(AF_INET, str(addr.ip), addr.network.prefixlen))
elif addr.version == 6:
ipv6s.append(NM.IPAddress(AF_INET6, str(addr.ip), addr.network.prefixlen))
Expand Down
8 changes: 4 additions & 4 deletions tests/test_nm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
@skipIf(not NMManager(EDUVPN).available, "Network manager not available")
class TestNm(TestCase):
def test_nm_available(self):
nm_manager = NMManager(EDUVPN)
nm_manager = NMManager(EDUVPN, None)
nm_manager.available

def test_import_ovpn(self):
nm_manager = NMManager(EDUVPN)
nm_manager = NMManager(EDUVPN, None)
ovpn = Ovpn.parse(mock_config)
nm_manager.import_ovpn(ovpn)

def test_get_add_connection(self):
nm_manager = NMManager(EDUVPN)
nm_manager = NMManager(EDUVPN, None)
ovpn = Ovpn.parse(mock_config)
simple_connection = nm_manager.import_ovpn(ovpn)
nm_manager.add_connection(simple_connection)

def test_get_uuid(self):
nm_manager = NMManager(EDUVPN)
nm_manager = NMManager(EDUVPN, None)
nm_manager.uuid
2 changes: 1 addition & 1 deletion tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def try_open(path: Path):
@patch("eduvpn.nm.NMManager.iface", new_callable=PropertyMock, return_value=MOCK_IFACE)
class TestStats(TestCase):
def test_stat_bytes(self, _):
nm_manager = NMManager(EDUVPN)
nm_manager = NMManager(EDUVPN, None)
with TemporaryDirectory() as tempdir:
# Create test data in the wanted files
# Use the tempdir so it is cleaned up later
Expand Down

0 comments on commit 24ec736

Please sign in to comment.