From c8b6ac409b7e727e49e1e66219c9c115c2357254 Mon Sep 17 00:00:00 2001 From: Daniel Markstedt Date: Mon, 8 Nov 2021 19:11:27 -0800 Subject: [PATCH] Indicator of a running macproxy daemon in the webapp (#436) * Support for downloading properties files * Show extra message when unzipping properties file * Add logic to unzip method * Move unzipped properties into CFG_DIR * Better status message * Cleanup * Generic process introspection method * Show macproxy status in the webapp --- src/web/pi_cmds.py | 29 ++++++++++++++++++++++++----- src/web/templates/index.html | 5 +++++ src/web/web.py | 7 +++++-- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/web/pi_cmds.py b/src/web/pi_cmds.py index 28f19260..db34ae9e 100644 --- a/src/web/pi_cmds.py +++ b/src/web/pi_cmds.py @@ -51,15 +51,16 @@ def running_env(): return {"git": ra_git_version, "env": pi_version} -def running_netatalk(): +def running_proc(daemon): """ - Returns (int) afpd, which is the number of afpd processes currently running + Takes (str) daemon + Returns (int) proc, which is the number of processes currently running """ process = subprocess.run(["ps", "aux"], capture_output=True) output = process.stdout.decode("utf-8") from re import findall - afpd = findall("afpd", output) - return len(afpd) + proc = findall(daemon, output) + return len(proc) def is_bridge_setup(): @@ -82,6 +83,24 @@ def disk_space(): total, used, free = disk_usage(__file__) return {"total": total, "used": used, "free": free} + +def get_ip_address(): + """ + Use a mock socket connection to identify the Pi's IP address + """ + from socket import socket, AF_INET, SOCK_DGRAM + sock = socket(AF_INET, SOCK_DGRAM) + try: + # mock ip address; doesn't have to be reachable + sock.connect(('10.255.255.255', 1)) + ip_addr = sock.getsockname()[0] + except Exception: + ip_addr = '127.0.0.1' + finally: + sock.close() + return ip_addr + + def introspect_file(file_path, re_term): """ Takes a (str) file_path and (str) re_term in regex format @@ -96,4 +115,4 @@ def introspect_file(file_path, re_term): for line in ifile: if match(re_term, line): return True - return False + return False \ No newline at end of file diff --git a/src/web/templates/index.html b/src/web/templates/index.html index 233d60c4..5fa7877b 100644 --- a/src/web/templates/index.html +++ b/src/web/templates/index.html @@ -317,6 +317,11 @@ +{% if macproxy_configured %} +

macproxy is running at {{ ip_addr }} port 5000

+{% else %} +

Install macproxy to browse the Web with your vintage browser.

+{% endif %}
diff --git a/src/web/web.py b/src/web/web.py index 9488fd8e..492fa37b 100644 --- a/src/web/web.py +++ b/src/web/web.py @@ -36,9 +36,10 @@ from pi_cmds import ( reboot_pi, running_env, systemd_service, - running_netatalk, + running_proc, is_bridge_setup, disk_space, + get_ip_address, introspect_file, ) from ractl_cmds import ( @@ -112,7 +113,9 @@ def index(): return render_template( "index.html", bridge_configured=is_bridge_setup(), - netatalk_configured=running_netatalk(), + netatalk_configured=running_proc("afpd"), + macproxy_configured=running_proc("macproxy"), + ip_addr=get_ip_address(), devices=formatted_devices, files=sorted_image_files, config_files=sorted_config_files,