Example #1
0
    def _try():
        hint = convert_legacy_hint(location)
        if ":" not in hint:
            raise InvalidHintError("no colon")
        hint_type = hint.split(":", 1)[0]
        plugin = connectionPlugins.get(hint_type)
        if not plugin:
            connectionInfo._describe_connection_handler(location, None)
            raise InvalidHintError("no handler registered")
        connectionInfo._describe_connection_handler(location,
                                                    describe_handler(plugin))
        _update_status("resolving hint")
        d = defer.maybeDeferred(plugin.hint_to_endpoint, hint, reactor,
                                _update_status)

        def problem(f):
            log.err(f,
                    "error in hint_to_endpoint",
                    level=UNUSUAL,
                    facility="foolscap.connection",
                    umid="Fxtg6A")
            # this hint will be ignored
            return f

        d.addErrback(problem)
        return d
Example #2
0
 def _try():
     hint = convert_legacy_hint(location)
     if ":" not in hint:
         raise InvalidHintError("no colon in hint")
     hint_type = hint.split(":", 1)[0]
     plugin = connectionPlugins.get(hint_type)
     if not plugin:
         raise InvalidHintError("no handler registered for hint")
     return plugin.hint_to_endpoint(hint, reactor)
Example #3
0
 def _try():
     hint = convert_legacy_hint(location)
     if ":" not in hint:
         raise InvalidHintError("no colon")
     hint_type = hint.split(":", 1)[0]
     plugin = connectionPlugins.get(hint_type)
     if not plugin:
         connectionInfo._describe_connection_handler(location, None)
         raise InvalidHintError("no handler registered")
     connectionInfo._describe_connection_handler(location,
                                                 describe_handler(plugin))
     _update_status("resolving hint")
     return plugin.hint_to_endpoint(hint, reactor, _update_status)
Example #4
0
 def testConvertLegacyHint(self):
     self.assertEqual(tcp.convert_legacy_hint("127.0.0.1:9900"),
                      "tcp:127.0.0.1:9900")
     self.assertEqual(tcp.convert_legacy_hint("tcp:127.0.0.1:9900"),
                      "tcp:127.0.0.1:9900")
     self.assertEqual(tcp.convert_legacy_hint("other:127.0.0.1:9900"),
                      "other:127.0.0.1:9900")
     # this is unfortunate
     self.assertEqual(tcp.convert_legacy_hint("unix:1"), "tcp:unix:1")
     # so new hints should do one of these:
     self.assertEqual(tcp.convert_legacy_hint("tor:host:1234"),
                      "tor:host:1234")  # multiple colons
     self.assertEqual(tcp.convert_legacy_hint("unix:fd=1"),
                      "unix:fd=1")  # equals signs, key=value -style
Example #5
0
 def testConvertLegacyHint(self):
     self.failUnlessEqual(tcp.convert_legacy_hint("127.0.0.1:9900"),
                          "tcp:127.0.0.1:9900")
     self.failUnlessEqual(tcp.convert_legacy_hint("tcp:127.0.0.1:9900"),
                          "tcp:127.0.0.1:9900")
     self.failUnlessEqual(tcp.convert_legacy_hint("other:127.0.0.1:9900"),
                          "other:127.0.0.1:9900")
     # this is unfortunate
     self.failUnlessEqual(tcp.convert_legacy_hint("unix:1"), "tcp:unix:1")
     # so new hints should do one of these:
     self.failUnlessEqual(tcp.convert_legacy_hint("tor:host:1234"),
                          "tor:host:1234") # multiple colons
     self.failUnlessEqual(tcp.convert_legacy_hint("unix:fd=1"),
                          "unix:fd=1") # equals signs, key=value -style
Example #6
0
def _tub_portlocation(config, get_local_addresses_sync, allocate_tcp_port):
    """
    Figure out the network location of the main tub for some configuration.

    :param get_local_addresses_sync: A function like
        ``iputil.get_local_addresses_sync``.

    :param allocate_tcp_port: A function like ``iputil.allocate_tcp_port``.

    :returns: None or tuple of (port, location) for the main tub based
        on the given configuration. May raise ValueError or PrivacyError
        if there are problems with the config
    """
    cfg_tubport = config.get_config("node", "tub.port", None)
    cfg_location = config.get_config("node", "tub.location", None)
    reveal_ip = config.get_config("node",
                                  "reveal-IP-address",
                                  True,
                                  boolean=True)
    tubport_disabled = False

    if cfg_tubport is not None:
        cfg_tubport = cfg_tubport.strip()
        if cfg_tubport == "":
            raise ValueError("tub.port must not be empty")
        if cfg_tubport == "disabled":
            tubport_disabled = True

    location_disabled = False
    if cfg_location is not None:
        cfg_location = cfg_location.strip()
        if cfg_location == "":
            raise ValueError("tub.location must not be empty")
        if cfg_location == "disabled":
            location_disabled = True

    if tubport_disabled and location_disabled:
        return None
    if tubport_disabled and not location_disabled:
        raise ValueError("tub.port is disabled, but not tub.location")
    if location_disabled and not tubport_disabled:
        raise ValueError("tub.location is disabled, but not tub.port")

    if cfg_tubport is None:
        # For 'tub.port', tahoe.cfg overrides the individual file on
        # disk. So only read config.portnum_fname if tahoe.cfg doesn't
        # provide a value.
        if os.path.exists(config.portnum_fname):
            file_tubport = fileutil.read(config.portnum_fname).strip()
            tubport = _convert_tub_port(file_tubport)
        else:
            tubport = "tcp:%d" % (allocate_tcp_port(), )
            fileutil.write_atomically(config.portnum_fname,
                                      tubport + "\n",
                                      mode="")
    else:
        tubport = _convert_tub_port(cfg_tubport)

    for port in tubport.split(","):
        if port in ("0", "tcp:0"):
            raise PortAssignmentRequired()

    if cfg_location is None:
        cfg_location = "AUTO"

    local_portnum = None  # needed to hush lgtm.com static analyzer
    # Replace the location "AUTO", if present, with the detected local
    # addresses. Don't probe for local addresses unless necessary.
    split_location = cfg_location.split(",")
    if "AUTO" in split_location:
        if not reveal_ip:
            raise PrivacyError("tub.location uses AUTO")
        local_addresses = get_local_addresses_sync()
        # tubport must be like "tcp:12345" or "tcp:12345:morestuff"
        local_portnum = int(tubport.split(":")[1])
    new_locations = []
    for loc in split_location:
        if loc == "AUTO":
            new_locations.extend(
                ["tcp:%s:%d" % (ip, local_portnum) for ip in local_addresses])
        else:
            if not reveal_ip:
                # Legacy hints are "host:port". We use Foolscap's utility
                # function to convert all hints into the modern format
                # ("tcp:host:port") because that's what the receiving
                # client will probably do. We test the converted hint for
                # TCP-ness, but publish the original hint because that
                # was the user's intent.
                from foolscap.connections.tcp import convert_legacy_hint
                converted_hint = convert_legacy_hint(loc)
                hint_type = converted_hint.split(":")[0]
                if hint_type == "tcp":
                    raise PrivacyError("tub.location includes tcp: hint")
            new_locations.append(loc)
    location = ",".join(new_locations)

    # Lacking this, Python 2 blows up in Foolscap when it is confused by a
    # Unicode FURL.
    location = location.encode("utf-8")

    return tubport, location
Example #7
0
    def get_tub_portlocation(self, cfg_tubport, cfg_location):
        # return None, or tuple of (port, location)

        tubport_disabled = False
        if cfg_tubport is not None:
            cfg_tubport = cfg_tubport.strip()
            if cfg_tubport == "":
                raise ValueError("tub.port must not be empty")
            if cfg_tubport == "disabled":
                tubport_disabled = True

        location_disabled = False
        if cfg_location is not None:
            cfg_location = cfg_location.strip()
            if cfg_location == "":
                raise ValueError("tub.location must not be empty")
            if cfg_location == "disabled":
                location_disabled = True

        if tubport_disabled and location_disabled:
            return None
        if tubport_disabled and not location_disabled:
            raise ValueError("tub.port is disabled, but not tub.location")
        if location_disabled and not tubport_disabled:
            raise ValueError("tub.location is disabled, but not tub.port")

        if cfg_tubport is None:
            # For 'tub.port', tahoe.cfg overrides the individual file on
            # disk. So only read self._portnumfile if tahoe.cfg doesn't
            # provide a value.
            if os.path.exists(self.config.portnum_fname):
                file_tubport = fileutil.read(self.config.portnum_fname).strip()
                tubport = self._convert_tub_port(file_tubport)
            else:
                tubport = "tcp:%d" % iputil.allocate_tcp_port()
                fileutil.write_atomically(self.config.portnum_fname,
                                          tubport + "\n",
                                          mode="")
        else:
            tubport = self._convert_tub_port(cfg_tubport)

        if cfg_location is None:
            cfg_location = "AUTO"

        local_portnum = None  # needed to hush lgtm.com static analyzer
        # Replace the location "AUTO", if present, with the detected local
        # addresses. Don't probe for local addresses unless necessary.
        split_location = cfg_location.split(",")
        if "AUTO" in split_location:
            if not self._reveal_ip:
                raise PrivacyError("tub.location uses AUTO")
            local_addresses = iputil.get_local_addresses_sync()
            # tubport must be like "tcp:12345" or "tcp:12345:morestuff"
            local_portnum = int(tubport.split(":")[1])
        new_locations = []
        for loc in split_location:
            if loc == "AUTO":
                new_locations.extend([
                    "tcp:%s:%d" % (ip, local_portnum) for ip in local_addresses
                ])
            else:
                if not self._reveal_ip:
                    # Legacy hints are "host:port". We use Foolscap's utility
                    # function to convert all hints into the modern format
                    # ("tcp:host:port") because that's what the receiving
                    # client will probably do. We test the converted hint for
                    # TCP-ness, but publish the original hint because that
                    # was the user's intent.
                    from foolscap.connections.tcp import convert_legacy_hint
                    converted_hint = convert_legacy_hint(loc)
                    hint_type = converted_hint.split(":")[0]
                    if hint_type == "tcp":
                        raise PrivacyError("tub.location includes tcp: hint")
                new_locations.append(loc)
        location = ",".join(new_locations)

        return tubport, location
Example #8
0
    def get_tub_portlocation(self, cfg_tubport, cfg_location):
        # return None, or tuple of (port, location)

        tubport_disabled = False
        if cfg_tubport is not None:
            cfg_tubport = cfg_tubport.strip()
            if cfg_tubport == "":
                raise ValueError("tub.port must not be empty")
            if cfg_tubport == "disabled":
                tubport_disabled = True

        location_disabled = False
        if cfg_location is not None:
            cfg_location = cfg_location.strip()
            if cfg_location == "":
                raise ValueError("tub.location must not be empty")
            if cfg_location == "disabled":
                location_disabled = True

        if tubport_disabled and location_disabled:
            return None
        if tubport_disabled and not location_disabled:
            raise ValueError("tub.port is disabled, but not tub.location")
        if location_disabled and not tubport_disabled:
            raise ValueError("tub.location is disabled, but not tub.port")

        if cfg_tubport is None:
            # For 'tub.port', tahoe.cfg overrides the individual file on
            # disk. So only read self._portnumfile if tahoe.cfg doesn't
            # provide a value.
            if os.path.exists(self._portnumfile):
                file_tubport = fileutil.read(self._portnumfile).strip()
                tubport = self._convert_tub_port(file_tubport)
            else:
                tubport = "tcp:%d" % iputil.allocate_tcp_port()
                fileutil.write_atomically(self._portnumfile, tubport + "\n",
                                          mode="")
        else:
            tubport = self._convert_tub_port(cfg_tubport)

        if cfg_location is None:
            cfg_location = "AUTO"

        # Replace the location "AUTO", if present, with the detected local
        # addresses. Don't probe for local addresses unless necessary.
        split_location = cfg_location.split(",")
        if "AUTO" in split_location:
            if not self._reveal_ip:
                raise PrivacyError("tub.location uses AUTO")
            local_addresses = iputil.get_local_addresses_sync()
            # tubport must be like "tcp:12345" or "tcp:12345:morestuff"
            local_portnum = int(tubport.split(":")[1])
        new_locations = []
        for loc in split_location:
            if loc == "AUTO":
                new_locations.extend(["tcp:%s:%d" % (ip, local_portnum)
                                      for ip in local_addresses])
            else:
                if not self._reveal_ip:
                    # Legacy hints are "host:port". We use Foolscap's utility
                    # function to convert all hints into the modern format
                    # ("tcp:host:port") because that's what the receiving
                    # client will probably do. We test the converted hint for
                    # TCP-ness, but publish the original hint because that
                    # was the user's intent.
                    from foolscap.connections.tcp import convert_legacy_hint
                    converted_hint = convert_legacy_hint(loc)
                    hint_type = converted_hint.split(":")[0]
                    if hint_type == "tcp":
                        raise PrivacyError("tub.location includes tcp: hint")
                new_locations.append(loc)
        location = ",".join(new_locations)

        return tubport, location
Example #9
0
def _tub_portlocation(config):
    """
    :returns: None or tuple of (port, location) for the main tub based
        on the given configuration. May raise ValueError or PrivacyError
        if there are problems with the config
    """
    cfg_tubport = config.get_config("node", "tub.port", None)
    cfg_location = config.get_config("node", "tub.location", None)
    reveal_ip = config.get_config("node", "reveal-IP-address", True, boolean=True)
    tubport_disabled = False

    if cfg_tubport is not None:
        cfg_tubport = cfg_tubport.strip()
        if cfg_tubport == "":
            raise ValueError("tub.port must not be empty")
        if cfg_tubport == "disabled":
            tubport_disabled = True

    location_disabled = False
    if cfg_location is not None:
        cfg_location = cfg_location.strip()
        if cfg_location == "":
            raise ValueError("tub.location must not be empty")
        if cfg_location == "disabled":
            location_disabled = True

    if tubport_disabled and location_disabled:
        return None
    if tubport_disabled and not location_disabled:
        raise ValueError("tub.port is disabled, but not tub.location")
    if location_disabled and not tubport_disabled:
        raise ValueError("tub.location is disabled, but not tub.port")

    if cfg_tubport is None:
        # For 'tub.port', tahoe.cfg overrides the individual file on
        # disk. So only read config.portnum_fname if tahoe.cfg doesn't
        # provide a value.
        if os.path.exists(config.portnum_fname):
            file_tubport = fileutil.read(config.portnum_fname).strip()
            tubport = _convert_tub_port(file_tubport)
        else:
            tubport = "tcp:%d" % iputil.allocate_tcp_port()
            fileutil.write_atomically(config.portnum_fname, tubport + "\n",
                                      mode="")
    else:
        tubport = _convert_tub_port(cfg_tubport)

    for port in tubport.split(","):
        if port in ("0", "tcp:0"):
            raise ValueError("tub.port cannot be 0: you must choose")

    if cfg_location is None:
        cfg_location = "AUTO"

    local_portnum = None # needed to hush lgtm.com static analyzer
    # Replace the location "AUTO", if present, with the detected local
    # addresses. Don't probe for local addresses unless necessary.
    split_location = cfg_location.split(",")
    if "AUTO" in split_location:
        if not reveal_ip:
            raise PrivacyError("tub.location uses AUTO")
        local_addresses = iputil.get_local_addresses_sync()
        # tubport must be like "tcp:12345" or "tcp:12345:morestuff"
        local_portnum = int(tubport.split(":")[1])
    new_locations = []
    for loc in split_location:
        if loc == "AUTO":
            new_locations.extend(["tcp:%s:%d" % (ip, local_portnum)
                                  for ip in local_addresses])
        else:
            if not reveal_ip:
                # Legacy hints are "host:port". We use Foolscap's utility
                # function to convert all hints into the modern format
                # ("tcp:host:port") because that's what the receiving
                # client will probably do. We test the converted hint for
                # TCP-ness, but publish the original hint because that
                # was the user's intent.
                from foolscap.connections.tcp import convert_legacy_hint
                converted_hint = convert_legacy_hint(loc)
                hint_type = converted_hint.split(":")[0]
                if hint_type == "tcp":
                    raise PrivacyError("tub.location includes tcp: hint")
            new_locations.append(loc)
    location = ",".join(new_locations)

    return tubport, location