Merge pull request #650 from ulrichwisser/main

cleanDeviceName rewritten to remove all _ labels and remove search list and local domain
This commit is contained in:
jokob-sk
2024-05-01 08:25:25 +10:00
committed by GitHub
5 changed files with 71 additions and 9 deletions

View File

@@ -12,7 +12,8 @@ ENV PATH="/opt/venv/bin:$PATH"
COPY . ${INSTALL_DIR}/
RUN pip install requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap \
RUN pip install requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython \
&& bash -c "find ${INSTALL_DIR} -type d -exec chmod 750 {} \;" \
&& bash -c "find ${INSTALL_DIR} -type f -exec chmod 640 {} \;" \
&& bash -c "find ${INSTALL_DIR} -type f \( -name '*.sh' -o -name '*.py' -o -name 'speedtest-cli' \) -exec chmod 750 {} \;"

View File

@@ -41,7 +41,8 @@ RUN phpenmod -v 8.2 sqlite3
# Setup virtual python environment and use pip3 to install packages
RUN apt-get install -y python3-venv
RUN python3 -m venv myenv
RUN /bin/bash -c "source myenv/bin/activate && update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && pip3 install requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap"
RUN /bin/bash -c "source myenv/bin/activate && update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && pip3 install requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython"
# Create a buildtimestamp.txt to later check if a new version was released
RUN date +%s > ${INSTALL_DIR}/front/buildtimestamp.txt

View File

@@ -605,6 +605,25 @@
"string": "The icon associated with the device. Check the <a href=\"https://github.com/jokob-sk/NetAlertX/blob/main/docs/ICONS.md\" target=\"_blank\">documentation on icons</a> for more details."
}
]
},
{
"function": "LESS_NAME_CLEANUP",
"type": "integer.checkbox",
"default_value": 0,
"options": [],
"localized": ["name", "description"],
"name": [
{
"language_code": "en_us",
"string": "Less Name Cleanup"
}
],
"description": [
{
"language_code": "en_us",
"string": "Check to start using the new code for cleaning device names. Removes all labels starting with underscore and removes network domain and search list."
}
]
}
],
"required": [
@@ -627,10 +646,9 @@
"dev_Archived",
"dev_Network_Node_MAC_ADDR",
"dev_Network_Node_port",
"dev_Icon"
"dev_Icon",
"LESS_NAME_CLEANUP"
],
"additionalProperties": false
}

View File

@@ -30,4 +30,5 @@ source myenv/bin/activate
update-alternatives --install /usr/bin/python python /usr/bin/python3 10
# install packages thru pip3
pip3 install requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap
pip3 install requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython

View File

@@ -570,12 +570,54 @@ def resolve_device_name_pholus (pMAC, pIP, allRes, nameNotFound, match_IP = Fals
#-------------------------------------------------------------------------------
import dns.resolver
def cleanDeviceName(str, match_IP):
if get_setting_value('NEWDEV_LESS_NAME_CLEANUP'):
mylog('debug', ["Using new cleanDeviceName(" + str + ")"])
# replace all labels starting with underscore
str = re.sub(r'^_[^\.]*\.', '', str) # leading label
str = re.sub(r'\._[^\.]*\.', '.', str) # nested label
# get a stub resolver for access to resolv.conf configuration
resolv = dns.resolver.Resolver()
# replace the local domain name
str = re.sub(r'\.' + resolv.domain.to_text() + r'$', '', str)
# replace dns search list
for name in resolv.search:
str = re.sub(r'\.' + name.to_text() + r'$', '', str)
# removing last part of e.g. Nest-Audio-ff77ff77ff77ff77ff77ff77ff77ff77
str = re.sub(r'-[a-fA-F0-9]{32}', '', str)
# Remove everything after '#' including the '#'
str = re.sub(r'#.*', '', str)
# remove trailing dot
if str.endswith('.'):
str = str[:-1]
# add matching info
if match_IP:
str = str + " (IP match)"
# done
mylog('debug', ["cleanDeviceName = " + str])
return str
################################
#
# OLD cleanDeviceName
mylog('debug', ["Using old cleanDeviceName(" + str + ")"])
# alternative str.split('.')[0]
str = str.replace("._airplay", "")
str = str.replace("._tcp", "")
str = str.replace(".localdomain", "")
str = str.replace(".local", "")
str = str.replace(".local", "")
str = str.replace("._esphomelib", "")
str = str.replace("._googlecast", "")
str = str.replace(".lan", "")
@@ -586,13 +628,12 @@ def cleanDeviceName(str, match_IP):
if str.endswith('.'):
str = str[:-1]
if match_IP:
str = str + " (IP match)"
mylog('debug', ["cleanDeviceName = " + str])
return str
#-------------------------------------------------------------------------------
# String manipulation methods
#-------------------------------------------------------------------------------