コード例 #1
0
async def test_dhtnode_blacklist():
    node1 = await hivemind.DHTNode.create(blacklist_time=999)
    node2 = await hivemind.DHTNode.create(
        blacklist_time=999, initial_peers=[f"{LOCALHOST}:{node1.port}"])
    node3 = await hivemind.DHTNode.create(
        blacklist_time=999, initial_peers=[f"{LOCALHOST}:{node1.port}"])
    node4 = await hivemind.DHTNode.create(
        blacklist_time=999, initial_peers=[f"{LOCALHOST}:{node1.port}"])

    assert await node2.store('abc',
                             123,
                             expiration_time=hivemind.get_dht_time() + 99)
    assert len(node2.blacklist.ban_counter) == 0

    await node3.shutdown()
    await node4.shutdown()

    assert await node2.store('def',
                             456,
                             expiration_time=hivemind.get_dht_time() + 99)

    assert len(node2.blacklist.ban_counter) == 2

    for banned_peer in node2.blacklist.ban_counter:
        assert any(
            banned_peer.endswith(str(port))
            for port in [node3.port, node4.port])

    node3_endpoint = await node3.protocol.get_outgoing_request_endpoint(
        f"{hivemind.LOCALHOST}:{node1.port}")
    node3_endpoint = replace_port(node3_endpoint, node3.port)
    assert await node1.get(
        'abc', latest=True
    )  # force node1 to crawl dht and discover unresponsive peers
    assert node3_endpoint in node1.blacklist

    node2_endpoint = await node2.protocol.get_outgoing_request_endpoint(
        f"{hivemind.LOCALHOST}:{node1.port}")
    node2_endpoint = replace_port(node2_endpoint, node2.port)
    assert await node1.get(
        'abc', latest=True
    )  # force node1 to crawl dht and discover unresponsive peers
    assert node2_endpoint not in node1.blacklist
コード例 #2
0
def _server_runner(pipe, *args, **kwargs):
    server = Server.create(*args, start=True, **kwargs)
    try:
        if server.dht is not None:
            dht_listen_on = hivemind.replace_port(server.dht.listen_on, server.dht.port)
        else:
            dht_listen_on = None
        pipe.send((server.listen_on, dht_listen_on))
        pipe.recv()  # wait for shutdown signal
    finally:
        logger.info("Shutting down server...")
        server.shutdown()
        server.join()
        logger.info("Server shut down.")
コード例 #3
0
ファイル: __init__.py プロジェクト: systemshift/hivemind
def _server_runner(pipe, *args, **kwargs):
    try:
        server = Server.create(*args, start=True, **kwargs)
    except Exception as e:
        logger.exception(
            f"Encountered an exception when starting a server: {e}")
        pipe.send((False, f'{type(e).__name__} {e}'))
        return

    try:
        if server.dht is not None:
            dht_listen_on = hivemind.replace_port(server.dht.listen_on,
                                                  server.dht.port)
        else:
            dht_listen_on = None
        pipe.send((True, (server.listen_on, dht_listen_on)))
        pipe.recv()  # wait for shutdown signal

    finally:
        logger.info("Shutting down server...")
        server.shutdown()
        server.join()
        logger.info("Server shut down.")