From e53bd81acda568d3a9203d05fbc656508029ece8 Mon Sep 17 00:00:00 2001 From: Leonardo Robol Date: Sat, 4 Sep 2010 11:26:59 +0200 Subject: [PATCH] Tradotti i commenti in italiano. --- 99_poisson_vpn.py | 111 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 43 deletions(-) diff --git a/99_poisson_vpn.py b/99_poisson_vpn.py index 63da7d7..c736e29 100755 --- a/99_poisson_vpn.py +++ b/99_poisson_vpn.py @@ -1,5 +1,26 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +# +# Questo è un semplice script che attiva automaticamente +# la VPN verso poisson per chi si connette ad una rete wireless +# dall'interno del dipartimenti di Matematica di Pisa. +# +# Io lo uso per evitare di dover fare un clic di troppo ;) +# +# Ringrazio Tambet Ingo-2 dal cui script ho preso ispirazione +# per cominciare questo. +# +# Potete trovare la sua versione di uno script in python per fare +# una cosa simile in questo thread: +# http://old.nabble.com/dbus-and-OpenVPN-Autostart-td21905375.html +# +# Questo codice è rilasciato sotto licenza GPL3 che si trova nel +# file LICENSE nell'archivio dei sorgenti che potete trovare clonando +# il repository git su http://poisson.phc.unipi.it/~robol/gits/AutoVPN.git +# +# + +__author__ = "Leonardo Robol " import dbus, sys @@ -9,64 +30,66 @@ DBusGMainLoop(set_as_default=True) def init(): """ - Preconnection instructions + Istruzioni da eseguire prima di fare ogni altra cosa. In poche + parole otteniamo il bus di sistema che ci servirà abbondantemente + in tutto lo script. """ global bus bus = dbus.SystemBus() def is_poisson_vpn(settings): """ - Recognize poisson vpn from the settings returned - by dbus + Questa funzione riconosce la vpn per connettersi alla rete esterna + tramite poisson facendo un check su nome dell'host remote e sulla + porta. Nonostante il nome non sia un fqdn, penso che il test sia + abbastanza deterministico. """ 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 + # 1194 è la porta di default per la vpn, quindi può anche essere non + # specificata nel file di configurazione. Controlliamo se è così, oppure + # se è stata specificata ed impostata a 1194. if settings['vpn']['data'].has_key("port") and settings['vpn']['data']['port'] != 1194: return False - # If you arrived here this really is poisson vpn! + # Se siamo arrivati a questo punto significa che questa è davvero + # la VPN per connettersi alla rete esterna da poisson return True def get_poisson_vpn(): """ - Obtain DBus object for the poisson vpn connection + Ottiene la vpn per poisson tramite dbus """ - # Try to load configuration for the VPN to poisson + # Tentiamo di caricare la configurazione dell'utente. 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 + # Ritorniamo None se non siamo in grado di trovare la VPN oppure + # se è già attiva 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?) + # Controlliamo tutte le connessione salvate nelle connessione dell'utente + # sperando di trovare la VPN. Se questa è specificata nelle connessioni di sistema, + # beh.. questa è una feature per la prossima versione. for connection in interface.ListConnections(): proxy = bus.get_object("org.freedesktop.NetworkManagerUserSettings", connection) settings = proxy.GetSettings(dbus_interface="org.freedesktop.NetworkManagerSettings.Connection") - # This should be a deterministic check :) + # Check per determinare se questa è la connessione giusta if is_poisson_vpn (settings): c = connection - # Check if VPN is already active. If it is, return None because we have nothing - # to do + # Controlliamo se la VPN è già attiva... 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 + # ...provando a confrontarla con tutte le connessioni attive 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") @@ -74,23 +97,22 @@ def get_poisson_vpn(): if connection == c: return None - # Return the connection (or None) + # Ritorniamo la connessione VPN o None se non c'è bisogno di fare nulla return c def get_base_connection(): """ - Obtain active connection if matching the ones that can connect to - poisson.phc-priv. If not, return None + Otteniamo la connessione su cui attivare la VPN (i.e. PHC-wifi o CDCWL1) """ - # Get the list of the active connections right now + # Otteniamo la lista di tutte le connessioni attive al momento 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 + # Esaminiamo ogni connessione attiva e verifichiamo se è una connessione wireless + # e se lo è se l'AP ha lo stesso MAC di PHC-wifi o di CDCWL1 for connection in active_connections: proxy = bus.get_object("org.freedesktop.NetworkManager", connection) @@ -102,8 +124,6 @@ def get_base_connection(): 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"): # MAC address of CDCWL1 ap @@ -114,20 +134,21 @@ def get_base_connection(): 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 + # Se siamo arrivati a questo punto significa che la connessione attiva + # non è direttamente connessa a poisson.phc-priv e qunidi possiamo + # anche lasciar perdere return None def activate_connection(connection): """ - Activate connection + Attiva la connessione """ proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager") interface = dbus.Interface (proxy, "org.freedesktop.NetworkManager") - # Obtain base connection to active VPN on + # Otteniamo la connessione di base su cui attivare la VPN base_connection = get_base_connection () if base_connection is not None: @@ -138,33 +159,37 @@ def activate_connection(connection): if __name__ == "__main__": - # If you turn vpn donw you probably don't - # want me to fire it up again + # Se hai appena staccato una VPN probabilmente non hai bisogno + # che ti riattivi quella per poisson (anche perché potresti stare + # cercando di liberartente). if sys.argv[2] == "vpn-down": sys.exit (0) - # If you just tried to attach a VPN, you probably - # don't want another one + # Se è appena stata attivata una VPN non ha senso cercare di + # attivarne un'altra (se non altro perché NM non ne supporta più + # di una nello stesso momento) 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ì? :)) + # Se hai appena deconfigurato un'interfaccia probabilmente non + # desideri attivare la VPN if sys.argv[2] == "down": sys.exit (0) - # Preconnection scripts + # Otteniamo il bus init () - # Obtain poisson vpn + # ...e la VPN per poisson poisson_vpn = get_poisson_vpn () - # If poisson_vpn is None it means that it - # is already active, so exit now + # Se non l'abbiamo trovata oppure è + # già attiva possiamo anche uscire if (poisson_vpn is None): sys.exit (0) - # Activate connection + # Altrimenti la attiviamo, e poi usciamo :) activate_connection (poisson_vpn) + sys.exit (0) + -- 2.1.4