mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-05-19 05:57:57 -04:00
@@ -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
|
||||
from api import update_api, check_activity
|
||||
from scan.session_events import process_scan
|
||||
from initialise import importConfigs, renameSettings
|
||||
from database import DB
|
||||
@@ -262,7 +262,25 @@ def main():
|
||||
update_api(db, all_plugins, True, ["devices"], userUpdatedDevices)
|
||||
|
||||
# loop
|
||||
time.sleep(5) # wait for N seconds
|
||||
# ------------------------------------------------------------------
|
||||
# Dynamic sleep (energy saving)
|
||||
# ------------------------------------------------------------------
|
||||
if conf.DEEP_SLEEP:
|
||||
is_active = check_activity()
|
||||
|
||||
if is_active:
|
||||
mylog("debug", ["[DEEP_SLEEP] Active Cycle"])
|
||||
time.sleep(5)
|
||||
else:
|
||||
mylog("debug", ["[DEEP_SLEEP] Passive Cycle"])
|
||||
for _ in range(3):
|
||||
if check_activity():
|
||||
break
|
||||
time.sleep(20)
|
||||
else:
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
|
||||
|
||||
# ===============================================================================
|
||||
|
||||
@@ -251,3 +251,31 @@ def stop_periodic_write():
|
||||
periodic_write_thread.join()
|
||||
periodic_write_running = False
|
||||
mylog("trace", ["[API] periodic_write thread stopped."])
|
||||
|
||||
|
||||
def check_activity():
|
||||
"""
|
||||
Check for active TCP connections on the host.
|
||||
|
||||
Reads `/proc/net/tcp` and looks for entries in the ESTABLISHED state
|
||||
(state code `01`). If any are found, the system is considered "active",
|
||||
typically indicating interaction via the web UI or API.
|
||||
|
||||
Returns:
|
||||
bool: True if at least one established TCP connection exists,
|
||||
False otherwise or if the check fails.
|
||||
|
||||
Notes:
|
||||
- Linux-only: relies on `/proc/net/tcp`.
|
||||
- Lightweight heuristic; does not distinguish connection origin
|
||||
(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
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
@@ -246,6 +246,15 @@ def importConfigs(pm, db, all_plugins):
|
||||
"[]",
|
||||
"General",
|
||||
)
|
||||
conf.DEEP_SLEEP = ccd(
|
||||
"DEEP_SLEEP",
|
||||
False,
|
||||
c_d,
|
||||
"Deep Sleep",
|
||||
"""{"dataType": "boolean","elements": [{"elementType": "input","elementOptions": [{ "type": "checkbox" }],"transformers": []}]}""",
|
||||
"[]",
|
||||
"General",
|
||||
)
|
||||
conf.DISCOVER_PLUGINS = ccd(
|
||||
"DISCOVER_PLUGINS",
|
||||
True,
|
||||
|
||||
Reference in New Issue
Block a user