Exemplo n.º 1
0
def setup_test(window=1, packet_size=1000, packets=10000):
    """
    Create two nodes who will be sending packets to each other.

    :param window: the window size for packets (1 is synchronous)
    :param packet_size: the size of each packet
    :param packets: the number of packets to send in the experiment
    :return: the deferred that fires once the experiment is complete
    """
    configuration = get_default_configuration()
    configuration['overlays'] = []
    configuration['keys'] = []

    master_peer = Peer(ECCrypto().generate_key(u"low"))

    peer_ipv8 = IPv8(configuration)
    peer_ipv8.keys = {'my_peer': Peer(ECCrypto().generate_key(u"low"))}
    peer_ipv8.overlays = [
        LoadOverlay(master_peer, peer_ipv8.keys['my_peer'], peer_ipv8.endpoint,
                    peer_ipv8.network, window, packet_size, packets)
    ]

    counterparty_ipv8 = IPv8(configuration)
    counterparty_ipv8.keys = {'my_peer': Peer(ECCrypto().generate_key(u"low"))}
    counterparty_ipv8.overlays = [
        LoadOverlay(master_peer, counterparty_ipv8.keys['my_peer'],
                    counterparty_ipv8.endpoint, counterparty_ipv8.network, 0,
                    packet_size, packets)
    ]

    peer_ipv8.overlays[0].send(counterparty_ipv8.endpoint.get_address())

    return DeferredList(
        [peer_ipv8.overlays[0].done, counterparty_ipv8.overlays[0].done])
Exemplo n.º 2
0
    async def run(self):
        await super().run()

        config = self.session.config

        self._task_manager = TaskManager()

        port = config.ipv8.port
        address = config.ipv8.address
        self.logger.info('Starting ipv8')
        self.logger.info(f'Port: {port}. Address: {address}')
        ipv8_config_builder = (
            ConfigBuilder().set_port(port).set_address(address).clear_overlays(
            ).clear_keys()  # We load the keys ourselves
            .set_working_directory(str(config.state_dir)).set_walker_interval(
                config.ipv8.walk_interval))

        if config.gui_test_mode:
            endpoint = DispatcherEndpoint([])
        else:
            # IPv8 includes IPv6 support by default.
            # We only load IPv4 to not kill all Tribler overlays (currently, it would instantly crash all users).
            # If you want to test IPv6 in Tribler you can set ``endpoint = None`` here.
            endpoint = DispatcherEndpoint(["UDPIPv4"],
                                          UDPIPv4={
                                              'port': port,
                                              'ip': address
                                          })
        ipv8 = IPv8(ipv8_config_builder.finalize(),
                    enable_statistics=config.ipv8.statistics
                    and not config.gui_test_mode,
                    endpoint_override=endpoint)
        await ipv8.start()
        self.ipv8 = ipv8

        key_component = await self.require_component(KeyComponent)
        self.peer = Peer(key_component.primary_key)

        if config.ipv8.statistics and not config.gui_test_mode:
            # Enable gathering IPv8 statistics
            for overlay in ipv8.overlays:
                ipv8.endpoint.enable_community_statistics(
                    overlay.get_prefix(), True)

        if config.ipv8.walk_scaling_enabled and not config.gui_test_mode:
            from tribler_core.components.ipv8.ipv8_health_monitor import IPv8Monitor
            IPv8Monitor(ipv8, config.ipv8.walk_interval,
                        config.ipv8.walk_scaling_upper_limit).start(
                            self._task_manager)

        if config.dht.enabled:
            self._init_dht_discovery_community()

        if not config.gui_test_mode:
            if config.discovery_community.enabled:
                self._init_peer_discovery_community()
        else:
            if config.dht.enabled:
                self.dht_discovery_community.routing_tables[
                    UDPv4Address] = RoutingTable('\x00' * 20)
Exemplo n.º 3
0
async def start_communities():
    for i in [1, 2]:
        configuration = get_default_configuration()
        configuration['keys'] = [{
            'alias': "my peer",
            'generation': u"medium",
            'file': u"ec%d.pem" % i
        }]
        configuration['port'] = 12000 + randint(0, 10000)
        configuration['overlays'] = [{
            'class':
            'MyCommunity',
            'key':
            "my peer",
            'walkers': [{
                'strategy': "RandomWalk",
                'peers': 10,
                'init': {
                    'timeout': 3.0
                }
            }],
            'bootstrappers': [DISPERSY_BOOTSTRAPPER],
            'initialize': {},
            'on_start': [('started', )]
        }]
        ipv8 = IPv8(configuration)
        await ipv8.start()
        INSTANCES.append(ipv8)
Exemplo n.º 4
0
    async def start_ipv8(self, statistics, no_rest_api):
        """
        Main method to startup IPv8.
        """
        self.ipv8 = IPv8(get_default_configuration(), enable_statistics=statistics)
        await self.ipv8.start()

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    await self.restapi.stop()
                await self.ipv8.stop()
                await gather(*all_tasks())
                get_event_loop().stop()

        signal.signal(signal.SIGINT, lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM, lambda sig, _: ensure_future(signal_handler(sig)))

        print("Starting IPv8")

        if not no_rest_api:
            self.restapi = RESTManager(self.ipv8)
            await self.restapi.start()
Exemplo n.º 5
0
    def start_crawler(self, options):
        """
        Main method to startup the TrustChain crawler.
        """
        root = logging.getLogger()
        root.setLevel(logging.INFO)

        stderr_handler = logging.StreamHandler(sys.stderr)
        stderr_handler.setLevel(logging.INFO)
        stderr_handler.setFormatter(
            logging.Formatter("%(asctime)s:%(levelname)s:%(message)s"))
        root.addHandler(stderr_handler)

        if options["statedir"]:
            # If we use a custom state directory, update various variables
            for key in crawler_config["keys"]:
                key["file"] = os.path.join(options["statedir"], key["file"])

            for community in crawler_config["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["initialize"]["working_directory"] = options[
                        "statedir"]

        if 'testnet' in options and options['testnet']:
            for community in crawler_config["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["class"] = "TrustChainTestnetCommunity"

        self.ipv8 = IPv8(crawler_config)

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    self.restapi.stop()
                self.ipv8.stop()

                if options['yappi']:
                    yappi.stop()
                    msg("Yappi has shutdown")
                    yappi_stats = yappi.get_func_stats()
                    yappi_stats.sort("tsub")
                    out_file = 'yappi'
                    if options["statedir"]:
                        out_file = os.path.join(options["statedir"], out_file)
                    yappi_stats.save(out_file, type='callgrind')

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        msg("Starting TrustChain crawler")

        if not options['no-rest-api']:
            self.restapi = RESTManager(self.ipv8)
            reactor.callLater(0.0, self.restapi.start, options["apiport"])

        if options['yappi']:
            yappi.start(builtins=True)
Exemplo n.º 6
0
def main(argv):
    parser = argparse.ArgumentParser(add_help=False, description=(
        'Simple client for various types attestations'))
    parser.add_argument('--help', '-h', action='help',
                        default=argparse.SUPPRESS, help='Show this help message and exit')
    parser.add_argument('--port', '-p', default=8124, type=int,
                        help='Listen port for web service (8124 by default)')
    parser.add_argument('--ip', '-i', default='127.0.0.1',
                        help='Listen IP for webservice (127.0.0.1 by default)')
    parser.add_argument('--reset', '-r', action='store_true', default=False,
                        help='Resets the identity')
    args = parser.parse_args(argv)

    if os.path.exists("temp") and args.reset:
        shutil.rmtree("temp")

    if not os.path.exists("temp"):
        os.mkdir("temp")
        with open('temp/state.json', 'w') as outfile:
            data = {
                "attributes": [],
                "providers": {}
            }
            json.dump(data, outfile)

    # Start REST endpoints using Twisted
    vars = globals()
    configuration = get_default_configuration()
    configuration['logger']['level'] = "ERROR"
    configuration['keys'] = [
        {'alias': "anonymous id", 'generation': u"curve25519",
            'file': u"temp/ec_multichain.pem"},
        {'alias': "my peer", 'generation': u"medium", 'file': u"temp/ec.pem"}
    ]

    # Only load the basic communities
    # requested_overlays = ['DiscoveryCommunity', 'AttestationCommunity', 'IdentityCommunity']
    # configuration['overlays'] = [o for o in configuration['overlays'] if o['class'] in requested_overlays]

    # Give each peer a separate working directory
    working_directory_overlays = ['AttestationCommunity', 'IdentityCommunity']
    for overlay in configuration['overlays']:
        if overlay['class'] in working_directory_overlays:
            overlay['initialize'] = {'working_directory': 'temp'}

    ipv8 = IPv8(configuration)
    config = {
        'mid_b64': b64encode(ipv8.keys["anonymous id"].mid),
    }
    rest_manager = IPv8RESTManager(ipv8)
    rest_manager.start(args.port)
    rest_manager.root_endpoint.putChild(b'api', APIEndpoint(config))
    rest_manager.root_endpoint.putChild(
        b'gui', File(os.path.join(BASE_DIR, 'webapp', 'dist')))

    print('mid_b64: ' + str(config['mid_b64']))

    reactor.run()
Exemplo n.º 7
0
    def __init__(self,
                 path,
                 port,
                 interface='127.0.0.1',
                 configuration=None):
        """
        Create a test peer with a REST API interface.

        :param path: the for the working directory of this peer
        :param port: this peer's port
        :param interface: IP or alias of the peer. Defaults to '127.0.0.1'
        :param configuration: IPv8 configuration object. Defaults to None
        """
        self._logger = logging.getLogger(self.__class__.__name__)
        self._logger.info("Peer starting-up.")

        self._rest_manager = None

        self._port = port
        self._interface = interface

        self._path = path
        self._configuration = configuration

        # Check to see if we've received a custom configuration
        if configuration is None:
            # Create a default configuration
            self._configuration = get_default_configuration()

            self._configuration['logger'] = {'level': "ERROR"}

            overlays = ['AttestationCommunity', 'IdentityCommunity']
            self._configuration['overlays'] = [o for o in self._configuration['overlays'] if o['class'] in overlays]
            for o in self._configuration['overlays']:
                o['walkers'] = [{
                    'strategy': "RandomWalk",
                    'peers': 20,
                    'init': {
                        'timeout': 60.0
                    }
                }]

        self._create_working_directory(self._path)
        self._logger.info("Created working directory.")
        os.chdir(self._path)

        self._ipv8 = IPv8(self._configuration)
        os.chdir(os.path.dirname(__file__))

        # Change the master_peers of the IPv8 object's overlays, in order to avoid conflict with the live networks
        for idx, overlay in enumerate(self._ipv8.overlays):
            self._ipv8.overlays[idx].master_peer = Peer(COMMUNITY_TO_MASTER_PEER_KEY[type(overlay).__name__])

        self._rest_manager = TestPeer.RestAPITestWrapper(self._ipv8, self._port, self._interface)
        self._rest_manager.start()
        self._logger.info("Peer started up.")
Exemplo n.º 8
0
async def main():
    configuration = get_default_configuration()
    configuration['keys'] = [{
        'alias': "my peer",
        'generation': "curve25519",
        'file': "ec.pem"
    }]
    configuration['port'] = 12000 + randint(0, 10000)
    configuration['overlays'] = []
    ipv8 = IPv8(configuration)
    await ipv8.start()

    overlay = MyCommunity(ipv8.keys['my peer'], ipv8.endpoint, ipv8.network)
    introductions = {}
    ips = {}
    for _ in range(count):
        for dns_addr in DISPERSY_BOOTSTRAPPER['init']['dns_addresses']:
            if 'tribler.org' not in dns_addr[0] and 'ip-v8.org' not in dns_addr[0]:
                continue

            try:
                ip = ips.get(dns_addr[0], gethostbyname(dns_addr[0]))
            except OSError:
                continue

            try:
                response = await wait_for(overlay.send_intro_request((ip, dns_addr[1])), timeout=5)
            except AsyncTimeoutError:
                continue

            reachable = False
            try:
                if response.wan_introduction_address != ('0.0.0.0', 0):
                    # Wait some time for puncture to be sent
                    await sleep(.1)
                    await wait_for(overlay.send_intro_request(response.wan_introduction_address), timeout=5)
                    reachable = True
            except AsyncTimeoutError:
                pass

            introductions[dns_addr] = introductions.get(dns_addr, [])
            introductions[dns_addr].append([response.wan_introduction_address,
                                            response.lan_introduction_address,
                                            reachable])

        await sleep(delay)

    with open('bootstrap_introductions.txt', 'w') as f:
        f.write('Address Peers Type')
        for dns_addr, responses in introductions.items():
            f.write(f"\n{dns_addr[0]}:{dns_addr[1]} {len([wan for wan, _, _ in responses if wan != ('0.0.0.0', 0)])} 0")
            f.write(f"\n{dns_addr[0]}:{dns_addr[1]} {len({wan for wan, _, _ in responses if wan != ('0.0.0.0', 0)})} 1")
            f.write(f"\n{dns_addr[0]}:{dns_addr[1]} {len({lan for _, lan, _ in responses if lan != ('0.0.0.0', 0)})} 3")
            f.write(f"\n{dns_addr[0]}:{dns_addr[1]} {len({wan for wan, _, reachable in responses if reachable})} 2")
Exemplo n.º 9
0
    async def start_ipv8(self, statedir, listen_port, statistics, no_rest_api):
        """
        Main method to startup IPv8.
        """
        configuration = get_default_configuration()
        configuration['port'] = listen_port

        # Open the database
        self.tc_persistence = TrustChainDB(statedir, 'trustchain')

        if statedir:
            # If we use a custom state directory, update various variables
            for key in configuration["keys"]:
                key["file"] = os.path.join(statedir, key["file"])

            for community in configuration["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["initialize"]["persistence"] = self.tc_persistence

        allowed_overlays = ['DHTDiscoveryCommunity', 'DiscoveryCommunity', 'HiddenTunnelCommunity',
                            'TrustChainCommunity']
        configuration['overlays'] = [overlay for overlay in configuration['overlays']
                                     if overlay['class'] in allowed_overlays]

        for overlay in configuration['overlays']:
            if overlay['class'] == 'HiddenTunnelCommunity':
                overlay['initialize']['settings']['min_circuits'] = 0
                overlay['initialize']['settings']['max_circuits'] = 0
                overlay['initialize']['settings']['max_relays_or_exits'] = 1000
                overlay['initialize']['settings']['peer_flags'] = {PEER_FLAG_EXIT_IPV8}

        self.ipv8 = IPv8(configuration, enable_statistics=statistics)
        await self.ipv8.start()

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    await self.restapi.stop()
                await self.ipv8.stop()
                await gather(*all_tasks())
                get_event_loop().stop()

        signal.signal(signal.SIGINT, lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM, lambda sig, _: ensure_future(signal_handler(sig)))

        print("Starting IPv8")

        if not no_rest_api:
            self.restapi = RESTManager(self.ipv8)
            await self.restapi.start()
Exemplo n.º 10
0
    async def start_ipv8(self, listen_port, statistics, no_rest_api):
        """
        Main method to startup IPv8.
        """
        configuration = get_default_configuration()
        configuration['port'] = listen_port

        allowed_overlays = [
            'DHTDiscoveryCommunity', 'DiscoveryCommunity',
            'HiddenTunnelCommunity', 'TrustChainCommunity'
        ]
        configuration['overlays'] = [
            overlay for overlay in configuration['overlays']
            if overlay['class'] in allowed_overlays
        ]

        for overlay in configuration['overlays']:
            if overlay['class'] == 'HiddenTunnelCommunity':
                overlay['initialize']['settings']['min_circuits'] = 0
                overlay['initialize']['settings']['max_circuits'] = 0
                overlay['initialize']['settings']['max_relays_or_exits'] = 1000
                overlay['initialize']['settings'][
                    'peer_flags'] = PEER_FLAG_EXIT_IPV8

        self.ipv8 = IPv8(configuration, enable_statistics=statistics)
        await self.ipv8.start()

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    await self.restapi.stop()
                await self.ipv8.stop()
                await gather(*all_tasks())
                get_event_loop().stop()

        signal.signal(signal.SIGINT,
                      lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM,
                      lambda sig, _: ensure_future(signal_handler(sig)))

        print("Starting IPv8")

        if not no_rest_api:
            self.restapi = RESTManager(self.ipv8)
            await self.restapi.start()
Exemplo n.º 11
0
async def start_communities():
    configuration = get_default_configuration()
    configuration['keys'] = [{
        'alias': "my peer",
        'generation': u"medium",
        'file': u"ec1.pem"
    }]
    configuration['port'] = 12000 + randint(0, 10000)
    configuration['overlays'] = [{
        'class': 'MyCommunity',
        'key': "my peer",
        'walkers': [],
        'initialize': {},
        'on_start': [('started', )]
    }]
    ipv8 = IPv8(configuration)
    await ipv8.start()
    INSTANCES.append(ipv8)
Exemplo n.º 12
0
async def start_community():
    for peer_id in [1, 2]:
        configuration = get_default_configuration()
        configuration['logger']['level'] = "ERROR"
        configuration['keys'] = [{'alias': "anonymous id",
                                  'generation': u"curve25519",
                                  'file': f"keyfile_{peer_id}.pem"}]
        configuration['working_directory'] = f"state_{peer_id}"
        configuration['overlays'] = []

        # Start the IPv8 service
        ipv8 = IPv8(configuration)
        await ipv8.start()
        rest_manager = RESTManager(ipv8)
        await rest_manager.start(14410 + peer_id)

        # Print the peer for reference
        print("Starting peer", b64encode(ipv8.keys["anonymous id"].mid))
Exemplo n.º 13
0
    def start_ipv8(self, options):
        """
        Main method to startup IPv8.
        """
        configuration = get_default_configuration()

        configuration['port'] = options["listen_port"]

        allowed_overlays = [
            'DHTDiscoveryCommunity', 'DiscoveryCommunity',
            'HiddenTunnelCommunity', 'TrustChainCommunity'
        ]
        configuration['overlays'] = [
            overlay for overlay in configuration['overlays']
            if overlay['class'] in allowed_overlays
        ]

        for overlay in configuration['overlays']:
            if overlay['class'] == 'HiddenTunnelCommunity':
                overlay['initialize']['settings']['min_circuits'] = 0
                overlay['initialize']['settings']['max_circuits'] = 0
                overlay['initialize']['settings']['max_relays_or_exits'] = 1000
                overlay['initialize']['settings'][
                    'peer_flags'] = PEER_FLAG_EXIT_IPV8

        self.ipv8 = IPv8(configuration,
                         enable_statistics=options['statistics'])

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    self.restapi.stop()
                self.ipv8.stop()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        msg("Starting IPv8")

        if not options['no-rest-api']:
            self.restapi = RESTManager(self.ipv8)
            reactor.callLater(0.0, self.restapi.start)
Exemplo n.º 14
0
    def start_openwallet(self, options):
        """
        Main method to startup OpenWallet.
        """
        configuration = get_default_configuration()
        configuration['logger']['level'] = "ERROR"
        configuration['keys'] = [{
            'alias': "anonymous id",
            'generation': u"curve25519",
            'file': u"temp/ec_multichain.pem"
        }, {
            'alias': "my peer",
            'generation': u"medium",
            'file': u"temp/ec.pem"
        }]

        working_directory_overlays = [
            'AttestationCommunity', 'IdentityCommunity'
        ]
        for overlay in configuration['overlays']:
            if overlay['class'] in working_directory_overlays:
                overlay['initialize'] = {'working_directory': 'temp'}

        self.ipv8 = IPv8(configuration)

        self.restapi = IPv8RESTManager(self.ipv8)
        self.port = options["port"]

        reactor.callLater(0.0, self.startApi)

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    self.restapi.stop()
                self.ipv8.stop()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        msg("Starting OpenWallet and IPv8")
        msg("Python Version: " + sys.version)
Exemplo n.º 15
0
    async def start_ipv8(self, statistics, no_rest_api, api_key, cert_file):
        """
        Main method to startup IPv8.
        """
        self.ipv8 = IPv8(get_default_configuration(),
                         enable_statistics=statistics)
        await self.ipv8.start()

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    await self.restapi.stop()
                await self.ipv8.stop()
                await gather(*all_tasks())
                get_event_loop().stop()

        signal.signal(signal.SIGINT,
                      lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM,
                      lambda sig, _: ensure_future(signal_handler(sig)))

        print("Starting IPv8")

        if not no_rest_api:
            # Load the certificate/key file. A new one can be generated as follows:
            # openssl req \
            #     -newkey rsa:2048 -nodes -keyout private.key \
            #     -x509 -days 365 -out certfile.pem
            # cat private.key >> certfile.pem
            # rm private.key
            ssl_context = None
            if cert_file:
                ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
                ssl_context.load_cert_chain(cert_file)

            self.restapi = RESTManager(self.ipv8)
            await self.restapi.start(api_key=api_key, ssl_context=ssl_context)
Exemplo n.º 16
0
    def start_ipv8(self, options):
        """
        Main method to startup IPv8.
        """
        self.ipv8 = IPv8(get_default_configuration())

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    self.restapi.stop()
                self.ipv8.stop()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        msg("Starting IPv8")

        if not options['no-rest-api']:
            self.restapi = RESTManager(self.ipv8)
            reactor.callLater(0.0, self.restapi.start)
Exemplo n.º 17
0
    def start_trust(self, options):
        """
        Main method to startup the trust.
        """
        root = logging.getLogger()
        root.setLevel(logging.INFO)

        stderr_handler = logging.StreamHandler(sys.stderr)
        stderr_handler.setLevel(logging.INFO)
        stderr_handler.setFormatter(
            logging.Formatter("%(asctime)s:%(levelname)s:%(message)s"))
        root.addHandler(stderr_handler)

        self.ipv8 = IPv8(configuration)

        # Peer
        my_peer = self.ipv8.keys.get('my peer')

        # trust community
        trust_community = TrustCommunity(my_peer, self.ipv8.endpoint,
                                         self.ipv8.network)
        self.ipv8.overlays.append(trust_community)
        self.ipv8.strategies.append((RandomWalk(trust_community), 10))

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                self.restapi.stop()
                self.ipv8.stop()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        self.restapi = RESTManager(self.ipv8)
        reactor.callLater(0.0, self.restapi.start, 8001)
Exemplo n.º 18
0
async def start_communities():
    # Launch two IPv8 services.
    # We run REST endpoints for these services on:
    #  - http://localhost:14411/
    #  - http://localhost:14412/
    # This script also prints the peer ids for reference with:
    #  - http://localhost:1441*/attestation?type=peers
    for i in [1, 2]:
        configuration = get_default_configuration()
        configuration['logger']['level'] = "ERROR"
        configuration['keys'] = [{
            'alias': "anonymous id",
            'generation': u"curve25519",
            'file': u"ec%d_multichain.pem" % i
        }]

        # Only load the basic communities
        configuration['overlays'] = [{
            'class':
            'IsolatedDiscoveryCommunity',
            'key':
            "anonymous id",
            'walkers': [{
                'strategy': "RandomWalk",
                'peers': 20,
                'init': {
                    'timeout': 3.0
                }
            }, {
                'strategy': "RandomChurn",
                'peers': -1,
                'init': {
                    'sample_size': 8,
                    'ping_interval': 10.0,
                    'inactive_time': 27.5,
                    'drop_time': 57.5
                }
            }, {
                'strategy': "PeriodicSimilarity",
                'peers': -1,
                'init': {}
            }],
            'bootstrappers': [DISPERSY_BOOTSTRAPPER],
            'initialize': {},
            'on_start': []
        }, {
            'class':
            'IsolatedAttestationCommunity',
            'key':
            "anonymous id",
            'walkers': [{
                'strategy': "RandomWalk",
                'peers': 20,
                'init': {
                    'timeout': 3.0
                }
            }],
            'bootstrappers': [DISPERSY_BOOTSTRAPPER],
            'initialize': {
                'working_directory': 'state_%d' % i
            },
            'on_start': []
        }, {
            'class':
            'IsolatedIdentityCommunity',
            'key':
            "anonymous id",
            'walkers': [{
                'strategy': "RandomWalk",
                'peers': 20,
                'init': {
                    'timeout': 3.0
                }
            }],
            'bootstrappers': [DISPERSY_BOOTSTRAPPER],
            'initialize': {
                'working_directory': 'state_%d' % i
            },
            'on_start': []
        }]

        # Start the IPv8 service
        ipv8 = IPv8(configuration,
                    extra_communities={
                        'IsolatedDiscoveryCommunity':
                        IsolatedDiscoveryCommunity,
                        'IsolatedAttestationCommunity':
                        IsolatedAttestationCommunity,
                        'IsolatedIdentityCommunity': IsolatedIdentityCommunity
                    })
        await ipv8.start()
        rest_manager = RESTManager(ipv8)
        await rest_manager.start(14410 + i)

        # Print the peer for reference
        print("Starting peer", b64encode(ipv8.keys["anonymous id"].mid))
Exemplo n.º 19
0
    async def start(self):
        """
        Start a Tribler session by initializing the LaunchManyCore class, opening the database and running the upgrader.
        Returns a deferred that fires when the Tribler session is ready for use.

        :param config: a TriblerConfig object
        """

        self._logger.info("Session is using state directory: %s",
                          self.config.get_state_dir())
        self.get_ports_in_config()
        self.create_state_directory_structure()
        self.init_keypair()

        # Start the REST API before the upgrader since we want to send interesting upgrader events over the socket
        if self.config.get_http_api_enabled():
            self.api_manager = RESTManager(self)
            self.readable_status = STATE_START_API
            await self.api_manager.start()

        if self.upgrader_enabled:
            self.upgrader = TriblerUpgrader(self)
            self.readable_status = STATE_UPGRADING_READABLE
            try:
                await self.upgrader.run()
            except Exception as e:
                self._logger.error("Error in Upgrader callback chain: %s", e)

        self.tracker_manager = TrackerManager(self)

        # On Mac, we bundle the root certificate for the SSL validation since Twisted is not using the root
        # certificates provided by the system trust store.
        if sys.platform == 'darwin':
            os.environ['SSL_CERT_FILE'] = str(
                (get_lib_path() / 'root_certs_mac.pem'))

        if self.config.get_chant_enabled():
            channels_dir = self.config.get_chant_channels_dir()
            metadata_db_name = 'metadata.db' if not self.config.get_testnet(
            ) else 'metadata_testnet.db'
            database_path = self.config.get_state_dir(
            ) / 'sqlite' / metadata_db_name
            self.mds = MetadataStore(database_path, channels_dir,
                                     self.trustchain_keypair)

        # IPv8
        if self.config.get_ipv8_enabled():
            from ipv8.configuration import get_default_configuration
            ipv8_config = get_default_configuration()
            ipv8_config['port'] = self.config.get_ipv8_port()
            ipv8_config['address'] = self.config.get_ipv8_address()
            ipv8_config['overlays'] = []
            ipv8_config['keys'] = []  # We load the keys ourselves
            ipv8_config['working_directory'] = str(self.config.get_state_dir())

            if self.config.get_ipv8_bootstrap_override():
                import ipv8.community as community_file
                community_file._DEFAULT_ADDRESSES = [
                    self.config.get_ipv8_bootstrap_override()
                ]
                community_file._DNS_ADDRESSES = []

            self.ipv8 = IPv8(
                ipv8_config,
                enable_statistics=self.config.get_ipv8_statistics())
            await self.ipv8.start()

            self.config.set_anon_proxy_settings(
                2, ("127.0.0.1",
                    self.config.get_tunnel_community_socks5_listen_ports()))
            self.ipv8_start_time = timemod.time()
            self.load_ipv8_overlays()
            self.enable_ipv8_statistics()
            if self.api_manager:
                self.api_manager.set_ipv8_session(self.ipv8)
            if self.config.get_tunnel_community_enabled():
                await self.tunnel_community.wait_for_socks_servers()

        # Note that currently we should only start libtorrent after the SOCKS5 servers have been started
        if self.config.get_libtorrent_enabled():
            self.readable_status = STATE_START_LIBTORRENT
            from tribler_core.modules.libtorrent.download_manager import DownloadManager
            self.dlmgr = DownloadManager(self)
            self.dlmgr.initialize()
            self.readable_status = STATE_LOAD_CHECKPOINTS
            await self.dlmgr.load_checkpoints()
        self.readable_status = STATE_READABLE_STARTED

        if self.config.get_torrent_checking_enabled():
            self.readable_status = STATE_START_TORRENT_CHECKER
            self.torrent_checker = TorrentChecker(self)
            await self.torrent_checker.initialize()

        if self.config.get_dummy_wallets_enabled():
            # For debugging purposes, we create dummy wallets
            dummy_wallet1 = DummyWallet1()
            self.wallets[dummy_wallet1.get_identifier()] = dummy_wallet1

            dummy_wallet2 = DummyWallet2()
            self.wallets[dummy_wallet2.get_identifier()] = dummy_wallet2

        if self.config.get_watch_folder_enabled():
            self.readable_status = STATE_START_WATCH_FOLDER
            self.watch_folder = WatchFolder(self)
            self.watch_folder.start()

        if self.config.get_resource_monitor_enabled():
            self.resource_monitor = ResourceMonitor(self)
            self.resource_monitor.start()

        if self.config.get_version_checker_enabled():
            self.version_check_manager = VersionCheckManager(self)
            self.version_check_manager.start()

        if self.config.get_ipv8_enabled(
        ) and self.config.get_trustchain_enabled():
            self.payout_manager = PayoutManager(self.trustchain_community,
                                                self.dht_community)

        # GigaChannel Manager should be started *after* resuming the downloads,
        # because it depends on the states of torrent downloads
        if self.config.get_chant_enabled() and self.config.get_chant_manager_enabled()\
                and self.config.get_libtorrent_enabled:
            self.gigachannel_manager = GigaChannelManager(self)
            self.gigachannel_manager.start()

        if self.config.get_bootstrap_enabled():
            self.register_task('bootstrap_download',
                               self.start_bootstrap_download)

        self.notifier.notify(NTFY.TRIBLER_STARTED)
Exemplo n.º 20
0
    async def start_anydex(self, options):
        """
        Main method to startup AnyDex.
        """
        config = get_anydex_configuration()

        if options.statedir:
            # If we use a custom state directory, update various variables
            for key in config["keys"]:
                key["file"] = os.path.join(options.statedir, key["file"])

            for community in config["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["initialize"]["working_directory"] = options.statedir

        if options.testnet:
            for community in config["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["class"] = "TrustChainTestnetCommunity"

        self.ipv8 = IPv8(config, enable_statistics=options.statistics)
        await self.ipv8.start()

        print("Starting AnyDex")

        if not options.no_rest_api:
            self.restapi = RESTManager(self.ipv8)
            await self.restapi.start(options.apiport)

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    await self.restapi.stop()
                    self.restapi = None
                await self.ipv8.stop()
                get_event_loop().stop()

        signal.signal(signal.SIGINT, lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM, lambda sig, _: ensure_future(signal_handler(sig)))

        # Get Trustchain + DHT overlays
        for overlay in self.ipv8.overlays:
            # Logger level required by bitcoinlib import
            overlay.logger.setLevel('INFO')
            if isinstance(overlay, (TrustChainCommunity, TrustChainTestnetCommunity)):
                self.trustchain = overlay
            elif isinstance(overlay, DHTDiscoveryCommunity):
                self.dht = overlay

        # Initialize dummy wallets
        dummy_wallet1 = DummyWallet1()
        self.wallets[dummy_wallet1.get_identifier()] = dummy_wallet1

        dummy_wallet2 = DummyWallet2()
        self.wallets[dummy_wallet2.get_identifier()] = dummy_wallet2

        # Bitcoinlib imports required to be later due to logger overlap
        from anydex.wallet.bitcoinlib.bitcoinlib_wallets import BitcoinTestnetWallet, BitcoinWallet, LitecoinWallet, \
            LitecoinTestnetWallet, DashWallet, DashTestnetWallet

        # Initialize bitcoin wallets
        btc_wallet = BitcoinWallet(os.path.join(options.statedir, 'sqlite'))
        btc_wallet.create_wallet()
        self.wallets[btc_wallet.get_identifier()] = btc_wallet

        btc_testnet_wallet = BitcoinTestnetWallet(os.path.join(options.statedir, 'sqlite'))
        btc_testnet_wallet.create_wallet()
        self.wallets[btc_testnet_wallet.get_identifier()] = btc_testnet_wallet

        # Initialize litecoin wallets
        ltc_wallet = LitecoinWallet(os.path.join(options.statedir, 'sqlite'))
        ltc_wallet.create_wallet()
        self.wallets[ltc_wallet.get_identifier()] = ltc_wallet

        ltc_testnet_wallet = LitecoinTestnetWallet(os.path.join(options.statedir, 'sqlite'))
        ltc_testnet_wallet.create_wallet()
        self.wallets[ltc_testnet_wallet.get_identifier()] = ltc_testnet_wallet

        # Initialize dash wallets
        dash_wallet = DashWallet(os.path.join(options.statedir, 'sqlite'))
        dash_wallet.create_wallet()
        self.wallets[dash_wallet.get_identifier()] = dash_wallet

        # DASH TESTNET HAS NO PROVIDERS IN BITCOINLIB
        # dash_testnet_wallet = DashTestnetWallet(os.path.join(options.statedir, 'sqlite'))
        # dash_testnet_wallet.create_wallet()
        # self.wallets[dash_testnet_wallet.get_identifier()] = dash_testnet_wallet

        # Initialize ethereum wallets
        eth_wallet = EthereumWallet(os.path.join(options.statedir, 'sqlite'))
        eth_wallet.create_wallet()
        self.wallets[eth_wallet.get_identifier()] = eth_wallet

        eth_testnet_wallet = EthereumTestnetWallet(os.path.join(options.statedir, 'sqlite'))
        eth_testnet_wallet.create_wallet()
        self.wallets[eth_testnet_wallet.get_identifier()] = eth_testnet_wallet

        # Initialize iota wallets
        iota_wallet = IotaWallet(os.path.join(options.statedir, 'sqlite'))
        iota_wallet.create_wallet()
        self.wallets[iota_wallet.get_identifier()] = iota_wallet

        iota_testnet_wallet = IotaTestnetWallet(os.path.join(options.statedir, 'sqlite'))
        iota_testnet_wallet.create_wallet()
        self.wallets[iota_testnet_wallet.get_identifier()] = iota_testnet_wallet

        # Initialize monero wallets
        # xmr_wallet = MoneroWallet(os.path.join(options.statedir, 'sqlite'))
        # xmr_wallet.create_wallet()
        # self.wallets[xmr_wallet.get_identifier()] = xmr_wallet

        # xmr_testnet_wallet = MoneroTestnetWallet(os.path.join(options.statedir, 'sqlite'))
        # xmr_testnet_wallet.create_wallet()
        # self.wallets[xmr_testnet_wallet.get_identifier()] = xmr_testnet_wallet

        # Initialize stellar wallets
        xlm_wallet = StellarWallet(os.path.join(options.statedir, 'sqlite'))
        xlm_wallet.create_wallet()
        self.wallets[xlm_wallet.get_identifier()] = xlm_wallet

        xlm_testnet_wallet = StellarTestnetWallet(os.path.join(options.statedir, 'sqlite'))
        xlm_testnet_wallet.create_wallet()
        self.wallets[xlm_testnet_wallet.get_identifier()] = xlm_testnet_wallet

        # Load market community
        self.market = MarketTestnetCommunity(self.trustchain.my_peer, self.ipv8.endpoint, self.ipv8.network,
                                             trustchain=self.trustchain,
                                             dht=self.dht,
                                             wallets=self.wallets,
                                             working_directory=options.statedir,
                                             record_transactions=False,
                                             is_matchmaker=not options.no_matchmaker)

        self.ipv8.overlays.append(self.market)
        self.ipv8.strategies.append((RandomWalk(self.market), 20))
Exemplo n.º 21
0
    requested_overlays = [
        'DiscoveryCommunity', 'AttestationCommunity', 'IdentityCommunity'
    ]
    configuration['overlays'] = [
        o for o in configuration['overlays']
        if o['class'] in requested_overlays
    ]

    # Give each peer a separate working directory
    working_directory_overlays = ['AttestationCommunity', 'IdentityCommunity']
    for overlay in configuration['overlays']:
        if overlay['class'] in working_directory_overlays:
            overlay['initialize'] = {'working_directory': 'temp/state_%d' % i}

    # Start the IPv8 service
    ipv8 = IPv8(configuration)
    rest_manager = RESTManager(ipv8)
    rest_manager.start(14410 + i)

    # Print the peer for reference
    print("Starting peer", i)
    print("port", (14410 + i))
    print("mid_b64", b64encode(ipv8.keys["anonymous id"].mid))
    print("mid_hex", hexlify(ipv8.keys["anonymous id"].mid))

    data[names[i - 1]] = {
        'port': 14410 + i,
        'mid_b64': b64encode(ipv8.keys["anonymous id"].mid),
        'mid_hex': hexlify(ipv8.keys["anonymous id"].mid)
    }
Exemplo n.º 22
0
    def start_anydex(self, options):
        """
        Main method to startup AnyDex.
        """
        config = get_anydex_configuration()

        if options["statedir"]:
            # If we use a custom state directory, update various variables
            for key in config["keys"]:
                key["file"] = os.path.join(options["statedir"], key["file"])

            for community in config["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["initialize"]["working_directory"] = options["statedir"]

        if 'testnet' in options and options['testnet']:
            for community in config["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["class"] = "TrustChainTestnetCommunity"

        self.ipv8 = IPv8(config, enable_statistics=options['statistics'])

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    self.restapi.stop()
                self.ipv8.stop()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        msg("Starting AnyDex")

        if not options['no-rest-api']:
            self.restapi = RESTManager(self.ipv8)
            reactor.callLater(0.0, self.restapi.start)

            factory = WebSocketServerFactory(u"ws://127.0.0.1:9000")
            factory.protocol = AnyDexWebsocketProtocol
            reactor.listenTCP(9000, factory)

        # Get Trustchain + DHT overlays
        for overlay in self.ipv8.overlays:
            if isinstance(overlay, TrustChainTestnetCommunity):
                self.trustchain = overlay
            elif isinstance(overlay, DHTDiscoveryCommunity):
                self.dht = overlay

        # Initialize wallets
        dummy_wallet1 = DummyWallet1()
        self.wallets[dummy_wallet1.get_identifier()] = dummy_wallet1

        dummy_wallet2 = DummyWallet2()
        self.wallets[dummy_wallet2.get_identifier()] = dummy_wallet2

        # Load market community
        self.market = MarketTestnetCommunity(self.trustchain.my_peer, self.ipv8.endpoint, self.ipv8.network,
                                             trustchain=self.trustchain,
                                             dht=self.dht,
                                             wallets=self.wallets,
                                             working_directory=options["statedir"],
                                             record_transactions=False)

        self.ipv8.overlays.append(self.market)
        self.ipv8.strategies.append((RandomWalk(self.market), 20))
Exemplo n.º 23
0
    async def start_anydex(self, options):
        """
        Main method to startup AnyDex.
        """
        config = get_anydex_configuration()

        if options.statedir:
            # If we use a custom state directory, update various variables
            for key in config["keys"]:
                key["file"] = os.path.join(options.statedir, key["file"])

            for community in config["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["initialize"][
                        "working_directory"] = options.statedir

        if options.testnet:
            for community in config["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["class"] = "TrustChainTestnetCommunity"

        self.ipv8 = IPv8(config, enable_statistics=options.statistics)
        await self.ipv8.start()

        print("Starting AnyDex")

        if not options.no_rest_api:
            self.restapi = RESTManager(self.ipv8)
            await self.restapi.start(options.apiport)

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    await self.restapi.stop()
                    self.restapi = None
                await self.ipv8.stop()
                get_event_loop().stop()

        signal.signal(signal.SIGINT,
                      lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM,
                      lambda sig, _: ensure_future(signal_handler(sig)))

        # Get Trustchain + DHT overlays
        for overlay in self.ipv8.overlays:
            if isinstance(overlay,
                          (TrustChainCommunity, TrustChainTestnetCommunity)):
                self.trustchain = overlay
            elif isinstance(overlay, DHTDiscoveryCommunity):
                self.dht = overlay

        # Initialize wallets
        dummy_wallet1 = DummyWallet1()
        self.wallets[dummy_wallet1.get_identifier()] = dummy_wallet1

        dummy_wallet2 = DummyWallet2()
        self.wallets[dummy_wallet2.get_identifier()] = dummy_wallet2

        # Load market community
        self.market = MarketTestnetCommunity(
            self.trustchain.my_peer,
            self.ipv8.endpoint,
            self.ipv8.network,
            trustchain=self.trustchain,
            dht=self.dht,
            wallets=self.wallets,
            working_directory=options.statedir,
            record_transactions=False,
            is_matchmaker=not options.no_matchmaker)

        self.ipv8.overlays.append(self.market)
        self.ipv8.strategies.append((RandomWalk(self.market), 20))
Exemplo n.º 24
0
    def start(self, options, service):
        """
        Main method to startup the cli and add a signal handler.
        """

        msg("Service: Starting")

        self.service = service

        # State directory
        state_directory = options['statedir']
        util.create_directory_if_not_exists(state_directory)

        # port
        network_port = options['port']

        # Initial configuration
        configuration = get_default_configuration()
        configuration['address'] = "0.0.0.0"
        configuration['port'] = network_port
        configuration['keys'] = [{
            'alias':
            'my peer',
            'generation':
            u"curve25519",
            'file':
            os.path.join(state_directory, u"ec.pem")
        }]
        configuration['logger'] = {'level': "ERROR"}
        configuration['overlays'] = [{
            'class':
            'DiscoveryCommunity',
            'key':
            "my peer",
            'walkers': [{
                'strategy': 'RandomWalk',
                'peers': -1,
                'init': {
                    'timeout': 3.0
                }
            }, {
                'strategy': 'RandomChurn',
                'peers': -1,
                'init': {
                    'sample_size': 64,
                    'ping_interval': 1.0,
                    'inactive_time': 1.0,
                    'drop_time': 3.0
                }
            }],
            'initialize': {},
            'on_start': [('resolve_dns_bootstrap_addresses', )]
        }]
        configuration['overlays'] = []

        # IPv8 instance
        self.ipv8 = IPv8(configuration)

        # Network port
        actual_network_port = self.ipv8.endpoint.get_address()[1]

        # Peer
        self.my_peer = self.ipv8.keys.get('my peer')

        # Trustchain community
        self.trustchain_community = TrustChainTestnetCommunity(
            self.my_peer,
            self.ipv8.endpoint,
            self.ipv8.network,
            working_directory=state_directory)
        self.ipv8.overlays.append(self.trustchain_community)
        self.ipv8.strategies.append((EdgeWalk(self.trustchain_community), 10))

        # Event bus
        self.bus = EventBus()

        # module community
        self.module_community = ModuleCommunity(
            self.my_peer,
            self.ipv8.endpoint,
            self.ipv8.network,
            trustchain=self.trustchain_community,
            bus=self.bus,
            working_directory=state_directory,
            ipv8=self.ipv8,
            service=self.service)
        self.ipv8.overlays.append(self.module_community)
        self.ipv8.strategies.append((RandomWalk(self.module_community), 10))

        # CLI
        self.cli = CLI(self, self.ipv8, self.module_community)

        def signal_handler(sig, _):
            msg("Service: Received shut down signal %s" % sig)
            if not self._stopping:
                self.stop()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        self.rest_api = RESTManager(self.ipv8)
        self.rest_api.start(actual_network_port + 1000)
        self.rest_api.root_endpoint.putChild('module',
                                             ModuleRootEndpoint(self.ipv8))

        StandardIO(self.cli)
Exemplo n.º 25
0
    async def start(self):
        """
        Start a Tribler session by initializing the LaunchManyCore class, opening the database and running the upgrader.
        Returns a deferred that fires when the Tribler session is ready for use.

        :param config: a TriblerConfig object
        """
        self._logger.info("Session is using state directory: %s", self.config.get_state_dir())
        self.get_ports_in_config()
        self.create_state_directory_structure()
        self.init_keypair()

        # we have to represent `user_id` as a string to make it equal to the
        # `user_id` on the GUI side
        user_id_str = hexlify(self.trustchain_keypair.key.pk).encode('utf-8')
        SentryReporter.set_user(user_id_str)

        # Start the REST API before the upgrader since we want to send interesting upgrader events over the socket
        if self.config.get_api_http_enabled() or self.config.get_api_https_enabled():
            from tribler_core.restapi.rest_manager import RESTManager
            self.api_manager = RESTManager(self)
            self.readable_status = STATE_START_API
            await self.api_manager.start()

        if self.upgrader_enabled and not self.core_test_mode:
            from tribler_core.upgrade.upgrade import TriblerUpgrader
            self.upgrader = TriblerUpgrader(self)
            self.readable_status = STATE_UPGRADING_READABLE
            await self.upgrader.run()

        self.tracker_manager = TrackerManager(self)

        # Start torrent checker before Popularity community is loaded
        if self.config.get_torrent_checking_enabled() and not self.core_test_mode:
            from tribler_core.modules.torrent_checker.torrent_checker import TorrentChecker
            self.readable_status = STATE_START_TORRENT_CHECKER
            self.torrent_checker = TorrentChecker(self)
            await self.torrent_checker.initialize()

        # On Mac, we bundle the root certificate for the SSL validation since Twisted is not using the root
        # certificates provided by the system trust store.
        if sys.platform == 'darwin':
            os.environ['SSL_CERT_FILE'] = str(get_lib_path() / 'root_certs_mac.pem')

        if self.config.get_chant_enabled():
            from tribler_core.modules.metadata_store.store import MetadataStore
            channels_dir = self.config.get_chant_channels_dir()
            metadata_db_name = 'metadata.db' if not self.config.get_chant_testnet() else 'metadata_testnet.db'
            database_path = self.config.get_state_dir() / 'sqlite' / metadata_db_name
            self.mds = MetadataStore(database_path, channels_dir, self.trustchain_keypair,
                                     notifier=self.notifier,
                                     disable_sync=self.core_test_mode)
            if self.core_test_mode:
                generate_test_channels(self.mds)

        # IPv8
        if self.config.get_ipv8_enabled():
            from ipv8.configuration import ConfigBuilder
            from ipv8.messaging.interfaces.dispatcher.endpoint import DispatcherEndpoint
            ipv8_config_builder = (ConfigBuilder()
                                   .set_port(self.config.get_ipv8_port())
                                   .set_address(self.config.get_ipv8_address())
                                   .clear_overlays()
                                   .clear_keys()  # We load the keys ourselves
                                   .set_working_directory(str(self.config.get_state_dir()))
                                   .set_walker_interval(self.config.get_ipv8_walk_interval()))

            if self.core_test_mode:
                endpoint = DispatcherEndpoint([])
            else:
                # IPv8 includes IPv6 support by default.
                # We only load IPv4 to not kill all Tribler overlays (currently, it would instantly crash all users).
                # If you want to test IPv6 in Tribler you can set ``endpoint = None`` here.
                endpoint = DispatcherEndpoint(["UDPIPv4"], UDPIPv4={'port': self.config.get_ipv8_port(),
                                                                    'ip': self.config.get_ipv8_address()})
            self.ipv8 = IPv8(ipv8_config_builder.finalize(),
                             enable_statistics=self.config.get_ipv8_statistics() and not self.core_test_mode,
                             endpoint_override=endpoint)
            await self.ipv8.start()

            self.config.set_anon_proxy_settings(2, ("127.0.0.1",
                                                    self.
                                                    config.get_tunnel_community_socks5_listen_ports()))
            self.ipv8_start_time = timemod.time()
            self.load_ipv8_overlays()
            if not self.core_test_mode:
                self.enable_ipv8_statistics()
            if self.api_manager:
                self.api_manager.set_ipv8_session(self.ipv8)
            if self.config.get_tunnel_community_enabled():
                await self.tunnel_community.wait_for_socks_servers()
            if self.config.get_ipv8_walk_scaling_enabled():
                from tribler_core.modules.ipv8_health_monitor import IPv8Monitor
                IPv8Monitor(self.ipv8,
                            self.config.get_ipv8_walk_interval(),
                            self.config.get_ipv8_walk_scaling_upper_limit()).start(self)

        # Note that currently we should only start libtorrent after the SOCKS5 servers have been started
        if self.config.get_libtorrent_enabled():
            self.readable_status = STATE_START_LIBTORRENT
            from tribler_core.modules.libtorrent.download_manager import DownloadManager
            self.dlmgr = DownloadManager(self, dummy_mode=self.core_test_mode)
            self.dlmgr.initialize()
            self.readable_status = STATE_LOAD_CHECKPOINTS
            await self.dlmgr.load_checkpoints()
            if self.core_test_mode:
                await self.dlmgr.start_download_from_uri("magnet:?xt=urn:btih:0000000000000000000000000000000000000000")
        self.readable_status = STATE_READABLE_STARTED

        if self.config.get_watch_folder_enabled():
            from tribler_core.modules.watch_folder import WatchFolder
            self.readable_status = STATE_START_WATCH_FOLDER
            self.watch_folder = WatchFolder(self)
            self.watch_folder.start()

        if self.config.get_resource_monitor_enabled() and not self.core_test_mode:
            from tribler_core.modules.resource_monitor.core import CoreResourceMonitor
            self.resource_monitor = CoreResourceMonitor(self)
            self.resource_monitor.start()

        if self.config.get_version_checker_enabled() and not self.core_test_mode:
            from tribler_core.modules.versioncheck_manager import VersionCheckManager
            self.version_check_manager = VersionCheckManager(self)
            self.version_check_manager.start()

        if self.config.get_ipv8_enabled():
            from tribler_core.modules.payout_manager import PayoutManager
            self.payout_manager = PayoutManager(self.bandwidth_community, self.dht_community)

            if self.core_test_mode:
                from ipv8.messaging.interfaces.udp.endpoint import UDPv4Address
                from ipv8.dht.routing import RoutingTable
                self.dht_community.routing_tables[UDPv4Address] = RoutingTable('\x00' * 20)

        # GigaChannel Manager should be started *after* resuming the downloads,
        # because it depends on the states of torrent downloads
        if self.config.get_chant_enabled() and self.config.get_chant_manager_enabled() \
                and self.config.get_libtorrent_enabled:
            from tribler_core.modules.metadata_store.gigachannel_manager import GigaChannelManager
            self.gigachannel_manager = GigaChannelManager(self)
            if not self.core_test_mode:
                self.gigachannel_manager.start()

        if self.config.get_bootstrap_enabled() and not self.core_test_mode:
            self.register_task('bootstrap_download', self.start_bootstrap_download)

        self.notifier.notify(NTFY.TRIBLER_STARTED, self.trustchain_keypair.key.pk)

        # If there is a config error, report to the user via GUI notifier
        if self.config.config_error:
            self.notifier.notify(NTFY.REPORT_CONFIG_ERROR, self.config.config_error)
Exemplo n.º 26
0
    async def start_crawler(self, statedir, apiport, no_rest_api, testnet,
                            yappi):
        """
        Main method to startup the TrustChain crawler.
        """

        # Open the database
        self.tc_persistence = TrustChainDB(statedir, 'trustchain')

        if statedir:
            # If we use a custom state directory, update various variables
            for key in crawler_config["keys"]:
                key["file"] = os.path.join(statedir, key["file"])

            for community in crawler_config["overlays"]:
                if community[
                        "class"] == "TrustChainCrawlerCommunity" or community[
                            "class"] == "TrustChainBackwardsCrawlerCommunity":
                    community["initialize"][
                        "persistence"] = self.tc_persistence

        if testnet:
            for community in crawler_config["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["class"] = "TrustChainTestnetCommunity"

        extra_communities = {
            "TrustChainCrawlerCommunity":
            TrustChainCrawlerCommunity,
            "TrustChainBackwardsCrawlerCommunity":
            TrustChainBackwardsCrawlerCommunity
        }
        self.ipv8 = IPv8(crawler_config, extra_communities=extra_communities)

        await self.ipv8.start()

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    await self.restapi.stop()
                await self.ipv8.stop()

                if yappi:
                    yappi.stop()
                    print("Yappi has shutdown")
                    yappi_stats = yappi.get_func_stats()
                    yappi_stats.sort("tsub")
                    out_file = 'yappi'
                    if statedir:
                        out_file = os.path.join(statedir, out_file)
                    yappi_stats.save(out_file, type='callgrind')

                await gather(*all_tasks())
                get_event_loop().stop()

        signal.signal(signal.SIGINT,
                      lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM,
                      lambda sig, _: ensure_future(signal_handler(sig)))

        print("Starting TrustChain crawler")

        if not no_rest_api:
            self.restapi = RESTManager(self.ipv8)
            await self.restapi.start(apiport)

        if yappi:
            yappi.start(builtins=True)
Exemplo n.º 27
0

_COMMUNITIES['MyCommunity'] = MyCommunity

for i in [1, 2]:
    configuration = get_default_configuration()
    configuration['keys'] = [{
        'alias': "my peer",
        'generation': u"medium",
        'file': u"ec%d.pem" % i
    }]
    configuration['port'] = 12000 + randint(0, 10000)
    configuration['overlays'] = [{
        'class':
        'MyCommunity',
        'key':
        "my peer",
        'walkers': [{
            'strategy': "RandomWalk",
            'peers': 10,
            'init': {
                'timeout': 3.0
            }
        }],
        'initialize': {},
        'on_start': [('started', )]
    }]
    INSTANCES.append(IPv8(configuration))

reactor.run()
Exemplo n.º 28
0
# Override the Community master peers so we don't interfere with the live network
# Also hook in our custom logic for introduction responses
for community_cls in _COMMUNITIES.values():
    community_cls.master_peer = Peer(ECCrypto().generate_key(u"medium"))
    community_cls.introduction_response_callback = custom_intro_response_cb

# Create two peers with separate working directories
previous_workdir = getcwd()
for i in [1, 2]:
    configuration = get_default_configuration()
    configuration['port'] = 12000 + randint(0, 10000)
    configuration['logger']['level'] = "CRITICAL"
    for overlay in configuration['overlays']:
        overlay['walkers'] = [
            walker for walker in overlay['walkers']
            if walker['strategy'] in _WALKERS
        ]
    workdir = path.abspath(path.join(path.dirname(__file__), "%d" % i))
    if not path.exists(workdir):
        mkdir(workdir)
    chdir(workdir)
    IPv8(configuration)
    chdir(previous_workdir)

# Actually start running everything, this blocks until the experiment finishes
reactor.callLater(30.0, on_timeout)
reactor.run()

# Print the introduction times for all default Communities, sorted alphabetically.
print(','.join(['%.4f' % RESULTS[key] for key in sorted(RESULTS)]))