connectors/PYRO/__init__.py
changeset 2312 84b3cc18893b
parent 2010 bb9c28bd204f
child 2313 2eaf235270f8
equal deleted inserted replaced
2311:bef2b4b87370 2312:84b3cc18893b
    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 
       
    40 service_type = '_PYRO._tcp.local.'
    39 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
    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         urlpath, 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()
    68             confnodesroot.logger.write(_("PYRO using certificates in '%s' \n")
       
    69                                        % (Pyro.config.PYROSSL_CERTDIR))
       
    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:
    65     else:
    87         schemename = "PYROLOC"
    66         schemename = "PYROLOC"
       
    67 
    88     if location.find(service_type) != -1:
    68     if location.find(service_type) != -1:
    89         try:
    69         try:
    90             from zeroconf import Zeroconf
    70             from zeroconf import Zeroconf
    91             r = Zeroconf()
    71             r = Zeroconf()
    92             i = r.get_service_info(service_type, location)
    72             i = r.get_service_info(service_type, location)
   159                 member = PyroCatcher(my_local_func, _special_return_funcs.get(attrName, None))
   139                 member = PyroCatcher(my_local_func, _special_return_funcs.get(attrName, None))
   160                 self.__dict__[attrName] = member
   140                 self.__dict__[attrName] = member
   161             return member
   141             return member
   162 
   142 
   163     return PyroProxyProxy()
   143     return PyroProxyProxy()
       
   144