Skip to content

Commit

Permalink
T4930: code style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sskaje committed Dec 30, 2024
1 parent 0321fb0 commit be3a6ff
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions python/vyos/ifconfig/wireguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,36 +181,54 @@ def get_latest_handshakes(self):

def reset_peer(self, peer_name=None, public_key=None):
from vyos.configquery import ConfigTreeQuery

c = ConfigTreeQuery()

max_dns_retry = c.value(['interfaces', 'wireguard', self.ifname, 'max-dns-retry'])
max_dns_retry = c.value(
['interfaces', 'wireguard', self.ifname, 'max-dns-retry']
)
if max_dns_retry is None:
max_dns_retry = 3

current_peers = self._dump().get(self.ifname, {}).get('peers', {})

for peer in c.list_nodes(['interfaces', 'wireguard', self.ifname, 'peer']):
peer_public_key = c.value(['interfaces', 'wireguard', self.ifname, 'peer', peer, 'public-key'])
peer_public_key = c.value(
['interfaces', 'wireguard', self.ifname, 'peer', peer, 'public-key']
)
if peer_name is None or peer == peer_name or public_key == peer_public_key:
address = c.value(['interfaces', 'wireguard', self.ifname, 'peer', peer, 'address'])
port = c.value(['interfaces', 'wireguard', self.ifname, 'peer', peer, 'port'])
address = c.value(
['interfaces', 'wireguard', self.ifname, 'peer', peer, 'address']
)
port = c.value(
['interfaces', 'wireguard', self.ifname, 'peer', peer, 'port']
)

if not address or not port:
if peer_name is not None:
print(f'Peer {peer_name} endpoint not set')
continue

if c.exists(['interfaces', 'wireguard', self.ifname, 'peer', peer, 'disable']):
if c.exists(
['interfaces', 'wireguard', self.ifname, 'peer', peer, 'disable']
):
continue

cmd = f"wg set {self.ifname} peer {peer_public_key} endpoint {address}:{port}"
cmd = f'wg set {self.ifname} peer {peer_public_key} endpoint {address}:{port}'
try:
if peer_public_key in current_peers and 'endpoint' in current_peers[peer_public_key] and current_peers[peer_public_key]['endpoint'] is not None:
if (
peer_public_key in current_peers
and 'endpoint' in current_peers[peer_public_key]
and current_peers[peer_public_key]['endpoint'] is not None
):
current_endpoint = current_peers[peer_public_key]['endpoint']
message = f'Resetting {self.ifname} peer {peer_public_key} from {current_endpoint} endpoint to {address}:{port} ... '
else:
message = f'Resetting {self.ifname} peer {peer_public_key} endpoint to {address}:{port} ... '
print(message, end='',)
print(
message,
end='',
)

self._cmd(
cmd, env={'WG_ENDPOINT_RESOLUTION_RETRIES': str(max_dns_retry)}
Expand Down

0 comments on commit be3a6ff

Please sign in to comment.