|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import re |
| 3 | +import hashlib |
| 4 | + |
| 5 | +from salt.utils.network import mac2eui64 |
| 6 | +from textwrap import wrap |
| 7 | +from typing import Dict, List |
| 8 | +from pyroute2 import WireGuard, IPRoute |
| 9 | +from pyroute2.netlink.rtnl import ndmsg |
| 10 | +from typing import Dict, List |
| 11 | +from dataclasses import dataclass |
| 12 | +from datetime import datetime, timedelta |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class WireGuardClient: |
| 17 | + """WireGuardClient describes complete configuration for a specific WireGuard client |
| 18 | +
|
| 19 | + Attributes: |
| 20 | + public_key: WireGuard Public key |
| 21 | + domain: Domain Name of the WireGuard peer |
| 22 | + lladdr: IPv6 lladdr of the WireGuard peer |
| 23 | + wg_interface: Name of the WireGuard interface this peer will use |
| 24 | + vx_interface: Name of the VXLAN interface we set a route for the lladdr to |
| 25 | + remove: Are we removing this peer or not? |
| 26 | + """ |
| 27 | + |
| 28 | + public_key: str |
| 29 | + domain: str |
| 30 | + lladdr: str |
| 31 | + wg_interface: str |
| 32 | + vx_interface: str |
| 33 | + remove: bool |
| 34 | + |
| 35 | + |
| 36 | +# we receive stuff from wgkex-broker |
| 37 | +def generate_lladdr(public_key: str) -> str: |
| 38 | + m = hashlib.md5() |
| 39 | + |
| 40 | + m.update(public_key.encode("ascii") + b"\n") |
| 41 | + hashed_key = m.hexdigest() |
| 42 | + hash_as_list = wrap(hashed_key, 2) |
| 43 | + temp_mac = ":".join(["02"] + hash_as_list[:5]) |
| 44 | + |
| 45 | + lladdr = re.sub(r"/\d+$", "/128", mac2eui64(mac=temp_mac, prefix="fe80::/10")) |
| 46 | + return lladdr |
| 47 | + |
| 48 | + |
| 49 | +def generate_ifname(peer: WireGuardClient) -> WireGuardClient: |
| 50 | + peer.wg_interface = "wg-" + peer.domain |
| 51 | + peer.vx_interface = "vx-" + peer.domain |
| 52 | + |
| 53 | + return peer |
| 54 | + |
| 55 | + |
| 56 | +def wg_flush_stale_peers(domain: str) -> List[Dict]: |
| 57 | + stale_clients = find_stale_wireguard_clients("wg-" + domain) |
| 58 | + result = [] |
| 59 | + for stale_client in stale_clients: |
| 60 | + stale_wireguard_client = WireGuardClient( |
| 61 | + public_key=stale_client, |
| 62 | + lladdr=generate_lladdr(stale_client), |
| 63 | + domain=domain, |
| 64 | + wg_interface="", |
| 65 | + vx_interface="", |
| 66 | + remove=True, |
| 67 | + ) |
| 68 | + stale_wireguard_client = generate_ifname(stale_wireguard_client) |
| 69 | + result = link_handler(stale_wireguard_client) |
| 70 | + return result |
| 71 | + |
| 72 | + |
| 73 | +# pyroute2 stuff |
| 74 | +def link_handler(client: WireGuardClient) -> Dict: |
| 75 | + results = {} |
| 76 | + |
| 77 | + results.update({"Wireguard": wireguard_handler(client)}) |
| 78 | + try: |
| 79 | + results.update({"Route": route_handler(client)}) |
| 80 | + except Exception as e: |
| 81 | + results.update({"Route": e}) |
| 82 | + results.update({"Bridge FDB": bridge_fdb_handler(client)}) |
| 83 | + |
| 84 | + return results |
| 85 | + |
| 86 | + |
| 87 | +def bridge_fdb_handler(client: WireGuardClient) -> Dict: |
| 88 | + |
| 89 | + action = "append" |
| 90 | + if client.remove: |
| 91 | + action = "del" |
| 92 | + |
| 93 | + with IPRoute() as ip: |
| 94 | + return ip.fdb( |
| 95 | + action, |
| 96 | + ifindex=ip.link_lookup(ifname=client.vx_interface)[0], |
| 97 | + lladdr="00:00:00:00:00:00", |
| 98 | + dst=re.sub("\/\d+$", "", client.lladdr), |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +def wireguard_handler(client: WireGuardClient) -> Dict: |
| 103 | + wg = WireGuard() |
| 104 | + |
| 105 | + wg_peer = { |
| 106 | + "public_key": client.public_key, |
| 107 | + "persistent_keepalive": 15, |
| 108 | + "allowed_ips": [client.lladdr], |
| 109 | + "remove": client.remove, |
| 110 | + } |
| 111 | + |
| 112 | + return wg.set(client.wg_interface, peer=wg_peer) |
| 113 | + |
| 114 | + |
| 115 | +def route_handler(client: WireGuardClient) -> Dict: |
| 116 | + with IPRoute() as ip: |
| 117 | + return ip.route( |
| 118 | + "del" if client.remove else "add", |
| 119 | + dst=client.lladdr, |
| 120 | + oif=ip.link_lookup(ifname=client.wg_interface)[0], |
| 121 | + ) |
| 122 | + |
| 123 | + |
| 124 | +def find_stale_wireguard_clients(wg_interface: str) -> List: |
| 125 | + wg = WireGuard() |
| 126 | + |
| 127 | + clients = wg.info(wg_interface)[0].WGDEVICE_A_PEERS.value |
| 128 | + |
| 129 | + three_hours_ago = (datetime.now() - timedelta(hours=3)).timestamp() |
| 130 | + |
| 131 | + stale_clients = [] |
| 132 | + for client in clients: |
| 133 | + latest_handshake = client.WGPEER_A_LAST_HANDSHAKE_TIME["tv_sec"] |
| 134 | + if latest_handshake < int(three_hours_ago): |
| 135 | + stale_clients.append(client.WGPEER_A_PUBLIC_KEY["value"].decode("utf-8")) |
| 136 | + |
| 137 | + return stale_clients |
0 commit comments