Skip to content

Commit 604a831

Browse files
committed
BE: DEEP_SLEEP #1555 co-author @legionGer
Signed-off-by: jokob-sk <jokob.sk@gmail.com>
1 parent 7c7beaa commit 604a831

3 files changed

Lines changed: 64 additions & 3 deletions

File tree

server/__main__.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from helper import filePermissions
2828
from utils.datetime_utils import timeNowUTC
2929
from app_state import updateState
30-
from api import update_api
30+
from api import update_api, check_activity, update_GUI_port
3131
from scan.session_events import process_scan
3232
from initialise import importConfigs, renameSettings
3333
from database import DB
@@ -85,6 +85,9 @@ def main():
8585
# Initialize the WorkflowManager
8686
workflow_manager = WorkflowManager(db)
8787

88+
#Run this once to update the defined GUI port for the activity check
89+
update_GUI_port()
90+
8891
# ===============================================================================
8992
# This is the main loop of NetAlertX
9093
# ===============================================================================
@@ -261,8 +264,23 @@ def main():
261264
if userUpdatedDevices:
262265
update_api(db, all_plugins, True, ["devices"], userUpdatedDevices)
263266

264-
# loop
265-
time.sleep(5) # wait for N seconds
267+
# ------------------------------------------------------------------
268+
# Loop with dynamic sleep if enabled (energy saving)
269+
# ------------------------------------------------------------------
270+
if conf.DEEP_SLEEP:
271+
is_active = check_activity()
272+
273+
if is_active:
274+
mylog("debug", ["[DEEP_SLEEP] Active Cycle"])
275+
time.sleep(5)
276+
else:
277+
mylog("debug", ["[DEEP_SLEEP] Passive Cycle"])
278+
for _ in range(3):
279+
if check_activity():
280+
break
281+
time.sleep(20)
282+
else:
283+
time.sleep(5)
266284

267285

268286
# ===============================================================================

server/api.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
import threading
55
import datetime
6+
import os
67

78
# Register NetAlertX modules
89
import conf
@@ -21,6 +22,7 @@
2122
sql_notifications_all,
2223
sql_online_history,
2324
sql_devices_filters,
25+
defaultWebPort,
2426
)
2527
from db.db_helper import get_sql_devices_tiles
2628
from logger import mylog
@@ -34,6 +36,8 @@
3436

3537
apiEndpoints = []
3638

39+
hex_gui_port = None
40+
3741
# Lock for thread safety
3842
api_lock = threading.Lock()
3943
periodic_write_lock = threading.Lock()
@@ -251,3 +255,41 @@ def stop_periodic_write():
251255
periodic_write_thread.join()
252256
periodic_write_running = False
253257
mylog("trace", ["[API] periodic_write thread stopped."])
258+
259+
260+
def update_GUI_port():
261+
"""
262+
Grabs the PORT for the webinterface and converts it to HEX to use for activity checks
263+
"""
264+
global hex_gui_port
265+
gui_port_string = port = os.environ.get('PORT', defaultWebPort)
266+
hex_gui_port = ':'+format(int(gui_port_string), '04X')
267+
268+
269+
def check_activity():
270+
"""
271+
Check for active TCP connections on the host.
272+
273+
Reads `/proc/net/tcp` and looks for entries in the ESTABLISHED state
274+
(state code `01`). If any are found, the system is considered "active",
275+
typically indicating interaction via the web UI or API.
276+
277+
Returns:
278+
bool: True if at least one established TCP connection exists,
279+
False otherwise or if the check fails.
280+
281+
Notes:
282+
- Linux-only: relies on `/proc/net/tcp`.
283+
- Lightweight heuristic; does not distinguish connection origin
284+
(e.g., UI vs other services).
285+
- Fail-safe: returns False on any read/parse error.
286+
"""
287+
288+
try:
289+
with open("/proc/net/tcp", "r") as f:
290+
for line in f:
291+
if hex_gui_port in line and " 01 " in line:
292+
return True
293+
except:
294+
pass
295+
return False

server/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
dbFileName = "app.db"
3232
confFileName = "app.conf"
33+
defaultWebPort = 20211
3334

3435
confPath = CONFIG_PATH_WITH_TRAILING_SEP + confFileName
3536
dbPath = DB_PATH_WITH_TRAILING_SEP + dbFileName

0 commit comments

Comments
 (0)