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)
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