mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-06 17:15:38 -08:00
heuristics refactor #1129
This commit is contained in:
200
back/device_heuristics_rules.json
Executable file
200
back/device_heuristics_rules.json
Executable file
@@ -0,0 +1,200 @@
|
||||
[
|
||||
{
|
||||
"dev_type": "Gateway",
|
||||
"icon_html": "<i class=\"fa fa-globe\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "INTERNET", "vendor": "" }
|
||||
],
|
||||
"name_pattern": []
|
||||
},
|
||||
{
|
||||
"dev_type": "Access Point",
|
||||
"icon_html": "<i class=\"fa fa-network-wired\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "74ACB9", "vendor": "Ubiquiti" },
|
||||
{ "mac_prefix": "002468", "vendor": "Cisco" },
|
||||
{ "mac_prefix": "F4F5D8", "vendor": "TP-Link" },
|
||||
{ "mac_prefix": "F88E85", "vendor": "Netgear" }
|
||||
],
|
||||
"name_pattern": ["router", "gateway", "ap", "access point", "access-point", "switch"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Phone",
|
||||
"icon_html": "<i class=\"fa-brands fa-apple\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "001A79", "vendor": "Apple" },
|
||||
{ "mac_prefix": "B0BE83", "vendor": "Samsung" },
|
||||
{ "mac_prefix": "BC926B", "vendor": "Motorola" }
|
||||
],
|
||||
"name_pattern": ["iphone", "ipad", "pixel", "galaxy", "redmi"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Phone",
|
||||
"icon_html": "<i class=\"fa-solid fa-mobile\"></i>",
|
||||
"matching_pattern": [
|
||||
],
|
||||
"name_pattern": ["android","samsung"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Tablet",
|
||||
"icon_html": "<i class=\"fa fa-tablet\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "001B63", "vendor": "Apple" },
|
||||
{ "mac_prefix": "BC4C4C", "vendor": "Samsung" }
|
||||
],
|
||||
"name_pattern": ["tablet", "pad"]
|
||||
},
|
||||
{
|
||||
"dev_type": "IoT",
|
||||
"icon_html": "<i class=\"fa-brands fa-raspberry-pi\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "B827EB", "vendor": "Raspberry Pi" },
|
||||
{ "mac_prefix": "DCA632", "vendor": "Raspberry Pi" }
|
||||
],
|
||||
"name_pattern": ["raspberry", "pi"]
|
||||
},
|
||||
{
|
||||
"dev_type": "IoT",
|
||||
"icon_html": "<i class=\"fa-solid fa-microchip\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "840D8E", "vendor": "Espressif" },
|
||||
{ "mac_prefix": "ECFABC", "vendor": "Espressif" },
|
||||
{ "mac_prefix": "7C9EBD", "vendor": "Espressif" }
|
||||
],
|
||||
"name_pattern": ["raspberry", "pi"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Desktop",
|
||||
"icon_html": "<i class=\"fa fa-desktop\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "001422", "vendor": "Dell" },
|
||||
{ "mac_prefix": "001874", "vendor": "Lenovo" },
|
||||
{ "mac_prefix": "00E04C", "vendor": "Hewlett Packard" }
|
||||
],
|
||||
"name_pattern": ["desktop", "pc", "computer"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Laptop",
|
||||
"icon_html": "<i class=\"fa fa-laptop\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "3C0754", "vendor": "HP" },
|
||||
{ "mac_prefix": "0017A4", "vendor": "Dell" },
|
||||
{ "mac_prefix": "F4CE46", "vendor": "Lenovo" },
|
||||
{ "mac_prefix": "409F38", "vendor": "Acer" }
|
||||
],
|
||||
"name_pattern": ["macbook", "imac", "laptop", "notebook"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Server",
|
||||
"icon_html": "<i class=\"fa fa-server\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "001CBF", "vendor": "Supermicro" },
|
||||
{ "mac_prefix": "002186", "vendor": "Dell" },
|
||||
{ "mac_prefix": "D02788", "vendor": "Hewlett Packard" },
|
||||
{ "mac_prefix": "002590", "vendor": "IBM" }
|
||||
],
|
||||
"name_pattern": ["server", "nas"]
|
||||
},
|
||||
{
|
||||
"dev_type": "VM",
|
||||
"icon_html": "<i class=\"fa fa-server\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "525400", "vendor": "QEMU" },
|
||||
{ "mac_prefix": "005056", "vendor": "VMware" },
|
||||
{ "mac_prefix": "000C29", "vendor": "VMware" },
|
||||
{ "mac_prefix": "000569", "vendor": "VMware" },
|
||||
{ "mac_prefix": "00163E", "vendor": "Xen" },
|
||||
{ "mac_prefix": "080027", "vendor": "VirtualBox" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"dev_type": "TV",
|
||||
"icon_html": "<i class=\"fa fa-tv\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "0013CE", "vendor": "Samsung" },
|
||||
{ "mac_prefix": "0017C8", "vendor": "LG" },
|
||||
{ "mac_prefix": "D46E0E", "vendor": "Sony" }
|
||||
],
|
||||
"name_pattern": ["tv", "television", "smarttv"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Gaming Console",
|
||||
"icon_html": "<i class=\"fa fa-gamepad\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "001FA7", "vendor": "Sony" },
|
||||
{ "mac_prefix": "7C04D0", "vendor": "Nintendo" },
|
||||
{ "mac_prefix": "EC26CA", "vendor": "Sony" }
|
||||
],
|
||||
"name_pattern": ["playstation", "xbox"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Camera",
|
||||
"icon_html": "<i class=\"fa fa-camera\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "A45E60", "vendor": "Hikvision" },
|
||||
{ "mac_prefix": "00408C", "vendor": "Axis" },
|
||||
{ "mac_prefix": "00156D", "vendor": "Amcrest" },
|
||||
{ "mac_prefix": "AC9E17", "vendor": "Reolink" }
|
||||
],
|
||||
"name_pattern": ["camera", "cam", "webcam"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Smart Speaker",
|
||||
"icon_html": "<i class=\"fa fa-volume-up\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "44650D", "vendor": "Amazon" },
|
||||
{ "mac_prefix": "74ACB9", "vendor": "Google" }
|
||||
],
|
||||
"name_pattern": ["echo", "alexa", "dot"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Router",
|
||||
"icon_html": "<i class=\"fa fa-random\"></i>",
|
||||
"matching_pattern": [
|
||||
{ "mac_prefix": "000C29", "vendor": "Cisco" },
|
||||
{ "mac_prefix": "00155D", "vendor": "MikroTik" }
|
||||
],
|
||||
"name_pattern": ["router", "gateway", "ap", "access point", "access-point"],
|
||||
"ip_pattern": [
|
||||
"^192\\.168\\.[0-1]\\.1$",
|
||||
"^10\\.0\\.0\\.1$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"dev_type": "Smart Light",
|
||||
"icon_html": "<i class=\"fa fa-lightbulb\"></i>",
|
||||
"matching_pattern": [],
|
||||
"name_pattern": ["hue", "lifx", "bulb"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Smart Home",
|
||||
"icon_html": "<i class=\"fa fa-house\"></i>",
|
||||
"matching_pattern": [],
|
||||
"name_pattern": ["google", "chromecast", "nest"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Smartwatch",
|
||||
"icon_html": "<i class=\"fa fa-watch\"></i>",
|
||||
"matching_pattern": [],
|
||||
"name_pattern": ["watch", "wear"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Printer",
|
||||
"icon_html": "<i class=\"fa fa-print\"></i>",
|
||||
"matching_pattern": [],
|
||||
"name_pattern": ["printer", "print"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Security Device",
|
||||
"icon_html": "<i class=\"fa fa-shield-alt\"></i>",
|
||||
"matching_pattern": [],
|
||||
"name_pattern": ["doorbell", "lock", "security"]
|
||||
},
|
||||
{
|
||||
"dev_type": "Smart Light",
|
||||
"icon_html": "<i class=\"fa-solid fa-lightbulb\"></i>",
|
||||
"matching_pattern": [
|
||||
],
|
||||
"name_pattern": ["light","bulb"]
|
||||
}
|
||||
]
|
||||
@@ -169,7 +169,7 @@ echo '<div class="box box-solid">
|
||||
?>
|
||||
|
||||
|
||||
<!-- DataTable initialization -->
|
||||
|
||||
<script>
|
||||
|
||||
// --------------------------------------------------------
|
||||
|
||||
@@ -171,7 +171,7 @@ def importConfigs (db, all_plugins):
|
||||
conf.REFRESH_FQDN = ccd('REFRESH_FQDN', False , c_d, 'Refresh FQDN', """{"dataType": "boolean","elements": [{"elementType": "input","elementOptions": [{ "type": "checkbox" }],"transformers": []}]}""", '[]', 'General')
|
||||
conf.API_CUSTOM_SQL = ccd('API_CUSTOM_SQL', 'SELECT * FROM Devices WHERE devPresentLastScan = 0' , c_d, 'Custom endpoint', '{"dataType":"string", "elements": [{"elementType" : "input", "elementOptions" : [] ,"transformers": []}]}', '[]', 'General')
|
||||
conf.VERSION = ccd('VERSION', '' , c_d, 'Version', '{"dataType":"string", "elements": [{"elementType" : "input", "elementOptions" : [{ "readonly": "true" }] ,"transformers": []}]}', '', 'General')
|
||||
conf.NETWORK_DEVICE_TYPES = ccd('NETWORK_DEVICE_TYPES', ['AP', 'Gateway', 'Firewall', 'Hypervisor', 'Powerline', 'Switch', 'WLAN', 'PLC', 'Router','USB LAN Adapter', 'USB WIFI Adapter', 'Internet'] , c_d, 'Network device types', '{"dataType":"array","elements":[{"elementType":"input","elementOptions":[{"placeholder":"Enter value"},{"suffix":"_in"},{"cssClasses":"col-sm-10"},{"prefillValue":"null"}],"transformers":[]},{"elementType":"button","elementOptions":[{"sourceSuffixes":["_in"]},{"separator":""},{"cssClasses":"col-xs-12"},{"onClick":"addList(this,false)"},{"getStringKey":"Gen_Add"}],"transformers":[]},{"elementType":"select", "elementHasInputValue":1,"elementOptions":[{"multiple":"true"},{"readonly":"true"},{"editable":"true"}],"transformers":[]},{"elementType":"button","elementOptions":[{"sourceSuffixes":[]},{"separator":""},{"cssClasses":"col-xs-6"},{"onClick":"removeAllOptions(this)"},{"getStringKey":"Gen_Remove_All"}],"transformers":[]},{"elementType":"button","elementOptions":[{"sourceSuffixes":[]},{"separator":""},{"cssClasses":"col-xs-6"},{"onClick":"removeFromList(this)"},{"getStringKey":"Gen_Remove_Last"}],"transformers":[]}]}', '[]', 'General')
|
||||
conf.NETWORK_DEVICE_TYPES = ccd('NETWORK_DEVICE_TYPES', ['AP', 'Access Point', 'Gateway', 'Firewall', 'Hypervisor', 'Powerline', 'Switch', 'WLAN', 'PLC', 'Router','USB LAN Adapter', 'USB WIFI Adapter', 'Internet'] , c_d, 'Network device types', '{"dataType":"array","elements":[{"elementType":"input","elementOptions":[{"placeholder":"Enter value"},{"suffix":"_in"},{"cssClasses":"col-sm-10"},{"prefillValue":"null"}],"transformers":[]},{"elementType":"button","elementOptions":[{"sourceSuffixes":["_in"]},{"separator":""},{"cssClasses":"col-xs-12"},{"onClick":"addList(this,false)"},{"getStringKey":"Gen_Add"}],"transformers":[]},{"elementType":"select", "elementHasInputValue":1,"elementOptions":[{"multiple":"true"},{"readonly":"true"},{"editable":"true"}],"transformers":[]},{"elementType":"button","elementOptions":[{"sourceSuffixes":[]},{"separator":""},{"cssClasses":"col-xs-6"},{"onClick":"removeAllOptions(this)"},{"getStringKey":"Gen_Remove_All"}],"transformers":[]},{"elementType":"button","elementOptions":[{"sourceSuffixes":[]},{"separator":""},{"cssClasses":"col-xs-6"},{"onClick":"removeFromList(this)"},{"getStringKey":"Gen_Remove_Last"}],"transformers":[]}]}', '[]', 'General')
|
||||
conf.GRAPHQL_PORT = ccd('GRAPHQL_PORT', 20212 , c_d, 'GraphQL port', '{"dataType":"integer", "elements": [{"elementType" : "input", "elementOptions" : [{"type": "number"}] ,"transformers": []}]}', '[]', 'General')
|
||||
conf.API_TOKEN = ccd('API_TOKEN', 't_' + generate_random_string(20) , c_d, 'API token', '{"dataType": "string","elements": [{"elementType": "input","elementHasInputValue": 1,"elementOptions": [{ "cssClasses": "col-xs-12" }],"transformers": []},{"elementType": "button","elementOptions": [{ "getStringKey": "Gen_Generate" },{ "customParams": "API_TOKEN" },{ "onClick": "generateApiToken(this, 20)" },{ "cssClasses": "col-xs-12" }],"transformers": []}]}', '[]', 'General')
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import sys
|
||||
import re
|
||||
import json
|
||||
import base64
|
||||
from pathlib import Path
|
||||
from typing import Optional, List, Tuple, Dict
|
||||
|
||||
# Register NetAlertX directories
|
||||
@@ -11,57 +14,167 @@ from const import *
|
||||
from logger import mylog
|
||||
from helper import timeNowTZ, get_setting_value
|
||||
|
||||
# Load MAC/device-type/icon rules from external file
|
||||
MAC_TYPE_ICON_PATH = Path(f"{INSTALL_PATH}/back/device_heuristics_rules.json")
|
||||
try:
|
||||
with open(MAC_TYPE_ICON_PATH, "r", encoding="utf-8") as f:
|
||||
MAC_TYPE_ICON_RULES = json.load(f)
|
||||
# Precompute base64-encoded icon_html once for each rule
|
||||
for rule in MAC_TYPE_ICON_RULES:
|
||||
icon_html = rule.get("icon_html", "")
|
||||
if icon_html:
|
||||
# encode icon_html to base64 string
|
||||
b64_bytes = base64.b64encode(icon_html.encode("utf-8"))
|
||||
rule["icon_base64"] = b64_bytes.decode("utf-8")
|
||||
else:
|
||||
rule["icon_base64"] = ""
|
||||
except Exception as e:
|
||||
MAC_TYPE_ICON_RULES = []
|
||||
mylog('none', f"[guess_device_attributes] Failed to load device_heuristics_rules.json: {e}")
|
||||
|
||||
# -----------------------------------------
|
||||
# Match device type and base64-encoded icon using MAC prefix and vendor patterns.
|
||||
def match_mac_and_vendor(
|
||||
mac_clean: str,
|
||||
vendor: str,
|
||||
default_type: str,
|
||||
default_icon: str
|
||||
) -> Tuple[str, str]:
|
||||
"""
|
||||
Match device type and base64-encoded icon using MAC prefix and vendor patterns.
|
||||
|
||||
Args:
|
||||
mac_clean: Cleaned MAC address (uppercase, no colons).
|
||||
vendor: Normalized vendor name (lowercase).
|
||||
default_type: Fallback device type.
|
||||
default_icon: Fallback base64 icon.
|
||||
|
||||
Returns:
|
||||
Tuple containing (device_type, base64_icon)
|
||||
"""
|
||||
for rule in MAC_TYPE_ICON_RULES:
|
||||
dev_type = rule.get("dev_type")
|
||||
base64_icon = rule.get("icon_base64", "")
|
||||
patterns = rule.get("matching_pattern", [])
|
||||
|
||||
for pattern in patterns:
|
||||
mac_prefix = pattern.get("mac_prefix", "").upper()
|
||||
vendor_pattern = pattern.get("vendor", "").lower()
|
||||
|
||||
if mac_clean.startswith(mac_prefix):
|
||||
if not vendor_pattern or vendor_pattern in vendor:
|
||||
|
||||
mylog('debug', f"[guess_device_attributes] Matched via MAC+Vendor")
|
||||
|
||||
type_ = dev_type
|
||||
icon = base64_icon or default_icon
|
||||
return type_, icon
|
||||
|
||||
return default_type, default_icon
|
||||
|
||||
# ---------------------------------------------------
|
||||
# Match device type and base64-encoded icon using vendor patterns.
|
||||
def match_vendor(
|
||||
vendor: str,
|
||||
default_type: str,
|
||||
default_icon: str
|
||||
) -> Tuple[str, str]:
|
||||
|
||||
vendor_lc = vendor.lower()
|
||||
|
||||
for rule in MAC_TYPE_ICON_RULES:
|
||||
dev_type = rule.get("dev_type")
|
||||
base64_icon = rule.get("icon_base64", "")
|
||||
patterns = rule.get("matching_pattern", [])
|
||||
|
||||
for pattern in patterns:
|
||||
# Only apply fallback when no MAC prefix is specified
|
||||
mac_prefix = pattern.get("mac_prefix", "")
|
||||
vendor_pattern = pattern.get("vendor", "").lower()
|
||||
|
||||
if vendor_pattern and vendor_pattern in vendor_lc:
|
||||
|
||||
mylog('debug', f"[guess_device_attributes] Matched via Vendor")
|
||||
|
||||
icon = base64_icon or default_icon
|
||||
|
||||
return dev_type, icon
|
||||
|
||||
return default_type, default_icon
|
||||
|
||||
# ---------------------------------------------------
|
||||
# Match device type and base64-encoded icon using name patterns.
|
||||
def match_name(
|
||||
name: str,
|
||||
default_type: str,
|
||||
default_icon: str
|
||||
) -> Tuple[str, str]:
|
||||
"""
|
||||
Match device type and base64-encoded icon using name patterns from global MAC_TYPE_ICON_RULES.
|
||||
|
||||
Args:
|
||||
name: Normalized device name (lowercase).
|
||||
default_type: Fallback device type.
|
||||
default_icon: Fallback base64 icon.
|
||||
|
||||
Returns:
|
||||
Tuple containing (device_type, base64_icon)
|
||||
"""
|
||||
name_lower = name.lower() if name else ""
|
||||
|
||||
for rule in MAC_TYPE_ICON_RULES:
|
||||
dev_type = rule.get("dev_type")
|
||||
base64_icon = rule.get("icon_base64", "")
|
||||
name_patterns = rule.get("name_pattern", [])
|
||||
|
||||
for pattern in name_patterns:
|
||||
# Use regex search to allow pattern substrings
|
||||
if re.search(pattern, name_lower, re.IGNORECASE):
|
||||
|
||||
mylog('debug', f"[guess_device_attributes] Matched via Name")
|
||||
|
||||
type_ = dev_type
|
||||
icon = base64_icon or default_icon
|
||||
return type_, icon
|
||||
|
||||
return default_type, default_icon
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# Base64 encoded HTML strings for FontAwesome icons, now with an extended icons dictionary for broader device coverage
|
||||
ICONS = {
|
||||
"globe": "PGkgY2xhc3M9ImZhcyBmYS1nbG9iZSI+PC9pPg==", # Internet or global network
|
||||
"phone": "PGkgY2xhc3M9ImZhcyBmYS1tb2JpbGUtYWx0Ij48L2k+", # Smartphone
|
||||
"laptop": "PGkgY2xhc3M9ImZhIGZhLWxhcHRvcCI+PC9pPg==", # Laptop
|
||||
"printer": "PGkgY2xhc3M9ImZhIGZhLXByaW50ZXIiPjwvaT4=", # Printer
|
||||
"router": "PGkgY2xhc3M9ImZhcyBmYS1yYW5kb20iPjwvaT4=", # Router or network switch
|
||||
"tv": "PGkgY2xhc3M9ImZhIGZhLXR2Ij48L2k+", # Television
|
||||
"desktop": "PGkgY2xhc3M9ImZhIGZhLWRlc2t0b3AiPjwvaT4=", # Desktop PC
|
||||
"tablet": "PGkgY2xhc3M9ImZhIGZhLXRhYmxldCI+PC9pPg==", # Tablet
|
||||
"watch": "PGkgY2xhc3M9ImZhcyBmYS1jbG9jayI+PC9pPg==", # Fallback to clock since smartwatch is nonfree in FontAwesome
|
||||
"camera": "PGkgY2xhc3M9ImZhIGZhLWNhbWVyYSI+PC9pPg==", # Camera or webcam
|
||||
"home": "PGkgY2xhc3M9ImZhIGZhLWhvbWUiPjwvaT4=", # Smart home device
|
||||
"apple": "PGkgY2xhc3M9ImZhYiBmYS1hcHBsZSI+PC9pPg==", # Apple device
|
||||
"ethernet": "PGkgY2xhc3M9ImZhcyBmYS1uZXR3b3JrLXdpcmVkIj48L2k+", # Free alternative for ethernet icon in FontAwesome
|
||||
"google": "PGkgY2xhc3M9ImZhYiBmYS1nb29nbGUiPjwvaT4=", # Google device
|
||||
"raspberry": "PGkgY2xhc3M9ImZhYiBmYS1yYXNwYmVycnktcGkiPjwvaT4=", # Raspberry Pi
|
||||
"microchip": "PGkgY2xhc3M9ImZhcyBmYS1taWNyb2NoaXAiPjwvaT4=", # IoT or embedded device
|
||||
"server": "PGkgY2xhc3M9ImZhcyBmYS1zZXJ2ZXIiPjwvaT4=", # Server
|
||||
"gamepad": "PGkgY2xhc3M9ImZhcyBmYS1nYW1lcGFkIj48L2k+", # Gaming console
|
||||
"lightbulb": "PGkgY2xhc3M9ImZhcyBmYS1saWdodGJ1bGIiPjwvaT4=", # Smart light
|
||||
"speaker": "PGkgY2xhc3M9ImZhcyBmYS12b2x1bWUtdXAiPjwvaT4=", # Free speaker alt icon for smart speakers in FontAwesome
|
||||
"lock": "PGkgY2xhc3M9ImZhcyBmYS1sb2NrIj48L2k+", # Security device
|
||||
}
|
||||
#
|
||||
def match_ip_rule(
|
||||
ip: str,
|
||||
default_type: str,
|
||||
default_icon: str
|
||||
) -> Tuple[str, str]:
|
||||
"""
|
||||
Match device type and base64-encoded icon using IP regex patterns from global JSON.
|
||||
|
||||
# Extended device types for comprehensive classification
|
||||
DEVICE_TYPES = {
|
||||
"Internet": "Internet Gateway",
|
||||
"Phone": "Smartphone",
|
||||
"Laptop": "Laptop",
|
||||
"Printer": "Printer",
|
||||
"Router": "Router",
|
||||
"TV": "Television",
|
||||
"Desktop": "Desktop PC",
|
||||
"Tablet": "Tablet",
|
||||
"Smartwatch": "Smartwatch",
|
||||
"Camera": "Camera",
|
||||
"SmartHome": "Smart Home Device",
|
||||
"Server": "Server",
|
||||
"GamingConsole": "Gaming Console",
|
||||
"IoT": "IoT Device",
|
||||
"NetworkSwitch": "Network Switch",
|
||||
"AccessPoint": "Access Point",
|
||||
"SmartLight": "Smart Light",
|
||||
"SmartSpeaker": "Smart Speaker",
|
||||
"SecurityDevice": "Security Device",
|
||||
"Unknown": "Unknown Device",
|
||||
}
|
||||
Args:
|
||||
ip: Device IP address as string.
|
||||
default_type: Fallback device type.
|
||||
default_icon: Fallback base64 icon.
|
||||
|
||||
Returns:
|
||||
Tuple containing (device_type, base64_icon)
|
||||
"""
|
||||
if not ip:
|
||||
return default_type, default_icon
|
||||
|
||||
for rule in MAC_TYPE_ICON_RULES:
|
||||
ip_patterns = rule.get("ip_pattern", [])
|
||||
dev_type = rule.get("dev_type")
|
||||
base64_icon = rule.get("icon_base64", "")
|
||||
|
||||
for pattern in ip_patterns:
|
||||
if re.match(pattern, ip):
|
||||
|
||||
mylog('debug', f"[guess_device_attributes] Matched via IP")
|
||||
|
||||
type_ = dev_type
|
||||
icon = base64_icon or default_icon
|
||||
return type_, icon
|
||||
|
||||
return default_type, default_icon
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# Guess device attributes such as type of device and associated device icon
|
||||
@@ -73,196 +186,45 @@ def guess_device_attributes(
|
||||
default_icon: str,
|
||||
default_type: str
|
||||
) -> Tuple[str, str]:
|
||||
"""
|
||||
Guess the appropriate FontAwesome icon and device type based on device attributes.
|
||||
|
||||
Args:
|
||||
vendor: Device vendor name.
|
||||
mac: Device MAC address.
|
||||
ip: Device IP address.
|
||||
name: Device name.
|
||||
default_icon: Default icon to return if no match is found.
|
||||
default_type: Default type to return if no match is found.
|
||||
|
||||
Returns:
|
||||
Tuple[str, str]: A tuple containing the guessed icon (Base64-encoded HTML string)
|
||||
and the guessed device type (string).
|
||||
"""
|
||||
mylog('debug', f"[guess_device_attributes] Guessing attributes for (vendor|mac|ip|name): ('{vendor}'|'{mac}'|'{ip}'|'{name}')")
|
||||
# Normalize inputs
|
||||
|
||||
# --- Normalize inputs ---
|
||||
vendor = str(vendor).lower().strip() if vendor else "unknown"
|
||||
mac = str(mac).upper().strip() if mac else "00:00:00:00:00:00"
|
||||
ip = str(ip).strip() if ip else "169.254.0.0" # APIPA address for unknown IPs per RFC 3927
|
||||
ip = str(ip).strip() if ip else "169.254.0.0"
|
||||
name = str(name).lower().strip() if name else "(unknown)"
|
||||
|
||||
# --- Icon Guessing Logic ---
|
||||
if mac == "INTERNET":
|
||||
icon = ICONS.get("globe", default_icon)
|
||||
else:
|
||||
# Vendor-based icon guessing
|
||||
icon_vendor_patterns = {
|
||||
"apple": "apple",
|
||||
"samsung|motorola|xiaomi|huawei": "phone",
|
||||
"dell|lenovo|asus|acer": "laptop",
|
||||
"hp|epson|canon|brother": "printer",
|
||||
"cisco|ubiquiti|netgear|tp-link|d-link|mikrotik": "router",
|
||||
"lg|samsung electronics|sony|vizio": "tv",
|
||||
"raspberry pi": "raspberry",
|
||||
"google": "google",
|
||||
"espressif|particle": "microchip",
|
||||
"intel|amd": "desktop",
|
||||
"amazon": "speaker",
|
||||
"philips hue|lifx": "lightbulb",
|
||||
"aruba|meraki": "ethernet",
|
||||
"qnap|synology": "server",
|
||||
"nintendo|sony interactive|microsoft": "gamepad",
|
||||
"ring|blink|arlo": "camera",
|
||||
"nest": "home",
|
||||
}
|
||||
for pattern, icon_key in icon_vendor_patterns.items():
|
||||
if re.search(pattern, vendor, re.IGNORECASE):
|
||||
icon = ICONS.get(icon_key, default_icon)
|
||||
break
|
||||
else:
|
||||
# MAC-based icon guessing
|
||||
mac_clean = mac.replace(':', '').replace('-', '').upper()
|
||||
icon_mac_patterns = {
|
||||
"001A79|B0BE83|BC926B": "apple",
|
||||
"001B63|BC4C4C": "tablet",
|
||||
"74ACB9|002468": "ethernet",
|
||||
"B827EB": "raspberry",
|
||||
"001422|001874": "desktop",
|
||||
"001CBF|002186": "server",
|
||||
}
|
||||
for pattern_str, icon_key in icon_mac_patterns.items():
|
||||
patterns = [p.replace(':', '').replace('-', '').upper() for p in pattern_str.split('|')]
|
||||
if any(mac_clean.startswith(p) for p in patterns):
|
||||
icon = ICONS.get(icon_key, default_icon)
|
||||
break
|
||||
else:
|
||||
# Name-based icon guessing
|
||||
icon_name_patterns = {
|
||||
"iphone|ipad|macbook|imac": "apple",
|
||||
"pixel|galaxy|redmi": "phone",
|
||||
"laptop|notebook": "laptop",
|
||||
"printer|print": "printer",
|
||||
"router|gateway|ap|access[ -]?point": "router",
|
||||
"tv|television|smarttv": "tv",
|
||||
"desktop|pc|computer": "desktop",
|
||||
"tablet|pad": "tablet",
|
||||
"watch|wear": "watch",
|
||||
"camera|cam|webcam": "camera",
|
||||
"echo|alexa|dot": "speaker",
|
||||
"hue|lifx|bulb": "lightbulb",
|
||||
"server|nas": "server",
|
||||
"playstation|xbox|switch": "gamepad",
|
||||
"raspberry|pi": "raspberry",
|
||||
"google|chromecast|nest": "google",
|
||||
"doorbell|lock|security": "lock",
|
||||
}
|
||||
for pattern, icon_key in icon_name_patterns.items():
|
||||
if re.search(pattern, name, re.IGNORECASE):
|
||||
icon = ICONS.get(icon_key, default_icon)
|
||||
break
|
||||
else:
|
||||
# IP-based icon guessing
|
||||
icon_ip_patterns = {
|
||||
r"^192\.168\.[0-1]\.1$": "router",
|
||||
r"^10\.0\.0\.1$": "router",
|
||||
r"^192\.168\.[0-1]\.[2-9]$": "desktop",
|
||||
r"^192\.168\.[0-1]\.1\d{2}$": "phone",
|
||||
}
|
||||
for pattern, icon_key in icon_ip_patterns.items():
|
||||
if re.match(pattern, ip):
|
||||
icon = ICONS.get(icon_key, default_icon)
|
||||
break
|
||||
else:
|
||||
icon = default_icon
|
||||
|
||||
# --- Type Guessing Logic ---
|
||||
if mac == "INTERNET":
|
||||
type_ = DEVICE_TYPES.get("Internet", default_type)
|
||||
else:
|
||||
# Vendor-based type guessing
|
||||
type_vendor_patterns = {
|
||||
"apple|samsung|motorola|xiaomi|huawei": "Phone",
|
||||
"dell|lenovo|asus|acer|hp": "Laptop",
|
||||
"epson|canon|brother": "Printer",
|
||||
"cisco|ubiquiti|netgear|tp-link|d-link|mikrotik|aruba|meraki": "Router",
|
||||
"lg|samsung electronics|sony|vizio": "TV",
|
||||
"raspberry pi": "IoT",
|
||||
"google|nest": "SmartHome",
|
||||
"espressif|particle": "IoT",
|
||||
"intel|amd": "Desktop",
|
||||
"amazon": "SmartSpeaker",
|
||||
"philips hue|lifx": "SmartLight",
|
||||
"qnap|synology": "Server",
|
||||
"nintendo|sony interactive|microsoft": "GamingConsole",
|
||||
"ring|blink|arlo": "Camera",
|
||||
}
|
||||
for pattern, type_key in type_vendor_patterns.items():
|
||||
if re.search(pattern, vendor, re.IGNORECASE):
|
||||
type_ = DEVICE_TYPES.get(type_key, default_type)
|
||||
break
|
||||
else:
|
||||
# MAC-based type guessing
|
||||
mac_clean = mac.replace(':', '').replace('-', '').upper()
|
||||
type_mac_patterns = {
|
||||
"00:1A:79|B0:BE:83|BC:92:6B": "Phone",
|
||||
"00:1B:63|BC:4C:4C": "Tablet",
|
||||
"74:AC:B9|00:24:68": "AccessPoint",
|
||||
"B8:27:EB": "IoT",
|
||||
"00:14:22|00:18:74": "Desktop",
|
||||
"00:1C:BF|00:21:86": "Server",
|
||||
}
|
||||
for pattern_str, type_key in type_mac_patterns.items():
|
||||
patterns = [p.replace(':', '').replace('-', '').upper() for p in pattern_str.split('|')]
|
||||
if any(mac_clean.startswith(p) for p in patterns):
|
||||
type_ = DEVICE_TYPES.get(type_key, default_type)
|
||||
break
|
||||
else:
|
||||
# Name-based type guessing
|
||||
type_name_patterns = {
|
||||
"iphone|ipad": "Phone",
|
||||
"macbook|imac": "Laptop",
|
||||
"pixel|galaxy|redmi": "Phone",
|
||||
"laptop|notebook": "Laptop",
|
||||
"printer|print": "Printer",
|
||||
"router|gateway|ap|access[ -]?point": "Router",
|
||||
"tv|television|smarttv": "TV",
|
||||
"desktop|pc|computer": "Desktop",
|
||||
"tablet|pad": "Tablet",
|
||||
"watch|wear": "Smartwatch",
|
||||
"camera|cam|webcam": "Camera",
|
||||
"echo|alexa|dot": "SmartSpeaker",
|
||||
"hue|lifx|bulb": "SmartLight",
|
||||
"server|nas": "Server",
|
||||
"playstation|xbox|switch": "GamingConsole",
|
||||
"raspberry|pi": "IoT",
|
||||
"google|chromecast|nest": "SmartHome",
|
||||
"doorbell|lock|security": "SecurityDevice",
|
||||
}
|
||||
for pattern, type_key in type_name_patterns.items():
|
||||
if re.search(pattern, name, re.IGNORECASE):
|
||||
type_ = DEVICE_TYPES.get(type_key, default_type)
|
||||
break
|
||||
else:
|
||||
# IP-based type guessing
|
||||
type_ip_patterns = {
|
||||
r"^192\.168\.[0-1]\.1$": "Router",
|
||||
r"^10\.0\.0\.1$": "Router",
|
||||
r"^192\.168\.[0-1]\.[2-9]$": "Desktop",
|
||||
r"^192\.168\.[0-1]\.1\d{2}$": "Phone",
|
||||
}
|
||||
for pattern, type_key in type_ip_patterns.items():
|
||||
if re.match(pattern, ip):
|
||||
type_ = DEVICE_TYPES.get(type_key, default_type)
|
||||
break
|
||||
else:
|
||||
type_ = default_type
|
||||
# # Internet shortcut
|
||||
# if mac == "INTERNET":
|
||||
# return ICONS.get("globe", default_icon), DEVICE_TYPES.get("Internet", default_type)
|
||||
|
||||
type_ = None
|
||||
icon = None
|
||||
|
||||
# --- Strict MAC + vendor rule matching from external file ---
|
||||
type_, icon = match_mac_and_vendor(mac_clean, vendor, default_type, default_icon)
|
||||
|
||||
# --- Loose Vendor-based fallback ---
|
||||
if not type_ or type_ == default_type:
|
||||
type_, icon = match_vendor(vendor, default_type, default_icon)
|
||||
|
||||
# --- Loose Name-based fallback ---
|
||||
if not type_ or type_ == default_type:
|
||||
type_, icon = match_name(name, default_type, default_icon)
|
||||
|
||||
# --- Loose IP-based fallback ---
|
||||
if (not type_ or type_ == default_type) or (not icon or icon == default_icon):
|
||||
type_, icon = match_ip_rule(ip, default_type, default_icon)
|
||||
|
||||
# Final fallbacks
|
||||
type_ = type_ or default_type
|
||||
icon = icon or default_icon
|
||||
|
||||
mylog('debug', f"[guess_device_attributes] Guessed attributes (icon|type_): ('{icon}'|'{type_}')")
|
||||
return icon, type_
|
||||
|
||||
|
||||
# Deprecated functions with redirects (To be removed once all calls for these have been adjusted to use the updated function)
|
||||
def guess_icon(
|
||||
vendor: Optional[str],
|
||||
@@ -308,7 +270,7 @@ def guess_type(
|
||||
default: Default type to return if no match is found.
|
||||
|
||||
Returns:
|
||||
str: Device type from DEVICE_TYPES dictionary.
|
||||
str: Device type.
|
||||
"""
|
||||
|
||||
_, type_ = guess_device_attributes(vendor, mac, ip, name, "unknown_icon", default)
|
||||
|
||||
Reference in New Issue
Block a user