mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-04-15 22:51:37 -07:00
PLG: add online option only via PIHOLEAPI_GET_OFFLINE #1436
Signed-off-by: jokob-sk <jokob.sk@gmail.com>
This commit is contained in:
@@ -234,6 +234,34 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"function": "GET_OFFLINE",
|
||||||
|
"type": {
|
||||||
|
"dataType": "boolean",
|
||||||
|
"elements": [
|
||||||
|
{
|
||||||
|
"elementType": "input",
|
||||||
|
"elementOptions": [{ "type": "checkbox" }],
|
||||||
|
"transformers": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"default_value": false,
|
||||||
|
"options": [],
|
||||||
|
"localized": ["name", "description"],
|
||||||
|
"name": [
|
||||||
|
{
|
||||||
|
"language_code": "en_us",
|
||||||
|
"string": "Import offline"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": [
|
||||||
|
{
|
||||||
|
"language_code": "en_us",
|
||||||
|
"string": "Import offline devices. By default only online devices are retrieved."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"function": "VERIFY_SSL",
|
"function": "VERIFY_SSL",
|
||||||
"type": {
|
"type": {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ PIHOLEAPI_SES_SID = None
|
|||||||
PIHOLEAPI_SES_CSRF = None
|
PIHOLEAPI_SES_CSRF = None
|
||||||
PIHOLEAPI_API_MAXCLIENTS = None
|
PIHOLEAPI_API_MAXCLIENTS = None
|
||||||
PIHOLEAPI_VERIFY_SSL = True
|
PIHOLEAPI_VERIFY_SSL = True
|
||||||
|
PIHOLEAPI_GET_OFFLINE = False
|
||||||
PIHOLEAPI_RUN_TIMEOUT = 10
|
PIHOLEAPI_RUN_TIMEOUT = 10
|
||||||
PIHOLEAPI_FAKE_MAC = get_setting_value('PIHOLEAPI_FAKE_MAC')
|
PIHOLEAPI_FAKE_MAC = get_setting_value('PIHOLEAPI_FAKE_MAC')
|
||||||
VERSION_DATE = "NAX-PIHOLEAPI-1.0"
|
VERSION_DATE = "NAX-PIHOLEAPI-1.0"
|
||||||
@@ -200,7 +201,7 @@ def gather_device_entries():
|
|||||||
"""
|
"""
|
||||||
entries = []
|
entries = []
|
||||||
|
|
||||||
iface_map = get_pihole_interface_data()
|
iface_map = get_pihole_interface_data() # mac -> [ips]
|
||||||
devices = get_pihole_network_devices()
|
devices = get_pihole_network_devices()
|
||||||
now_ts = int(datetime.datetime.now().timestamp())
|
now_ts = int(datetime.datetime.now().timestamp())
|
||||||
|
|
||||||
@@ -211,31 +212,41 @@ def gather_device_entries():
|
|||||||
|
|
||||||
macVendor = device.get('macVendor', '')
|
macVendor = device.get('macVendor', '')
|
||||||
lastQuery = device.get('lastQuery')
|
lastQuery = device.get('lastQuery')
|
||||||
# 'ips' is a list of dicts: {ip, name}
|
|
||||||
for ip_info in device.get('ips', []):
|
# collect all IPs for this device
|
||||||
|
device_ips = device.get('ips', [])
|
||||||
|
if not device_ips:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for ip_info in device_ips:
|
||||||
ip = ip_info.get('ip')
|
ip = ip_info.get('ip')
|
||||||
if not ip:
|
if not ip:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
name = ip_info.get('name') or '(unknown)'
|
name = ip_info.get('name') or '(unknown)'
|
||||||
|
|
||||||
# mark active if ip present on local interfaces
|
# Determine if this device is "online"
|
||||||
for mac, iplist in iface_map.items():
|
online = any(ip in iplist for iplist in iface_map.values())
|
||||||
if ip in iplist:
|
|
||||||
lastQuery = str(now_ts)
|
# Skip offline devices unless PIHOLEAPI_GET_OFFLINE=True
|
||||||
|
if not online and not PIHOLEAPI_GET_OFFLINE:
|
||||||
|
continue
|
||||||
|
|
||||||
tmpMac = hwaddr.lower()
|
tmpMac = hwaddr.lower()
|
||||||
|
|
||||||
# ensure fake mac if enabled
|
# ensure fake mac if enabled
|
||||||
if PIHOLEAPI_FAKE_MAC and is_mac(tmpMac) is False:
|
if PIHOLEAPI_FAKE_MAC and not is_mac(tmpMac):
|
||||||
tmpMac = string_to_fake_mac(ip)
|
tmpMac = string_to_fake_mac(ip)
|
||||||
|
|
||||||
|
# mark lastQuery as now if online, else keep original
|
||||||
|
last_query_val = str(now_ts) if online else str(lastQuery) if lastQuery else ''
|
||||||
|
|
||||||
entries.append({
|
entries.append({
|
||||||
'mac': tmpMac,
|
'mac': tmpMac,
|
||||||
'ip': ip,
|
'ip': ip,
|
||||||
'name': name,
|
'name': name,
|
||||||
'macVendor': macVendor,
|
'macVendor': macVendor,
|
||||||
'lastQuery': str(lastQuery) if lastQuery is not None else ''
|
'lastQuery': last_query_val
|
||||||
})
|
})
|
||||||
|
|
||||||
return entries
|
return entries
|
||||||
@@ -244,7 +255,7 @@ def gather_device_entries():
|
|||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
def main():
|
def main():
|
||||||
"""Main plugin entrypoint."""
|
"""Main plugin entrypoint."""
|
||||||
global PIHOLEAPI_URL, PIHOLEAPI_PASSWORD, PIHOLEAPI_API_MAXCLIENTS, PIHOLEAPI_VERIFY_SSL, PIHOLEAPI_RUN_TIMEOUT
|
global PIHOLEAPI_URL, PIHOLEAPI_PASSWORD, PIHOLEAPI_API_MAXCLIENTS, PIHOLEAPI_VERIFY_SSL, PIHOLEAPI_RUN_TIMEOUT, PIHOLEAPI_GET_OFFLINE
|
||||||
|
|
||||||
mylog('verbose', [f'[{pluginName}] start script.'])
|
mylog('verbose', [f'[{pluginName}] start script.'])
|
||||||
|
|
||||||
@@ -260,6 +271,7 @@ def main():
|
|||||||
# Accept boolean or string "True"/"False"
|
# Accept boolean or string "True"/"False"
|
||||||
PIHOLEAPI_VERIFY_SSL = get_setting_value('PIHOLEAPI_SSL_VERIFY')
|
PIHOLEAPI_VERIFY_SSL = get_setting_value('PIHOLEAPI_SSL_VERIFY')
|
||||||
PIHOLEAPI_RUN_TIMEOUT = get_setting_value('PIHOLEAPI_RUN_TIMEOUT')
|
PIHOLEAPI_RUN_TIMEOUT = get_setting_value('PIHOLEAPI_RUN_TIMEOUT')
|
||||||
|
PIHOLEAPI_GET_OFFLINE = get_setting_value('PIHOLEAPI_GET_OFFLINE')
|
||||||
|
|
||||||
# Authenticate
|
# Authenticate
|
||||||
if not pihole_api_auth():
|
if not pihole_api_auth():
|
||||||
|
|||||||
Reference in New Issue
Block a user