Skip to content

Commit

Permalink
Add checking for IPv6 regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Grennith committed Nov 29, 2023
1 parent bf47ccb commit 7b47dd0
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion mapadroid/websocket/communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,17 @@ async def get_external_ip(self) -> Optional[str]:
found = re.search('(((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\\b){4})', res)
if found:
ip_address_found = found.group(1)
else:
# No IPv4 address found - could be an IPv6 address was returned...
# https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
ipv6_search = re.search('(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))', res)
if ipv6_search:
ip_address_found = found.group(1)
except Exception as e:
logger.error(f"Failed parsing external IP: {e}")
return None

if ip_address_found and type(ip_address(ip_address_found)) is IPv4Address:
if ip_address_found and ip_address(ip_address_found):
return ip_address_found
else:
logger.error(f"{ip_address_found} is not a valid IPv4 address")
Expand Down

0 comments on commit 7b47dd0

Please sign in to comment.