From 9e86f0e5c6e3e124985d68c3e92d4fe130ea3f60 Mon Sep 17 00:00:00 2001 From: Mat Kelly Date: Tue, 5 Apr 2022 14:22:36 -0400 Subject: [PATCH 1/4] An approach for #760, see below for caveats This approach allows the replay to run on the specified port but the Link header for mementos does not use this value. It instead uses the value in the ipwb config. However, we might not want to overwrite the port in the config for this one-off specification. Let's see which way @ibnesayeed prefers in #760 before we move forward in remedying the issue. --- ipwb/__main__.py | 14 +++++++++++++- ipwb/replay.py | 6 +++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/ipwb/__main__.py b/ipwb/__main__.py index 42da2d3d..c3432bea 100644 --- a/ipwb/__main__.py +++ b/ipwb/__main__.py @@ -58,9 +58,14 @@ def check_args_replay(args): print(f'Proxying to {args.proxy}') proxy = args.proxy + port = replay.IPWBREPLAY_PORT + if hasattr(args, 'port') and args.port is not None: + print(f'Using custom port {args.port} for replay.') + port = args.port + # TODO: add any other sub-arguments for replay here if supplied_index_parameter: - replay.start(cdxj_file_path=args.index, proxy=proxy) + replay.start(cdxj_file_path=args.index, proxy=proxy, port=port) else: print('ERROR: An index file must be specified if not piping, e.g.,') print(("> ipwb replay " @@ -133,6 +138,13 @@ def check_args(args_in): help='Proxy URL', metavar='', nargs='?') + replay_parser.add_argument( + '-p', '--port', + help='Custom Port', + #metavar=f'custom port', + type=int, + default=util.IPWBREPLAY_PORT + ) replay_parser.set_defaults(func=check_args_replay, onError=replay_parser.print_help) diff --git a/ipwb/replay.py b/ipwb/replay.py index dd6ae15e..fd8d5f41 100755 --- a/ipwb/replay.py +++ b/ipwb/replay.py @@ -1047,7 +1047,7 @@ def get_cdxj_line_binary_search( return line_found -def start(cdxj_file_path, proxy=None): +def start(cdxj_file_path, proxy=None, port=IPWBREPLAY_PORT): host_port = ipwb_utils.get_ipwb_replay_config() app.proxy = proxy @@ -1062,9 +1062,9 @@ def start(cdxj_file_path, proxy=None): try: print((f'IPWB replay started on ' - f'http://{IPWBREPLAY_HOST}:{IPWBREPLAY_PORT}')) + f'http://{IPWBREPLAY_HOST}:{port}')) - app.run(host='0.0.0.0', port=IPWBREPLAY_PORT) + app.run(host='0.0.0.0', port=port) except gaierror: print('Detected no active Internet connection.') print('Overriding to use default IP and port configuration.') From 402934aec465b3890d6ea59d9a4ecb699683b705 Mon Sep 17 00:00:00 2001 From: Mat Kelly Date: Tue, 5 Apr 2022 15:38:16 -0400 Subject: [PATCH 2/4] Use and retain custom port for replay Closes #760 --- ipwb/__main__.py | 5 ++--- ipwb/replay.py | 7 ++++--- ipwb/util.py | 6 ++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ipwb/__main__.py b/ipwb/__main__.py index c3432bea..5c26d7ca 100644 --- a/ipwb/__main__.py +++ b/ipwb/__main__.py @@ -61,11 +61,11 @@ def check_args_replay(args): port = replay.IPWBREPLAY_PORT if hasattr(args, 'port') and args.port is not None: print(f'Using custom port {args.port} for replay.') - port = args.port + util.set_ipwb_replay_config_portonly(args.port) # TODO: add any other sub-arguments for replay here if supplied_index_parameter: - replay.start(cdxj_file_path=args.index, proxy=proxy, port=port) + replay.start(cdxj_file_path=args.index, proxy=proxy) else: print('ERROR: An index file must be specified if not piping, e.g.,') print(("> ipwb replay " @@ -141,7 +141,6 @@ def check_args(args_in): replay_parser.add_argument( '-p', '--port', help='Custom Port', - #metavar=f'custom port', type=int, default=util.IPWBREPLAY_PORT ) diff --git a/ipwb/replay.py b/ipwb/replay.py index fd8d5f41..a033409a 100755 --- a/ipwb/replay.py +++ b/ipwb/replay.py @@ -1047,12 +1047,13 @@ def get_cdxj_line_binary_search( return line_found -def start(cdxj_file_path, proxy=None, port=IPWBREPLAY_PORT): +def start(cdxj_file_path, proxy=None): host_port = ipwb_utils.get_ipwb_replay_config() app.proxy = proxy if not host_port: ipwb_utils.set_ipwb_replay_config(IPWBREPLAY_HOST, IPWBREPLAY_PORT) + host_port = (IPWBREPLAY_HOST, IPWBREPLAY_PORT) # This will throw an exception if daemon is not available. ipwb_utils.check_daemon_is_alive() @@ -1062,9 +1063,9 @@ def start(cdxj_file_path, proxy=None, port=IPWBREPLAY_PORT): try: print((f'IPWB replay started on ' - f'http://{IPWBREPLAY_HOST}:{port}')) + f'http://{host_port[0]}:{host_port[1]}')) - app.run(host='0.0.0.0', port=port) + app.run(host='0.0.0.0', port=host_port[1]) except gaierror: print('Detected no active Internet connection.') print('Overriding to use default IP and port configuration.') diff --git a/ipwb/util.py b/ipwb/util.py index dbbf12b6..5dbc14ab 100644 --- a/ipwb/util.py +++ b/ipwb/util.py @@ -269,6 +269,12 @@ def get_ipwb_replay_config(ipfs_json=None): return None +def set_ipwb_replay_config_portonly(port): + ipfs_json = read_ipfs_config() + ipfs_json['Ipwb']['Replay']['Port'] = port + write_ipfs_config(ipfs_json) + + def set_ipwb_replay_config(Host, Port, ipfs_json=None): if not ipfs_json: ipfs_json = read_ipfs_config() From e488089a43b03473c0f4704d400a17eb5f49bb5c Mon Sep 17 00:00:00 2001 From: Mat Kelly Date: Tue, 5 Apr 2022 15:40:32 -0400 Subject: [PATCH 3/4] Rm unused var 'port' --- ipwb/__main__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ipwb/__main__.py b/ipwb/__main__.py index 5c26d7ca..105d137e 100644 --- a/ipwb/__main__.py +++ b/ipwb/__main__.py @@ -58,7 +58,6 @@ def check_args_replay(args): print(f'Proxying to {args.proxy}') proxy = args.proxy - port = replay.IPWBREPLAY_PORT if hasattr(args, 'port') and args.port is not None: print(f'Using custom port {args.port} for replay.') util.set_ipwb_replay_config_portonly(args.port) From 3435e5b45078e84bf8ac06cd31bec8e298807171 Mon Sep 17 00:00:00 2001 From: Mat Kelly Date: Tue, 5 Apr 2022 16:14:54 -0400 Subject: [PATCH 4/4] Check that ipwb config is set in ipfs config file before trying to write to it For #760 Re: #761 --- ipwb/__main__.py | 5 +++-- ipwb/replay.py | 8 +++++--- ipwb/util.py | 6 ------ 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/ipwb/__main__.py b/ipwb/__main__.py index 105d137e..84201529 100644 --- a/ipwb/__main__.py +++ b/ipwb/__main__.py @@ -58,13 +58,14 @@ def check_args_replay(args): print(f'Proxying to {args.proxy}') proxy = args.proxy + port = replay.IPWBREPLAY_PORT if hasattr(args, 'port') and args.port is not None: print(f'Using custom port {args.port} for replay.') - util.set_ipwb_replay_config_portonly(args.port) + port = args.port # TODO: add any other sub-arguments for replay here if supplied_index_parameter: - replay.start(cdxj_file_path=args.index, proxy=proxy) + replay.start(cdxj_file_path=args.index, proxy=proxy, port=port) else: print('ERROR: An index file must be specified if not piping, e.g.,') print(("> ipwb replay " diff --git a/ipwb/replay.py b/ipwb/replay.py index a033409a..d83fa427 100755 --- a/ipwb/replay.py +++ b/ipwb/replay.py @@ -1047,13 +1047,15 @@ def get_cdxj_line_binary_search( return line_found -def start(cdxj_file_path, proxy=None): +def start(cdxj_file_path, proxy=None, port=IPWBREPLAY_PORT): host_port = ipwb_utils.get_ipwb_replay_config() app.proxy = proxy + # Retain port for subsequent runs + ipwb_utils.set_ipwb_replay_config(IPWBREPLAY_HOST, port) + if not host_port: - ipwb_utils.set_ipwb_replay_config(IPWBREPLAY_HOST, IPWBREPLAY_PORT) - host_port = (IPWBREPLAY_HOST, IPWBREPLAY_PORT) + host_port = (IPWBREPLAY_HOST, port) # This will throw an exception if daemon is not available. ipwb_utils.check_daemon_is_alive() diff --git a/ipwb/util.py b/ipwb/util.py index 5dbc14ab..dbbf12b6 100644 --- a/ipwb/util.py +++ b/ipwb/util.py @@ -269,12 +269,6 @@ def get_ipwb_replay_config(ipfs_json=None): return None -def set_ipwb_replay_config_portonly(port): - ipfs_json = read_ipfs_config() - ipfs_json['Ipwb']['Replay']['Port'] = port - write_ipfs_config(ipfs_json) - - def set_ipwb_replay_config(Host, Port, ipfs_json=None): if not ipfs_json: ipfs_json = read_ipfs_config()