diff --git a/server/__main__.py b/server/__main__.py index 0ae5d3c7..1948bd4d 100755 --- a/server/__main__.py +++ b/server/__main__.py @@ -27,7 +27,7 @@ from logger import mylog from helper import filePermissions from utils.datetime_utils import timeNowUTC from app_state import updateState -from api import update_api, check_activity +from api import update_api, check_activity, update_GUI_port from scan.session_events import process_scan from initialise import importConfigs, renameSettings from database import DB @@ -85,6 +85,9 @@ def main(): # Initialize the WorkflowManager workflow_manager = WorkflowManager(db) + #Run this once to update the defined GUI port for the activity check + update_GUI_port() + # =============================================================================== # This is the main loop of NetAlertX # =============================================================================== @@ -261,9 +264,8 @@ def main(): if userUpdatedDevices: update_api(db, all_plugins, True, ["devices"], userUpdatedDevices) - # loop # ------------------------------------------------------------------ - # Dynamic sleep (energy saving) + # Loop with dynamic sleep if enabled (energy saving) # ------------------------------------------------------------------ if conf.DEEP_SLEEP: is_active = check_activity() @@ -281,8 +283,6 @@ def main(): time.sleep(5) - - # =============================================================================== # BEGIN # =============================================================================== diff --git a/server/api.py b/server/api.py index 90b1db49..b65d23e1 100755 --- a/server/api.py +++ b/server/api.py @@ -3,6 +3,7 @@ import json import time import threading import datetime +import os # Register NetAlertX modules import conf @@ -21,6 +22,7 @@ from const import ( sql_notifications_all, sql_online_history, sql_devices_filters, + defaultWebPort, ) from db.db_helper import get_sql_devices_tiles from logger import mylog @@ -34,6 +36,8 @@ from api_server.api_server_start import start_server apiEndpoints = [] +hex_gui_port = None + # Lock for thread safety api_lock = threading.Lock() periodic_write_lock = threading.Lock() @@ -253,6 +257,21 @@ def stop_periodic_write(): mylog("trace", ["[API] periodic_write thread stopped."]) +def update_GUI_port(): + """ + Grabs the PORT for the webinterface and converts it to HEX to use for activity checks + """ + global hex_gui_port + + gui_port_string = os.environ.get('PORT', str(defaultWebPort)) + try: + port = int(gui_port_string) + except (TypeError, ValueError): + mylog("none", [f"[API] Invalid PORT value '{gui_port_string}', falling back to {defaultWebPort}"]) + port = defaultWebPort + hex_gui_port = ':' + format(port, '04X') + + def check_activity(): """ Check for active TCP connections on the host. @@ -271,10 +290,11 @@ def check_activity(): (e.g., UI vs other services). - Fail-safe: returns False on any read/parse error. """ + try: with open("/proc/net/tcp", "r") as f: for line in f: - if " 01 " in line: # ESTABLISHED + if hex_gui_port in line and " 01 " in line: return True except: pass diff --git a/server/const.py b/server/const.py index 9762ed69..30e91be5 100755 --- a/server/const.py +++ b/server/const.py @@ -30,6 +30,7 @@ logRoot = LOG_PATH_STR dbFileName = "app.db" confFileName = "app.conf" +defaultWebPort = 20211 confPath = CONFIG_PATH_WITH_TRAILING_SEP + confFileName dbPath = DB_PATH_WITH_TRAILING_SEP + dbFileName