def test_tor_i2p_listeners(self): basedir = self.mktemp() config_fname = os.path.join(basedir, "tahoe.cfg") os.mkdir(basedir) os.mkdir(os.path.join(basedir, "private")) with open(config_fname, "w") as f: f.write(BASE_CONFIG) f.write("[node]\n") f.write("tub.port = listen:i2p,listen:tor\n") f.write("tub.location = tcp:example.org:1234\n") config = client.read_config(basedir, "client.port") tub_options = create_tub_options(config) t = FakeTub() i2p_provider = mock.Mock() tor_provider = mock.Mock() dfh, fch = create_connection_handlers(None, config, i2p_provider, tor_provider) with mock.patch("allmydata.node.Tub", return_value=t): create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider) self.assertEqual(i2p_provider.get_listener.mock_calls, [mock.call()]) self.assertEqual(tor_provider.get_listener.mock_calls, [mock.call()]) self.assertEqual( t.listening_ports, [ i2p_provider.get_listener(), tor_provider.get_listener(), ] )
def test_listen_on_zero(self): """ Trying to listen on port 0 should be an error """ basedir = self.mktemp() create_node_dir(basedir, "testing") with open(os.path.join(basedir, "tahoe.cfg"), "w") as f: f.write(BASE_CONFIG) f.write("[node]\n") f.write("tub.port = tcp:0\n") f.write("tub.location = AUTO\n") config = client.read_config(basedir, "client.port") i2p_provider = mock.Mock() tor_provider = mock.Mock() dfh, fch = create_connection_handlers(None, config, i2p_provider, tor_provider) tub_options = create_tub_options(config) t = FakeTub() with mock.patch("allmydata.node.Tub", return_value=t): with self.assertRaises(ValueError) as ctx: create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider) self.assertIn( "you must choose", str(ctx.exception), )
def test_multiple_ports(self): basedir = self.mktemp() create_node_dir(basedir, "testing") port1 = iputil.allocate_tcp_port() port2 = iputil.allocate_tcp_port() port = ("tcp:%d:interface=127.0.0.1,tcp:%d:interface=127.0.0.1" % (port1, port2)) location = "tcp:localhost:%d,tcp:localhost:%d" % (port1, port2) with open(os.path.join(basedir, "tahoe.cfg"), "w") as f: f.write(BASE_CONFIG) f.write("[node]\n") f.write("tub.port = %s\n" % port) f.write("tub.location = %s\n" % location) config = client.read_config(basedir, "client.port") i2p_provider = mock.Mock() tor_provider = mock.Mock() dfh, fch = create_connection_handlers(None, config, i2p_provider, tor_provider) tub_options = create_tub_options(config) t = FakeTub() with mock.patch("allmydata.node.Tub", return_value=t): create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider) self.assertEqual(t.listening_ports, ["tcp:%d:interface=127.0.0.1" % port1, "tcp:%d:interface=127.0.0.1" % port2])
def test_multiple_ports(self, ports): """ When there are multiple listen addresses suggested by the ``tub.port`` and ``tub.location`` configuration, the node's *main* port listens on all of them. """ basedir = self.mktemp() config_fname = os.path.join(basedir, "tahoe.cfg") os.mkdir(basedir) os.mkdir(os.path.join(basedir, "private")) port1, port2 = iter(ports) port = ("tcp:%d:interface=127.0.0.1,tcp:%d:interface=127.0.0.1" % (port1, port2)) location = "tcp:localhost:%d,tcp:localhost:%d" % (port1, port2) with open(config_fname, "w") as f: f.write(BASE_CONFIG) f.write("[node]\n") f.write("tub.port = %s\n" % port) f.write("tub.location = %s\n" % location) config = client.read_config(basedir, "client.port") i2p_provider = mock.Mock() tor_provider = mock.Mock() dfh, fch = create_connection_handlers(None, config, i2p_provider, tor_provider) tub_options = create_tub_options(config) t = FakeTub() with mock.patch("allmydata.node.Tub", return_value=t): create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider) self.assertEqual(t.listening_ports, [ "tcp:%d:interface=127.0.0.1" % port1, "tcp:%d:interface=127.0.0.1" % port2 ])
def testing_tub(config_data=''): """ Creates a 'main' Tub for testing purposes, from config data """ from twisted.internet import reactor basedir = 'dummy_basedir' config = config_from_string(basedir, 'DEFAULT_PORTNUMFILE_BLANK', config_data) fileutil.make_dirs(os.path.join(basedir, 'private')) i2p_provider = create_i2p_provider(reactor, config) tor_provider = create_tor_provider(reactor, config) handlers = create_connection_handlers(reactor, config, i2p_provider, tor_provider) default_connection_handlers, foolscap_connection_handlers = handlers tub_options = create_tub_options(config) main_tub = create_main_tub(config, tub_options, default_connection_handlers, foolscap_connection_handlers, i2p_provider, tor_provider, cert_filename='DEFAULT_CERTFILE_BLANK') return main_tub
def test_tor_i2p_listeners(self): basedir = self.mktemp() config_fname = os.path.join(basedir, "tahoe.cfg") os.mkdir(basedir) os.mkdir(os.path.join(basedir, "private")) with open(config_fname, "w") as f: f.write(BASE_CONFIG) f.write("tub.port = listen:i2p,listen:tor\n") f.write("tub.location = tcp:example.org:1234\n") config = client.read_config(basedir, "client.port") tub_options = create_tub_options(config) t = FakeTub() i2p_provider = mock.Mock() tor_provider = mock.Mock() dfh, fch = create_connection_handlers(None, config, i2p_provider, tor_provider) with mock.patch("allmydata.node.Tub", return_value=t): create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider) self.assertEqual(i2p_provider.get_listener.mock_calls, [mock.call()]) self.assertEqual(tor_provider.get_listener.mock_calls, [mock.call()]) self.assertEqual( t.listening_ports, [ i2p_provider.get_listener(), tor_provider.get_listener(), ] )
def test_multiple_ports(self): basedir = self.mktemp() create_node_dir(basedir, "testing") port1 = iputil.allocate_tcp_port() port2 = iputil.allocate_tcp_port() port = ("tcp:%d:interface=127.0.0.1,tcp:%d:interface=127.0.0.1" % (port1, port2)) location = "tcp:localhost:%d,tcp:localhost:%d" % (port1, port2) with open(os.path.join(basedir, "tahoe.cfg"), "w") as f: f.write(BASE_CONFIG) f.write("tub.port = %s\n" % port) f.write("tub.location = %s\n" % location) config = client.read_config(basedir, "client.port") i2p_provider = mock.Mock() tor_provider = mock.Mock() dfh, fch = create_connection_handlers(None, config, i2p_provider, tor_provider) tub_options = create_tub_options(config) t = FakeTub() with mock.patch("allmydata.node.Tub", return_value=t): create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider) self.assertEqual(t.listening_ports, ["tcp:%d:interface=127.0.0.1" % port1, "tcp:%d:interface=127.0.0.1" % port2])
def test_listen_on_zero(self): """ Trying to listen on port 0 should be an error """ basedir = self.mktemp() create_node_dir(basedir, "testing") with open(os.path.join(basedir, "tahoe.cfg"), "w") as f: f.write(BASE_CONFIG) f.write("tub.port = tcp:0\n") f.write("tub.location = AUTO\n") config = client.read_config(basedir, "client.port") i2p_provider = mock.Mock() tor_provider = mock.Mock() dfh, fch = create_connection_handlers(None, config, i2p_provider, tor_provider) tub_options = create_tub_options(config) t = FakeTub() with mock.patch("allmydata.node.Tub", return_value=t): with self.assertRaises(ValueError) as ctx: create_main_tub(config, tub_options, dfh, fch, i2p_provider, tor_provider) self.assertIn( "you must choose", str(ctx.exception), )
def create_client_from_config(config, _client_factory=None): """ Creates a new client instance (a subclass of Node). Most code should probably use `create_client` instead. :returns: Deferred yielding a _Client instance :param config: configuration instance (from read_config()) which encapsulates everything in the "node directory". :param _client_factory: for testing; the class to instantiate instead of _Client """ try: if _client_factory is None: _client_factory = _Client i2p_provider = create_i2p_provider(reactor, config) tor_provider = create_tor_provider(reactor, config) handlers = node.create_connection_handlers(reactor, config, i2p_provider, tor_provider) default_connection_handlers, foolscap_connection_handlers = handlers tub_options = node.create_tub_options(config) main_tub = node.create_main_tub( config, tub_options, default_connection_handlers, foolscap_connection_handlers, i2p_provider, tor_provider, ) control_tub = node.create_control_tub() introducer_clients = create_introducer_clients(config, main_tub) storage_broker = create_storage_farm_broker( config, default_connection_handlers, foolscap_connection_handlers, tub_options, introducer_clients) client = _client_factory( config, main_tub, control_tub, i2p_provider, tor_provider, introducer_clients, storage_broker, ) i2p_provider.setServiceParent(client) tor_provider.setServiceParent(client) for ic in introducer_clients: ic.setServiceParent(client) storage_broker.setServiceParent(client) return defer.succeed(client) except Exception: return defer.fail()
def create_client_from_config(config, _client_factory=None): """ Creates a new client instance (a subclass of Node). Most code should probably use `create_client` instead. :returns: Deferred yielding a _Client instance :param config: configuration instance (from read_config()) which encapsulates everything in the "node directory". :param _client_factory: for testing; the class to instantiate instead of _Client """ try: if _client_factory is None: _client_factory = _Client i2p_provider = create_i2p_provider(reactor, config) tor_provider = create_tor_provider(reactor, config) handlers = node.create_connection_handlers(reactor, config, i2p_provider, tor_provider) default_connection_handlers, foolscap_connection_handlers = handlers tub_options = node.create_tub_options(config) main_tub = node.create_main_tub( config, tub_options, default_connection_handlers, foolscap_connection_handlers, i2p_provider, tor_provider, ) control_tub = node.create_control_tub() introducer_clients = create_introducer_clients(config, main_tub) storage_broker = create_storage_farm_broker( config, default_connection_handlers, foolscap_connection_handlers, tub_options, introducer_clients ) client = _client_factory( config, main_tub, control_tub, i2p_provider, tor_provider, introducer_clients, storage_broker, ) i2p_provider.setServiceParent(client) tor_provider.setServiceParent(client) for ic in introducer_clients: ic.setServiceParent(client) storage_broker.setServiceParent(client) return defer.succeed(client) except Exception: return Failure()
def create_introducer(basedir=u"."): """ :returns: a Deferred that yields a new _IntroducerNode instance """ try: # see https://tahoe-lafs.org/trac/tahoe-lafs/ticket/2946 from twisted.internet import reactor if not os.path.exists(basedir): create_node_dir(basedir, INTRODUCER_README) config = read_config( basedir, u"client.port", generated_files=["introducer.furl"], _valid_config=_valid_config(), ) i2p_provider = create_i2p_provider(reactor, config) tor_provider = create_tor_provider(reactor, config) default_connection_handlers, foolscap_connection_handlers = create_connection_handlers( reactor, config, i2p_provider, tor_provider) tub_options = create_tub_options(config) # we don't remember these because the Introducer doesn't make # outbound connections. i2p_provider = None tor_provider = None main_tub = create_main_tub( config, tub_options, default_connection_handlers, foolscap_connection_handlers, i2p_provider, tor_provider, ) control_tub = create_control_tub() node = _IntroducerNode( config, main_tub, control_tub, i2p_provider, tor_provider, ) return defer.succeed(node) except Exception: return Failure()
def create_introducer(basedir=u"."): """ :returns: a Deferred that yields a new _IntroducerNode instance """ try: # see https://tahoe-lafs.org/trac/tahoe-lafs/ticket/2946 from twisted.internet import reactor if not os.path.exists(basedir): create_node_dir(basedir, INTRODUCER_README) config = read_config( basedir, u"client.port", generated_files=["introducer.furl"], _valid_config_sections=_valid_config_sections, ) i2p_provider = create_i2p_provider(reactor, config) tor_provider = create_tor_provider(reactor, config) default_connection_handlers, foolscap_connection_handlers = create_connection_handlers(reactor, config, i2p_provider, tor_provider) tub_options = create_tub_options(config) # we don't remember these because the Introducer doesn't make # outbound connections. i2p_provider = None tor_provider = None main_tub = create_main_tub( config, tub_options, default_connection_handlers, foolscap_connection_handlers, i2p_provider, tor_provider, ) control_tub = create_control_tub() node = _IntroducerNode( config, main_tub, control_tub, i2p_provider, tor_provider, ) return defer.succeed(node) except Exception: return Failure()
def testing_tub(config_data=''): """ Creates a 'main' Tub for testing purposes, from config data """ from twisted.internet import reactor basedir = 'dummy_basedir' config = config_from_string(basedir, 'DEFAULT_PORTNUMFILE_BLANK', config_data) fileutil.make_dirs(os.path.join(basedir, 'private')) i2p_provider = create_i2p_provider(reactor, config) tor_provider = create_tor_provider(reactor, config) handlers = create_connection_handlers(reactor, config, i2p_provider, tor_provider) default_connection_handlers, foolscap_connection_handlers = handlers tub_options = create_tub_options(config) main_tub = create_main_tub( config, tub_options, default_connection_handlers, foolscap_connection_handlers, i2p_provider, tor_provider, cert_filename='DEFAULT_CERTFILE_BLANK' ) return main_tub
def create_client_from_config(config, _client_factory=None, _introducer_factory=None): """ Creates a new client instance (a subclass of Node). Most code should probably use `create_client` instead. :returns: Deferred yielding a _Client instance :param config: configuration instance (from read_config()) which encapsulates everything in the "node directory". :param _client_factory: for testing; the class to instantiate instead of _Client :param _introducer_factory: for testing; the class to instantiate instead of IntroducerClient """ if _client_factory is None: _client_factory = _Client i2p_provider = create_i2p_provider(reactor, config) tor_provider = create_tor_provider(reactor, config) handlers = node.create_connection_handlers(reactor, config, i2p_provider, tor_provider) default_connection_handlers, foolscap_connection_handlers = handlers tub_options = node.create_tub_options(config) main_tub = node.create_main_tub( config, tub_options, default_connection_handlers, foolscap_connection_handlers, i2p_provider, tor_provider, ) control_tub = node.create_control_tub() introducer_clients = create_introducer_clients(config, main_tub, _introducer_factory) storage_broker = create_storage_farm_broker(config, default_connection_handlers, foolscap_connection_handlers, tub_options, introducer_clients) client = _client_factory( config, main_tub, control_tub, i2p_provider, tor_provider, introducer_clients, storage_broker, ) # Initialize storage separately after creating the client. This is # necessary because we need to pass a reference to the client in to the # storage plugins to allow them to initialize themselves (specifically, # they may want the anonymous IStorageServer implementation so they don't # have to duplicate all of its basic storage functionality). A better way # to do this, eventually, may be to create that implementation first and # then pass it in to both storage plugin creation and the client factory. # This avoids making a partially initialized client object escape the # client factory and removes the circular dependency between these # objects. storage_plugins = yield _StoragePlugins.from_config( client.get_anonymous_storage_server, config, ) client.init_storage(storage_plugins.announceable_storage_servers) i2p_provider.setServiceParent(client) tor_provider.setServiceParent(client) for ic in introducer_clients: ic.setServiceParent(client) storage_broker.setServiceParent(client) defer.returnValue(client)