def get_peer_info(ifname, wg, kind=None): results = {} if kind == 'wireguard' or os.environ.get("SYNTROPY_WIREGUARD"): try: ss = wg.info(ifname) except NetlinkError as e: return results wg_info = dict(ss[0]['attrs']) peers = wg_info.get('WGDEVICE_A_PEERS', []) for peer in peers: peer = dict(peer['attrs']) try: results[peer['WGPEER_A_PUBLIC_KEY'].decode('utf-8')] = [ allowed_ip['addr'] for allowed_ip in peer['WGPEER_A_ALLOWEDIPS'] ] except KeyError: results[peer['WGPEER_A_PUBLIC_KEY'].decode('utf-8')] = [] else: wg = WireGuardRead() ifaces = wg.wg_info(ifname) if not ifaces: return results iface = ifaces[0] for peer in iface['peers']: results[peer['peer']] = peer['allowed_ips'] return results
def get_iface_public_key(ifname): wg = WireGuardRead() ifaces = wg.wg_info(ifname) if not ifaces: return iface = ifaces[0] return iface.get('public_key')
def get_peer_info_all(ifname, wg, kind=None): results = [] # TODO NEED FIX pyroute2 wireguard info solution, because of missing peers when getting info. # if kind == 'wireguard' or os.environ.get("SYNTROPY_WIREGUARD"): # try: # ss = wg.info(ifname) # except NetlinkError as e: # return results # wg_info = dict(ss[0]['attrs']) # peers = wg_info.get('WGDEVICE_A_PEERS', []) # for peer in peers: # try: # peer_dict = dict(peer['attrs']) # results.append({ # "public_key": peer_dict['WGPEER_A_PUBLIC_KEY'].decode('utf-8'), # "allowed_ips": [allowed_ip['addr'] for allowed_ip in peer_dict['WGPEER_A_ALLOWEDIPS']], # "last_handshake": datetime.datetime.strptime( # peer_dict['WGPEER_A_LAST_HANDSHAKE_TIME']['latest handshake'], # "%a %b %d %H:%M:%S %Y").isoformat(), # "keep_alive_interval": peer_dict['WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL'], # "rx_bytes": peer_dict['WGPEER_A_RX_BYTES'], # "tx_bytes": peer_dict['WGPEER_A_TX_BYTES'], # }) # except KeyError: # continue wg = WireGuardRead() ifaces = wg.wg_info(ifname) if not ifaces: return results iface = ifaces[0] for peer in iface['peers']: try: results.append({ "public_key": peer['peer'], "last_handshake": datetime.datetime.now().isoformat() if peer['latest_handshake'] else None, "keep_alive_interval": int(''.join( filter(str.isdigit, peer.get('persistent_keepalive', '15')))), "allowed_ips": peer['allowed_ips'], }) except KeyError: continue return results
def __init__(self, client, interval=60): super().__init__() self.client = client self.interval = interval self.wg = WireGuard() if module_loaded( "wireguard") else WireGuardRead() self.stop_peer_watcher = threading.Event() self.daemon = True
def __init__(self, client, interval=1): logger.debug(f"[REROUTING] Initializing") super().__init__() self.interval = interval self.client = client self.wg = WireGuard() if module_loaded( "wireguard") else WireGuardRead() self.routes = Routes() self.stop_rerouting = threading.Event() self.daemon = True
def __init__(self, interval=10): self.interval = interval self.wg = WireGuard() if module_loaded( "wireguard") else WireGuardRead()
def test_wireguard_read(patch_cmd_read, mock_wg_show, wg_show_dict): patch_cmd_read().read.return_value = mock_wg_show wg = WireGuardRead() wg_info = wg.wg_info() assert wg_info == wg_show_dict