Esempio n. 1
0
async def _unix_handle_request() -> asyncio.events.AbstractServer:
    """
    Listens for request from frontend and sends response over unix socket

    :return: An asyncio Server for Unix sockets
    """

    try:
        os.unlink(
            os.path.join(get_full_init_directory(), FRONTEND_UNIX_ADDRESS), )
    except OSError:
        # does not yet exist, do nothing
        pass

    return await asyncio.start_unix_server(
        async_handle_request,
        path=os.path.join(
            get_full_init_directory(),
            FRONTEND_UNIX_ADDRESS,
        ),
    )
Esempio n. 2
0
async def load_config_file() -> Dict[str, Any]:
    """
    Read and parse the Drop Peer Store config

    :raises MissingConfigError: If the dps config cannot be found
    :return: Parsed dict of config file contents
    """
    init_directory = get_full_init_directory(None)
    dps_config_path = os.path.join(init_directory, DEFAULT_DPS_CONFIG_FILE)

    if not os.path.isfile(dps_config_path):
        raise MissingConfigError()

    async with aiofiles.open(dps_config_path) as f:
        config_txt = await f.read()
        config_file = json.loads(config_txt)

    return config_file
Esempio n. 3
0
def main() -> None:
    args = parser().parse_args()

    config = {
        'type': 'tracker',
        'ip': args.ip,
        'port': args.port,
    }

    init_directory = get_full_init_directory(None)
    pks_config_path = os.path.join(init_directory, DEFAULT_PKS_CONFIG_FILE)
    dps_config_path = os.path.join(init_directory, DEFAULT_DPS_CONFIG_FILE)

    with open(pks_config_path, 'w') as f:
        json.dump(config, f, ensure_ascii=False)
    print("PublicKeyStore Tracker Config file created at: {}".format(
        pks_config_path))

    with open(dps_config_path, 'w') as f:
        json.dump(config, f, ensure_ascii=False)
    print("DropPeerStore Tracker Config file created at: {}".format(
        dps_config_path))
Esempio n. 4
0
def _unix_send_message(msg):
    """
    Sends message to backend over unix socket and awaits a response
    """

    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    s.settimeout(TIMEOUT)
    s.connect(os.path.join(get_full_init_directory(), FRONTEND_UNIX_ADDRESS))

    # Send request
    s.sendall(msg)
    s.shutdown(socket.SHUT_WR)

    # Read response from backend
    response = b''
    while True:
        data = s.recv(BUFFER_SIZE)
        if not data:
            break
        else:
            response += data

    s.close()
    return response
Esempio n. 5
0
def main() -> None:
    args = parser().parse_args()

    iplist = []  # type: List[str]
    portlist = []  # type: List[int]
    if args.bootstrap_peers is not None:
        peerlist = args.bootstrap_peers.split(',')
        try:
            iplist = list(map(lambda x: x.split(':')[0], peerlist))

            portlist = list(map(lambda x: int(x.split(':')[1]), peerlist))

        except IndexError:
            print("Must have at least 1 bootstrap ip")
            exit(1)

    config = {
        'type': 'dht',
        'bootstrap_ips': iplist,
        'bootstrap_ports': portlist,
        'listen_port': args.listenport,
    }
    logger.info("Created DHT config file")
    init_directory = get_full_init_directory(None)
    pks_config_path = os.path.join(init_directory, DEFAULT_PKS_CONFIG_FILE)
    dps_config_path = os.path.join(init_directory, DEFAULT_DPS_CONFIG_FILE)

    with open(pks_config_path, 'w') as f:
        json.dump(config, f, ensure_ascii=False)
    print("PublicKeyStore DHT Config file created at: {}".format(
        pks_config_path))

    with open(dps_config_path, 'w') as f:
        json.dump(config, f, ensure_ascii=False)
    print(
        "DropPeerStore DHT Config file created at: {}".format(dps_config_path))
Esempio n. 6
0
async def get_pub_key(node_id: bytes) -> crypto_util.rsa.RSAPublicKey:
    """
    Gets the public key from disk if possible otherwise request it from
    PublicKeyStore

    :param node_id: bytes for the node you want public key of
    :raises VerificationException: If the pub key cannot be retrieved
    :return: PublicKey
    """
    init_directory = get_full_init_directory(None)
    pub_key_directory = os.path.join(
        init_directory,
        DEFAULT_PUB_KEY_LOOKUP_LOCATION,
    )

    if not os.path.isdir(pub_key_directory):
        os.makedirs(pub_key_directory)

    key_file_name = "{}.pub".format(crypto_util.b64encode(node_id))
    key_path = os.path.join(pub_key_directory, key_file_name)

    if os.path.isfile(key_path):
        async with aiofiles.open(key_path, 'rb') as pub_file:
            pub_key = await pub_file.read()
            return load_public_key(pub_key)
    else:
        key_bytes = await load_private_key_from_disk()
        this_node_id = await node_id_from_private_key(key_bytes)
        public_key_store = await get_public_key_store(this_node_id)
        key_request = await public_key_store.request_key(node_id)
        if key_request[0] and key_request[1] is not None:
            pub_key = key_request[1].encode('utf-8')
            await _save_key_to_disk(key_path, pub_key)
            return load_public_key(pub_key)
        else:
            raise VerificationException()
Esempio n. 7
0
def _get_save_path() -> str:
    node_info_path = node_init.get_full_init_directory()
    save_path = os.path.join(node_info_path, DEFAULT_METADATA_LOOKUP_LOCATION)
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    return save_path