async def extract_swagger(destination_fn): config = TriblerConfig() config.api.key = 'apikey' config.api.http_enabled = False config.api.https_enabled = False root_endpoint = RootEndpoint( middlewares=[ApiKeyMiddleware(config.api.key), error_middleware]) api_manager = RESTManager(config=config.api, root_endpoint=root_endpoint, state_dir=config.state_dir) await api_manager.start() fp = StringIO() proto = aiohttp.web_protocol.RequestHandler(api_manager.runner._server, loop=get_event_loop()) proto.connection_made( Mock(is_closing=lambda: False, write=lambda b: fp.write(b.decode()))) proto.data_received(b'GET /docs/swagger.json HTTP/1.1\r\n' b'Connection: close\r\n\r\n') await proto.start() api_spec = json.loads(fp.getvalue().split('\r\n\r\n')[1]) # All responses should have a description for _, path in api_spec['paths'].items(): for _, spec in path.items(): for _, response in spec['responses'].items(): if 'description' not in response: response['description'] = '' # Convert to yaml with open(destination_fn, 'w') as destination_fp: destination_fp.write(yaml.dump(api_spec))
async def test_config_error_notification(tmpdir): """ Test if config error is notified on session startup. """ # 1. Setup corrupted config shutil.copy2(CONFIG_PATH / 'corrupt-triblerd.conf', tmpdir / 'triblerd.conf') tribler_config = TriblerConfig(tmpdir, config_file=tmpdir / 'triblerd.conf', reset_config_on_error=True) # 2. Initialize session with corrupted config, disable upgrader for simplicity. # By mocking the notifier, we can check if the notify was called with correct parameters session = Session(tribler_config) session.upgrader_enabled = False session.notifier = Mock() # 3. Start the session which should internally call the notifier. await session.start() # Notifier uses asyncio loop internally, so we must wait at least a single loop cycle await asyncio.sleep(0) # 4. There could be multiple notify calls but we are only interested in the config error # so using 'assert_any_call' here to check if the notify was called. session.notifier.notify.assert_any_call(NTFY.REPORT_CONFIG_ERROR, tribler_config.config_error) # 5. Always shutdown the session at the end. await session.shutdown()
async def test_copy(tmpdir): config = TriblerConfig(state_dir=tmpdir, file=tmpdir / '1.txt') config.api.http_port = 42 cloned = config.copy() assert cloned.api.http_port == 42 assert cloned.state_dir == tmpdir assert cloned.file == tmpdir / '1.txt'
async def setUp(self): """ Create a new TriblerConfig instance """ await super(TestTriblerConfig, self).setUp() self.tribler_config = TriblerConfig(self.getRootStateDir()) self.assertIsNotNone(self.tribler_config)
def test_write_load(self): """ When writing and reading a config the options should remain the same. """ port = 4444 self.tribler_config.set_anon_listen_port(port) self.tribler_config.write() path = self.tribler_config.get_state_dir() / CONFIG_FILENAME read_config = TriblerConfig(self.root_state_dir, config_file=path) self.assertEqual(read_config.get_anon_listen_port(), port)
def test_write_load(tribler_config): """ When writing and reading a config the options should remain the same. """ port = 4444 tribler_config.set_anon_listen_port(port) tribler_config.write() path = tribler_config.get_state_dir() / CONFIG_FILENAME read_config = TriblerConfig(path, config_file=path) assert read_config.get_anon_listen_port() == port
async def start(self, options): # Determine ipv8 port ipv8_port = options.ipv8_port if ipv8_port == -1 and "HELPER_INDEX" in os.environ and "HELPER_BASE" in os.environ: base_port = int(os.environ["HELPER_BASE"]) ipv8_port = base_port + int(os.environ["HELPER_INDEX"]) * 5 statedir = Path( os.path.join(get_root_state_directory(), "tunnel-%d") % ipv8_port) config = TriblerConfig(statedir, config_file=statedir / 'triblerd.conf') config.set_tunnel_community_socks5_listen_ports([]) config.set_tunnel_community_random_slots(options.random_slots) config.set_tunnel_community_competing_slots(options.competing_slots) config.set_torrent_checking_enabled(False) config.set_ipv8_enabled(True) config.set_libtorrent_enabled(False) config.set_ipv8_port(ipv8_port) config.set_ipv8_address(options.ipv8_address) config.set_trustchain_enabled(True) config.set_market_community_enabled(False) config.set_dht_enabled(True) config.set_tunnel_community_exitnode_enabled(bool(options.exit)) config.set_popularity_community_enabled(False) config.set_testnet(bool(options.testnet)) config.set_chant_enabled(False) config.set_bootstrap_enabled(False) if not options.no_rest_api: config.set_http_api_enabled(True) api_port = options.restapi if "HELPER_INDEX" in os.environ and "HELPER_BASE" in os.environ: api_port = int(os.environ["HELPER_BASE"]) + 10000 + int( os.environ["HELPER_INDEX"]) config.set_http_api_port(api_port) if options.ipv8_bootstrap_override is not None: config.set_ipv8_bootstrap_override(options.ipv8_bootstrap_override) self.session = Session(config) self.log_circuits = options.log_circuits self.session.notifier.add_observer(NTFY.TUNNEL_REMOVE, self.circuit_removed) await self.session.start() if options.log_rejects: # We set this after Tribler has started since the tunnel_community won't be available otherwise self.session.tunnel_community.reject_callback = self.on_circuit_reject # Tunnel helpers store more TrustChain blocks self.session.trustchain_community.settings.max_db_blocks = 5000000 self.tribler_started()
async def start_tribler(self, options): """ Main method to startup Tribler. """ async def signal_handler(sig): print("Received shut down signal %s" % sig) if not self._stopping: self._stopping = True await self.session.shutdown() print("Tribler shut down") get_event_loop().stop() self.process_checker.remove_lock_file() signal.signal(signal.SIGINT, lambda sig, _: ensure_future(signal_handler(sig))) signal.signal(signal.SIGTERM, lambda sig, _: ensure_future(signal_handler(sig))) statedir = Path(options.statedir or Path(get_appstate_dir(), '.Tribler')) config = TriblerConfig(statedir, config_file=statedir / 'triblerd.conf') # Check if we are already running a Tribler instance self.process_checker = ProcessChecker() if self.process_checker.already_running: print("Another Tribler instance is already using statedir %s" % config.get_state_dir()) get_event_loop().stop() return print("Starting Tribler") if options.restapi > 0: config.set_http_api_enabled(True) config.set_http_api_port(options.restapi) if options.ipv8 > 0: config.set_ipv8_port(options.ipv8) elif options.ipv8 == 0: config.set_ipv8_enabled(False) if options.libtorrent != -1 and options.libtorrent > 0: config.set_libtorrent_port(options.libtorrent) if options.ipv8_bootstrap_override is not None: config.set_ipv8_bootstrap_override(options.ipv8_bootstrap_override) if options.testnet: config.set_testnet(True) self.session = Session(config) try: await self.session.start() except Exception as e: print(str(e)) get_event_loop().stop() else: print("Tribler started")
def test_convert_tribler_conf_76(tmpdir): """ Tests conversion of the Tribler 7.5 config """ shutil.copy2(CONFIG_PATH / 'triblerd75.conf', tmpdir / 'triblerd.conf') convert_config_to_tribler76(tmpdir) new_config = TriblerConfig(tmpdir, tmpdir / 'triblerd.conf') assert new_config.get_api_key() == '7671750ba34423c97dc3c6763041e4cb' assert new_config.get_api_http_port() == 8085 assert new_config.get_api_http_enabled() assert not new_config.get_api_https_enabled()
async def test_get_path_relative(tmpdir): config = TriblerConfig(state_dir=tmpdir) config.general.log_dir = None assert not config.general.log_dir config.general.log_dir = '.' assert config.general.get_path_as_absolute('log_dir', tmpdir) == Path(tmpdir) config.general.log_dir = '1' assert config.general.get_path_as_absolute('log_dir', tmpdir) == Path(tmpdir) / '1'
def __init__(self, timeout_in_sec, working_dir): super().__init__(config=TriblerConfig(state_dir=working_dir), timeout_in_sec=timeout_in_sec, working_dir=working_dir, components=[ SocksServersComponent(), LibtorrentComponent(), TorrentCheckerComponent(), MetadataStoreComponent(), KeyComponent(), RESTComponent(), Ipv8Component(), ObservablePopularityComponent() ])
def test_invalid_config_recovers(tmpdir): default_config_file = tmpdir / 'triblerd.conf' shutil.copy2(CONFIG_PATH / 'corrupt-triblerd.conf', default_config_file) # By default, recover_error set to False when loading the config file so # if the config file is corrupted, it should raise a ParseError. with pytest.raises(ParseError): _ = TriblerConfig(tmpdir, config_file=default_config_file) # If recover_error is set to True, the config should successfully load using # the default config in case of corrupted config file and the error is saved. tribler_conf = TriblerConfig(tmpdir, config_file=default_config_file, reset_config_on_error=True) assert "configobj.ParseError: Invalid line" in tribler_conf.config_error assert tribler_conf.get_state_dir() is not None # Since the config should be saved on previous recovery, subsequent instantiation of TriblerConfig # should work without the reset flag. tribler_conf2 = TriblerConfig(tmpdir, config_file=default_config_file, reset_config_on_error=False) assert tribler_conf2
async def test_load_write_nonascii(tmpdir): config = TriblerConfig(state_dir=tmpdir) filename = 'test_read_write.ini' config.download_defaults.saveas = 'ыюя' assert not config.file config.write(tmpdir / filename) assert config.file == tmpdir / filename config = TriblerConfig.load(file=tmpdir / filename, state_dir=tmpdir) assert config.download_defaults.saveas == 'ыюя' assert config.file == tmpdir / filename
def create_default_config(working_dir, config_path): config = TriblerConfig(working_dir, config_path) config.set_tunnel_community_enabled(False) config.set_popularity_community_enabled(False) config.set_bootstrap_enabled(False) config.set_torrent_checking_enabled(True) config.set_ipv8_enabled(False) config.set_libtorrent_enabled(False) config.set_dht_enabled(False) config.set_chant_enabled(False) return config
def __init__(self, source_dir, working_dir): super().__init__(TriblerConfig(state_dir=working_dir), working_dir=working_dir, components=[ RESTComponent(), KeyComponent(), SocksServersComponent(), LibtorrentComponent(), Ipv8Component(), MetadataStoreComponent(), GigachannelManagerComponent(), GigaChannelComponent() ]) self.source_dir = Path(source_dir)
async def fixture_rest_manager(api_port, tmp_path, endpoint): config = TriblerConfig() config.api.http_enabled = True config.api.http_port = api_port root_endpoint = RootEndpoint( middlewares=[ApiKeyMiddleware(config.api.key), error_middleware]) root_endpoint.add_endpoint('/events', endpoint) rest_manager = RESTManager(config=config.api, root_endpoint=root_endpoint, state_dir=tmp_path) await rest_manager.start() yield rest_manager await rest_manager.stop()
def _tribler_config(tribler_state_dir, tribler_download_dir) -> TriblerConfig: config = TriblerConfig(state_dir=tribler_state_dir) config.download_defaults.put_path_as_relative('saveas', tribler_download_dir, state_dir=tribler_state_dir) config.torrent_checking.enabled = False config.ipv8.enabled = False config.ipv8.walk_scaling_enabled = False config.discovery_community.enabled = False config.libtorrent.enabled = False config.libtorrent.dht_readiness_timeout = 0 config.tunnel_community.enabled = False config.popularity_community.enabled = False config.dht.enabled = False config.libtorrent.dht = False config.chant.enabled = False config.resource_monitor.enabled = False config.bootstrap.enabled = False return config
async def setUp(self): await super(TestDHTSession, self).setUp() self.session = Session(TriblerConfig(self.root_state_dir)) self.session.dlmgr = MockObject() self.session.dlmgr.dht_health_manager = MockObject() dht_health_dict = { "infohash": hexlify(b'a' * 20), "seeders": 1, "leechers": 2 } self.session.dlmgr.dht_health_manager.get_health = lambda *_, **__: succeed( {"DHT": [dht_health_dict]}) self.dht_session = FakeDHTSession(self.session, b'a' * 20, 10) self.bep33_dht_session = FakeBep33DHTSession(self.session, b'a' * 20, 10)
async def torrent_checker(loop, mock_dlmgr, metadata_store): # Initialize the torrent checker config = TriblerConfig() config.download_defaults.number_hops = 0 tracker_manager = MagicMock() tracker_manager.blacklist = [] notifier = MagicMock() torrent_checker = TorrentChecker( config=config, download_manager=mock_dlmgr, tracker_manager=tracker_manager, metadata_store=metadata_store, notifier=notifier, socks_listen_ports=[2000, 3000], ) await torrent_checker.initialize() yield torrent_checker await torrent_checker.shutdown()
async def test_load_write(tmpdir): config = TriblerConfig(state_dir=tmpdir) filename = 'test_read_write.ini' config.general.log_dir = '1' config.general.version_checker_enabled = False config.libtorrent.port = None config.libtorrent.proxy_type = 2 assert not config.file config.write(tmpdir / filename) assert config.file == tmpdir / filename config = TriblerConfig.load(file=tmpdir / filename, state_dir=tmpdir) assert config.general.log_dir == '1' assert config.general.version_checker_enabled is False assert config.libtorrent.port is None assert config.libtorrent.proxy_type == 2 assert config.file == tmpdir / filename
async def start_tribler(): # Check if we are already running a Tribler instance process_checker = ProcessChecker(root_state_dir) if process_checker.already_running: return process_checker.create_lock_file() # Before any upgrade, prepare a separate state directory for the update version so it does not # affect the older version state directory. This allows for safe rollback. version_history = VersionHistory(root_state_dir) version_history.fork_state_directory_if_necessary() version_history.save_if_necessary() state_dir = version_history.code_version.directory config = TriblerConfig(state_dir, config_file=state_dir / CONFIG_FILENAME, reset_config_on_error=True) if not config.get_core_error_reporting_requires_user_consent(): SentryReporter.global_strategy = SentryStrategy.SEND_ALLOWED config.set_api_http_port(int(api_port)) # If the API key is set to an empty string, it will remain disabled if config.get_api_key() not in ('', api_key): config.set_api_key(api_key) config.write( ) # Immediately write the API key so other applications can use it config.set_api_http_enabled(True) priority_order = config.get_cpu_priority_order() set_process_priority(pid=os.getpid(), priority_order=priority_order) global trace_logger # Enable tracer if --trace-debug or --trace-exceptions flag is present in sys.argv trace_logger = check_and_enable_code_tracing('core', config.get_log_dir()) session = Session(config, core_test_mode=core_test_mode) signal.signal(signal.SIGTERM, lambda signum, stack: shutdown(session, signum, stack)) await session.start()
def _tribler_config(tribler_state_dir, tribler_download_dir): config = TriblerConfig(tribler_state_dir) config.set_default_destination_dir(tribler_download_dir) config.set_torrent_checking_enabled(False) config.set_ipv8_enabled(False) config.set_discovery_community_enabled(False) config.set_ipv8_walk_scaling_enabled(False) config.set_libtorrent_enabled(False) config.set_libtorrent_dht_readiness_timeout(0) config.set_api_http_enabled(False) config.set_tunnel_community_enabled(False) config.set_popularity_community_enabled(False) config.set_dht_enabled(False) config.set_version_checker_enabled(False) config.set_libtorrent_dht_enabled(False) config.set_chant_enabled(False) config.set_resource_monitor_enabled(False) config.set_bootstrap_enabled(False) return config
def __init__(self, config: TriblerConfig = None, components: List[Component] = (), shutdown_event: Event = None, notifier: Notifier = None, failfast: bool = True): # deepcode ignore unguarded~next~call: not necessary to catch StopIteration on infinite iterator self.id = next(Session._next_session_id) self.failfast = failfast self.logger = logging.getLogger(self.__class__.__name__) self.config: TriblerConfig = config or TriblerConfig() self.shutdown_event: Event = shutdown_event or Event() self.notifier: Notifier = notifier or Notifier() self.components: Dict[Type[Component], Component] = {} for component in components: self.register(component.__class__, component) # Reserve various (possibly) fixed ports to prevent # components from occupying those accidentally reserve_ports([ config.libtorrent.port, config.api.http_port, config.api.https_port, config.ipv8.port ])
def tribler_config(tmp_path): config = TriblerConfig(tmp_path) return config
def start_tribler(self, options): """ Main method to startup Tribler. """ def on_tribler_shutdown(_): msg("Tribler shut down") reactor.stop() self.process_checker.remove_lock_file() def signal_handler(sig, _): msg("Received shut down signal %s" % sig) if not self._stopping: self._stopping = True self.session.shutdown().addCallback(on_tribler_shutdown) signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) config = TriblerConfig() self.process_checker = ProcessChecker() if self.process_checker.already_running: self.shutdown_process( "Another Tribler instance is already using statedir %s" % config.get_state_dir()) return # Enable exitnode if set in options if "exitnode" in options and options["exitnode"]: msg("Enabling exitnode") config.set_tunnel_community_enabled(True) config.set_tunnel_community_exitnode_enabled(True) else: config.set_tunnel_community_exitnode_enabled(False) # Enable bitcoin testnet if "testnet" in options and options["testnet"]: msg("Enabling bitcoin testnet") config.set_testnet(True) # Enable dummy wallets if "dummy" in options and options["dummy"]: msg("Enabling dummy wallets") config.set_dummy_wallets_enabled(True) # Enable record transactions msg("Enabling record transactions") config.set_record_transactions(True) # Minimize functionality enabled for plebnet # For now, config taken from market_plugin in devos tribler repo config.set_http_api_enabled(True) config.set_video_server_enabled(False) config.set_torrent_search_enabled(False) config.set_channel_search_enabled(False) config.set_mainline_dht_enabled(True) config.set_dht_enabled(True) config.set_chant_enabled(False) config.set_bitcoinlib_enabled(True) msg("Starting Tribler") if options["statedir"]: config.set_state_dir(options["statedir"]) if options["restapi"] > 0: config.set_http_api_enabled(True) config.set_http_api_port(options["restapi"]) if options["dispersy"] != -1 and options["dispersy"] > 0: config.set_dispersy_port(options["dispersy"]) if options["libtorrent"] != -1 and options["libtorrent"] > 0: config.set_libtorrent_port(options["libtorrent"]) self.session = Session(config) self.session.start().addErrback( lambda failure: self.shutdown_process(failure.getErrorMessage())) msg("Tribler started")
description=('Start a crawler in the bandwidth accounting community')) parser.add_argument('--statedir', '-s', default='bw_crawler', type=str, help='Use an alternate statedir') parser.add_argument('--restapi', '-p', default=52194, type=str, help='Use an alternate port for the REST API', action=PortAction, metavar='{0..65535}') args = parser.parse_args(sys.argv[1:]) config = TriblerConfig(args.statedir, config_file=Path(args.statedir) / 'triblerd.conf') config.set_state_dir(Path(args.statedir).absolute()) config.set_tunnel_community_enabled(False) config.set_libtorrent_enabled(False) config.set_bootstrap_enabled(False) config.set_chant_enabled(False) config.set_torrent_checking_enabled(False) config.set_api_http_enabled(True) config.set_api_http_port(args.restapi) loop = get_event_loop() coro = start_crawler(config) ensure_future(coro) loop.run_forever()
def create_config(working_dir, config_path): config = TriblerConfig(state_dir=working_dir, file=config_path) config.dht.enabled = True return config
async def test_get_path_absolute(tmpdir): config = TriblerConfig(state_dir=tmpdir) config.general.log_dir = str(Path(tmpdir).parent) assert config.general.get_path_as_absolute( property_name='log_dir', state_dir=tmpdir) == Path(tmpdir).parent
async def test_create(tmpdir): config = TriblerConfig(state_dir=tmpdir) assert config assert config.state_dir == Path(tmpdir)
async def test_base_getters_and_setters(tmpdir): config = TriblerConfig(state_dir=tmpdir) assert config.state_dir == Path(tmpdir) config.set_state_dir('.') assert config.state_dir == Path('.')