(args)
Listen to API messages received over satellite
Listen to API messages received over satellite
Listen to API messages received over satellite
def listen(args):
    """Listen to API messages received over satellite"""
    gnupghome = os.path.join(args.cfg_dir, args.gnupghome)
    download_dir = os.path.join(args.cfg_dir, "api", "downloads")

    # Override some options based on the mutual exclusive --gossip and
    # --btc-src arguments, if present
    if (args.gossip):
        channel = ApiChannel.GOSSIP.value
        server_addr = server_map['gossip']
        args.plaintext = True
    elif (args.btc_src):
        channel = ApiChannel.BTC_SRC.value
        server_addr = server_map['btc-src']
        args.plaintext = True
    else:
        channel = args.channel
        server_addr = _get_server_addr(args.net, args.server)

    historian_cli = os.path.join(args.historian_path, 'historian-cli') \
        if (args.historian_path) else 'historian-cli'

    # Argument validation and special cases
    if (args.gossip):
        if (which(historian_cli) is None):
            logger.error("Option --gossip requires the historian-cli "
                         "application but could not find it")
            logger.info("Use option --historian-path to specify the path to "
                        "historian-cli")
            raise ValueError("Could not find the historian-cli application")

        if (args.stdout):
            raise ValueError("Argument --gossip is not allowed with argument "
                             "--stdout")
        elif (args.no_save):
            raise ValueError("Argument --gossip is not allowed with argument "
                             "--no-save")

        gossip_opts = {
            'cli': historian_cli,
            'dest': args.historian_destination
        }
    else:
        gossip_opts = None

    _warn_common_overrides(args)  # warn args overridden by --gossip/--btc-src

    if (args.exec):
        # Do not support the option in plaintext mode
        if (args.plaintext):
            raise ValueError("Option --exec is not allowed in plaintext mode")

        # Require either --sender or --insecure
        if (args.sender is None and not args.insecure):
            raise ValueError("Option --exec requires a specified --sender")

        # Warn about unsafe commands
        base_exec_cmd = shlex.split(args.exec)[0]
        unsafe_cmds = ["/bin/bash", "bash", "/bin/sh", "sh", "rm"]
        if (base_exec_cmd in unsafe_cmds):
            logger.warning("Running {} on --exec is considered unsafe".format(
                base_exec_cmd))

    # GPG wrapper object
    if (not args.plaintext or args.sender):
        gpg = Gpg(gnupghome, interactive=True)
        config_keyring(gpg)
    else:
        gpg = None

    # Define the interface used to listen for messages
    if (args.interface):
        interface = args.interface
    elif (args.demo):
        interface = "lo"
    else:
        # Infer the interface based on the user's setup
        user_info = blocksatcli_config.read_cfg_file(args.cfg, args.cfg_dir)
        interface = blocksatcli_config.get_net_if(user_info)
    logger.info("Listening on interface: {}".format(interface))
    logger.info("Downloads will be saved at: {}".format(download_dir))

    # A passphrase is required for decryption (but not for signature
    # verification)
    if (not args.plaintext and not args.no_password):
        gpg.prompt_passphrase('GPG keyring password for decryption: ')

    # Listen continuously
    listen_loop = ApiListener()
    listen_loop.run(gpg,
                    download_dir,
                    args.sock_addr,
                    interface,
                    channel,
                    args.plaintext,
                    args.save_raw,
                    sender=args.sender,
                    stdout=args.stdout,
                    no_save=args.no_save,
                    echo=args.echo,
                    exec_cmd=args.exec,
                    gossip_opts=gossip_opts,
                    server_addr=server_addr,
                    tls_cert=args.tls_cert,
                    tls_key=args.tls_key,
                    region=args.region)
def listen ( args ) : gnupghome = os . path . join ( args . cfg_dir , args . gnupghome ) download_dir = os . path . join ( args . cfg_dir , "api" , "downloads" ) # Override some options based on the mutual exclusive --gossip and # --btc-src arguments, if present if ( args . gossip ) : channel = ApiChannel . GOSSIP . value server_addr = server_map [ 'gossip' ] args . plaintext = True elif ( args . btc_src ) : channel = ApiChannel . BTC_SRC . value server_addr = server_map [ 'btc-src' ] args . plaintext = True else : channel = args . channel server_addr = _get_server_addr ( args . net , args . server ) historian_cli = os . path . join ( args . historian_path , 'historian-cli' ) if ( args . historian_path ) else 'historian-cli' # Argument validation and special cases if ( args . gossip ) : if ( which ( historian_cli ) is None ) : logger . error ( "Option --gossip requires the historian-cli " "application but could not find it" ) logger . info ( "Use option --historian-path to specify the path to " "historian-cli" ) raise ValueError ( "Could not find the historian-cli application" ) if ( args . stdout ) : raise ValueError ( "Argument --gossip is not allowed with argument " "--stdout" ) elif ( args . no_save ) : raise ValueError ( "Argument --gossip is not allowed with argument " "--no-save" ) gossip_opts = { 'cli' : historian_cli , 'dest' : args . historian_destination } else : gossip_opts = None _warn_common_overrides ( args ) # warn args overridden by --gossip/--btc-src if ( args . exec ) : # Do not support the option in plaintext mode if ( args . plaintext ) : raise ValueError ( "Option --exec is not allowed in plaintext mode" ) # Require either --sender or --insecure if ( args . sender is None and not args . insecure ) : raise ValueError ( "Option --exec requires a specified --sender" ) # Warn about unsafe commands base_exec_cmd = shlex . split ( args . exec ) [ 0 ] unsafe_cmds = [ "/bin/bash" , "bash" , "/bin/sh" , "sh" , "rm" ] if ( base_exec_cmd in unsafe_cmds ) : logger . warning ( "Running {} on --exec is considered unsafe" . format ( base_exec_cmd ) ) # GPG wrapper object if ( not args . plaintext or args . sender ) : gpg = Gpg ( gnupghome , interactive = True ) config_keyring ( gpg ) else : gpg = None # Define the interface used to listen for messages if ( args . interface ) : interface = args . interface elif ( args . demo ) : interface = "lo" else : # Infer the interface based on the user's setup user_info = blocksatcli_config . read_cfg_file ( args . cfg , args . cfg_dir ) interface = blocksatcli_config . get_net_if ( user_info ) logger . info ( "Listening on interface: {}" . format ( interface ) ) logger . info ( "Downloads will be saved at: {}" . format ( download_dir ) ) # A passphrase is required for decryption (but not for signature # verification) if ( not args . plaintext and not args . no_password ) : gpg . prompt_passphrase ( 'GPG keyring password for decryption: ' ) # Listen continuously listen_loop = ApiListener ( ) listen_loop . run ( gpg , download_dir , args . sock_addr , interface , channel , args . plaintext , args . save_raw , sender = args . sender , stdout = args . stdout , no_save = args . no_save , echo = args . echo , exec_cmd = args . exec , gossip_opts = gossip_opts , server_addr = server_addr , tls_cert = args . tls_cert , tls_key = args . tls_key , region = args . region )