Skip to content

Commit

Permalink
lib.utils: Added logging info to get_all_local_ipv4_addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
msinn committed Apr 26, 2024
1 parent 72556b6 commit de483dc
Showing 1 changed file with 29 additions and 54 deletions.
83 changes: 29 additions & 54 deletions lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,53 +196,45 @@ def get_local_ipv6_address():
return IP

@staticmethod
def get_all_local_ipv4_addresses() -> list:
def get_all_addresses_for_addressfamily(af: int) -> list:
"""
Get all ipv4 addresses as a list
Get all addresses for an address-family as a list
https://stackoverflow.com/questions/270745/how-do-i-determine-all-of-my-ip-addresses-when-i-have-multiple-nics/274644#274644
:return: ipv4 adresses
:return: addresses
"""
from netifaces import interfaces, ifaddresses, AF_INET
from netifaces import interfaces, ifaddresses

ip_list = []
interf = interfaces()
interf_data = {}
for interface in interfaces():
try:
interf_data[interface] = ifaddresses(interface)
for link in ifaddresses(interface)[AF_INET]:
if af in ifaddresses(interface).keys():
for link in ifaddresses(interface)[af]:
ip_list.append(link['addr'])
except Exception as ex:
logger.warning(f"get_all_local_ipv4_addresses: Exception {ex} \n- interfaces={interf}\n-act. interface={interface}\n-act. interface data={interf_data}\n- {ifaddresses(interface)}")
ip_list.append(Utils.get_local_ipv4_address())
return ip_list

@staticmethod
def get_all_local_ipv6_addresses() -> list:
def get_all_local_ipv4_addresses() -> list:
"""
Get all ipv6 addresses as a list
Get ipv4 addresses of all interfaces as a list
https://stackoverflow.com/questions/270745/how-do-i-determine-all-of-my-ip-addresses-when-i-have-multiple-nics/274644#274644
:return: ipv4 addresses
"""
from netifaces import AF_INET
return Utils.get_all_addresses_for_addressfamily(AF_INET)

:return: ipv6 adresses
@staticmethod
def get_all_local_ipv6_addresses() -> list:
"""
from netifaces import interfaces, ifaddresses, AF_INET6
Get ipv6 addresses of all interfaces as a list
ip_list = []
interf = interfaces()
for interface in interfaces():
try:
for link in ifaddresses(interface)[AF_INET6]:
ip_list.append(link['addr'])
except Exception as ex:
logger.warning(f"get_all_local_ipv6_addresses: Exception {ex} \n- interfaces={interf}\n-act. interface={interface}\n- {ifaddresses(interface)}")
ip_list.append(Utils.get_local_ipv6_address())
return ip_list
:return: ipv6 addresses
"""
from netifaces import AF_INET6
return Utils.get_all_addresses_for_addressfamily(AF_INET6)

@staticmethod
def is_knx_groupaddress(groupaddress):
def is_knx_groupaddress(groupaddress: str) -> bool:
"""
Checks if the passed string is a valid knx goup address
Expand Down Expand Up @@ -278,7 +270,7 @@ def is_knx_groupaddress(groupaddress):


@staticmethod
def is_timeframe(string):
def is_timeframe(string: str) -> bool:
"""
Checks if a string is a timeframe. A timeframe consists of a
number and an optional unit identifier (e.g. 2h, 30m, ...).
Expand All @@ -299,7 +291,7 @@ def is_timeframe(string):
return False

@staticmethod
def to_timeframe(value):
def to_timeframe(value: str | int) -> int:
"""
Converts a timeframe value to milliseconds. See is_timeframe() method.
The special value 'now' is supported for the current time.
Expand All @@ -308,7 +300,6 @@ def to_timeframe(value):
:type value: str, int, ...
:return: True if cant be converted and is true, False otherwise.
:rtype: bool
"""

minute = 60 * 1000
Expand All @@ -332,15 +323,13 @@ def to_timeframe(value):
return int(amount)

@staticmethod
def is_int(string):
def is_int(string: str) -> bool:
"""
Checks if a string is a integer.
:param string: String to check.
:type string: str
:return: True if a cast to int works, false otherwise.
:rtype: bool
"""

try:
Expand All @@ -352,15 +341,13 @@ def is_int(string):
return False

@staticmethod
def is_float(string):
def is_float(string: str) -> bool:
"""
Checks if a string is a float.
:param string: String to check.
:type string: str
:return: True if a cast to float works, false otherwise.
:rtype: bool
"""

try:
Expand All @@ -372,7 +359,7 @@ def is_float(string):
return False

@staticmethod
def to_bool(value, default='exception'):
def to_bool(value: str|int|float, default: bool='exception') -> bool:
"""
Converts a value to boolean.
Expand All @@ -384,11 +371,8 @@ def to_bool(value, default='exception'):
:param value : value to convert
:param default: optional, value to return if value can not be parsed, if default is not set this method throws an exception
:type value: str, object, int, ...
:type value: str, object, int, ...
:return: True if cant be converted and is true, False otherwise.
:rtype: bool
"""
# -> should it be possible to cast strings: 0 -> False and non-0 -> True (analog to integer values)?
if isinstance(value, str):
Expand All @@ -403,31 +387,27 @@ def to_bool(value, default='exception'):
return bool(value)

@staticmethod
def create_hash(plaintext):
def create_hash(plaintext: str) -> str:
"""
Create hash (currently sha512) for given plaintext value
:param plaintext: plaintext
:type plaintext: str
:return: hash of plaintext, lowercase letters
:rtype: str
"""

hashfunc = hashlib.sha512()
hashfunc.update(plaintext.encode())
return "".join(format(b, "02x") for b in hashfunc.digest())

@staticmethod
def is_hash(value):
def is_hash(value: str) -> bool:
"""
Check if value is a valid hash (currently sha512) value
:param value: value to check
:type value: str
:return: True: given value can be a sha512 hash, False: given value can not be a sha512 hash
:rtype: bool
"""

# a valid sha512 hash is a 128 charcter long string value
Expand All @@ -442,18 +422,15 @@ def is_hash(value):
return False

@staticmethod
def check_hashed_password(pwd_to_check, hashed_pwd):
def check_hashed_password(pwd_to_check: str, hashed_pwd: str) -> bool:
"""
Check if given plaintext password matches the hashed password
An empty password is always rejected
:param pwd_to_check: plaintext password to check
:type pwd_to_check: str
:param hashed_pwd: hashed password
:type hashed_pwd: str
:return: True: password matches, False: password does not match
:rtype: bool
"""

if pwd_to_check is None or pwd_to_check == '':
Expand All @@ -465,16 +442,14 @@ def check_hashed_password(pwd_to_check, hashed_pwd):
return Utils.create_hash(pwd_to_check) == hashed_pwd.lower()

@staticmethod
def strip_quotes(string):
def strip_quotes(string: str) -> str:
"""
If a string contains quotes as first and last character, this function
returns the string without quotes, otherwise the string is returned unchanged
:param string: string to check for quotes
:type string: str
:return: string with quotes stripped
:rtype: str
"""
if type(string) is str:
string = string.strip()
Expand Down

0 comments on commit de483dc

Please sign in to comment.