connectors/PYRO/__init__.py
changeset 2315 523559fe6352
parent 2314 e927c101ce6d
child 2324 1cf3768ebf85
equal deleted inserted replaced
2309:d8fb90a2e11f 2315:523559fe6352
    34 import Pyro
    34 import Pyro
    35 import Pyro.core
    35 import Pyro.core
    36 import Pyro.util
    36 import Pyro.util
    37 from Pyro.errors import PyroError
    37 from Pyro.errors import PyroError
    38 
    38 
    39 
    39 zeroconf_service_type = '_PYRO._tcp.local.'
    40 service_type = '_PYRO._tcp.local.'
       
    41 # this module attribute contains a list of DNS-SD (Zeroconf) service types
    40 # this module attribute contains a list of DNS-SD (Zeroconf) service types
    42 # supported by this connector confnode.
    41 # supported by this connector confnode.
    43 #
    42 #
    44 # for connectors that do not support DNS-SD, this attribute can be omitted
    43 # for connectors that do not support DNS-SD, this attribute can be omitted
    45 # or set to an empty list.
    44 # or set to an empty list.
    51     """
    50     """
    52     confnodesroot.logger.write(_("PYRO connecting to URI : %s\n") % uri)
    51     confnodesroot.logger.write(_("PYRO connecting to URI : %s\n") % uri)
    53 
    52 
    54     servicetype, location = uri.split("://")
    53     servicetype, location = uri.split("://")
    55     if servicetype == "PYROS":
    54     if servicetype == "PYROS":
    56         schemename = "PYROLOCSSL"
    55         import connectors.PYRO.PSK_Adapter
    57         # Protect against name->IP substitution in Pyro3
    56         schemename = "PYROLOCPSK"
    58         Pyro.config.PYRO_DNS_URI = True
    57         url, ID = location.split('#')
    59         # Beware Pyro lib need str path, not unicode
    58         # load PSK from project
    60         # don't rely on PYRO_STORAGE ! see documentation
    59         secpath = os.path.join(str(confnodesroot.ProjectPath), 'psk', ID+'.secret')
    61         Pyro.config.PYROSSL_CERTDIR = os.path.abspath(str(confnodesroot.ProjectPath) + '/certs')
    60         if not os.path.exists(secpath):
    62         if not os.path.exists(Pyro.config.PYROSSL_CERTDIR):
       
    63             confnodesroot.logger.write_error(
    61             confnodesroot.logger.write_error(
    64                 'Error : the directory %s is missing for SSL certificates (certs_dir).'
    62                 'Error: Pre-Shared-Key Secret in %s is missing!\n' % secpath)
    65                 'Please fix it in your project.\n' % Pyro.config.PYROSSL_CERTDIR)
       
    66             return None
    63             return None
    67         else:
    64         Pyro.config.PYROPSK = (open(secpath).read(), ID)
    68             confnodesroot.logger.write(_("PYRO using certificates in '%s' \n")
    65         # strip ID from URL, so that pyro can understand it.
    69                                        % (Pyro.config.PYROSSL_CERTDIR))
    66         location = url
    70         Pyro.config.PYROSSL_CERT = "client.crt"
       
    71         Pyro.config.PYROSSL_KEY = "client.key"
       
    72 
       
    73         # Ugly Monkey Patching
       
    74         def _gettimeout(self):
       
    75             return self.timeout
       
    76 
       
    77         def _settimeout(self, timeout):
       
    78             self.timeout = timeout
       
    79         from M2Crypto.SSL import Connection  # pylint: disable=import-error
       
    80         Connection.timeout = None
       
    81         Connection.gettimeout = _gettimeout
       
    82         Connection.settimeout = _settimeout
       
    83         # M2Crypto.SSL.Checker.WrongHost: Peer certificate commonName does not
       
    84         # match host, expected 127.0.0.1, got server
       
    85         Connection.clientPostConnectionCheck = None
       
    86     else:
    67     else:
    87         schemename = "PYROLOC"
    68         schemename = "PYROLOC"
    88     if location.find(service_type) != -1:
    69 
       
    70     if location.find(zeroconf_service_type) != -1:
    89         try:
    71         try:
    90             from zeroconf import Zeroconf
    72             from zeroconf import Zeroconf
    91             r = Zeroconf()
    73             r = Zeroconf()
    92             i = r.get_service_info(service_type, location)
    74             i = r.get_service_info(zeroconf_service_type, location)
    93             if i is None:
    75             if i is None:
    94                 raise Exception("'%s' not found" % location)
    76                 raise Exception("'%s' not found" % location)
    95             ip = str(socket.inet_ntoa(i.address))
    77             ip = str(socket.inet_ntoa(i.address))
    96             port = str(i.port)
    78             port = str(i.port)
    97             newlocation = ip + ':' + port
    79             newlocation = ip + ':' + port
   159                 member = PyroCatcher(my_local_func, _special_return_funcs.get(attrName, None))
   141                 member = PyroCatcher(my_local_func, _special_return_funcs.get(attrName, None))
   160                 self.__dict__[attrName] = member
   142                 self.__dict__[attrName] = member
   161             return member
   143             return member
   162 
   144 
   163     return PyroProxyProxy()
   145     return PyroProxyProxy()
       
   146