Beispiel #1
0
        def __init__(self, configuration, endpoint_override=None, enable_statistics=False, extra_communities=None):
            if endpoint_override:
                self.endpoint = endpoint_override
            else:
                self.endpoint = UDPEndpoint(port=configuration['port'], ip=configuration['address'])
                self.endpoint.open()
                if enable_statistics:
                    self.endpoint = StatisticsEndpoint(self, self.endpoint)

            self.network = Network()

            # Load/generate keys
            self.keys = {}
            for key_block in configuration['keys']:
                if key_block['file'] and isfile(key_block['file']):
                    with open(key_block['file'], 'r') as f:
                        content = f.read()
                        try:
                            # IPv8 Standardized bin format
                            self.keys[key_block['alias']] = Peer(default_eccrypto.key_from_private_bin(content))
                        except ValueError:
                            try:
                                # Try old Tribler M2Crypto PEM format
                                content = b64decode(content[31:-30].replace('\n', ''))
                                peer = Peer(M2CryptoSK(keystring=content))
                                peer.mid # This will error out if the keystring is not M2Crypto
                                self.keys[key_block['alias']] = peer
                            except:
                                # Try old LibNacl format
                                content = "LibNaCLSK:" + content
                                self.keys[key_block['alias']] = Peer(default_eccrypto.key_from_private_bin(content))
                else:
                    self.keys[key_block['alias']] = Peer(default_eccrypto.generate_key(key_block['generation']))
                    if key_block['file']:
                        with open(key_block['file'], 'w') as f:
                            f.write(self.keys[key_block['alias']].key.key_to_bin())

            # Setup logging
            logging.basicConfig(**configuration['logger'])

            self.overlay_lock = RLock()
            self.strategies = []
            self.overlays = []

            for overlay in configuration['overlays']:
                overlay_class = _COMMUNITIES.get(overlay['class'], (extra_communities or {}).get(overlay['class']))
                my_peer = self.keys[overlay['key']]
                overlay_instance = overlay_class(my_peer, self.endpoint, self.network, **overlay['initialize'])
                self.overlays.append(overlay_instance)
                for walker in overlay['walkers']:
                    strategy_class = _WALKERS.get(walker['strategy'],
                                                  overlay_instance.get_available_strategies().get(walker['strategy']))
                    args = walker['init']
                    target_peers = walker['peers']
                    self.strategies.append((strategy_class(overlay_instance, **args), target_peers))
                for config in overlay['on_start']:
                    reactor.callWhenRunning(getattr(overlay_instance, config[0]), *config[1:])

            self.state_machine_lc = LoopingCall(self.on_tick)
            self.state_machine_lc.start(configuration['walker_interval'], False)
Beispiel #2
0
    async def run(self):
        await super().run()
        await self.get_component(ReporterComponent)

        self._ipv8_component = await self.require_component(Ipv8Component)
        metadata_store_component = await self.require_component(
            MetadataStoreComponent)
        torrent_checker_component = await self.require_component(
            TorrentCheckerComponent)

        config = self.session.config
        community = PopularityCommunity(
            self._ipv8_component.peer,
            self._ipv8_component.ipv8.endpoint,
            Network(),
            settings=config.popularity_community,
            rqc_settings=config.remote_query_community,
            metadata_store=metadata_store_component.mds,
            torrent_checker=torrent_checker_component.torrent_checker)
        self.community = community

        self._ipv8_component.initialise_community_by_default(
            community, default_random_walk_max_peers=30)
        self._ipv8_component.ipv8.add_strategy(community,
                                               RemovePeers(community),
                                               INFINITE)
    async def run(self):
        await super().run()
        await self.get_component(ReporterComponent)

        config = self.session.config
        notifier = self.session.notifier

        self._ipv8_component = await self.require_component(Ipv8Component)
        metadata_store_component = await self.require_component(
            MetadataStoreComponent)
        tag_component = await self.get_component(TagComponent)

        giga_channel_cls = GigaChannelTestnetCommunity if config.general.testnet else GigaChannelCommunity
        community = giga_channel_cls(
            self._ipv8_component.peer,
            self._ipv8_component.ipv8.endpoint,
            Network(),
            notifier=notifier,
            settings=config.chant,
            rqc_settings=config.remote_query_community,
            metadata_store=metadata_store_component.mds,
            max_peers=50,
            tags_db=tag_component.tags_db if tag_component else None)
        self.community = community
        self._ipv8_component.initialise_community_by_default(
            community, default_random_walk_max_peers=30)
        self._ipv8_component.ipv8.add_strategy(community,
                                               RemovePeers(community),
                                               INFINITE)
Beispiel #4
0
 def __init__(self, endpoint):
     my_peer = Peer(default_eccrypto.generate_key(u"very-low"))
     self.signature_length = default_eccrypto.get_signature_length(
         my_peer.public_key)
     super(EndpointServer, self).__init__(my_peer, endpoint, Network())
     self.churn_strategy = SimpleChurn(self)
     self.churn_task = self.register_task("churn",
                                          self.churn_strategy.take_step,
                                          interval=30)
Beispiel #5
0
 def __init__(self, endpoint):
     my_peer = Peer(default_eccrypto.generate_key(u"very-low"))
     self.signature_length = default_eccrypto.get_signature_length(
         my_peer.public_key)
     super(EndpointServer, self).__init__(my_peer, endpoint, Network())
     self.churn_strategy = SimpleChurn(self)
     self.churn_lc = self.register_task(
         "churn",
         LoopingCall(self.churn_strategy.take_step)).start(30.0, now=False)
Beispiel #6
0
    def __init__(self, crypto_curve, overlay_class, *args, **kwargs):
        self.endpoint = AutoMockEndpoint()
        self.endpoint.open()
        self.network = Network()
        self.my_peer = Peer(ECCrypto().generate_key(crypto_curve), self.endpoint.wan_address)
        self.overlay = overlay_class(self.my_peer, self.endpoint, self.network, *args, **kwargs)
        self.discovery = MockWalk(self.overlay)

        self.overlay.my_estimated_wan = self.endpoint.wan_address
        self.overlay.my_estimated_lan = self.endpoint.lan_address
Beispiel #7
0
    async def sanitize_network(self, session):
        # We disable the discovery communities in this session since we don't want to walk to the live network
        for overlay in session.ipv8.overlays:
            if isinstance(overlay, DiscoveryCommunity):
                await overlay.unload()
        session.ipv8.overlays = []
        session.ipv8.strategies = []

        # Also reset the IPv8 network
        session.ipv8.network = Network()
Beispiel #8
0
 def __init__(self, endpoint):
     my_peer = Peer(default_eccrypto.generate_key(u"very-low"))
     self.signature_length = default_eccrypto.get_signature_length(
         my_peer.public_key)
     super(EndpointServer, self).__init__(my_peer, endpoint, Network())
     self.endpoint.add_listener(
         self
     )  # Listen to all incoming packets (not just the fake community_id).
     self.churn_strategy = SimpleChurn(self)
     self.churn_task = self.register_task("churn",
                                          self.churn_strategy.take_step,
                                          interval=30)
Beispiel #9
0
    def cache_exitnodes_to_disk(self):
        """
        Write a copy of the exit_candidates to the file self.exitnode_cache.

        :returns: None
        """
        exit_nodes = Network()
        for peer in self.get_candidates(PEER_FLAG_EXIT_BT):
            exit_nodes.add_verified_peer(peer)
        self.logger.debug('Writing exit nodes to cache: %s',
                          self.exitnode_cache)
        with open(self.exitnode_cache, 'wb') as cache:
            cache.write(exit_nodes.snapshot())
Beispiel #10
0
    def __init__(self, my_peer, endpoint, network, metadata_store, **kwargs):
        kwargs["settings"] = kwargs.get("settings", GigaChannelCommunitySettings())
        self.notifier = kwargs.pop("notifier", None)

        # ACHTUNG! We create a separate instance of Network for this community because it
        # walks aggressively and wants lots of peers, which can interfere with other communities
        super().__init__(my_peer, endpoint, Network(), metadata_store, **kwargs)

        # This set contains all the peers that we queried for subscribed channels over time.
        # It is emptied regularly. The purpose of this set is to work as a filter so we never query the same
        # peer twice. If we do, this should happen really rarely
        # TODO: use Bloom filter here instead. We actually *want* it to be all-false-positives eventually.
        self.queried_peers = set()

        self.discovery_booster = DiscoveryBooster(timeout_in_sec=30)
        self.discovery_booster.apply(self)
Beispiel #11
0
    def restore_exitnodes_from_disk(self):
        """
        Send introduction requests to peers stored in the file self.exitnode_cache.

        :returns: None
        """
        if self.exitnode_cache.is_file():
            self.logger.debug('Loading exit nodes from cache: %s',
                              self.exitnode_cache)
            exit_nodes = Network()
            with self.exitnode_cache.open('rb') as cache:
                exit_nodes.load_snapshot(cache.read())
            for exit_node in exit_nodes.get_walkable_addresses():
                self.endpoint.send(exit_node,
                                   self.create_introduction_request(exit_node))
        else:
            self.logger.warning(
                'Could not retrieve backup exitnode cache, file does not exist!'
            )
Beispiel #12
0
    def __init__(self, my_peer, endpoint, network, **kwargs):
        self.torrent_checker = kwargs.pop('torrent_checker', None)

        # Creating a separate instance of Network for this community to find more peers
        super().__init__(my_peer, endpoint, Network(), **kwargs)

        self.add_message_handler(TorrentsHealthPayload,
                                 self.on_torrents_health)

        self.logger.info('Popularity Community initialized (peer mid %s)',
                         hexlify(self.my_peer.mid))
        self.register_task(
            "gossip_popular_torrents",
            self.gossip_popular_torrents_health,
            interval=PopularityCommunity.GOSSIP_INTERVAL_FOR_POPULAR_TORRENTS)
        self.register_task(
            "gossip_random_torrents",
            self.gossip_random_torrents_health,
            interval=PopularityCommunity.GOSSIP_INTERVAL_FOR_RANDOM_TORRENTS)
Beispiel #13
0
 def __init__(self, endpoint):
     my_peer = Peer(ECCrypto().generate_key(u"very-low"))
     super(EndpointServer, self).__init__(my_peer, endpoint, Network())
     self.churn_lc = self.register_task("churn", LoopingCall(self.do_churn)).start(5.0, now=False)
     self.churn_strategy = RandomChurn(self)
     self.crypto = ECCrypto()
Beispiel #14
0
 def setUp(self):
     self.network = Network()
Beispiel #15
0
 def __init__(self):
     self.fetch_next_called = False
     self.send_random_to_called = []
     self.get_peers_return = []
     self.network = Network()
Beispiel #16
0
        def __init__(self,
                     configuration,
                     endpoint_override=None,
                     enable_statistics=False,
                     extra_communities=None):
            super(IPv8, self).__init__()
            self.configuration = configuration

            if endpoint_override:
                self.endpoint = endpoint_override
            else:
                self.endpoint = DispatcherEndpoint(["UDPIPv4"],
                                                   UDPIPv4={
                                                       'port':
                                                       configuration['port'],
                                                       'ip':
                                                       configuration['address']
                                                   })
            if enable_statistics:
                self.endpoint = StatisticsEndpoint(self.endpoint)
            if any([
                    overlay.get('initialize', {}).get('anonymize')
                    for overlay in configuration['overlays']
            ]):
                self.endpoint = TunnelEndpoint(self.endpoint)

            self.network = Network()

            # Load/generate keys
            self.keys = {}
            for key_block in configuration['keys']:
                if key_block['file'] and isfile(key_block['file']):
                    with open(key_block['file'], 'rb') as f:
                        content = f.read()
                        try:
                            # IPv8 Standardized bin format
                            self.keys[key_block['alias']] = Peer(
                                default_eccrypto.key_from_private_bin(content))
                        except ValueError:
                            try:
                                # Try old Tribler M2Crypto PEM format
                                content = b64decode(content[31:-30].replace(
                                    '\n', ''))
                                peer = Peer(M2CryptoSK(keystring=content))
                                peer.mid  # This will error out if the keystring is not M2Crypto
                                self.keys[key_block['alias']] = peer
                            except Exception:
                                # Try old LibNacl format
                                content = "LibNaCLSK:" + content
                                self.keys[key_block['alias']] = Peer(
                                    default_eccrypto.key_from_private_bin(
                                        content))
                else:
                    self.keys[key_block['alias']] = Peer(
                        default_eccrypto.generate_key(key_block['generation']))
                    if key_block['file']:
                        with open(key_block['file'], 'wb') as f:
                            f.write(
                                self.keys[key_block['alias']].key.key_to_bin())

            # Setup logging
            logging.basicConfig(**configuration['logger'])

            self.overlay_lock = RLock()
            self.strategies = []
            self.overlays = []
            self.on_start = []

            for overlay in configuration['overlays']:
                overlay_class = _COMMUNITIES.get(overlay['class'],
                                                 (extra_communities
                                                  or {}).get(overlay['class']))
                my_peer = self.keys[overlay['key']]
                overlay_instance = overlay_class(my_peer, self.endpoint,
                                                 self.network,
                                                 **overlay['initialize'])
                self.overlays.append(overlay_instance)
                for walker in overlay['walkers']:
                    strategy_class = _WALKERS.get(
                        walker['strategy'],
                        overlay_instance.get_available_strategies().get(
                            walker['strategy']))
                    args = walker['init']
                    target_peers = walker['peers']
                    self.strategies.append(
                        (strategy_class(overlay_instance,
                                        **args), target_peers))
                for config in overlay['on_start']:
                    self.on_start.append((getattr(overlay_instance,
                                                  config[0]), config[1:]))
                for bootstrapper in overlay['bootstrappers']:
                    bootstrapper_class = _BOOTSTRAPPERS.get(
                        bootstrapper['class'])
                    if bootstrapper_class:
                        overlay_instance.bootstrappers.append(
                            bootstrapper_class(**bootstrapper['init']))
            self.walk_interval = configuration['walker_interval']
            self.state_machine_task = None
Beispiel #17
0
 def __init__(self):
     endpoint = AutoMockEndpoint()
     endpoint.open()
     network = Network()
     peer = Peer(ECCrypto().generate_key(u"very-low"), endpoint.wan_address)
     super(MockCommunity, self).__init__(peer, endpoint, network)
Beispiel #18
0
 def __init__(self):
     self.exit_candidates = []
     self.network = Network()
Beispiel #19
0
 def __init__(self):
     self.exit_candidates = []
     self.candidates = {}
     self.network = Network()
     self.send_introduction_request = Mock()