コード例 #1
0
    def start_tracker(self, options):
        """
        Main method to startup the tracker.
        """
        self.endpoint = UDPEndpoint(options["listen_port"])
        self.endpoint.open()
        self.overlay = EndpointServer(self.endpoint)

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

                def close_ep():
                    self.endpoint.close().addCallback(
                        lambda *args, **kwargs: reactor.callFromThread(reactor.
                                                                       stop))

                self.overlay.unload()
                close_ep()

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

        msg("Started tracker")
コード例 #2
0
    def test_tunnel_data(self):
        """
        Check if data is correctly exited.
        """
        # Listen in on communication of the target
        self.public_endpoint = UDPEndpoint(8080)
        self.public_endpoint.open()
        ep_listener = MockEndpointListener(self.public_endpoint)

        # Build a tunnel
        self.nodes[1].overlay.settings.become_exitnode = True
        yield self.introduce_nodes()
        self.nodes[0].overlay.build_tunnels(1)
        yield self.deliver_messages()

        # Construct a data packet
        prefix = '\x00' * 23
        data = prefix + ''.join([chr(i) for i in range(256)])

        self.public_endpoint.assert_open()

        # Tunnel the data to the endpoint
        circuit = self.nodes[0].overlay.circuits.values()[0]
        self.nodes[0].overlay.send_data(
            [circuit.sock_addr], circuit.circuit_id,
            ('localhost', self.public_endpoint.get_address()[1]),
            ('0.0.0.0', 0), data)
        # This is not test communication, but actual socket communication, we can't do a smart sleep
        yield self.sleep()

        self.assertEqual(len(ep_listener.received_packets), 1)
        self.assertEqual(ep_listener.received_packets[0][1], data)
コード例 #3
0
ファイル: tracker_plugin.py プロジェクト: kozlovsky/py-ipv8
    async def start_tracker(self, listen_port):
        """
        Main method to startup the tracker.
        """
        self.endpoint = UDPEndpoint(listen_port)
        await self.endpoint.open()
        self.overlay = EndpointServer(self.endpoint)

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self.stopping:
                self.stopping = True
                await self.overlay.unload()
                self.endpoint.close()
                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("Started tracker")
コード例 #4
0
ファイル: ipv8_service.py プロジェクト: vandenheuvel/py-ipv8
        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)
                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:
                                # 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 = []

            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)
コード例 #5
0
ファイル: ipv8_service.py プロジェクト: teddyk251/py-ipv8
 async def produce_anonymized_endpoint(self):
     base_endpoint = UDPEndpoint(port=0,
                                 ip=self.configuration['address'])
     await base_endpoint.open()
     return TunnelEndpoint(base_endpoint)