|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | +VXLAN_IPV4_METHOD = os.environ.get('WIREGUARD_VXLAN_IPV4_METHOD', 'link-local') |
| 9 | +VXLAN_IPV6_METHOD = os.environ.get('WIREGUARD_VXLAN_IPV6_METHOD', 'link-local') |
| 10 | + |
| 11 | +try: |
| 12 | + peer_file_path = sys.argv[1] |
| 13 | +except IndexError: |
| 14 | + print('peer file must be passed as first argument', file=sys.stderr) |
| 15 | + sys.exit(1) |
| 16 | + |
| 17 | +try: |
| 18 | + with open(peer_file_path, 'r') as peer_file: |
| 19 | + contents = peer_file.read() |
| 20 | +except FileNotFoundError as e: |
| 21 | + print(e, file=sys.stderr) |
| 22 | + sys.exit(2) |
| 23 | + |
| 24 | +try: |
| 25 | + peers = json.loads(contents) |
| 26 | + assert isinstance(peers, list) |
| 27 | +except Exception as e: |
| 28 | + print(f'Error while parsing JSON file: {e}', file=sys.stderr) |
| 29 | + sys.exit(3) |
| 30 | + |
| 31 | + |
| 32 | +remote_peers = {} |
| 33 | + |
| 34 | +for peer in peers: |
| 35 | + remote_peers[f'vxlan-vxlan{peer["vni"]}'] = peer |
| 36 | + |
| 37 | + |
| 38 | +class Nmcli: |
| 39 | + @classmethod |
| 40 | + def _exec_command(cls, command): |
| 41 | + process = subprocess.Popen( |
| 42 | + command.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE |
| 43 | + ) |
| 44 | + stdout, stderr = process.communicate() |
| 45 | + if stderr: |
| 46 | + raise ValueError(stderr) |
| 47 | + return stdout.decode('utf8').strip() |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def list_connections(cls, type=None): |
| 51 | + output = cls._exec_command('nmcli connection show') |
| 52 | + lines = output.split('\n') |
| 53 | + connections = [] |
| 54 | + for line in lines[1:]: |
| 55 | + parts = line.split() |
| 56 | + connection = { |
| 57 | + 'name': parts[0].strip(), |
| 58 | + 'uuid': parts[1].strip(), |
| 59 | + 'type': parts[2].strip(), |
| 60 | + 'device': parts[3].strip(), |
| 61 | + } |
| 62 | + if not type or type and type == connection['type']: |
| 63 | + connections.append(connection) |
| 64 | + return connections |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def get_connection(cls, connection): |
| 68 | + output = cls._exec_command(f'sudo nmcli connection show {connection}') |
| 69 | + data = {} |
| 70 | + lines = output.split('\n') |
| 71 | + for line in lines: |
| 72 | + parts = line.split() |
| 73 | + data[parts[0][:-1]] = parts[1] |
| 74 | + return data |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def get_local_vxlan_peers(cls): |
| 78 | + peers = {} |
| 79 | + vxlan_connections = cls.list_connections(type='vxlan') |
| 80 | + for vxlan in vxlan_connections: |
| 81 | + data = cls.get_connection(vxlan['uuid']) |
| 82 | + peers[data['connection.id']] = { |
| 83 | + 'remote': data['vxlan.remote'], |
| 84 | + 'vni': int(data['vxlan.id']), |
| 85 | + } |
| 86 | + return peers |
| 87 | + |
| 88 | + @classmethod |
| 89 | + def add_connection(cls, ifname, vni, remote): |
| 90 | + return cls._exec_command( |
| 91 | + f'sudo nmcli connection add type vxlan ifname {ifname} ' |
| 92 | + f'id {vni} remote {remote} destination-port 4789 ' |
| 93 | + f'ipv4.method {VXLAN_IPV4_METHOD} ipv6.method {VXLAN_IPV6_METHOD}' |
| 94 | + ) |
| 95 | + |
| 96 | + @classmethod |
| 97 | + def edit_connection(cls, connection, vni, remote): |
| 98 | + return cls._exec_command( |
| 99 | + f'sudo nmcli connection modify {connection}' |
| 100 | + f' vxlan.id {vni} vxlan.remote {remote}' |
| 101 | + ) |
| 102 | + |
| 103 | + @classmethod |
| 104 | + def delete_connection(cls, connection): |
| 105 | + return cls._exec_command(f'sudo nmcli connection delete {connection}') |
| 106 | + |
| 107 | + |
| 108 | +local_peers = Nmcli.get_local_vxlan_peers() |
| 109 | + |
| 110 | + |
| 111 | +for connection_name, peer_data in local_peers.items(): |
| 112 | + if connection_name not in remote_peers: |
| 113 | + Nmcli.delete_connection(connection_name) |
| 114 | + print(f'Removed {connection_name}') |
| 115 | + |
| 116 | + |
| 117 | +for connection_name, peer_data in remote_peers.items(): |
| 118 | + vni = peer_data['vni'] |
| 119 | + remote = peer_data['remote'] |
| 120 | + if connection_name not in local_peers: |
| 121 | + Nmcli.add_connection(f'vxlan{vni}', vni, remote) |
| 122 | + print(f'Added {connection_name}') |
| 123 | + continue |
| 124 | + elif peer_data == local_peers[connection_name]: |
| 125 | + print(f'Skipping {connection_name}, already up to date') |
| 126 | + continue |
| 127 | + else: |
| 128 | + Nmcli.edit_connection(connection_name, vni, remote) |
| 129 | + print(f'Updated {connection_name}') |
0 commit comments