#!/usr/bin/python3 # # OpenVPN 3 Linux client -- Next generation OpenVPN client # # SPDX-License-Identifier: AGPL-3.0-only # # Copyright (C) 2019 - 2023 OpenVPN Inc # Copyright (C) 2019 - 2023 David Sommerseth # ## # @file netcfg-subscription-test # # @brief Simple test program making use of the openvpn3.NetCfgManager # module to subscribe to certain events. # # NOTE: The openvpn3-linux default configuration requires the # the openvpn user account to run this program # import dbus from dbus.mainloop.glib import DBusGMainLoop from gi.repository import GLib import openvpn3 from openvpn3 import NetCfgChangeType as CT # Simple callback function which prints the NetworkChange event to console def netchg_callback(event): print ("Device: %s - Type: %s" % (event.GetDevice(), event.GetType().name)) for (k,v) in event.GetAllDetails().items(): print (" - %s: %s" % (str(k), str(v))) print("-----------") # Simple callback function printing Log events to console def log_callback(grp, ctg, msg): print("LOG: [%u:%u] %s" % (grp, ctg, msg)) # Prepare the D-Bus connection, it needs to have a mainloop for # asynchronous handling mainloop = GLib.MainLoop() dbusloop = DBusGMainLoop(set_as_default=True) sysbus = dbus.SystemBus(mainloop=dbusloop) # Get a connection to the net.openvpn.v3.netcfg service (NetCfg service) nc = openvpn3.NetCfgManager(sysbus) # Subscribe to log events nc.LogCallback(log_callback) # Subscribe to device, IP address and route related events nc.SubscribeNetworkChange(netchg_callback, CT.DEVICE_ADDED | CT.DEVICE_REMOVED | CT.IPADDR_ADDED | CT.IPADDR_REMOVED | CT.ROUTE_ADDED | CT.ROUTE_REMOVED) # Main loop - wait for events to occur try: mainloop.run() except KeyboardInterrupt: print("Stopping") pass # Clean up and unsubscribe nc.UnsubscribeNetworkChange()