viewgit/index.php:465 Only variables should be passed by reference [2048]

viewgit/index.php:466 Non-static method GeSHi::get_language_name_from_extension() should not be called statically [2048]

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4.  
  5. import dbus, sys
  6. from dbus.mainloop.glib import DBusGMainLoop
  7.  
  8. DBusGMainLoop(set_as_default=True)
  9.  
  10. def init():
  11. """
  12. Preconnection instructions
  13. """
  14. global bus
  15. bus = dbus.SystemBus()
  16.  
  17. def is_poisson_vpn(settings):
  18. """
  19. Recognize poisson vpn from the settings returned
  20. by dbus
  21. """
  22. if settings['connection']['type'] != "vpn":
  23. return False
  24. if settings['vpn']['data']['remote'] != "poisson.phc-priv":
  25. return False
  26.  
  27. # 1194 is the default port for the VPN so you don't need to specify it in the
  28. # configuration, but if you do and if you speciy something different from 1194,
  29. # this is not the right VPN
  30. if settings['vpn']['data'].has_key("port") and settings['vpn']['data']['port'] != 1194:
  31. return False
  32.  
  33. # If you arrived here this really is poisson vpn!
  34. return True
  35.  
  36. def get_poisson_vpn():
  37. """
  38. Obtain DBus object for the poisson vpn connection
  39. """
  40.  
  41. # Try to load configuration for the VPN to poisson
  42. proxy = bus.get_object("org.freedesktop.NetworkManagerUserSettings",
  43. "/org/freedesktop/NetworkManagerSettings")
  44. interface = dbus.Interface(proxy, "org.freedesktop.NetworkManagerSettings")
  45.  
  46. # If we can't find the vpn matching the following:
  47. # - remote: poisson.phc-priv
  48. # - port: 1194
  49. # then we return None
  50. c = None
  51.  
  52. # Check all connections stored in UserSettings to see if they match the
  53. # Poisson vpn. We don't check SystemSettings at the moment being because
  54. # it's likely to require root privileges (or not?)
  55. for connection in interface.ListConnections():
  56. proxy = bus.get_object("org.freedesktop.NetworkManagerUserSettings",
  57. connection)
  58. settings = proxy.GetSettings(dbus_interface="org.freedesktop.NetworkManagerSettings.Connection")
  59.  
  60. # This should be a deterministic check :)
  61. if is_poisson_vpn (settings):
  62. c = connection
  63.  
  64. # Check if VPN is already active. If it is, return None because we have nothing
  65. # to do
  66. proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
  67. interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
  68.  
  69. # Try to match ConnectionSettings to an ActiveConnection
  70. for connection in interface.Get("org.freedesktop.NetworkManager", "ActiveConnections"):
  71. proxy = bus.get_object("org.freedesktop.NetworkManager", connection)
  72. interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
  73. connection = interface.Get("org.freedesktop.NetworkManager.Connection.Active", "Connection")
  74. if connection == c:
  75. return None
  76.  
  77. # Return the connection (or None)
  78. return c
  79.  
  80. def get_base_connection():
  81. """
  82. Obtain active connection if matching the ones that can connect to
  83. poisson.phc-priv. If not, return None
  84. """
  85.  
  86. # Get the list of the active connections right now
  87. proxy = bus.get_object("org.freedesktop.NetworkManager",
  88. "/org/freedesktop/NetworkManager")
  89. interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
  90. active_connections = interface.Get('org.freedesktop.NetworkManager', 'ActiveConnections')
  91.  
  92. # Examine every active connection and if one of the matching MAC address
  93. # is found return the connection object
  94. for connection in active_connections:
  95. proxy = bus.get_object("org.freedesktop.NetworkManager",
  96. connection)
  97. interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
  98. path = interface.Get('org.freedesktop.NetworkManager.Connection.Active', 'Connection')
  99.  
  100. proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings',
  101. path)
  102. interface = dbus.Interface(proxy, 'org.freedesktop.NetworkManagerSettings.Connection')
  103. settings = interface.GetSettings()
  104.  
  105. # Check if the user is connected to a known wireless ap. If it is, then
  106. # we can activate the VPN so we return a connection DBus object
  107. if settings.has_key("802-11-wireless"):
  108.  
  109. # MAC address of CDCWL1 ap
  110. if settings['802-11-wireless']['seen-bssids'][0] == u'00:12:0E:8C:AE:A0':
  111. return connection
  112.  
  113. # MAC address of PHC-wifi ap
  114. if settings['802-11-wireless']['seen-bssids'][0] == u'00:0f:cb:aa:16:52':
  115. return connection
  116.  
  117. # The active connections aren't directly connected to poisson.phc-priv
  118. # so we can't activate the VPN
  119. return None
  120.  
  121.  
  122. def activate_connection(connection):
  123. """
  124. Activate connection
  125. """
  126. proxy = bus.get_object("org.freedesktop.NetworkManager",
  127. "/org/freedesktop/NetworkManager")
  128. interface = dbus.Interface (proxy, "org.freedesktop.NetworkManager")
  129.  
  130. # Obtain base connection to active VPN on
  131. base_connection = get_base_connection ()
  132.  
  133. if base_connection is not None:
  134. interface.ActivateConnection('org.freedesktop.NetworkManagerUserSettings',
  135. connection,
  136. dbus.ObjectPath("/"),
  137. base_connection)
  138.  
  139. if __name__ == "__main__":
  140.  
  141. # If you turn vpn donw you probably don't
  142. # want me to fire it up again
  143. if sys.argv[2] == "vpn-down":
  144. sys.exit (0)
  145.  
  146. # If you just tried to attach a VPN, you probably
  147. # don't want another one
  148. if sys.argv[2] == "vpn-up":
  149. sys.exit (0)
  150.  
  151. # If you deconfigured an interface, you probably
  152. # don't want to autostart a VPN (o sì? :))
  153. if sys.argv[2] == "down":
  154. sys.exit (0)
  155.  
  156. # Preconnection scripts
  157. init ()
  158.  
  159. # Obtain poisson vpn
  160. poisson_vpn = get_poisson_vpn ()
  161.  
  162. # If poisson_vpn is None it means that it
  163. # is already active, so exit now
  164. if (poisson_vpn is None):
  165. sys.exit (0)
  166.  
  167. # Activate connection
  168. activate_connection (poisson_vpn)
  169.  
  170.