def _setup_tub(self, ign): # we can't get a dynamically-assigned portnum until our Tub is # running, which means after startService. l = self.tub.getListeners()[0] portnum = l.getPortnum() # record which port we're listening on, so we can grab the same one # next time fileutil.write_atomically(self._portnumfile, "%d\n" % portnum, mode="") location = self.get_config("node", "tub.location", "AUTO") # Replace the location "AUTO", if present, with the detected local addresses. split_location = location.split(",") if "AUTO" in split_location: d = iputil.get_local_addresses_async() def _add_local(local_addresses): while "AUTO" in split_location: split_location.remove("AUTO") split_location.extend(["%s:%d" % (addr, portnum) for addr in local_addresses]) return ",".join(split_location) d.addCallback(_add_local) else: d = defer.succeed(location) def _got_location(location): self.log("Tub location set to %s" % (location,)) self.tub.setLocation(location) return self.tub d.addCallback(_got_location) return d
def get_tub_port(self): # return a descriptor string cfg_tubport = self.get_config("node", "tub.port", "") if cfg_tubport: return self._convert_tub_port(cfg_tubport) # 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() return self._convert_tub_port(file_tubport) tubport = "tcp:%d" % iputil.allocate_tcp_port() fileutil.write_atomically(self._portnumfile, tubport + "\n", mode="") return tubport
def _setup_tub(self, local_addresses): # we can't get a dynamically-assigned portnum until our Tub is # running, which means after startService. l = self.tub.getListeners()[0] portnum = l.getPortnum() # record which port we're listening on, so we can grab the same one # next time fileutil.write_atomically(self._portnumfile, "%d\n" % portnum, mode="") base_location = ",".join([ "%s:%d" % (addr, portnum) for addr in local_addresses ]) location = self.get_config("node", "tub.location", base_location) self.log("Tub location set to %s" % location) self.tub.setLocation(location) return self.tub
def _write_nodeurl_file(ign): # this file will be created with default permissions line = self.getURL() + "\n" fileutil.write_atomically(nodeurl_path, line, mode="")
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
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
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) # Lacking this, Python 2 blows up in Foolscap when it is confused by a # Unicode FURL. location = location.encode("utf-8") return tubport, location
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