Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

Commit 24ec736

Browse files
committed
NM: Calculate WG gateway using eduvpn-common
1 parent 36f8ee8 commit 24ec736

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

eduvpn/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ def search_custom(self, query: str) -> Iterator[Any]:
588588
class Application:
589589
def __init__(self, variant: ApplicationVariant, common: EduVPN) -> None:
590590
self.variant = variant
591-
self.nm_manager = nm.NMManager(variant)
591+
self.nm_manager = nm.NMManager(variant, common)
592592
self.common = common
593593
directory = variant.config_prefix
594594
self.config = Configuration.load(directory)

eduvpn/nm.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import enum
2-
import ipaddress
32
import logging
43
import os
54
import time
@@ -12,7 +11,7 @@
1211
from tempfile import mkdtemp
1312
from typing import Any, Callable, Optional, TextIO, Tuple
1413

15-
from eduvpn_common.main import Jar
14+
from eduvpn_common.main import EduVPN, Jar
1615
from gi.repository.Gio import Cancellable, Task # type: ignore
1716

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

8281
# A manager for a manager :-)
8382
class NMManager:
84-
def __init__(self, variant: ApplicationVariant):
83+
def __init__(self, variant: ApplicationVariant, common_lib: EduVPN):
8584
self.variant = variant
8685
self.proxy = None
8786
try:
8887
self._client = NM.Client.new(None)
89-
self.wg_gateway_ip: Optional[ipaddress.IPv4Address] = None
88+
self.wg_gateway_ip: Optional[str] = None
9089
except Exception:
9190
self._client = None
9291
self.cancel_jar = Jar(lambda x: x.cancel())
92+
self.common_lib = common_lib
9393

9494
@property
9595
def client(self) -> "NM.Client":
@@ -304,7 +304,7 @@ def failover_endpoint_ip(self) -> Optional[str]:
304304
if not self.wg_gateway_ip:
305305
_logger.debug("no wg gateway ip found in failover endpoint")
306306
return None
307-
return str(self.wg_gateway_ip)
307+
return self.wg_gateway_ip
308308
else:
309309
_logger.debug(f"Unknown protocol: {protocol}")
310310
return None
@@ -452,7 +452,8 @@ def start_wireguard_connection( # noqa: C901
452452
addr = ip_interface(ip.strip())
453453
if addr.version == 4:
454454
if not self.wg_gateway_ip:
455-
self.wg_gateway_ip = addr.network[1]
455+
net_str = str(addr.network)
456+
self.wg_gateway_ip = self.common_lib.calculate_gateway(net_str)
456457
ipv4s.append(NM.IPAddress(AF_INET, str(addr.ip), addr.network.prefixlen))
457458
elif addr.version == 6:
458459
ipv6s.append(NM.IPAddress(AF_INET6, str(addr.ip), addr.network.prefixlen))

tests/test_nm.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
@skipIf(not NMManager(EDUVPN).available, "Network manager not available")
1010
class TestNm(TestCase):
1111
def test_nm_available(self):
12-
nm_manager = NMManager(EDUVPN)
12+
nm_manager = NMManager(EDUVPN, None)
1313
nm_manager.available
1414

1515
def test_import_ovpn(self):
16-
nm_manager = NMManager(EDUVPN)
16+
nm_manager = NMManager(EDUVPN, None)
1717
ovpn = Ovpn.parse(mock_config)
1818
nm_manager.import_ovpn(ovpn)
1919

2020
def test_get_add_connection(self):
21-
nm_manager = NMManager(EDUVPN)
21+
nm_manager = NMManager(EDUVPN, None)
2222
ovpn = Ovpn.parse(mock_config)
2323
simple_connection = nm_manager.import_ovpn(ovpn)
2424
nm_manager.add_connection(simple_connection)
2525

2626
def test_get_uuid(self):
27-
nm_manager = NMManager(EDUVPN)
27+
nm_manager = NMManager(EDUVPN, None)
2828
nm_manager.uuid

tests/test_stats.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def try_open(path: Path):
2727
@patch("eduvpn.nm.NMManager.iface", new_callable=PropertyMock, return_value=MOCK_IFACE)
2828
class TestStats(TestCase):
2929
def test_stat_bytes(self, _):
30-
nm_manager = NMManager(EDUVPN)
30+
nm_manager = NMManager(EDUVPN, None)
3131
with TemporaryDirectory() as tempdir:
3232
# Create test data in the wanted files
3333
# Use the tempdir so it is cleaned up later

0 commit comments

Comments
 (0)