Nuova versione dello script che non si basa più sul nome
Leonardo Robol [2010-09-04 08:53]
Nuova versione dello script che non si basa più sul nome
della VPN.
diff --git a/activate_vpn.py b/activate_vpn.py
index b7a90f8..63da7d7 100755
--- a/activate_vpn.py
+++ b/activate_vpn.py
@@ -13,43 +13,84 @@ def init():
"""
global bus
bus = dbus.SystemBus()
+
+def is_poisson_vpn(settings):
+ """
+ Recognize poisson vpn from the settings returned
+ by dbus
+ """
+ if settings['connection']['type'] != "vpn":
+ return False
+ if settings['vpn']['data']['remote'] != "poisson.phc-priv":
+ return False
+
+ # 1194 is the default port for the VPN so you don't need to specify it in the
+ # configuration, but if you do and if you speciy something different from 1194,
+ # this is not the right VPN
+ if settings['vpn']['data'].has_key("port") and settings['vpn']['data']['port'] != 1194:
+ return False
+
+ # If you arrived here this really is poisson vpn!
+ return True
def get_poisson_vpn():
"""
Obtain DBus object for the poisson vpn connection
"""
+
+ # Try to load configuration for the VPN to poisson
proxy = bus.get_object("org.freedesktop.NetworkManagerUserSettings",
"/org/freedesktop/NetworkManagerSettings")
interface = dbus.Interface(proxy, "org.freedesktop.NetworkManagerSettings")
+ # If we can't find the vpn matching the following:
+ # - remote: poisson.phc-priv
+ # - port: 1194
+ # then we return None
c = None
+
+ # Check all connections stored in UserSettings to see if they match the
+ # Poisson vpn. We don't check SystemSettings at the moment being because
+ # it's likely to require root privileges (or not?)
for connection in interface.ListConnections():
proxy = bus.get_object("org.freedesktop.NetworkManagerUserSettings",
connection)
settings = proxy.GetSettings(dbus_interface="org.freedesktop.NetworkManagerSettings.Connection")
- if (settings['connection']['type'] == "vpn" and settings['connection']['id'] == "Poisson"):
+
+ # This should be a deterministic check :)
+ if is_poisson_vpn (settings):
c = connection
- # Check if VPN is already active
+ # Check if VPN is already active. If it is, return None because we have nothing
+ # to do
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
+
+ # Try to match ConnectionSettings to an ActiveConnection
for connection in interface.Get("org.freedesktop.NetworkManager", "ActiveConnections"):
proxy = bus.get_object("org.freedesktop.NetworkManager", connection)
interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
connection = interface.Get("org.freedesktop.NetworkManager.Connection.Active", "Connection")
if connection == c:
return None
+
+ # Return the connection (or None)
return c
def get_base_connection():
"""
- Obtain active connection
+ Obtain active connection if matching the ones that can connect to
+ poisson.phc-priv. If not, return None
"""
+
+ # Get the list of the active connections right now
proxy = bus.get_object("org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager")
interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
active_connections = interface.Get('org.freedesktop.NetworkManager', 'ActiveConnections')
+ # Examine every active connection and if one of the matching MAC address
+ # is found return the connection object
for connection in active_connections:
proxy = bus.get_object("org.freedesktop.NetworkManager",
connection)
@@ -60,11 +101,21 @@ def get_base_connection():
path)
interface = dbus.Interface(proxy, 'org.freedesktop.NetworkManagerSettings.Connection')
settings = interface.GetSettings()
+
+ # Check if the user is connected to a known wireless ap. If it is, then
+ # we can activate the VPN so we return a connection DBus object
if settings.has_key("802-11-wireless"):
- # PHC-wifi
+
+ # MAC address of CDCWL1 ap
+ if settings['802-11-wireless']['seen-bssids'][0] == u'00:12:0E:8C:AE:A0':
+ return connection
+
+ # MAC address of PHC-wifi ap
if settings['802-11-wireless']['seen-bssids'][0] == u'00:0f:cb:aa:16:52':
return connection
+ # The active connections aren't directly connected to poisson.phc-priv
+ # so we can't activate the VPN
return None
@@ -75,7 +126,10 @@ def activate_connection(connection):
proxy = bus.get_object("org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager")
interface = dbus.Interface (proxy, "org.freedesktop.NetworkManager")
+
+ # Obtain base connection to active VPN on
base_connection = get_base_connection ()
+
if base_connection is not None:
interface.ActivateConnection('org.freedesktop.NetworkManagerUserSettings',
connection,
@@ -93,8 +147,12 @@ if __name__ == "__main__":
# don't want another one
if sys.argv[2] == "vpn-up":
sys.exit (0)
+
+ # If you deconfigured an interface, you probably
+ # don't want to autostart a VPN (o sì? :))
+ if sys.argv[2] == "down":
+ sys.exit (0)
-
# Preconnection scripts
init ()
@@ -108,3 +166,5 @@ if __name__ == "__main__":
# Activate connection
activate_connection (poisson_vpn)
+
+